- Home /
Can I modify grass or details on the terrain at runtime?
Can i have opportunity to remove a type of grasslayer via scrpt? for example when my cube collide on a selected type of grass,the grass die.Is it possible?
Here's an article about removing grass from a Unity terrain and its limitations: http://bit.ly/1aQb0W9
Answer by duck · Mar 18, 2010 at 10:50 AM
You can use these (undocumented) commands to read and write the terrain detail layers:
// read all detail layers into a 3D int array: int numDetails = terrainData.detailPrototypes.Length; int [,,] detailMapData = new int[terrainData.detailWidth, terrainData.detailHeight, numDetails]; for (int layerNum=0; layerNum < numDetails; layerNum++) { int[,] detailLayer = terrainData.GetDetailLayer(int x, int y, int width, int height, int layerNum); }
// write all detail data to terrain data: for (int n = 0; n < detailMapData.Length; n++) { terrainData.SetDetailLayer(0, 0, n, detailMapData[n]); }
So, using the above scripts, you would read the layers, edit the values in the array relating to the details at a particular area, and then write the details back to the terrainData.
Because these functions are undocumented, any future updates of the unity engine might change or remove these functions from the API. This means your project may not work in future versions of the Unity editor, and webplayer builds may not work with future versions of the plugin.
i made a script that alters the materials emission of my grass prefab, i have added it as a detail mesh and i have tried everything to try and get the material to update.
now let me explain in more detail, my material is a HDRP lit, my script compares the distance between the player and the grass transforms, if it is less than the specified range then the materials emission changes, this all works, specifically it works when placing the prefab in the scene, but not when painting the prefab on the terrain, i have the nature render er Asset pack to render the materials, and have no idea how to go about making the material in my prefab, in my terrain detailmesh update.
Answer by runevision · Mar 18, 2010 at 10:51 AM
I assume you are talking about grass on the built-in terrains in Unity.
You can modify the terrain at runtime. See this Q&A:
http://answers.unity3d.com/questions/2224/just-how-malleable-are-terrains-really
You can use that functionality to alter where the grass is on a terrain.
Your answer
Follow this Question
Related Questions
How can I automatically place grass and other details on my terrain to correspond with the splatmap? 5 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Add Detail Mesh at runtime 1 Answer
Painting grass with detail mesh and grass texture? 0 Answers
Detail mesh pipeline 1 Answer