Hello,
I am trying to integrate unity’s terrain with a custom grass renderer and the renderer needs positions in order to render the grass pieces. My best approach so far has been to export the terrain as a mesh and paint vertex colors on that separate mesh using PolyBrush and use the vertex position/color data to calculate position/density of grass. This work-around works but it would be a lot nicer if I would be able to somehow read the data from the splat maps of the terrain (because as far as I know the terrain textures are stored in the rgba channels). So if anyone knows a way to paint vertex colors on the terrain like you can do on a regular mesh using PolyBrush. Or a way to at least read the texture layer data than I would like to hear how. Or some way to convert the terrain into a unity mesh class so I can do it easier in the editor. (because I’m currently using this to export the mesh as obj and I don’t really understand how it works so I kind of need a push in the right direction as to how to get the vertices)
Thanks for any advice in advance.
I managed to solve this problem so for anyone having the same problem I used this script to solve it (kind of)
List<Vector3> GetPositions(Terrain terrain, Texture2D mask, float range, int instanceCount)
{
var positions = new List<Vector3>();
int vertexLimit = instanceCount / mask.width * mask.height; // Grass Pos Density
// Diagnostics and debugging
Debug.Log("Per Vertex Limit: " + vertexLimit);
var iterations = 0;
var timer = System.Diagnostics.Stopwatch.StartNew();
for (int x = 0; x < mask.width; x++)
{
for (int z = 0; z < mask.height; z++)
{
for (int i = 0; i < vertexLimit * mask.GetPixel(x, z).g; i++) // Read Density
{
var pos = new Vector3();
Vector2 rndPoint = Random.insideUnitCircle * range;
pos.x = rndPoint.x;
pos.z = rndPoint.y;
pos += new Vector3((terrain.terrainData.size.x / mask.width) * x, 0, (terrain.terrainData.size.z / mask.height) * z);
pos.y = terrain.SampleHeight(pos);
positions.Add(pos);
iterations++;
}
}
}
// Diagnostics and debugging
timer.Stop();
Debug.Log("Total time: " + timer.ElapsedMilliseconds + "ms");
Debug.Log("Total Iterations: " + iterations);
return positions;
}
2 Likes