コード例 #1
0
    // ######################## FUNCTIONALITY ######################## //
    /// <summary>
    /// Rotates the Hypercube and projects it on the mesh
    /// </summary>
    public void UpdateMesh()
    {
        // rotate and project
        for (int i = 0; i < Verts.Length; ++i)
        {
            // set up rotation amtrices
            _rotateXW.SetRow(0, new Vector4(Mathf.Cos(_wRot.XW), 0, 0, -Mathf.Sin(_wRot.XW)));
            _rotateXW.SetRow(3, new Vector4(Mathf.Sin(_wRot.XW), 0, 0, Mathf.Cos(_wRot.XW)));

            _rotateYW.SetRow(1, new Vector4(0, Mathf.Cos(_wRot.YW), 0, -Mathf.Sin(_wRot.YW)));
            _rotateYW.SetRow(3, new Vector4(0, Mathf.Sin(_wRot.YW), 0, Mathf.Cos(_wRot.YW)));

            _rotateZW.SetRow(2, new Vector4(0, 0, Mathf.Cos(_wRot.ZW), -Mathf.Sin(_wRot.ZW)));
            _rotateZW.SetRow(3, new Vector4(0, 0, Mathf.Sin(_wRot.ZW), Mathf.Cos(_wRot.ZW)));

            // rotate vertex
            Vector4 vert = Verts[i];
            vert = _rotateXW * _rotateYW * _rotateZW * vert;

            // set up projection matrix
            float     projDist   = 1 / (_projectionDist - vert.w);
            Matrix4x4 projection = new Matrix4x4(new Vector4(projDist, 0, 0, 0), new Vector4(0, projDist, 0, 0), new Vector4(0, 0, projDist, 0), new Vector4(0, 0, 0, 1));

            // project vertex
            ProjectedVerts[i] = projection.MultiplyPoint3x4(vert);
        }


        // set up per-face vertices of the mesh
        _meshVerts = new Vector3[]
        {
            //Front
            ProjectedVerts[0],
            ProjectedVerts[1],
            ProjectedVerts[2],
            ProjectedVerts[3],

            //Front Flipped
            ProjectedVerts[3],
            ProjectedVerts[2],
            ProjectedVerts[1],
            ProjectedVerts[0],

            // left
            ProjectedVerts[0],
            ProjectedVerts[3],
            ProjectedVerts[7],
            ProjectedVerts[4],

            // left flipped
            ProjectedVerts[4],
            ProjectedVerts[7],
            ProjectedVerts[3],
            ProjectedVerts[0],

            // top
            ProjectedVerts[0],
            ProjectedVerts[4],
            ProjectedVerts[5],
            ProjectedVerts[1],

            // top flipped
            ProjectedVerts[1],
            ProjectedVerts[5],
            ProjectedVerts[4],
            ProjectedVerts[0],

            // right
            ProjectedVerts[1],
            ProjectedVerts[5],
            ProjectedVerts[6],
            ProjectedVerts[2],

            // right flipped
            ProjectedVerts[2],
            ProjectedVerts[6],
            ProjectedVerts[5],
            ProjectedVerts[1],

            // bottom
            ProjectedVerts[2],
            ProjectedVerts[6],
            ProjectedVerts[7],
            ProjectedVerts[3],

            // bottom flipped
            ProjectedVerts[3],
            ProjectedVerts[7],
            ProjectedVerts[6],
            ProjectedVerts[2],

            // back
            ProjectedVerts[4],
            ProjectedVerts[7],
            ProjectedVerts[6],
            ProjectedVerts[5],

            // back flipped
            ProjectedVerts[5],
            ProjectedVerts[6],
            ProjectedVerts[7],
            ProjectedVerts[4],

            //Front
            ProjectedVerts[8 + 0],
            ProjectedVerts[8 + 1],
            ProjectedVerts[8 + 2],
            ProjectedVerts[8 + 3],

            //FrontW Flipped
            ProjectedVerts[8 + 3],
            ProjectedVerts[8 + 2],
            ProjectedVerts[8 + 1],
            ProjectedVerts[8 + 0],

            // leftW
            ProjectedVerts[8 + 0],
            ProjectedVerts[8 + 3],
            ProjectedVerts[8 + 7],
            ProjectedVerts[8 + 4],

            // leftW flipped
            ProjectedVerts[8 + 4],
            ProjectedVerts[8 + 7],
            ProjectedVerts[8 + 3],
            ProjectedVerts[8 + 0],

            // topW
            ProjectedVerts[8 + 0],
            ProjectedVerts[8 + 4],
            ProjectedVerts[8 + 5],
            ProjectedVerts[8 + 1],

            // topW flipped
            ProjectedVerts[8 + 1],
            ProjectedVerts[8 + 5],
            ProjectedVerts[8 + 4],
            ProjectedVerts[8 + 0],

            // rightW
            ProjectedVerts[8 + 1],
            ProjectedVerts[8 + 5],
            ProjectedVerts[8 + 6],
            ProjectedVerts[8 + 2],

            // rightW flipped
            ProjectedVerts[8 + 2],
            ProjectedVerts[8 + 6],
            ProjectedVerts[8 + 5],
            ProjectedVerts[8 + 1],

            // bottomW
            ProjectedVerts[8 + 2],
            ProjectedVerts[8 + 6],
            ProjectedVerts[8 + 7],
            ProjectedVerts[8 + 3],

            // bottomW flipp8+ed
            ProjectedVerts[8 + 3],
            ProjectedVerts[8 + 7],
            ProjectedVerts[8 + 6],
            ProjectedVerts[8 + 2],

            // backW
            ProjectedVerts[8 + 4],
            ProjectedVerts[8 + 7],
            ProjectedVerts[8 + 6],
            ProjectedVerts[8 + 5],

            // backW flipped
            ProjectedVerts[8 + 5],
            ProjectedVerts[8 + 6],
            ProjectedVerts[8 + 7],
            ProjectedVerts[8 + 4],

            //FrontFrontWLeft
            ProjectedVerts[0],
            ProjectedVerts[8],
            ProjectedVerts[11],
            ProjectedVerts[3],

            //FrontFrontWTop
            ProjectedVerts[0],
            ProjectedVerts[1],
            ProjectedVerts[9],
            ProjectedVerts[8],

            //FrontFrontWRight
            ProjectedVerts[9],
            ProjectedVerts[1],
            ProjectedVerts[2],
            ProjectedVerts[10],

            //FrontFrontWBottom
            ProjectedVerts[11],
            ProjectedVerts[10],
            ProjectedVerts[2],
            ProjectedVerts[3],

            //BackBackWLeft
            ProjectedVerts[4],
            ProjectedVerts[7],
            ProjectedVerts[15],
            ProjectedVerts[12],

            //BackBackWTop
            ProjectedVerts[5],
            ProjectedVerts[4],
            ProjectedVerts[12],
            ProjectedVerts[13],

            //BackBackWRight
            ProjectedVerts[5],
            ProjectedVerts[13],
            ProjectedVerts[14],
            ProjectedVerts[6],

            //BackBackWBottom
            ProjectedVerts[7],
            ProjectedVerts[6],
            ProjectedVerts[14],
            ProjectedVerts[15],

            //LeftLeftWLeft
            ProjectedVerts[4],
            ProjectedVerts[12],
            ProjectedVerts[15],
            ProjectedVerts[7],

            //LeftLeftWTop
            ProjectedVerts[4],
            ProjectedVerts[0],
            ProjectedVerts[8],
            ProjectedVerts[12],

            //LeftLeftWRight
            ProjectedVerts[0],
            ProjectedVerts[3],
            ProjectedVerts[11],
            ProjectedVerts[8],

            //LeftLeftWBottom
            ProjectedVerts[3],
            ProjectedVerts[7],
            ProjectedVerts[15],
            ProjectedVerts[11],

            //TopTopWLeft
            ProjectedVerts[4],
            ProjectedVerts[12],
            ProjectedVerts[8],
            ProjectedVerts[0],

            //TopTopWTop
            ProjectedVerts[4],
            ProjectedVerts[5],
            ProjectedVerts[13],
            ProjectedVerts[12],

            //TopTopWRight
            ProjectedVerts[5],
            ProjectedVerts[1],
            ProjectedVerts[9],
            ProjectedVerts[13],

            //TopTopWBottom
            ProjectedVerts[1],
            ProjectedVerts[0],
            ProjectedVerts[8],
            ProjectedVerts[9],

            //RightRightWLeft
            ProjectedVerts[2],
            ProjectedVerts[1],
            ProjectedVerts[9],
            ProjectedVerts[10],

            //RightRightWTop
            ProjectedVerts[1],
            ProjectedVerts[5],
            ProjectedVerts[13],
            ProjectedVerts[9],

            //RightRightWRight
            ProjectedVerts[5],
            ProjectedVerts[6],
            ProjectedVerts[14],
            ProjectedVerts[13],

            //RightRightWBottom
            ProjectedVerts[6],
            ProjectedVerts[2],
            ProjectedVerts[10],
            ProjectedVerts[14],

            //BottomBottomWLeft
            ProjectedVerts[7],
            ProjectedVerts[3],
            ProjectedVerts[11],
            ProjectedVerts[15],

            //BottomBottomWTop
            ProjectedVerts[3],
            ProjectedVerts[2],
            ProjectedVerts[10],
            ProjectedVerts[11],

            //BottomBottomWRight
            ProjectedVerts[2],
            ProjectedVerts[6],
            ProjectedVerts[14],
            ProjectedVerts[10],

            //BottomBottomWBottom
            ProjectedVerts[6],
            ProjectedVerts[7],
            ProjectedVerts[15],
            ProjectedVerts[14],
        };

        Mesh3D.vertices = _meshVerts;

        // generate triangles
        if (_tris == null)
        {
            _tris = new int[_meshVerts.Length + _meshVerts.Length / 2];

            // set up triangles
            for (int i = 0; i <= _tris.Length - 6; i += 6)
            {
                _tris[i + 0] = (i / 6) * 4 + 0;
                _tris[i + 1] = (i / 6) * 4 + 1;
                _tris[i + 2] = (i / 6) * 4 + 2;
                _tris[i + 3] = (i / 6) * 4 + 2;
                _tris[i + 4] = (i / 6) * 4 + 3;
                _tris[i + 5] = (i / 6) * 4 + 0;
            }
            Mesh3D.triangles = _tris;
        }


        // finalize mesh
        Mesh3D.RecalculateNormals();
        Mesh3D.RecalculateTangents();
        Mesh3D.RecalculateBounds();
    }