예제 #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 + ").");
        }
    }
    private void ConvertGeometry(Matrix4x4 iTopMatrixWorld, Transform iTransform, Mesh iMesh, vrPhysicsGeometry ioGeometry, uint iGeometryIndex)
    {
        var vertices  = iMesh.vertices;
        var triangles = iMesh.triangles;

        MiddleVRTools.Log(3,
                          "[ ] PhysicsBody: Number of vertices in sub-geometry '" +
                          iGeometryIndex + "': " + vertices.Length);
        MiddleVRTools.Log(3,
                          "[ ] PhysicsBody: Number of triangles in sub-geometry '" +
                          iGeometryIndex + "': " + triangles.Length);

        var m = iTopMatrixWorld.inverse * iTransform.localToWorldMatrix;

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

        MiddleVRTools.Log(6, "[>] PhysicsBody: Adding vertices.");

        foreach (Vector3 vertex in vertices)
        {
            var vertexPos = m.MultiplyPoint3x4(vertex);
            MiddleVRTools.FromUnity(vertexPos, ref vPos);
            ioGeometry.AddVertex(vPos, iGeometryIndex);

            MiddleVRTools.Log(6,
                              "[ ] PhysicsBody: Adding a vertex at position (" +
                              vPos.x() + ", " + vPos.y() + ", " + vPos.z() +
                              ") to sub-geometry '" + iGeometryIndex + "'.");
        }

        MiddleVRTools.Log(6, "[<] PhysicsBody: End of adding vertices.");
        MiddleVRTools.Log(6, "[>] PhysicsBody: Adding triangles.");

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

            ioGeometry.AddTriangle(index0, index1, index2, iGeometryIndex);

            MiddleVRTools.Log(6,
                              "[ ] PhysicsBody: Adding a triangle with vertex indexes (" +
                              index0 + ", " + index1 + ", " + index2 + ") to sub-geometry '" +
                              iGeometryIndex + "'.");
        }

        MiddleVRTools.Log(6, "[<] PhysicsBody: End of adding triangles.");
    }