Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Raising Terrain at a Given Point

Discussion in 'Scripting' started by Agilapathy, Jul 13, 2018.

  1. Agilapathy

    Agilapathy

    Joined:
    Jun 29, 2018
    Posts:
    21
    Hi!

    I was wondering if there was a simple(or complex) way to enter height map coordinates into a Vector2 that will run through some sort of equation, along with other values given, to spawn a gradient rise in a terrain originating from the coordinates you put.

    It would look something like this:

    I would enter these values:
    upload_2018-7-12_20-29-9.png

    Mountain Position - The place that the mountain originates from
    Multiplier - the amplitude of the mountain
    Spread - How far the mountain would stretch out (bigger values would mean a bigger circle of effect and vice versa)

    For the sake of simplicity, it would generate when the game starts.

    Then my Terrain would look something like this:

    upload_2018-7-12_20-31-9.png

    This mountain wasn't generated by these values, I manually raised the mountain and put public values that weren't attached to anything in a script for example purposes.

    If anyone can help lead me in the right direction as to how I would go about doing something like this, it would be much appreciated!

    Thank you!
     
  2. FlashMuller

    FlashMuller

    Joined:
    Sep 25, 2013
    Posts:
    450
    Not a pro on Terrain, but you could get the heightmap and "paint" onto it via script, changing the brush using your variables.
     
  3. Agilapathy

    Agilapathy

    Joined:
    Jun 29, 2018
    Posts:
    21
    I've looked everywhere and I can't seem to find a way to do this, I think it has something to do with the SetAlphamaps() method but I'm not too sure. I couldn't find much on that either.

    Do you think you could point me in the right direction? Thanks.
     
  4. neoshaman

    neoshaman

    Joined:
    Feb 11, 2011
    Posts:
    6,493
  5. Agilapathy

    Agilapathy

    Joined:
    Jun 29, 2018
    Posts:
    21
    Thank you so much for this, I'm definitely on the right track here.

    After experimenting for hours, I've morphed the code for the mountains found in the video into my code to have a similar effect. I can now generate things like this:

    upload_2018-7-13_20-48-29.png

    But pyramids aren't exactly what I'm aiming for. I've tried everything I could to get them to look something like what I want, but I can't figure out how to.

    Here's my code:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class MountainPlacer : MonoBehaviour {
    4.  
    5.     public Terrain ThisTerrain;
    6.     [Range(0,1)]
    7.     public float mountainHeight;
    8.     [Range(0,1f)]
    9.     public float mountainSlope;
    10.  
    11.  
    12.     private void Update()
    13.     {
    14.         if (Input.GetKey(KeyCode.Space))
    15.         {
    16.             int posX = Random.Range(0, ThisTerrain.terrainData.heightmapWidth); //Get a random point in the terrain map
    17.             int posY = Random.Range(0, ThisTerrain.terrainData.heightmapHeight);
    18.  
    19.             float[,] heights = ThisTerrain.terrainData.GetHeights(0, 0, ThisTerrain.terrainData.heightmapWidth, ThisTerrain.terrainData.heightmapHeight);
    20.  
    21.             GenerateMountain(posX, posY, mountainHeight, mountainSlope, heights);
    22.  
    23.             ThisTerrain.terrainData.SetHeights(0, 0, heights);
    24.         }  
    25.     }
    26.  
    27.     void GenerateMountain(int x, int y, float height, float slope, float[,] heights)
    28.     {
    29.  
    30.         if (x <= 0 || x >= ThisTerrain.terrainData.heightmapWidth)
    31.         {
    32.             return;
    33.         }
    34.         if (y <= 0 || y >= ThisTerrain.terrainData.heightmapHeight)
    35.         {
    36.             return;
    37.         }
    38.  
    39.         if(height <= 0)
    40.         {
    41.             return;
    42.         }
    43.  
    44.         if(heights[x,y] >= height)
    45.         {
    46.             return;
    47.         }
    48.  
    49.         heights[x, y] = height;
    50.  
    51.         float newHeight = height - slope;
    52.  
    53.         GenerateMountain(x - 1, y, newHeight, slope, heights);
    54.         GenerateMountain(x + 1, y, newHeight, slope, heights);
    55.         GenerateMountain(x, y - 1, newHeight, slope, heights);
    56.         GenerateMountain(x, y + 1, newHeight, slope, heights);
    57.  
    58.     }
    59. }
    60.  
    And here's what my viewports look like just in case you wanted to know how the script was hooked up:

    upload_2018-7-13_20-51-22.png

    If someone could give me some sort of equation that can mesh into what I have to make rounded mountains, it would be much appreciated! And again, thank you neoshaman for the video link! :)
     
  6. neoshaman

    neoshaman

    Joined:
    Feb 11, 2011
    Posts:
    6,493
    For every point you will draw down, check the distance to the center, normalize that into a 0,1 range, use a easing algorithm to shape the curve of the range, multiply result by target max height.