Single Mesh Quad generates 4 triangles instead of 2?

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:

using UnityEngine;

[RequireComponent(typeof(MeshFilter))]
[RequireComponent(typeof(MeshRenderer))]
public class TestQuadScript : MonoBehaviour
{
    void Start()
    {
        MeshFilter meshFilter = GetComponent<MeshFilter>();
        meshFilter.mesh = BuildQuad(1f, 1f);
    }
    private Mesh BuildQuad (float width, float height)
    {
        Mesh mesh = new Mesh ();
     
        // Setup vertices
        Vector3[] newVertices = new Vector3[4];
        float halfHeight = height * 0.5f;
        float halfWidth = width * 0.5f;
        newVertices [0] = new Vector3 (-halfWidth, 0,-halfHeight);
        newVertices [1] = new Vector3 (-halfWidth, 0,halfHeight);
        newVertices [2] = new Vector3 (halfWidth, 0,-halfHeight);
        newVertices [3] = new Vector3 (halfWidth, 0,halfHeight);
     
        // Setup UVs
        Vector2[] newUVs = new Vector2[newVertices.Length];
        newUVs [0] = new Vector2 (0, 0);
        newUVs [1] = new Vector2 (0, 1);
        newUVs [2] = new Vector2 (1, 0);
        newUVs [3] = new Vector2 (1, 1);
     
        // Setup triangles
        int[] newTriangles = new int[6] { 0, 1, 2, 3, 2, 1 };
     
        // Setup normals
        Vector3[] newNormals = new Vector3[newVertices.Length];
        for (int i = 0; i < newNormals.Length; i++) {
            newNormals [i] = Vector3.forward;
        }
     
        // Create quad
        mesh.vertices = newVertices;
        mesh.uv = newUVs;
        mesh.triangles = newTriangles;
        mesh.normals = newNormals;
        mesh.RecalculateNormals(); // ? Better to use?
        return mesh;
    }
}
  1. I now have 6 Tris and 12 Verts? What am I doing wrong 2x the number expected? [Image 2 attached below]

  2. Is RecalculateNormals faster than the code above?

  3. 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!

7002962--827792--emptyproject.gif
7002962--827795--empty2.gif

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


How to get 0 verts in an empty scene:

  • Remove skybox
  • In Camera inspector set HDR to Off
  • 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

2 Likes

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.

1 Like

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.

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?