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

Single Mesh Quad generates 4 triangles instead of 2?

Discussion in 'Scripting' started by timfrombriz, Apr 3, 2021.

  1. timfrombriz

    timfrombriz

    Joined:
    Jun 23, 2014
    Posts:
    30
    Hi.

    I have four things I cant wrap my head around.

    1. When I have an empty scene with a camera and directional light, I have 2 Tris and 4 Verts by default. I went to lighting and deleted the skybox and now have a blue background. Why do I have an empty scene with Tris and Verts already? [Image 1 attached below]

    I generate a single mesh with the following code:

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3.  
    4. [RequireComponent(typeof(MeshFilter))]
    5. [RequireComponent(typeof(MeshRenderer))]
    6. public class TestQuadScript : MonoBehaviour
    7. {
    8.     void Start()
    9.     {
    10.         MeshFilter meshFilter = GetComponent<MeshFilter>();
    11.         meshFilter.mesh = BuildQuad(1f, 1f);
    12.     }
    13.     private Mesh BuildQuad (float width, float height)
    14.     {
    15.         Mesh mesh = new Mesh ();
    16.      
    17.         // Setup vertices
    18.         Vector3[] newVertices = new Vector3[4];
    19.         float halfHeight = height * 0.5f;
    20.         float halfWidth = width * 0.5f;
    21.         newVertices [0] = new Vector3 (-halfWidth, 0,-halfHeight);
    22.         newVertices [1] = new Vector3 (-halfWidth, 0,halfHeight);
    23.         newVertices [2] = new Vector3 (halfWidth, 0,-halfHeight);
    24.         newVertices [3] = new Vector3 (halfWidth, 0,halfHeight);
    25.      
    26.         // Setup UVs
    27.         Vector2[] newUVs = new Vector2[newVertices.Length];
    28.         newUVs [0] = new Vector2 (0, 0);
    29.         newUVs [1] = new Vector2 (0, 1);
    30.         newUVs [2] = new Vector2 (1, 0);
    31.         newUVs [3] = new Vector2 (1, 1);
    32.      
    33.         // Setup triangles
    34.         int[] newTriangles = new int[6] { 0, 1, 2, 3, 2, 1 };
    35.      
    36.         // Setup normals
    37.         Vector3[] newNormals = new Vector3[newVertices.Length];
    38.         for (int i = 0; i < newNormals.Length; i++) {
    39.             newNormals [i] = Vector3.forward;
    40.         }
    41.      
    42.         // Create quad
    43.         mesh.vertices = newVertices;
    44.         mesh.uv = newUVs;
    45.         mesh.triangles = newTriangles;
    46.         mesh.normals = newNormals;
    47.         mesh.RecalculateNormals(); // ? Better to use?
    48.         return mesh;
    49.     }
    50. }

    2. I now have 6 Tris and 12 Verts? What am I doing wrong 2x the number expected? [Image 2 attached below]

    3. Is RecalculateNormals faster than the code above?

    4. When posting here with an image URL tag, whats the best image hosting service? I tried imgur but the link shows a picture with a red X when linking (like below). What's the preferred means to upload files when posting here?




    Cheers for your help!
     

    Attached Files:

  2. timfrombriz

    timfrombriz

    Joined:
    Jun 23, 2014
    Posts:
    30
    Also, in this 30x30 grid of quads I populate, my Orthographic camera renders so many Tris and Verts for the rendered gameview?

    I have commented out OnGui() and deactivated Canvas's to get this number?

    Im confused
     

    Attached Files:

  3. villevli

    villevli

    Joined:
    Jan 19, 2016
    Posts:
    86
    How to get 0 verts in an empty scene:
    1. Remove skybox
    2. In Camera inspector set HDR to Off
    3. In Camera inspector set MSAA to Off
    You can use Window > Analysis > Frame Debugger to find out what Unity renders. You can click on each draw call and in the Preview tab see how many verts it renders in that draw call.

    In your 30x30 quads case I would expect the scene to contain a total of 30x30x2 = 1800 triangles and the 1.7k in your stats seems to be close to that. Do you expect less triangles due to not all of the quads being visible to the camera? If frustum culling worked as expected I would expect less than 1800 but my guess is that Unity has combined all the quads into one large mesh because they have the same material and you have static or dynamic batching enabled.

    Remember though that without batching you would have 900 draw calls if all the quads were visible at once and that would be much worse for performance so in this case it's better to render 1800 triangles in just a few draw calls
     
    timfrombriz and Kurt-Dekker like this.
  4. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,528
    Note that the rendering stats window is not a triangle or vertex staticsts of your scene, but a rendering statistic. So the number of tris / verts listed there are how many verts / tris has been rendered per frame. A two pass shader would render the same triangles twice. Likewise fullscreen post processing effects usually add another fullscreen quad (2 triangles) to the statistics. See Rendering Statistics Window for more details.
     
    timfrombriz likes this.
  5. timfrombriz

    timfrombriz

    Joined:
    Jun 23, 2014
    Posts:
    30
    Thanks for your reply, appreciated. I guess another way to approach that would be to break the grid into chunks, that way you get the benefits of batching and frustrum culling whilst adding a few draw calls if more than one chunk was visible at the same time in the camera view.
     
  6. timfrombriz

    timfrombriz

    Joined:
    Jun 23, 2014
    Posts:
    30
    These numbers are for both the scene and game view? If you close the scene view, does that guarantee the game view is only counted?