Search Unity

Procedural mesh renders as just one triangle using any non-sprite material

Discussion in 'Scripting' started by TimWillebrands, Nov 9, 2018.

  1. TimWillebrands

    TimWillebrands

    Joined:
    Feb 6, 2017
    Posts:
    3
    I'm doing a line-of-sight learning project (somewhat based on this: https://ncase.me/sight-and-light/) but I'm running into trouble when I attempt to render my generated view-mesh with any material that's not Sprite or UI: only the first triangle of the mesh is rendered. What is so strange to me is that the mesh is rendered perfectly when using the sprite material.
    This is an issue as I wanted to try writing a ReplacementShader for a 2nd camera that turns the screen black and you can only see trough the view-mesh.

    I'm guessing the problem has to do with the way I generate the mesh but I don't know enough about this topic to get very far:
    Code (CSharp):
    1.  
    2. private void UpdateMesh(Vector2[] vertices2D){
    3.     var vertices3D = System.Array.ConvertAll<Vector2, Vector3>(vertices2D, v => v);
    4.     var colors = Enumerable.Range(0, vertices3D.Length)
    5.         .Select(i => Color.Lerp(Color.black, Color.clear,.5f))
    6.         .ToArray();
    7.  
    8.     if(mesh == null){
    9.         mesh = new Mesh();
    10.         gameObject.AddComponent<MeshRenderer>();
    11.         GetComponent<MeshRenderer>().material = new Material(Shader.Find("Sprites/Diffuse"));
    12.         gameObject.AddComponent<MeshFilter>();
    13.     }
    14.  
    15.     if(GetComponent<MeshRenderer>())
    16.         GetComponent<MeshRenderer>().enabled = true;
    17.  
    18.     var triangles = new int[3 * (vertices3D.Length - 1)];
    19.     int C1 = 0;
    20.     int C2 = 1;
    21.     int C3 = 2;
    22.  
    23.     for(int x = 0; x < triangles.Length-3; x+=3) {
    24.         triangles[x] = C1;
    25.         triangles[x+1] = C2++;
    26.         triangles[x+2] = C3++;
    27.     }
    28.    
    29.     triangles[triangles.Length - 3] = 0;
    30.     triangles[triangles.Length - 2] = 1;
    31.     triangles[triangles.Length - 1] = C3-1;
    32.  
    33.     mesh.Clear();
    34.     mesh.vertices = vertices3D;
    35.     mesh.triangles = triangles;
    36.     mesh.colors = colors;
    37.    
    38.     //mesh.RecalculateNormals();
    39.     //mesh.RecalculateBounds();
    40.     GetComponent<MeshFilter>().mesh = mesh;
    41. }
    42.  
    upload_2018-11-9_16-29-50.png

    If anyone wants to take a look at my project (beware horribly formatted code from an absolute amateur) I've added it to the attached files. Any other comments are also appreciated :)
     

    Attached Files:

  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Quick check if its rendering on the backside?
    If yes - your triangle winding is backwards
     
  3. TimWillebrands

    TimWillebrands

    Joined:
    Feb 6, 2017
    Posts:
    3
    Does the sprite shader disregard the triangle orientation? But it would make sense.
     
  4. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    sprite shader probably set to render both sides of a triangle.
    Whatever your other shader is might not
     
  5. TimWillebrands

    TimWillebrands

    Joined:
    Feb 6, 2017
    Posts:
    3
    Welp now I feel silly, but it was indeed the triangle backface thing. Thanks!