Home Unity

How to get a vertex position after the vert has been displaced?

polycounter lvl 5
Offline / Send Message
2cat polycounter lvl 5
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?

YdNyjPt.png

Replies

  • RC-1290
    Offline / Send Message
    RC-1290 polycounter lvl 7
    You say that you modify the height using the alpha value of the vertex color. What is stopping you from using that height value and assigning it to the y value of the vertex position, instead of to the vertex color?

    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.
  • Farfarer
    You'll have to update the mesh you're using for the collider manually. You're currently editing the mesh as part of a shader and the collision mesh doesn't do anything like that - it's getting the undisplaced base mesh that's being fed into the shader. So after any height edits, you'll need to adjust the mesh used for collision.

    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.
  • 2cat
    Offline / Send Message
    2cat polycounter lvl 5
    Thanks for your posts guys. Sorry for the late response.

    @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.
  • RC-1290
    Offline / Send Message
    RC-1290 polycounter lvl 7
    Good to hear you solved it!
    2cat wrote: »
    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.
    The positions should be local (object space), they should not depend on the transform position of any GameObject using it. Were you using world-space coordinates perhaps?
  • 2cat
    Offline / Send Message
    2cat polycounter lvl 5
    I'm not a scripting wizard, far from it. But since I wanted to paint on objects in the Unity editor the only way I was able to paint on objects was with a Matrix(which I right now have a hard time with).

    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.
Sign In or Register to comment.