Modifying Geometry in Realtime

So I’ve got a terrain, right?

If I place a hard surface designed object onto my terrain which would be somewhat rocky, how can I flatten that terrain in that spot so that the object looks correct after its been placed?

To expand upon that, if the need arises can I get low enough into the renderer so that I might merge about 20-30 vertices in realtime?

Merging vertices gets hairy as Unity has no built in method to do this. Better solution would probably be to just set the Y coordinate of each vertex’s Vector3 to be identical in order to create a flat space.

Thanks KelsoMRK, that works to a degree, but when I have joining terrains with different normals on the edges, its becomes very obvious that they’re two separate meshes.

Are you using the Unity terrain or a generated plane mesh terrain?

A modeled Plane-ish mesh terrain. Basically a 6-sided cubesphere terrain.

then you can use the Mesh class and change the vertices positions as you want

I don’t know much about the Unity terrains,but if your terrain is a generated mesh,you might try this (but could be slow) :
-Place the rock where you want it to be
-Get the closest point (Closest point on mesh / collider - Questions & Answers - Unity Discussions) on the terrain relative to the rock
-Get an array of the surrounding vertices of the closest vert on the terrain and get them at the same level as the closest vertex on the rock relative to the closest point on the terrain O.o
[If the center of the rock is under the terrain mesh you would have to substract the mesh.bounds in order to get the lower limit or you would have to instead of getting the closest point on the rock relative to the terrain closest point,to define directly which vert is the lowest on the rock (if you had a simple plane that would be easy,but on a quadsphere…)]

You might also want to use some sort of interpolation (i think it’s called) so you won’t get very rough results.You can try Mathf.Lerp.

I haven’t tested it,tho.Especially on QuadSpheres…That might be even more complicated.
About the performance : 20-30 vertices shouldn’t really be a problem.It depends on the size of the mesh since it would have to loop and check the distance of each vert.You can use Threads if it is too slow.

Hope it helps! :slight_smile: