# ConvertPrototypesToGrassGameObjects

### Convert Prototypes To Grass GameObjects

```csharp
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 [TerrainToMeshPrototype](https://amazing-assets.gitbook.io/terrain-to-mesh/run-time-api/terraintomeshprototype)\[] data.

Returned dictionary ***key*** is the grass index inside [TerrainData.detailPrototypes](https://docs.unity3d.com/ScriptReference/TerrainData-treePrototypes.html) array and ***value*** holds the parent gameObject containing instantiated grass prefabs with the same index.

<div align="left"><figure><img src="https://1477282451-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIJi2f6hCjtiGR5oUl9gw%2Fuploads%2Fyz8cucIpbgpdDsMb6hT5%2Fdetail_index.png?alt=media&#x26;token=ed6c7f7d-85f5-4039-b02c-5c97963a4b5a" alt="" width="396"><figcaption></figcaption></figure></div>

{% hint style="danger" %}
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.
{% endhint %}

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