Пример #1
0
 /// <summary>
 /// set color of cube side
 /// </summary>
 /// <param name="in_color">a color to set up, Vector3 should have 3 components, between 0 and 1</param>
 /// <param name="in_targetSide">side of cube to be colored</param>
 public void setColorOfCube(Vector3 in_color, cubeside in_targetSide)
 {
     ColorsOfCube[(int)in_targetSide]     = in_color;
     ColorsOfCube[(int)in_targetSide + 1] = in_color;
     ColorsOfCube[(int)in_targetSide + 2] = in_color;
     ColorsOfCube[(int)in_targetSide + 3] = in_color;
 }
Пример #2
0
    void createQuad(cubeside side)
    {
        mesh      = new Mesh();
        mesh.name = "cubeMesh" + side.ToString();

        Vector3[] vertices  = new Vector3[4];
        Vector3[] normals   = new Vector3[4];
        Vector2[] uvs       = new Vector2[4];
        int[]     triangles = new int[6];

        Vector2 uv00 = new Vector2(0f, 0f);
        Vector2 uv10 = new Vector2(1f, 0f);
        Vector2 uv01 = new Vector2(0f, 1f);
        Vector2 uv11 = new Vector2(1f, 1f);

        Vector3 p0 = new Vector3(-0.5f, -0.5f, 0.5f);
        Vector3 p1 = new Vector3(0.5f, -0.5f, 0.5f);
        Vector3 p2 = new Vector3(0.5f, -0.5f, -0.5f);
        Vector3 p3 = new Vector3(-0.5f, -0.5f, -0.5f);
        Vector3 p4 = new Vector3(-0.5f, 0.5f, 0.5f);
        Vector3 p5 = new Vector3(0.5f, 0.5f, 0.5f);
        Vector3 p6 = new Vector3(0.5f, 0.5f, -0.5f);
        Vector3 p7 = new Vector3(-0.5f, 0.5f, -0.5f);

        switch (side)
        {
        case cubeside.BOTTOM:
            vertices = new Vector3[] { p0, p1, p2, p3 };
            normals  = new Vector3[] { Vector3.down, Vector3.down,
                                       Vector3.down, Vector3.down };
            uvs       = new Vector2[] { uv11, uv01, uv00, uv10 };
            triangles = new int[] { 3, 1, 0, 3, 2, 1 };
            break;

        case cubeside.TOP:
            vertices = new Vector3[] { p7, p6, p5, p4 };
            normals  = new Vector3[] { Vector3.up, Vector3.up,
                                       Vector3.up, Vector3.up };
            uvs       = new Vector2[] { uv11, uv01, uv00, uv10 };
            triangles = new int[] { 3, 1, 0, 3, 2, 1 };
            break;

        case cubeside.LEFT:
            vertices = new Vector3[] { p7, p4, p0, p3 };
            normals  = new Vector3[] { Vector3.left, Vector3.left,
                                       Vector3.left, Vector3.left };
            uvs       = new Vector2[] { uv11, uv01, uv00, uv10 };
            triangles = new int[] { 3, 1, 0, 3, 2, 1 };
            break;

        case cubeside.RIGHT:
            vertices = new Vector3[] { p5, p6, p2, p1 };
            normals  = new Vector3[] { Vector3.right, Vector3.right,
                                       Vector3.right, Vector3.right };
            uvs       = new Vector2[] { uv11, uv01, uv00, uv10 };
            triangles = new int[] { 3, 1, 0, 3, 2, 1 };
            break;

        case cubeside.FRONT:
            vertices = new Vector3[] { p4, p5, p1, p0 };
            normals  = new Vector3[] { Vector3.forward, Vector3.forward,
                                       Vector3.forward, Vector3.forward };
            uvs       = new Vector2[] { uv11, uv01, uv00, uv10 };
            triangles = new int[] { 3, 1, 0, 3, 2, 1 };
            break;

        case cubeside.BACK:
            vertices = new Vector3[] { p6, p7, p3, p2 };
            normals  = new Vector3[] { Vector3.back, Vector3.back,
                                       Vector3.back, Vector3.back };
            uvs       = new Vector2[] { uv11, uv01, uv00, uv10 };
            triangles = new int[] { 3, 1, 0, 3, 2, 1 };
            break;
        }

        mesh.vertices  = vertices;
        mesh.normals   = normals;
        mesh.uv        = uvs;
        mesh.triangles = triangles;

        mesh.RecalculateBounds();
        quad = new GameObject("Quad");
        quad.transform.parent = this.gameObject.transform;
        meshFilter            = (MeshFilter)quad.AddComponent(typeof(MeshFilter));
        meshFilter.mesh       = mesh;
        MeshRenderer renderer = quad.AddComponent(typeof(MeshRenderer)) as MeshRenderer;

        renderer.material = cubeMaterial;
    }