Jichael
Jichael

Reputation: 820

Procedurally generated mesh has all normals inverted

Hellos guys,

I'm currently working on a project where I need to read a json file to create a scene at runtime. Serialization/deserialization works fine.

The main problem is, I want to be able to create meshes from 8 points/vertices, I sort of achieve this, but every single normal is inverted, and I don't understand why.

This is my json input for one :

"name": "whatever",
        "points": [
            {
                "x": -1,
                "y": -0.05,
                "z": 1
            },
            {
                "x": -1,
                "y": 0.05,
                "z": 1
            },
            {
                "x": 1,
                "y": 0.05,
                "z": 1
            },
            {
                "x": 1,
                "y": -0.05,
                "z": 1
            },
            {
                "x": -1,
                "y": -0.05,
                "z": -1
            },
            {
                "x": -1,
                "y": 0.05,
                "z": -1
            },
            {
                "x": 1,
                "y": 0.05,
                "z": -1
            },
            {
                "x": 1,
                "y": -0.05,
                "z": -1
            }
        ]

and this is my mesh generation code in c# :

public void GenerateMesh(SerializedMesh data)
{
    Vector3[] vertices = new Vector3[data.points.Count];

    for (int i = 0; i < data.points.Count; i++)
    {
        vertices[i] = new Vector3(data.points[i].x, data.points[i].y, data.points[i].z);
    }

    int[] triangles = {
        // Front face
        0, 1, 2,
        2, 3, 0,

        // Back face
        7, 6, 5,
        5, 4, 7,

        // Left face
        4, 5, 1,
        1, 0, 4,

        // Right face
        3, 2, 6,
        6, 7, 3,

        // Top face
        1, 5, 6,
        6, 2, 1,

        // Bottom face
        4, 0, 3,
        3, 7, 4
    };

    // TODO : check what all this does
    Mesh.Clear();
    Mesh.vertices = vertices;
    Mesh.triangles = triangles;
    //Mesh.RecalculateBounds();
    Mesh.RecalculateNormals();
    //Mesh.RecalculateTangents();
    //Mesh.Optimize();
}

I represented my vertices in this order :

cube

How come everything is inverted ?

Upvotes: 1

Views: 733

Answers (2)

Jichael
Jichael

Reputation: 820

Ok, I was just silly. My "reference" cube had a Y rotation of 180, I thought it would not do any difference but it does.

The problem was that in the json, the Z positions were inverted (1 -> -1 and vice versa)

Everything works fine now, thanks !

Upvotes: 0

Brian Choi
Brian Choi

Reputation: 753

  • Because you tried counter-clockwise winding you should change vertex list or index list as clockwise winding. for your case, -1 for z-axis is front face and 1 is back face so you should set index list as opposite. I just converted first 2 triangles.
int[] triangles = {
        // Back face
        0, 3, 2,
        0, 2, 1,

        ...
        ...
    };

Upvotes: 2

Related Questions