Ejemplo n.º 1
0
    public static void CreateCube(ProceduralMesh mesh, float width, float height, float depth, Vector3 center, Rect[] uvRect, int excludeFaces = 0)
    {
        float halfWidth = width * 0.5f;
        float halfHeight = height * 0.5f;
        float halfDepth = depth * 0.5f;

        bool hasFrontFace = (excludeFaces & (int)FaceDirection.FRONT) == 0,
        hasTopFace = (excludeFaces & (int)FaceDirection.TOP) == 0,
        hasRightFace = (excludeFaces & (int)FaceDirection.RIGHT) == 0,
        hasBackFace = (excludeFaces & (int)FaceDirection.BACK) == 0,
        hasBottomFace = (excludeFaces & (int)FaceDirection.BOTTOM) == 0,
        hasLeftFace = (excludeFaces & (int)FaceDirection.LEFT) == 0;

        int i = mesh.vertexCount;

        // front
        mesh.AddVertex (center + new Vector3 (-halfWidth, halfHeight, -halfDepth));
        mesh.AddVertex (center + new Vector3 (halfWidth, halfHeight, -halfDepth));
        mesh.AddVertex (center + new Vector3 (halfWidth, -halfHeight, -halfDepth));
        mesh.AddVertex (center + new Vector3 (-halfWidth, -halfHeight, -halfDepth));

        // top
        mesh.AddVertex (center + new Vector3 (-halfWidth, halfHeight, halfDepth));
        mesh.AddVertex (center + new Vector3 (halfWidth, halfHeight, halfDepth));
        mesh.AddVertex (center + new Vector3 (halfWidth, halfHeight, -halfDepth));
        mesh.AddVertex (center + new Vector3 (-halfWidth, halfHeight, -halfDepth));

        // right
        mesh.AddVertex (center + new Vector3 (halfWidth, halfHeight, -halfDepth));
        mesh.AddVertex (center + new Vector3 (halfWidth, halfHeight, halfDepth));
        mesh.AddVertex (center + new Vector3 (halfWidth, -halfHeight, halfDepth));
        mesh.AddVertex (center + new Vector3 (halfWidth, -halfHeight, -halfDepth));

        // back
        mesh.AddVertex (center + new Vector3 (halfWidth, halfHeight, halfDepth));
        mesh.AddVertex (center + new Vector3 (-halfWidth, halfHeight, halfDepth));
        mesh.AddVertex (center + new Vector3 (-halfWidth, -halfHeight, halfDepth));
        mesh.AddVertex (center + new Vector3 (halfWidth, -halfHeight, halfDepth));

        // bottom
        mesh.AddVertex (center + new Vector3 (halfWidth, -halfHeight, -halfDepth));
        mesh.AddVertex (center + new Vector3 (-halfWidth, -halfHeight, -halfDepth));
        mesh.AddVertex (center + new Vector3 (-halfWidth, -halfHeight, halfDepth));
        mesh.AddVertex (center + new Vector3 (halfWidth, -halfHeight, halfDepth));

        // left
        mesh.AddVertex (center + new Vector3 (-halfWidth, halfHeight, -halfDepth));
        mesh.AddVertex (center + new Vector3 (-halfWidth, halfHeight, halfDepth));
        mesh.AddVertex (center + new Vector3 (-halfWidth, -halfHeight, halfDepth));
        mesh.AddVertex (center + new Vector3 (-halfWidth, -halfHeight, -halfDepth));

        mesh.AddNormal (new Vector3 (0.0f, 0.0f, -1.0f));
        mesh.AddNormal (new Vector3 (0.0f, 0.0f, -1.0f));
        mesh.AddNormal (new Vector3 (0.0f, 0.0f, -1.0f));
        mesh.AddNormal (new Vector3 (0.0f, 0.0f, -1.0f));

        mesh.AddNormal (new Vector3 (0.0f, 1.0f, 0.0f));
        mesh.AddNormal (new Vector3 (0.0f, 1.0f, 0.0f));
        mesh.AddNormal (new Vector3 (0.0f, 1.0f, 0.0f));
        mesh.AddNormal (new Vector3 (0.0f, 1.0f, 0.0f));

        mesh.AddNormal (new Vector3 (1.0f, 0.0f, 0.0f));
        mesh.AddNormal (new Vector3 (1.0f, 0.0f, 0.0f));
        mesh.AddNormal (new Vector3 (1.0f, 0.0f, 0.0f));
        mesh.AddNormal (new Vector3 (1.0f, 0.0f, 0.0f));

        mesh.AddNormal (new Vector3 (0.0f, 0.0f, 1.0f));
        mesh.AddNormal (new Vector3 (0.0f, 0.0f, 1.0f));
        mesh.AddNormal (new Vector3 (0.0f, 0.0f, 1.0f));
        mesh.AddNormal (new Vector3 (0.0f, 0.0f, 1.0f));

        mesh.AddNormal (new Vector3 (0.0f, -1.0f, 0.0f));
        mesh.AddNormal (new Vector3 (0.0f, -1.0f, 0.0f));
        mesh.AddNormal (new Vector3 (0.0f, -1.0f, 0.0f));
        mesh.AddNormal (new Vector3 (0.0f, -1.0f, 0.0f));

        mesh.AddNormal (new Vector3 (-1.0f, 0.0f, 0.0f));
        mesh.AddNormal (new Vector3 (-1.0f, 0.0f, 0.0f));
        mesh.AddNormal (new Vector3 (-1.0f, 0.0f, 0.0f));
        mesh.AddNormal (new Vector3 (-1.0f, 0.0f, 0.0f));

        // front
        mesh.AddUv (new Vector2 (uvRect [0].xMin, uvRect [0].yMax));
        mesh.AddUv (new Vector2 (uvRect [0].xMax, uvRect [0].yMax));
        mesh.AddUv (new Vector2 (uvRect [0].xMax, uvRect [0].yMin));
        mesh.AddUv (new Vector2 (uvRect [0].xMin, uvRect [0].yMin));

        // top
        mesh.AddUv (new Vector2 (uvRect [1].xMin, uvRect [1].yMax));
        mesh.AddUv (new Vector2 (uvRect [1].xMax, uvRect [1].yMax));
        mesh.AddUv (new Vector2 (uvRect [1].xMax, uvRect [1].yMin));
        mesh.AddUv (new Vector2 (uvRect [1].xMin, uvRect [1].yMin));

        // right
        mesh.AddUv (new Vector2 (uvRect [2].xMin, uvRect [2].yMax));
        mesh.AddUv (new Vector2 (uvRect [2].xMax, uvRect [2].yMax));
        mesh.AddUv (new Vector2 (uvRect [2].xMax, uvRect [2].yMin));
        mesh.AddUv (new Vector2 (uvRect [2].xMin, uvRect [2].yMin));

        // back
        mesh.AddUv (new Vector2 (uvRect [3].xMin, uvRect [3].yMax));
        mesh.AddUv (new Vector2 (uvRect [3].xMax, uvRect [3].yMax));
        mesh.AddUv (new Vector2 (uvRect [3].xMax, uvRect [3].yMin));
        mesh.AddUv (new Vector2 (uvRect [3].xMin, uvRect [3].yMin));

        // bottom
        mesh.AddUv (new Vector2 (uvRect [4].xMin, uvRect [4].yMin));
        mesh.AddUv (new Vector2 (uvRect [4].xMax, uvRect [4].yMin));
        mesh.AddUv (new Vector2 (uvRect [4].xMax, uvRect [4].yMax));
        mesh.AddUv (new Vector2 (uvRect [4].xMin, uvRect [4].yMax));

        // left
        mesh.AddUv (new Vector2 (uvRect [5].xMin, uvRect [5].yMax));
        mesh.AddUv (new Vector2 (uvRect [5].xMax, uvRect [5].yMax));
        mesh.AddUv (new Vector2 (uvRect [5].xMax, uvRect [5].yMin));
        mesh.AddUv (new Vector2 (uvRect [5].xMin, uvRect [5].yMin));

        // front
        if (hasFrontFace) {
            mesh.AddIndex (i);
            mesh.AddIndex (i + 1);
            mesh.AddIndex (i + 2);
            mesh.AddIndex (i);
            mesh.AddIndex (i + 2);
            mesh.AddIndex (i + 3);
        } else {
            mesh.AddIndex (0);
            mesh.AddIndex (0);
            mesh.AddIndex (0);
            mesh.AddIndex (0);
            mesh.AddIndex (0);
            mesh.AddIndex (0);
        }

        // top
        if (hasTopFace) {
            mesh.AddIndex (i + 4);
            mesh.AddIndex (i + 5);
            mesh.AddIndex (i + 6);
            mesh.AddIndex (i + 4);
            mesh.AddIndex (i + 6);
            mesh.AddIndex (i + 7);
        } else {
            mesh.AddIndex (0);
            mesh.AddIndex (0);
            mesh.AddIndex (0);
            mesh.AddIndex (0);
            mesh.AddIndex (0);
            mesh.AddIndex (0);
        }

        // right
        if (hasRightFace) {
            mesh.AddIndex (i + 8);
            mesh.AddIndex (i + 9);
            mesh.AddIndex (i + 10);
            mesh.AddIndex (i + 8);
            mesh.AddIndex (i + 10);
            mesh.AddIndex (i + 11);
        } else {
            mesh.AddIndex (0);
            mesh.AddIndex (0);
            mesh.AddIndex (0);
            mesh.AddIndex (0);
            mesh.AddIndex (0);
            mesh.AddIndex (0);
        }

        // back
        if (hasBackFace) {
            mesh.AddIndex (i + 12);
            mesh.AddIndex (i + 13);
            mesh.AddIndex (i + 14);
            mesh.AddIndex (i + 12);
            mesh.AddIndex (i + 14);
            mesh.AddIndex (i + 15);
        } else {
            mesh.AddIndex (0);
            mesh.AddIndex (0);
            mesh.AddIndex (0);
            mesh.AddIndex (0);
            mesh.AddIndex (0);
            mesh.AddIndex (0);
        }

        // bottom
        if (hasBottomFace) {
            mesh.AddIndex (i + 16);
            mesh.AddIndex (i + 18);
            mesh.AddIndex (i + 17);
            mesh.AddIndex (i + 16);
            mesh.AddIndex (i + 19);
            mesh.AddIndex (i + 18);
        } else {
            mesh.AddIndex (0);
            mesh.AddIndex (0);
            mesh.AddIndex (0);
            mesh.AddIndex (0);
            mesh.AddIndex (0);
            mesh.AddIndex (0);
        }

        // left
        if (hasLeftFace) {
            mesh.AddIndex (i + 20);
            mesh.AddIndex (i + 22);
            mesh.AddIndex (i + 21);
            mesh.AddIndex (i + 20);
            mesh.AddIndex (i + 23);
            mesh.AddIndex (i + 22);
        } else {
            mesh.AddIndex (0);
            mesh.AddIndex (0);
            mesh.AddIndex (0);
            mesh.AddIndex (0);
            mesh.AddIndex (0);
            mesh.AddIndex (0);
        }
    }