Search Unity

Newbie question: Case of the vanishing vertices

Discussion in 'Shaders' started by Rastar, Aug 22, 2013.

  1. Rastar

    Rastar

    Joined:
    Sep 14, 2012
    Posts:
    73
    Hi,

    this is my first step in to dynamically generating meshes and writing shaders, so please be patient...

    I create two strips of 2x63 triangles as a mesh. First it seems to work, but when I displace them in the vertex shader, e.g. by doing

    vertex.r += 100.0

    things get messy: In the scene view of the editor, those strips quickly vanish when I scroll closer to them (getting closer from left to right, if I zoom in on the right picture the remaining strip vanishes as well):

    $sceneview.png

    Similar in the game view (watching from an FP controller): looking at certain angles I can see both strips, lookinh and moving around one or both vanish:

    $gameview.png

    When they vanish from the view the Draw Call count drops as well. I haven't got a clue what's happening here. Somebody got an idea?
     
  2. Rastar

    Rastar

    Joined:
    Sep 14, 2012
    Posts:
    73
    Somehow the scene view picture didn't make it, here it comes

    $sceneview2.png
     
  3. Farfarer

    Farfarer

    Joined:
    Aug 17, 2010
    Posts:
    2,249
    Once you're done generating your meshes, try calling
    Code (csharp):
    1. mesh.RecalculateBounds();
    I suspect the bounding box info is wrong because the meshes are now far bigger than their bounds, and so they might be being culled when they shouldn't be.


    Other than that, 100 units is a very large distance to displace verts by... it could be simply too far for the culling system to figure out what's actually drawing on-screen.
     
    Last edited: Aug 22, 2013
  4. Rastar

    Rastar

    Joined:
    Sep 14, 2012
    Posts:
    73
    These meshes will be part of a displacement mapped terrain, so eventually I will displace them way more than 100 units. But you are right: If I move the mesh origin rather than displace them in the shader, everything's fine. Thanks for your help!
     
  5. Farfarer

    Farfarer

    Joined:
    Aug 17, 2010
    Posts:
    2,249
    100 units is 100m in-game... I'd probably try not to displace things more than a few metres.

    I think you might be better off displacing the mesh as part of the mesh generation process that way it'll work fine regardless.
     
  6. Rastar

    Rastar

    Joined:
    Sep 14, 2012
    Posts:
    73
    Those meshes are actually patches that are reused across the terrain, drawn several times using DrawMesh() (to save vertex buffers and because these patches can be easily checked for visibility). But if I displace them using the position parameter in DrawMesh() I should be fine, right?