Convert Prototypes To Detail Mesh GameObjects

Dictionary<int, GameObject> ConvertPrototypesToDetailMeshGameObjects(TerrainToMeshPrototype[] data, InstantiateGameObjectMethod instantiateGameObjectMethod, float exportPercentage, float yRotation = 360, float yRotationRandomize = 360, float followTerrainSurface = 0, bool followTerrainSurfaceRandomize = false)

Instantiates detail mesh gameObjects based on the TerrainToMeshPrototype[] data.

Returned dictionary key is the grass index inside TerrainData.detailPrototypes array and value holds the parent gameObject containing instantiated detail mesh prefabs with the same index.

//Example of exporting detail meshes from TerrainData 
//and instantiating gameObjects 

//Exporting terrain mesh
Mesh terrainMesh = terrainData.TerrainToMesh().ExportMesh(100, 100);

//Creaing GameObject using terrain mesh
GameObject terrainGO = new GameObject("Terrain");
terrainGO.AddComponent<MeshFilter>().sharedMesh = terrainMesh;
terrainGO.AddComponent<MeshRenderer>().sharedMaterial = new Material(TerrainToMeshUtilities.GetDefaultShader());


//Exporting detail meshes from the TerrainData
TerrainToMeshPrototype[] detailMeshPrototypes = terrainData.TerrainToMesh().ExportPrototypes(TerrainToMeshEnum.Prototype.DetailMesh);


//Instantiating 100% of detail mesh prefabs as gameObjects
Dictionary<int, GameObject> detailMeshGOs = TerrainToMeshUtilities.ConvertPrototypesToDetailMeshGameObjects(detailMeshPrototypes, null, 100);


//Attaching instantiated detail mesh gameObjects to the terrainMesh
foreach (var item in detailMeshGOs)
{
    item.Value.transform.SetParent(terrainGO.transform);
}

Last updated