How do I flatten part of mesh, based on another object?
I have a homemade procedurally generated terrain that is Mesh based, so -not- Unitys Terrain. Now when I generate the terrain I place a structure somewhere randomly on the terrain, and I want to flatten the mesh below it.
My approach to this, was to take the bounds of the structure, intersect it with the mesh terrain, and then increase the height of the affected vertices- The thing is, my approach is super slow, as I manually have to go through all the vertices of the mesh. Here's the method I wrote:
public void FlattenTerrain(Bounds bounds, float desiredHeight)
{
foreach(var chunk in Chunks)
{
// If the chunk doesn't intersect the bounds, do nothing
if (!chunk.PrimaryMesh.bounds.Intersects(bounds))
continue;
for(int i = 0; i < chunk.PrimaryMesh.vertices.Length; i++)
{
if (bounds.Contains(chunk.PrimaryMesh.vertices[i]))
chunk.PrimaryMesh.vertices[i].y = desiredHeight;
}
chunk.PrimaryMesh.RecalculateBounds();
}
}
There are two issues with this- First off, mesh.bounds is always local space, which means that if my bounds intersect one, it will intersect all (as all the meshes of the chunk will have the same local coords), but this issue I can get around. The second is that I go through every single vertex, which is super expensive, and takes ages- Even though this is done at load time, it still takes far too long - We're talking several minutes here.
My question would be: Are there an easy way to get the vertices of the intersecting bounds, OR if there's generally a smarter way of doing this?
Thanks for your time :-)
Your answer
Follow this Question
Related Questions
Getting intersection points between a plane and an object 0 Answers
Procedural World O'neill Cylinder and gravity 2 Answers
Find Intersection Point Two Sphere 0 Answers
Help setting UV coordinates for code-generated plane 0 Answers
Clear intersection Texture 0 Answers