Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Unity 2022.2 is now available as the latest Tech release.
    Dismiss Notice
  3. We are making some changes to the DOTS forums.
    Dismiss Notice
  4. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

Object2Terrain Code Manipulation

Discussion in 'World Building' started by atx22, Jun 19, 2022.

  1. atx22


    Jun 8, 2022
    Currently I'm generating "terrain" as a mesh, and I'd like to turn it into a terrain object. I'm using the Object2Terrain script found here, where you point to an object in the GUI, then use menu options to configure the conversion. I'd like to accomplish this at runtime but am confused where the mesh object being converted is declared or pointed at in the code.

    When going the GUI rout, the selected object is used. I see the check for an object selected, but the object is not passed into the CreateTerrain Function?

    Code (CSharp):
    1. if(GUILayout.Button("Create Terrain")){
    2.             if(Selection.activeGameObject == null){
    3.                 EditorUtility.DisplayDialog("No object selected", "Please select an object.", "Ok");
    4.                 return;
    5.             }
    6.             else{
    7.                 CreateTerrain();
    8.             }
    And when the CreateTerrain() function is called I also fail to see where the object is again specified anywhere:

    Code (CSharp):
    1. public void CreateTerrain(TerrainData terrain, ){  
    2.         //fire up the progress bar
    3.         ShowProgressBar(1, 100);
    4.         TerrainData terrain = new TerrainData();
    5.         terrain.heightmapResolution = 512f;
    6.         GameObject terrainObject = Terrain.CreateTerrainGameObject(terrain);
    7.         Undo.RegisterCreatedObjectUndo(terrainObject, "Object to Terrain");
    8.         MeshCollider collider = Selection.activeGameObject.GetComponent<MeshCollider>();
    9.         CleanUp cleanUp = null;
    10.         //Add a collider to our source object if it does not exist.
    11.         //Otherwise raycasting doesn't work.
    12.         if(!collider){
    13.             collider = Selection.activeGameObject.AddComponent<MeshCollider>();
    14.             cleanUp = () => DestroyImmediate(collider);
    15.         }
    16.         Bounds bounds = collider.bounds;  
    17.         float sizeFactor = collider.bounds.size.y / (collider.bounds.size.y + addTerrain.y);
    18.         terrain.size = collider.bounds.size + addTerrain;
    19.         bounds.size = new Vector3(terrain.size.x, collider.bounds.size.y, terrain.size.z);
    20.         //height Calculations
    21.        terrain.SetHeights(0, 0, heights)
    So the "terrain" variable being manipulated is my Terrain Object, but it was set = new TerrainData(); . So how can I manipulate this so that I can attach it to my mesh objects that I want to convert?

    Additionally, I'm somewhat confused with the TerrainData object I'm supposed to build out, what do I populate it with and how do I connect the two?
  2. Kurt-Dekker


    Mar 16, 2013
    Line 8 gets the Mesh off of the MeshCollider of the currently selected object.

    This code is heavily laced with UnityEditor only functions. To make it work at runtime you will need to extract the world heights off that collider and "imprint" the terrain heightmap with them.

    For a precise example of what is involved, you're welcome to study this utility that I put together with another fellow here on the forum:

    Full source linked in comments:

    This little demo from my MakeGeo project might be useful too:

    MakeGeo is presently hosted at these locations: