Esempio n. 1
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.");
                }
            }
        }
    }