Search Unity

Resolved Tilt terrain script

Discussion in 'Scripting' started by tawdry, Apr 18, 2021.

  1. tawdry

    tawdry

    Joined:
    Sep 3, 2014
    Posts:
    1,357
    Found this code online.It gives the impression that a flat terrain is tilted by setting the height at one end higher.Anyone know what to change to allow for that tilt to be given a value right now its tilting at around a 60 degree angle and Iwant to be able to set it for whatever value I need instead.
    The relevant code is contained from line 34-79 I tried imputing value in most of those lines but didn't get the desired effect so asking here.Thx



    Code (CSharp):
    1. using UnityEditor;
    2. using UnityEngine;
    3. using System.Collections;
    4. using System;
    5. using System.Collections.Generic;
    6.  
    7. public class terraintilt : EditorWindow {
    8.  
    9.     static float _startingX = 0f;
    10.     static float _startingY = 0f;
    11.     static float _endingX = 1f;
    12.     static float _endingY = 0f;
    13.     static float _scale = 0f;
    14.  
    15.     [MenuItem("Meriwether/Show Terrain Slope Tool")]
    16.     static void ShowWindow() {
    17.         EditorWindow.GetWindow<terraintilt> ();
    18.     }
    19.  
    20.     public void OnGUI(){
    21.         if (GUILayout.Button("Apply Slope")){
    22.             SlopeTerrain();
    23.         }
    24.  
    25.         if (GUILayout.Button("Lower Terrain")){
    26.             LowerTerrain();
    27.         }
    28.  
    29.         if (GUILayout.Button("Scale Terrain")){
    30.             ScaleTerrain();
    31.         }
    32.     }
    33.  
    34.     static void SlopeTerrain(){
    35.         GameObject source = Selection.activeGameObject;
    36.  
    37.         if (source == null || source.GetComponent(typeof(Terrain)) == null){
    38.             Debug.LogError("Cannot apply slope, no valid terrain selected!");
    39.             return;
    40.         }
    41.  
    42.         Terrain sourceTerrain = (Terrain)source.GetComponent(typeof(Terrain));
    43.         TerrainData tdata = sourceTerrain.terrainData;
    44.  
    45.         float[,] sourceHeights = tdata.GetHeights(0,0,tdata.heightmapResolution,tdata.heightmapResolution);
    46.         float[,] newHeights = tdata.GetHeights(0,0,tdata.heightmapResolution,tdata.heightmapResolution);
    47.  
    48.         bool clippedBelow = false;
    49.         bool clippedAbove = false;
    50.  
    51.         for (int i = 0; i < tdata.heightmapResolution; i++){
    52.             for (int j = 0; j < tdata.heightmapResolution; j++){
    53.                 float floati = i;
    54.                 float floatj = j;
    55.                 float widthp = floati / tdata.heightmapResolution;
    56.                 float heightp = floatj / tdata.heightmapResolution;
    57.  
    58.                 newHeights[i,j] = sourceHeights[i,j] + Mathf.Lerp(_startingX, _endingX, widthp) + Mathf.Lerp(_startingY, _endingY, heightp);
    59.  
    60.                 if (newHeights[i,j] < 0){
    61.                     clippedBelow = true;
    62.                 } else if (newHeights[i,j] > 1) {
    63.                     clippedAbove = true;
    64.                 }
    65.             }
    66.         }
    67.  
    68.         if (clippedBelow){
    69.             Debug.LogWarning("Warning: Clipped below 0!");
    70.         }
    71.         if (clippedAbove){
    72.             Debug.LogWarning("Warning: Clipped above 1!");
    73.         }
    74.  
    75.         tdata.SetHeights(0, 0, newHeights);
    76.  
    77.         tdata.RefreshPrototypes();
    78.         sourceTerrain.Flush();
    79.     }
    80.  
    81.     static void LowerTerrain(){
    82.         GameObject source = Selection.activeGameObject;
    83.  
    84.         if (source == null || source.GetComponent(typeof(Terrain)) == null){
    85.             Debug.LogError("Cannot lower terrain, no valid terrain selected!");
    86.             return;
    87.         }
    88.  
    89.         Terrain sourceTerrain = (Terrain)source.GetComponent(typeof(Terrain));
    90.         TerrainData tdata = sourceTerrain.terrainData;
    91.  
    92.         float[,] sourceHeights = tdata.GetHeights(0,0,tdata.heightmapResolution,tdata.heightmapResolution);
    93.         float[,] newHeights = tdata.GetHeights(0,0,tdata.heightmapResolution,tdata.heightmapResolution);
    94.  
    95.         float lowestValue = 1;
    96.  
    97.         for (int i = 0; i < tdata.heightmapResolution; i++){
    98.             for (int j = 0; j < tdata.heightmapResolution; j++){
    99.                 if (sourceHeights[i,j] < lowestValue){
    100.                     lowestValue = sourceHeights[i,j];
    101.                 }
    102.             }
    103.         }
    104.  
    105.         for (int i = 0; i < tdata.heightmapResolution; i++){
    106.             for (int j = 0; j < tdata.heightmapResolution; j++){
    107.                 if (sourceHeights[i,j] < lowestValue){
    108.                     lowestValue = sourceHeights[i,j];
    109.                 }
    110.                 newHeights[i,j] = sourceHeights[i,j] - lowestValue;
    111.             }
    112.         }
    113.  
    114.         tdata.SetHeights(0, 0, newHeights);
    115.  
    116.         tdata.RefreshPrototypes();
    117.         sourceTerrain.Flush();
    118.     }
    119.  
    120.     static void ScaleTerrain(){
    121.         GameObject source = Selection.activeGameObject;
    122.  
    123.         if (source == null || source.GetComponent(typeof(Terrain)) == null){
    124.             Debug.LogError("Cannot lower terrain, no valid terrain selected!");
    125.             return;
    126.         }
    127.  
    128.         Terrain sourceTerrain = (Terrain)source.GetComponent(typeof(Terrain));
    129.         TerrainData tdata = sourceTerrain.terrainData;
    130.  
    131.         float[,] sourceHeights = tdata.GetHeights(0,0,tdata.heightmapResolution,tdata.heightmapResolution);
    132.         float[,] newHeights = tdata.GetHeights(0,0,tdata.heightmapResolution,tdata.heightmapResolution);
    133.  
    134.         for (int i = 0; i < tdata.heightmapResolution; i++){
    135.             for (int j = 0; j < tdata.heightmapResolution; j++){
    136.  
    137.                 newHeights[i,j] = sourceHeights[i,j] * _scale;
    138.             }
    139.         }
    140.  
    141.         tdata.SetHeights(0, 0, newHeights);
    142.  
    143.         tdata.RefreshPrototypes();
    144.         sourceTerrain.Flush();
    145.     }
    146. }
    147.  
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,442
    in line 58 it calculates new height,
    so adjusting the starting and ending values should do it.

    but note that it looks like to be normalized value, since it clips them between 0-1 in the next line.

    in that case,check your terrain settings - it has that "terrain height" value,
    so getting value 1 from that line 58, would equal that max height value.
     
  3. tawdry

    tawdry

    Joined:
    Sep 3, 2014
    Posts:
    1,357
    Ok nice thx worked it out the key is to change the line 11 static float _endingX = 1f; to whatever smaller float value u prefer.