Search Unity

[Solved] Issue with a mesh

Discussion in 'Scripting' started by Guit0x, Mar 28, 2019.

  1. Guit0x

    Guit0x

    Joined:
    Mar 15, 2019
    Posts:
    7
    Hi guys,

    I'm trying to render a mesh based on ray cast points. But when the source point move, the mesh move in a weird way.

    Here a video that reproduce the behavior of the mesh (first 10 seconds without mesh then with mesh) :



    As you can see, there is a huge difference between the ray cast (green line) and the mesh when moving.

    And the update function for the mesh :

    Code (CSharp):
    1. private void UpdateMesh()
    2. {
    3.     int totalVertices = m_hitRays.Count * 3;
    4.  
    5.     Vector3[] vertices = new Vector3[totalVertices];
    6.  
    7.     int[] triangles = new int[totalVertices];
    8.     for(int i = 0; i < triangles.Length; ++i) {
    9.         triangles[i] = i;
    10.     }
    11.  
    12.     for(int i = 0; i < m_hitRays.Count; ++i) {
    13.         vertices[i * 3] = m_castOrigin;
    14.         vertices[(i * 3) + 1] = m_hitRays[i].point;
    15.         int nextRay = i + 1;
    16.         if(nextRay < m_hitRays.Count) {
    17.             vertices[(i * 3) + 2] = m_hitRays[nextRay].point;
    18.         }
    19.         else {
    20.             vertices[(i * 3) + 2] = m_hitRays[0].point;
    21.         }
    22.     }
    23.  
    24.     Mesh mesh = GetComponent<MeshFilter>().mesh;
    25.     mesh.Clear();
    26.     mesh.vertices = vertices;
    27.     mesh.triangles = triangles;
    28. }
    I don't know what I'm doing wrong, if anyone had an idea it would be great :).

    Thank you.
     
    Last edited: Mar 28, 2019
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    Your triangles are presently:

    0,1,2
    3,4,5
    6,7,8

    By lines 7 through 10.

    What you want is:

    0,1,2
    0,2,3
    0,3,4
    0,4,5

    If I read your vertex calculations properly. (Clearly I misread; see below)

    Also, you need to have three times as many int[] record in your triangles as you have triangles... and your vert count should be the count of hitrays + 1 (center).

    So... you're really close... just a few more twiddles!
     
    Last edited: Mar 28, 2019
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    Actually I am going to take back what I said above. I misread how you're generating verts... you're actually doing it as ALL separate vertices and it seems reasonable now.

    I'm not actually sure anymore why you're not seeing what you want to see.

    Make a few no-collider spheres in the scene and just constantly move their position to the hitpoints in realtime, make sure those hitpoints are really where you think they are.
     
  4. Guit0x

    Guit0x

    Joined:
    Mar 15, 2019
    Posts:
    7
    Thanks for reply.

    Yes, exactly.

    Ok so I tested and hit points are good :



    I don't really know why the mesh doesn't match correctly, the shape seems to be good but not the global mesh's position.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    Oh I think I see it: you want to be putting LOCAL coordinates into your verts. The coordinates that come back on a raycast hit are global. Therefore when you move your light mesh, its position is added to already-world coordinates.

    Should be able to fix it just by removing the transform.position of the light mesh, assuming you're not rotating or scaling that GameObject at all, which I assume you are not.
     
    Guit0x likes this.
  6. Guit0x

    Guit0x

    Joined:
    Mar 15, 2019
    Posts:
    7
    Ohhhh Thank you Kurt ! That was it :)

    So to fix that, I removed the mesh from the light point object and created a separate game object for it. Then I passed the mesh object to the ray cast script (that is attached to the light point) and now it works properly !



    Now it's time to optimize !

    Thanks again ;)
     
    Kurt-Dekker likes this.
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    EXCELLENT! I love it when a mesh comes together.

    Here's a thought though: you probably don't want to leave that GameObject at (0,0,0) if you plan on going far. It is unclear to me if your game world is larger than your screen or not, but if it is, you want to keep the GameObject that contains the light mesh close to its rough center. This way culling will work properly, as the bounding box on the mesh will be more useful.

    But none of that may matter if a) you are not scrolling very far (or at all), or b) you expect the light mesh to always be visible because that's where the player is.

    In any case, be sure you call .RecalculateBounds() on the mesh if you do move the camera away, otherwise the view bounds might not be correct and your mesh may get culled away. It's possible Unity now recalculates the bounds for you when you set the mesh however.
     
    Guit0x likes this.
  8. Guit0x

    Guit0x

    Joined:
    Mar 15, 2019
    Posts:
    7
    I don't yet know what i'm going to plan but thanks for advice ;)