Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Procedural Mesh not rendering at verticies

Discussion in 'Scripting' started by prattmyster, Feb 14, 2022.

  1. prattmyster

    prattmyster

    Joined:
    Apr 15, 2015
    Posts:
    17
    im trying to get a mesh plane of 16 segments to render from a point to the mouse position.
    i have a debug.drawline that draws a horizontal line from the left hand side of where the mesh should be to the right and it seems to work. However when the mesh renders its completely off where the verticie locatons actually are.

    public void DrawBlueprintRoad(float width, Vector3 startPos, Vector3 mousePos)
    {
    if (startPos != mousePos)
    {
    Mesh mesh = new Mesh();
    mesh.name = "Blueprint_Mesh";
    blueprintHolder.GetComponent<MeshFilter>().sharedMesh = mesh;
    int segments = 16;
    verts = new Vector3[segments * 3];
    tris = new int[(segments - 1) * 12];
    Vector2[] uvs = new Vector2[segments * 3];
    float lengthDifference = Vector3.Distance(startPos, mousePos);
    float segmentLength = lengthDifference / segments;
    currentNode.transform.LookAt(mousePos);

    //Create Verts
    for (int i = 0; i < segments; i++)
    {
    Vector3 drawPoint = Vector3.Lerp(startPos, mousePos, (segmentLength / lengthDifference) * i);
    Vector3 vertL = drawPoint + (currentNode.transform.right * (width / 2));
    Vector3 vertR = drawPoint - (currentNode.transform.right * (width / 2));
    verts[i * 3] = vertL;
    verts[i * 3 + 1] = drawPoint;
    verts[i * 3 + 2] = vertR;
    Debug.DrawLine(vertL, vertR);
    float y = i / segments;
    uvs = new Vector2(0, y);
    uvs[i + 1] = new Vector2(0.5f, y);
    uvs[i + 2] = new Vector2(1, y);
    }
    //Create Triangle
    for(int i = 0; i < segments - 1; i++)
    {
    int tri0 = i * 3;
    int tri1 = i * 3 + 1;
    int tri2 = i * 3 + 2;
    int tri3 = i * 3 + 3;
    int tri4 = i * 3 + 4;
    int tri5 = i * 3 + 5;

    tris[i * 12] = tri2;
    tris[i * 12 + 1] = tri5;
    tris[i * 12 + 2] = tri1;
    tris[i * 12 + 3] = tri5;
    tris[i * 12 + 4] = tri4;
    tris[i * 12 + 5] = tri1;
    tris[i * 12 + 6] = tri4;
    tris[i * 12 + 7] = tri3;
    tris[i * 12 + 8] = tri0;
    tris[i * 12 + 9] = tri4;
    tris[i * 12 + 10] = tri1;
    tris[i * 12 + 11] = tri0;
    }
    mesh.vertices = verts;
    mesh.triangles = tris;
    mesh.uv = uvs;
    mesh.Optimize();
    }
    }
     

    Attached Files:

  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,749
    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/

    Mesh positions are local, so the Transform on the mesh renderer is applied.

    If you use those positions in Debug.DrawRay you need to similarly transform those dots, because DrawRay is world coordinates.
     
    prattmyster likes this.
  3. prattmyster

    prattmyster

    Joined:
    Apr 15, 2015
    Posts:
    17
    thank you for your help. I've removed the blueprint holder from its parent and set it to vector3.zero now the mesh is rendering fine :)