Hi guys. I've been working yesterday and today on a little custom terain editor and I'm getting some decent results. Blending of textures is done by RGB blending and height is done by the alpha channel of the vertex. I can paint these in unity as it goes.
But there's on problem that I haven't been able to solve. That is the issue of the mesh collider on the plane. As you can see in the picture below the collider does not follow the displaced terrain. That in itself isn't that strange, as the collider uses the same mesh as the plane(which is flat but displaced in the vertex shader).
However, after painting the height of the terrain I want to be able to update the collider to follow said terrain.
I thought of trying to instantiate the current mesh info to a new mesh and assigning that as the collider. But when doing that it is taking the original mesh data, and not the displaced positions. I found that there is a BakeMesh() function for SkinnedMeshRenderer but that's not available for regular MeshRenderers.
Is there any way at all to find the current position of the vertices of a displaced mesh?
Replies
Good thing you kept the mesh small, by the way, because mesh colliders can take quite a while for PhysX to process when they're first assigned at runtime. If you use a lot of large meshes, it can freeze your game for several seconds.
I'm also assuming that it's the same mesh you're using for both rendering and collision. So you'll edit that mesh for the collision to update... but then your vertices will be further modified by the shader, pushing them higher/lower than they currently are already.
Your best bet would be to not touch the vertex positions in the shader and instead just write directly to the mesh, that way both the mesh and the collision will update and stay in sync - it'll also be cheaper to render.
@RC-1290
I saw that page before and tried to work with that but it gave me a crapton of trouble. After you suggested it again I took a better look and I finally solved it. I had to assign all the verts to a new array, then multiply the inverse of the matrix containing the old verts, and assigning those to the new vert array. If I didn't, all the verts would go haywire if the object wasn't in 0,0,0 position.
@Farfarer
Yea I only edited in the height in the shader to get a result, since I wasn't yet able to get the height displacement of vertices correct yet.
Thanks guys.
If there is an easier way I wouldn't mind if you pointed me in the right direction. At the moment though it's working pretty well, I'm pleased to have gotten further than I previously imagined I would.