Пример #1
0
        void Awake()
        {
            // We will create a cube, so we set 12 triangles (6 sides á 2 triangles) and 8 vertices.
            // To do a cube with proper shading, we should actually create 4 vertices per side and have
            // normals pointing outwards, but for this example let's keep it simple.
            meshCreator = new MeshCreator(12, 8);

            // Create the vertices
            var behindUpperLeft  = new MeshVertex(new Vector3(-1, 1, 1), Vector3.zero, Color.white);
            var behindUpperRight = new MeshVertex(new Vector3(1, 1, 1), Vector3.zero, Color.white);
            var behindLowerLeft  = new MeshVertex(new Vector3(-1, -1, 1), Vector3.zero, Color.white);
            var behindLowerRight = new MeshVertex(new Vector3(1, -1, 1), Vector3.zero, Color.white);
            var frontUpperLeft   = new MeshVertex(new Vector3(-1, 1, -1), Vector3.zero, Color.white);
            var frontUpperRight  = new MeshVertex(new Vector3(1, 1, -1), Vector3.zero, Color.white);
            var frontLowerLeft   = new MeshVertex(new Vector3(-1, -1, -1), Vector3.zero, Color.white);
            var frontLowerRight  = new MeshVertex(new Vector3(1, -1, -1), Vector3.zero, Color.white);

            // Create the quads
            meshCreator.AddQuad(frontUpperLeft, frontUpperRight, frontLowerRight, frontLowerLeft);
            meshCreator.AddQuad(behindUpperRight, behindUpperLeft, behindLowerLeft, behindLowerRight);
            meshCreator.AddQuad(frontUpperRight, behindUpperRight, behindLowerRight, frontLowerRight);
            meshCreator.AddQuad(frontUpperLeft, frontLowerLeft, behindLowerLeft, behindUpperLeft);
            meshCreator.AddQuad(frontUpperLeft, behindUpperLeft, behindUpperRight, frontUpperRight);
            meshCreator.AddQuad(frontLowerLeft, frontLowerRight, behindLowerRight, behindLowerLeft);

            // Create a new mesh and set it in the mesh filter
            meshFilter            = GetComponent <MeshFilter>();
            meshFilter.sharedMesh = meshCreator.CreateMesh();
            meshFilter.sharedMesh.RecalculateNormals();
        }