Search Unity

Help With Terrain Composer Runtime

Discussion in 'Scripting' started by ValrikRobot, Oct 6, 2015.

  1. ValrikRobot

    ValrikRobot

    Joined:
    Jun 26, 2013
    Posts:
    206
    I was wondering if anyone had any experience using the runtime scripting to activate terrains and generate trees, grass and objects at a given time.

    Plus how does the terrain array work. if i have 25 terrains, would x0_y0 be terrain[0]. Is there a math equation i can use to convert x + y to the terrain array ID?
     
  2. Txguug

    Txguug

    Joined:
    Jun 16, 2014
    Posts:
    18
    Slap this on a blank terrain to generate some hills...
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class FormTerrain : MonoBehaviour {
    6.  
    7.   TerrainData terrainData;
    8.   int width;
    9.   int heightmapHeight;
    10.   float height;
    11.   float terrainSize;
    12.  
    13.   void Start()
    14.   {
    15.   float noiseSize = 0.01f;
    16.   float heightScale = 0.1f;
    17.   Terrain terrain = GetComponent<Terrain>();
    18.   terrainData = terrain.terrainData;
    19.   width = terrainData.heightmapWidth;
    20.   heightmapHeight = terrainData.heightmapHeight;
    21.   terrainSize = terrainData.size.x;
    22.   float[,] heights = terrainData.GetHeights(0, 0, width, heightmapHeight);
    23.  
    24.   // calculate heights
    25.   for (int z = 0; z < width; z++)
    26.   {
    27.   for (int x = 0; x < width; x++)
    28.   {
    29.   float height = (Mathf.PerlinNoise(x * noiseSize, z * noiseSize)) * heightScale;
    30.   heights[x, z] = height;
    31.   }
    32.   }
    33.   terrainData.SetHeights(0, 0, heights);  
    34.  
    35.   }
    36.  
    37. }
    38.  
    39.  
     
  3. ValrikRobot

    ValrikRobot

    Joined:
    Jun 26, 2013
    Posts:
    206
    thanks but thats not quite what i meant
     
    Txguug likes this.
  4. ZenMicro

    ZenMicro

    Joined:
    Aug 9, 2015
    Posts:
    206
    I'm also very keen to learn more about this feature, it sounds like it is the solution to my edge of world issues - with a vehicle it doesn't take too long to reach the edge of the world and worse you can see the edge for miles away if you are elevated. Surprised there is no other posts about it yet. Will post when i learn more.. diving into the scripts now...

    The readme.txt state the following;

    The RuntimeTerrains script allows you to create and generate terrains in runtime from your TerrainComposer project.

    You need to drag and drop the RuntimeTerrains prefab in your Scene.
    There are 2 options. You can precreate the (flat) terrains with TerrainComposer or create the terrains on the fly in runtime.

    The RegenerateTerrains you can use for testing generating random terrain. The RuntimeTerrains needs to be assigned to it and
    with pressing 'g' in playmode or in build it will regenerate the terrains.

    Create terrains on the fly:
    This options will save build size and handy for web player builds where small build size is important.
    To active, enable 'Create Terrains On The Fly'. You also have to create 1 terrain in your Scene with splat textures assigned.
    Then Unity will render the terrain correct in the build. You can use the lowest resolution terrain for this and disable
    it's rendering. Then after that the terrains in TC terrain list needs to have 1 terrain that is empty and set to 'None'.
    The settings you use on that terrain, the runtime terrains will have. The amount of terrains to be created can be selected
    with Terrain Instances slider.

    The script options:
    ---------------------------------------------------------------------
    * Move Terrains With Camera -> Unlimited procedural terrains. This will automatically regenerate terrains around the camera.
    * Update Check Time -> The interval in which the moving of terrains check is performed.
    * Main Camera -> Assign your main camera if you enable 'Move Terrains With Camera'.
    ---------------------------------------------------------------------

    * Generate On Start -> Generate terrains on Scene load, otherwise you have to manually call the 'GenerateStart()' function in
    the script.

    * Auto Speed -> This can be used if you want to generate terrains while keeping framerate. If not you can create the terrains
    with a faster manual speed.

    * Target Frames -> While generating with auto speed, TerrainComposer will return from the loop as soon as the fps goes lower
    then the set frames. So with this you can make sure your game keeps running a certain fps, although when using too high
    resolutions, the fps will hang on updating the Unity terrain.

    * Generate Speed -> The manual generation speed.

    * Heightmap, Splat, Tree, Grass and Object output -> Enables the generating of the output.

    * Seed -> Can choose a seed number for random output, it will always give the same 'Random' result for the same number.

    * Random Seed -> A random seed will be choosen based on time. The terrains will be really random.

    * Randomize and Randomize Range -> Enable/Disable the randomness for the output and choose the random range for it.


    If you have any questions, please contact me at Nathaniel_Doldersum@hotmail.com
     
    Last edited: Oct 25, 2015