예제 #1
0
    protected void Update()
    {
        if (m_Geometry != null && m_PhysicsBody.IsInSimulation())
        {
            // The geometry was used for creation so it can be deleted.

            // Clear now content to avoid a delayed memory deallocation.
            m_Geometry.Clear();
            m_Geometry.Dispose();

            m_Geometry = null;
        }
    }
예제 #2
0
    protected void Start()
    {
        if (MiddleVR.VRClusterMgr.IsCluster() && !MiddleVR.VRClusterMgr.IsServer())
        {
            enabled = false;
            return;
        }

        if (MiddleVR.VRPhysicsMgr == null)
        {
            MiddleVRTools.Log(0, "[X] PhysicsBody: No PhysicsManager found.");
            enabled = false;
            return;
        }

        vrPhysicsEngine physicsEngine = MiddleVR.VRPhysicsMgr.GetPhysicsEngine();

        if (physicsEngine == null)
        {
            MiddleVRTools.Log(0, "[X] PhysicsBody: Failed to access a physics engine for body '" + name + "'.");
            enabled = false;
            return;
        }

        if (m_PhysicsBody == null)
        {
            m_PhysicsBody = physicsEngine.CreateBodyWithUniqueName(name);

            if (m_PhysicsBody == null)
            {
                MiddleVRTools.Log(0, "[X] PhysicsBody: Failed to create a physics body for '" + name + "'.");
                enabled = false;
                return;
            }
            else
            {
                GC.SuppressFinalize(m_PhysicsBody);

                m_MVREventListener = new vrEventListener(OnMVRNodeDestroy);
                m_PhysicsBody.AddEventListener(m_MVREventListener);

                var nodesMapper = MVRNodesMapper.Instance;

                nodesMapper.AddMapping(
                    gameObject, m_PhysicsBody,
                    MVRNodesMapper.ENodesSyncDirection.MiddleVRToUnity,
                    MVRNodesMapper.ENodesInitValueOrigin.FromUnity);

                m_PhysicsBodyName = m_PhysicsBody.GetName();

                string geometryName = m_PhysicsBodyName + ".Geometry";

                MiddleVRTools.Log(4,
                    "[>] PhysicsBody: Creation of the physics geometry '" +
                    geometryName + "'.");

                m_Geometry = new vrPhysicsGeometry(geometryName);
                GC.SuppressFinalize(m_Geometry);

                Mesh mesh = null;

                MeshCollider meshCollider = GetComponent<MeshCollider>();

                if (meshCollider != null)
                {
                    mesh = meshCollider.sharedMesh;

                    if (mesh != null)
                    {
                        MiddleVRTools.Log(2, "[ ] PhysicsBody: the physics geometry '" + geometryName +
                            "' uses the mesh of its MeshCollider.");
                    }
                }

                // No mesh from collider was found so let's try from the mesh filter.
                if (mesh == null)
                {
                    var meshFilter = GetComponent<MeshFilter>();

                    if (meshFilter != null)
                    {
                        mesh = meshFilter.sharedMesh;

                        if (mesh != null)
                        {
                            MiddleVRTools.Log(2, "[ ] PhysicsBody: the physics geometry '" + geometryName +
                                "' uses the mesh of its MeshFilter.");
                        }
                    }
                }

                if (mesh != null)
                {
                    ConvertGeometry(mesh);
                    MiddleVRTools.Log(4, "[ ] PhysicsBody: Physics geometry created.");
                }
                else
                {
                    MiddleVRTools.Log(
                        0,
                        "[X] PhysicsBody: Failed to create the physics geometry '" +
                        geometryName + "'.");
                }

                MiddleVRTools.Log(4,
                    "[<] PhysicsBody: Creation of the physics geometry '" +
                    geometryName + "' ended.");

                m_PhysicsBody.SetGeometry(m_Geometry);

                m_PhysicsBody.SetStatic(m_Static);
                m_PhysicsBody.SetMass(m_Mass);
                m_PhysicsBody.SetRotationDamping(m_RotationDamping);
                m_PhysicsBody.SetTranslationDamping(m_TranslationDamping);

                m_PhysicsBody.SetMargin(m_Margin);

                if (physicsEngine.AddBody(m_PhysicsBody))
                {
                    MiddleVRTools.Log(3, "[ ] PhysicsBody: The physics body '" + m_PhysicsBodyName +
                        "' was added to the physics simulation.");
                }
                else
                {
                    MiddleVRTools.Log(0, "[X] PhysicsBody: Failed to add the body '" +
                         m_PhysicsBodyName + "' to the physics simulation.");
                }
            }
        }
    }
예제 #3
0
    protected void Update()
    {
        if (m_Geometry != null && m_PhysicsBody.IsInSimulation())
        {
            // The geometry was used for creation so it can be deleted.

            // Clear now content to avoid a delayed memory deallocation.
            m_Geometry.Clear();
            m_Geometry.Dispose();

            m_Geometry = null;
        }
    }
예제 #4
0
    protected void Start()
    {
        if (MiddleVR.VRClusterMgr.IsCluster() && !MiddleVR.VRClusterMgr.IsServer())
        {
            enabled = false;
            return;
        }

        if (MiddleVR.VRPhysicsMgr == null)
        {
            MiddleVRTools.Log(0, "[X] PhysicsBody: No PhysicsManager found.");
            enabled = false;

            return;
        }

        vrPhysicsEngine physicsEngine = MiddleVR.VRPhysicsMgr.GetPhysicsEngine();

        if (physicsEngine == null)
        {
            MiddleVRTools.Log(0, "[X] PhysicsBody: Failed to access a physics engine for body '" + name + "'.");
            enabled = false;

            return;
        }

        if (m_PhysicsBody == null)
        {
            m_PhysicsBody = physicsEngine.CreateBodyWithUniqueName(name);

            if (m_PhysicsBody == null)
            {
                MiddleVRTools.Log(0, "[X] PhysicsBody: Failed to create a physics body for '" + name + "'.");
                enabled = false;

                return;
            }
            else
            {
                GC.SuppressFinalize(m_PhysicsBody);

                m_MVREventListener = new vrEventListener(OnMVRNodeDestroy);
                m_PhysicsBody.AddEventListener(m_MVREventListener);

                var nodesMapper = MVRNodesMapper.Instance;

                nodesMapper.AddMapping(
                    gameObject, m_PhysicsBody,
                    MVRNodesMapper.ENodesSyncDirection.MiddleVRToUnity,
                    MVRNodesMapper.ENodesInitValueOrigin.FromUnity);

                m_PhysicsBodyName = m_PhysicsBody.GetName();

                string geometryName = m_PhysicsBodyName + ".Geometry";

                MiddleVRTools.Log(4,
                                  "[>] PhysicsBody: Creation of the physics geometry '" +
                                  geometryName + "'.");

                m_Geometry = new vrPhysicsGeometry(geometryName);
                GC.SuppressFinalize(m_Geometry);

                Mesh mesh = null;

                MeshCollider meshCollider = GetComponent <MeshCollider>();

                if (meshCollider != null)
                {
                    mesh = meshCollider.sharedMesh;

                    if (mesh != null)
                    {
                        MiddleVRTools.Log(2, "[ ] PhysicsBody: the physics geometry '" + geometryName +
                                          "' uses the mesh of its MeshCollider.");
                    }
                }

                // No mesh from collider was found so let's try from the mesh filter.
                if (mesh == null)
                {
                    var meshFilter = GetComponent <MeshFilter>();

                    if (meshFilter != null)
                    {
                        mesh = meshFilter.sharedMesh;

                        if (mesh != null)
                        {
                            MiddleVRTools.Log(2, "[ ] PhysicsBody: the physics geometry '" + geometryName +
                                              "' uses the mesh of its MeshFilter.");
                        }
                    }
                }

                if (mesh != null)
                {
                    ConvertGeometry(mesh);
                    MiddleVRTools.Log(4, "[ ] PhysicsBody: Physics geometry created.");
                }
                else
                {
                    MiddleVRTools.Log(
                        0,
                        "[X] PhysicsBody: Failed to create the physics geometry '" +
                        geometryName + "'.");
                }

                MiddleVRTools.Log(4,
                                  "[<] PhysicsBody: Creation of the physics geometry '" +
                                  geometryName + "' ended.");

                m_PhysicsBody.SetGeometry(m_Geometry);

                m_PhysicsBody.SetStatic(m_Static);
                m_PhysicsBody.SetMass(m_Mass);
                m_PhysicsBody.SetRotationDamping(m_RotationDamping);
                m_PhysicsBody.SetTranslationDamping(m_TranslationDamping);

                m_PhysicsBody.SetMargin(m_Margin);

                if (physicsEngine.AddBody(m_PhysicsBody))
                {
                    MiddleVRTools.Log(3, "[ ] PhysicsBody: The physics body '" + m_PhysicsBodyName +
                                      "' was added to the physics simulation.");
                }
                else
                {
                    MiddleVRTools.Log(3, "[X] PhysicsBody: Failed to add the body '" +
                                      m_PhysicsBodyName + "' to the physics simulation.");
                }
            }
        }
    }
    protected void Start()
    {
        if (MiddleVR.VRClusterMgr.IsCluster() && !MiddleVR.VRClusterMgr.IsServer())
        {
            enabled = false;
            return;
        }

        if (MiddleVR.VRPhysicsMgr == null)
        {
            MiddleVRTools.Log(0, "[X] PhysicsBody: No PhysicsManager found.");
            enabled = false;
            return;
        }

        vrPhysicsEngine physicsEngine = MiddleVR.VRPhysicsMgr.GetPhysicsEngine();

        if (physicsEngine == null)
        {
            MiddleVRTools.Log(0, "[X] PhysicsBody: Failed to access a physics engine for body '" + name + "'.");
            enabled = false;
            return;
        }

        if (m_PhysicsBody == null)
        {
            m_PhysicsBody = physicsEngine.CreateBodyWithUniqueName(name);

            if (m_PhysicsBody == null)
            {
                MiddleVRTools.Log(0, "[X] PhysicsBody: Failed to create a physics body for '" + name + "'.");
                enabled = false;
                return;
            }
            else
            {
                GC.SuppressFinalize(m_PhysicsBody);

                m_MVREventListener = new vrEventListener(OnMVRNodeDestroy);
                m_PhysicsBody.AddEventListener(m_MVREventListener);

                var nodesMapper = MVRNodesMapper.Instance;

                nodesMapper.AddMapping(
                    gameObject, m_PhysicsBody,
                    MVRNodesMapper.ENodesSyncDirection.MiddleVRToUnity,
                    MVRNodesMapper.ENodesInitValueOrigin.FromUnity);

                m_PhysicsBodyName = m_PhysicsBody.GetName();

                m_Geometry = CreateGeometry(m_MergeChildGeometries);
                if (m_Geometry != null)
                {
                    GC.SuppressFinalize(m_Geometry);
                }

                m_PhysicsBody.SetGeometry(m_Geometry);

                m_PhysicsBody.SetStatic(m_Static);
                m_PhysicsBody.SetMass(m_Mass);
                m_PhysicsBody.SetRotationDamping(m_RotationDamping);
                m_PhysicsBody.SetTranslationDamping(m_TranslationDamping);

                m_PhysicsBody.SetMargin(m_Margin);

                if (physicsEngine.AddBody(m_PhysicsBody))
                {
                    MiddleVRTools.Log(3, "[ ] PhysicsBody: The physics body '" + m_PhysicsBodyName +
                                      "' was added to the physics simulation.");
                }
                else
                {
                    MiddleVRTools.Log(0, "[X] PhysicsBody: Failed to add the body '" +
                                      m_PhysicsBodyName + "' to the physics simulation.");
                }
            }
        }
    }
    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.");
    }
    private vrPhysicsGeometry CreateGeometry(bool iAreChildGeometriesMerged)
    {
        string geometryName = m_PhysicsBodyName + ".Geometry";

        MiddleVRTools.Log(4,
                          "[>] PhysicsBody: Creation of the physics geometry '" +
                          geometryName + "'.");

        var rootMatrixWorld = transform.localToWorldMatrix;

        vrPhysicsGeometry ret = new vrPhysicsGeometry(geometryName);

        uint geometryIndex = 0;

        var queue = new Queue <GameObject>();

        queue.Enqueue(gameObject);

        while (queue.Count > 0)
        {
            var go = queue.Dequeue();

            Mesh mesh = null;

            var meshCollider = go.GetComponent <MeshCollider>();

            if (meshCollider != null && meshCollider.enabled)
            {
                mesh = meshCollider.sharedMesh;

                if (mesh != null)
                {
                    MiddleVRTools.Log(2,
                                      "[ ] PhysicsBody: the GO '" + go.name +
                                      "' will furnish a physics geometry for '" + geometryName +
                                      "' from its MeshCollider.");
                }
            }

            // No mesh from collider was found so let's try from the mesh filter.
            if (mesh == null)
            {
                var meshFilter = go.GetComponent <MeshFilter>();

                if (meshFilter != null)
                {
                    mesh = meshFilter.sharedMesh;

                    if (mesh != null)
                    {
                        MiddleVRTools.Log(2,
                                          "[ ] PhysicsBody: the GO '" + go.name +
                                          "' will furnish a physics geometry for '" + geometryName +
                                          "' from its MeshFilter.");
                    }
                }
            }

            if (mesh != null)
            {
                // The top geometry keep its name, others will get the word "sub".
                if (geometryIndex != 0)
                {
                    ret.SetSubGeometryName(geometryIndex, geometryName + ".sub." + go.name);
                }

                ConvertGeometry(rootMatrixWorld, go.transform, mesh, ret, geometryIndex);

                ++geometryIndex;

                MiddleVRTools.Log(4,
                                  "[ ] PhysicsBody: Physics geometry created from GO '" +
                                  go.name + "'.");
            }

            if (iAreChildGeometriesMerged)
            {
                foreach (Transform child in go.transform)
                {
                    if (child.gameObject.activeSelf)
                    {
                        queue.Enqueue(child.gameObject);
                    }
                }
            }
        }

        var scaleShearMatrix = MVRTools.ComputeScaleShearMatrixWorld(transform);

        ret.TransformStoredVertices(MVRTools.FromUnity(scaleShearMatrix));

        MiddleVRTools.Log(4,
                          "[<] PhysicsBody: Creation of the physics geometry '" +
                          geometryName + "' ended.");

        return(ret);
    }