Collision with custom terrain (heightmaps)

Hello everyone!

I have been working on a custom runtime terrain which uses height maps that are render targets. I just paint on them and assign it to the painted texture to the world pos input in its material.

Instead of just raising and lowering height, it pushes the vertices towards or away from the camera view and creates slopes and things like that. (You can see in the pic)

The grid you see is collision. I can’t manage to make the collision fit the height map :frowning:

What I tried-
I added a procedural mesh component and made a mesh from the flat terrain mesh. Then I cached all the vertex locations on begin-play. When I finished painting the terrain, I looked up the pixel data for each vert in the procedural mesh and set it’s value to match. (Quite literally making a mesh from a height-map).
Here I expected the procedural mesh verts to snap to the location values of the corresponding pixel in the height-map, but the whole mesh disappears. It’s also painfully slow when looking up pixel values.

What works well in terms of performance: I can go through every vert in the procedural mesh and set its Z pos to be a random number. When I paint, the mesh updates very quickly. The only problem is when I retrieve the Z value from the height map, it’s soooo so slow.

So in a nutshell, how could I go about creating the collision for the height map? Am I on the right track?

Any push in a direction would be greatly appreciated :slight_smile:

I am currently on the same path as you using World Position Offset in unreal 5, accessing the height map texture CPU side is going to be very expensive. If possible, you might be able to cache the texture into a TArray and use that to access the height map data more quickly than a texture. The problem is when you need to go between Texture2D and TArray which is where you’ll see slow down again.

I am thinking of making a system that uses a compute shader to either generate the collision mesh at runtime, or output the height data in a more digestible format on the CPU end. Did you manage to get anywhere on your solution? I am still trying to figure out where to start and what method might be best.