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(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); var noScaleWorldMat = new vrMatrix(); noScaleWorldMat.MakeTranslationRotation( new vrVec3(transform.position.x, transform.position.y, transform.position.z), new vrQuat(transform.rotation.x, transform.rotation.y, transform.rotation.z, transform.rotation.w)); var unityNoScaleWorldMat = MiddleVRTools.RawToUnity(noScaleWorldMat); var scalingMatrix = unityNoScaleWorldMat.inverse * transform.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 = scalingMatrix.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() + ")."); } 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]; var index1 = (uint)triangles[i + 1]; var index2 = (uint)triangles[i + 2]; m_Geometry.AddTriangle(index0, index1, index2); MiddleVRTools.Log(6, "[ ] PhysicsBody: Adding a triangle with vertex indexes (" + index0 + ", " + index1 + ", " + index2 + ")."); } MiddleVRTools.Log(6, "[<] PhysicsBody: End of adding triangles."); }
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 + ")."); } }