Object2Terrain Code Manipulation

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?

if(GUILayout.Button("Create Terrain")){
            if(Selection.activeGameObject == null){
                EditorUtility.DisplayDialog("No object selected", "Please select an object.", "Ok");
                return;
            }
            else{
                CreateTerrain();
            }

And when the CreateTerrain() function is called I also fail to see where the object is again specified anywhere:

public void CreateTerrain(TerrainData terrain, ){   
        //fire up the progress bar
        ShowProgressBar(1, 100);
        TerrainData terrain = new TerrainData();
        terrain.heightmapResolution = 512f;
        GameObject terrainObject = Terrain.CreateTerrainGameObject(terrain);
        Undo.RegisterCreatedObjectUndo(terrainObject, "Object to Terrain");
        MeshCollider collider = Selection.activeGameObject.GetComponent<MeshCollider>();
        CleanUp cleanUp = null;
        //Add a collider to our source object if it does not exist.
        //Otherwise raycasting doesn't work.
        if(!collider){
            collider = Selection.activeGameObject.AddComponent<MeshCollider>();
            cleanUp = () => DestroyImmediate(collider);
        }
        Bounds bounds = collider.bounds;   
        float sizeFactor = collider.bounds.size.y / (collider.bounds.size.y + addTerrain.y);
        terrain.size = collider.bounds.size + addTerrain;
        bounds.size = new Vector3(terrain.size.x, collider.bounds.size.y, terrain.size.z);
        //height Calculations
       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?

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:

https://www.youtube.com/watch?v=FykNmJ3NIpI

Full source linked in comments:

https://gist.github.com/kurtdekker/f9e7b0bdf4b2f9c0455a99f7f0f4a77a

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

https://www.youtube.com/watch?v=PvRsMuo4AlA

MakeGeo is presently hosted at these locations:

https://bitbucket.org/kurtdekker/makegeo