I’m trying to generate a terrain on top of random meshes for a player to walk on. I figured, I would then hide the terrain and the players camera would do a Raycast from above the terrain to always stay on top. This way I would only need to do one Raycast per frame instead of checking all objects and also get a smoother walking experience.
Here’s an image of my hoped result (sideview). The red planeGeometry gets wrapped closely on top of all geometry:
My first approach is the following:
- Position of Objects
- Create a Plane covering all objects with a relative amount of vertices (depending on the size)
- Go through each vertice and do a raycast between that point and the group of objects below
- Move the vertice down for the calculated distance (results in red line)
This works quite well. The only problem is doing the Raycasts, which seem to be a huge bottleneck. It takes about 5 seconds)
I am now wondering about more performant approaches. One idea I have is to use a MeshDepthMaterial on the objects, then render a height map from that and then apply that heightmap to the plane. (Although I don’t know yet if the depthmaterial is relative to the camera distance globally or per mesh)
Another idea would be to somehow quickly generate a lot of points on the surface of the models and then apply a triangulation algorithm on that set of points. Something like this: Triangulation
I hope my question is clear and would be very happy for some advice or other ideas.