예제 #1
0
    private void ConvertGeometry(Mesh mesh)
    {
        Vector3[] vertices  = mesh.vertices;
        int[]     triangles = mesh.triangles;

        MiddleVRTools.Log(3, "PhysicsBody: Number of vertices: " + vertices.Length);
        MiddleVRTools.Log(3, "PhysicsBody: Number of Triangles: " + triangles.Length);

        // We will reuse the same vectors to avoid many memory allocations.
        Vector3 vertexPos = new Vector3();
        vrVec3  vPos      = new vrVec3();

        // We compute a matrix to scale vertices according to their world
        // coordinates, so this scale depends on the scales of the GameObject
        // parents. Matrices 4x4 are used because matrices 3x3 aren't available.
        vrMatrix worldMatrix = MVRTools.RawFromUnity(transform.localToWorldMatrix);

        worldMatrix.SetCol(3, new vrVec4(0.0f, 0.0f, 0.0f, 0.0f));
        worldMatrix.SetRow(3, new vrVec4(0.0f, 0.0f, 0.0f, 0.0f));

        vrQuat invRotWorlQ = worldMatrix.GetRotation();

        invRotWorlQ = invRotWorlQ.Normalize().GetInverse();

        vrMatrix invRotWorldMatrix = new vrMatrix();

        invRotWorldMatrix.SetRotation(invRotWorlQ);
        invRotWorldMatrix.SetCol(3, new vrVec4(0.0f, 0.0f, 0.0f, 0.0f));
        invRotWorldMatrix.SetRow(3, new vrVec4(0.0f, 0.0f, 0.0f, 0.0f));

        vrMatrix  scaleWorldMatrix      = invRotWorldMatrix.Mult(worldMatrix);
        Matrix4x4 scaleWorldMatrixUnity = MiddleVRTools.RawToUnity(scaleWorldMatrix);

        foreach (Vector3 vertex in vertices)
        {
            vertexPos = scaleWorldMatrixUnity.MultiplyPoint3x4(vertex);

            MiddleVRTools.FromUnity(vertexPos, ref vPos);

            m_Geometry.AddVertex(vPos);

            MiddleVRTools.Log(6, "PhysicsBody: Adding a vertex at position (" +
                              vPos.x() + ", " + vPos.y() + ", " + vPos.z() + ").");
        }

        for (int i = 0, iEnd = triangles.Length; i < iEnd; i += 3)
        {
            uint index0 = (uint)triangles[i];
            uint index1 = (uint)triangles[i + 1];
            uint index2 = (uint)triangles[i + 2];

            m_Geometry.AddTriangle(index0, index1, index2);

            MiddleVRTools.Log(6, "PhysicsBody: Adding a triangle with vertex indexes (" +
                              index0 + ", " + index1 + ", " + index2 + ").");
        }
    }