Terrain To Mesh
  • Terrain To Mesh
  • Quick Start
  • Editor Window Settings
    • Mesh
    • Material
    • Objects
    • Save
  • Update Splatmap Shader
  • Run-time API
    • TerrainToMesh
      • ExportMesh
      • ExportTerrainLayers
      • ExportSplatmapMaterial
      • ExportSplatmapTextures
      • HasHoles
      • ExportHolesmapTexture
      • ExportBasemapDiffuseTexture
      • ExportBasemapNormalTexture
      • ExportBasemapMaskTexture
      • ExportBasemapOcclusionTexture
      • ExportGrassTextures
      • ExportGrassAtlasTexture
      • HasPrototypes
      • CountPrototypes
      • ExportPrototypes
    • TerrainToMeshUtilities
      • GenerateGrassMesh
      • GenerateEdgeFallTexture
      • ConvertMeshToOBJ
      • ConvertMeshToOBJAndSaveToFile
      • ConvertMeshToOBJAndAppendToFile
      • GetDefaultMaterial
      • GetDefaultShader
      • GetDefaultShaderProperty
      • SetupDefaultMaterial
      • SetupAlphaCutoutForDefaultMaterial
      • ConvertPrototypesToTreeGameObjects
      • ConvertPrototypesToGrassGameObjects
      • ConvertPrototypesToGrassMeshes
      • ConvertPrototypesToDetailMeshGameObjects
      • ConvertPrototypesToDetailMeshes
      • CalculateExportedMeshVertexCount
      • SetMeshPivotPoint
    • TerrainToMeshEdgeFall
    • TerrainToMeshPrototype
    • TerrainToMeshConversionDetails
  • Help & Contact
Powered by GitBook
On this page
PreviousConvertPrototypesToGrassMeshesNextConvertPrototypesToDetailMeshes

Last updated 2 months ago

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 [] data.

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

TerrainData may contain thousands of detail mesh objects and instantiating separate GameObject for each one may not be fast and optimized solution.

It is better to combine exported grass objects into a single (or several) meshes and render them in one pass.

//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);
}

TerrainToMeshPrototype
TerrainData.detailPrototypes