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
PreviousConvertPrototypesToTreeGameObjectsNextConvertPrototypesToGrassMeshes

Last updated 1 month ago

Convert Prototypes To Grass GameObjects

Dictionary<int, GameObject> ConvertPrototypesToGrassGameObjects(TerrainToMeshPrototype[] data, Mesh grassMesh, Dictionary<Texture2D, Material> perLayerMaterial, float exportPercentage, float yRotation = 360, float yRotationRandomize = 360, float followTerrainSurface = 0, bool followTerrainSurfaceRandomize = false)

Instantiates grass gameObjects based on the [] data.

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

TerrainData may contain thousands of grass 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 grass 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 grass from TerrainData
TerrainToMeshPrototype[] grassPrototypes = terrainData.TerrainToMesh().ExportPrototypes(TerrainToMeshEnum.Prototype.Grass);

//Creating Quad mesh for grass rendering
Mesh grassMesh = TerrainToMeshUtilities.GenerateGrassMesh();

//Exporting grass textures from TerrainData
Texture2D[] grassTextures = terrainData.TerrainToMesh().ExportGrassTextures();

//Creating material for each grass
Dictionary<Texture2D, Material> grassMaterials = new Dictionary<Texture2D, Material>();
for (int i = 0; i < grassTextures.Length; i++)
{
    Material material = new Material(TerrainToMeshUtilities.GetDefaultShader());
    material.name = grassTextures[i].name;
    
    //Setting up material to use grass texture for '_MainTex' and enabling Alpha Cutout
    TerrainToMeshUtilities.SetupDefaultMaterial(material, grassTextures[i], true, null, null, null);

    //Adding grass material to the dictionary
    grassMaterials.Add(grassTextures[i], material);
}

//Instantiating 100% of grass gameObjects
Dictionary<int, GameObject> grassGO = TerrainToMeshUtilities.ConvertPrototypesToGrassGameObjects(grassPrototypes, grassMesh, grassMaterials, 100);


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

TerrainToMeshPrototype
TerrainData.detailPrototypes