Exemplo n.º 1
0
        private void DoFinalization()
        {
            if ((physicsBuildTask != null) && (physicsBuildTask.isReady))
            {
                if (physicsBuildTask.voxelContainerList != null)
                {
                    for (int i = 0; i < physicsBuildTask.voxelContainerList.Count; i++)
                    {
                        physicsBuildTask.voxelContainerList[i].enabled = true;
                        physicsBuildTask.voxelContainerList[i].gameObject.GetComponent <Rigidbody>().mass = physicsBuildTask.voxelContainerList[i].voxels.Count;
                        physicsBuildTask.voxelContainerList[i].CreateBoxColliders(physicsBuildTask.colliders[i]);
                        ///physicsBuildTask.voxelContainerList[i].gameObject.GetComponent<Rigidbody>().drag = physicsBuildTask.voxelContainerList[i].voxels.Count;
                        MeshRenderer meshRenderer = physicsBuildTask.voxelContainerList[i].GetComponent <MeshRenderer>();
                        meshRenderer.enabled = true;
                        physicsBuildTask.voxelContainerList[i].mustRebuildBeforeUpdate = true;
                    }
                }
                this.container.CreateBoxColliders(physicsBuildTask.mainBodyCollider);
                physicsBuildTask = null;
                return;
            }

            if (physicsBuildTask == null)
            {
                if ((!this.container.mustRebuildBeforeUpdate) && (this.IsAllFragmentsRebuilded()))
                {
                    if (showDebugMessages)
                    {
                        Debug.Log("VoxelMax Physics: Reenabling modules");
                    }

                    this.container.mustRebuildBeforeUpdate = true;

                    foreach (VoxelContainer voxContainer in this.fragmentContainers)
                    {
                        Rigidbody    rigidBody    = voxContainer.GetComponent <Rigidbody>();
                        MeshCollider meshCollider = voxContainer.GetComponent <MeshCollider>();
                        VoxelPhysics voxelPhysics = voxContainer.GetComponent <VoxelPhysics>();
                        rigidBody.useGravity = true;
                        if (meshCollider != null)
                        {
                            meshCollider.enabled = true;
                        }
                        voxelPhysics.enabled = true;
                    }

                    //  this.gameObject.GetComponent<MeshCollider>().enabled = true;
                    if (this.gameObject.GetComponent <Rigidbody>() != null)
                    {
                        this.gameObject.GetComponent <Rigidbody>().useGravity = true;
                    }

                    this.currentState = PhysicsState.WaitingForTask;
                }
            }
        }
Exemplo n.º 2
0
        public void DoObjectCreation()
        {
            this.currentState = PhysicsState.ObjectCreation;
            if (showDebugMessages)
            {
                Debug.Log("VoxelMax Physics: Object creation update");
            }


            if (this.physicsAggregatedList == null)
            {
                if (showDebugMessages)
                {
                    Debug.Log("VoxelMax Physics: No fragmented object to create");
                }
                this.currentState = PhysicsState.WaitingForTask;
                return;
            }

            if (this.physicsAggregatedList.Count > 1)
            {
                //Prepare GameObjectList because it can be only done in the main thread
                List <GameObject>     newGameObjectList = new List <GameObject>();
                List <VoxelContainer> newContainerList  = new List <VoxelContainer>();
                for (int i = 0; i < this.physicsAggregatedList.Count - 1; i++)
                {
                    GameObject newGameObject = new GameObject();
                    newGameObject.transform.parent = this.transform;

                    VoxelContainer newContainer = newGameObject.AddComponent <VoxelContainer>();
                    newContainer.enabled         = false;
                    newContainer.modelFilename   = this.container.modelFilename;
                    newContainer.modelName       = this.container.modelName;
                    newContainer.textureFilename = this.container.textureFilename;
                    //newContainer.AssignUVDictionary(this.container);
                    newContainerList.Add(newContainer);
                    this.fragmentContainers.Add(newContainer);

                    MeshRenderer meshRenderer = newGameObject.AddComponent <MeshRenderer>();
                    Rigidbody    rigidBody    = newGameObject.AddComponent <Rigidbody>();
                    newContainer.enabled = false;
                    meshRenderer.enabled = false;
                    rigidBody.useGravity = false;
                    newGameObject.transform.localScale = new Vector3(1f, 1f, 1f);
                    newGameObject.transform.rotation   = this.gameObject.transform.rotation;
                    newGameObject.transform.position   = this.gameObject.transform.position;

                    //Add physics to fragments
                    VoxelPhysics newVoxelPhysics = newGameObject.AddComponent <VoxelPhysics>();
                    newVoxelPhysics.tensionCalculation   = this.tensionCalculation;
                    newVoxelPhysics.layerWidthBreakPoint = this.layerWidthBreakPoint;
                    newVoxelPhysics.enabled = false;

                    newGameObject.GetComponent <Renderer>().sharedMaterial = this.gameObject.GetComponent <MeshRenderer>().sharedMaterial;

                    newGameObjectList.Add(newGameObject);
                }

                //  this.gameObject.GetComponent<MeshCollider>().enabled = false;
                if (this.gameObject.GetComponent <Rigidbody>() != null)
                {
                    this.gameObject.GetComponent <Rigidbody>().useGravity = false;
                }

                this.physicsBuildTask = new PhysicsBuildTask(newGameObjectList, this.physicsAggregatedList, newContainerList);

                this.DoMoveDataToObjects();
            }
            else
            {
                if (this.physicsAggregatedList.Count == 1)
                {
                    if (this.gameObject.GetComponent <Rigidbody>() == null)
                    {
                        //   Rigidbody curRigidBody = this.gameObject.AddComponent<Rigidbody>();
                        //   curRigidBody.mass = this.container.voxels.Count;
                    }

                    MeshCollider meshCollider = this.gameObject.GetComponent <MeshCollider>();
                    if (meshCollider != null)
                    {
                        Destroy(meshCollider);
                    }
                    this.physicsBuildTask = new PhysicsBuildTask(null, this.physicsAggregatedList, null);

                    this.DoMoveDataToObjects();
                }
            }
        }
Exemplo n.º 3
0
        // Update is called once per frame
        void Update()
        {
            if ((triggered) && (!this.exploded))
            {
                this.exploded = true;

                //Get the root object
                rootObjectForParticles = GameObject.Find("VoxelMaxExplosionRoot");
                if (rootObjectForParticles == null)
                {
                    rootObjectForParticles      = new GameObject();
                    rootObjectForParticles.name = "VoxelMaxExplosionRoot";
                }

                Collider[] colliderList = Physics.OverlapSphere(this.gameObject.transform.position, this.explosionRadius);
                foreach (Collider curCollider in colliderList)
                {
                    if (curCollider.gameObject.tag == "Indestructible")
                    {
                        continue;
                    }
                    VoxelContainer voxelContainer = curCollider.gameObject.GetComponent <VoxelContainer>();
                    if (voxelContainer != null)
                    {
                        Vector3      torqueVector   = new Vector3(Random.Range(0f, 1f), Random.Range(0f, 1f), Random.Range(0f, 1f)) * this.explosionForce;
                        Vector3      voxelPoint     = voxelContainer.gameObject.transform.InverseTransformPoint(this.gameObject.transform.position);
                        List <Voxel> voxelsToRemove = new List <Voxel>();

                        Vector3      transformedDistance = voxelContainer.gameObject.transform.InverseTransformVector(explosionRadius, 0f, 0f);
                        List <Voxel> voxelList;
                        lock (voxelContainer.voxels)
                        {
                            voxelList = new List <Voxel>(voxelContainer.voxels.Values);
                        }
                        foreach (Voxel curVoxel in voxelList)
                        {
                            float voxDistance = Vector3.Distance(curVoxel.position, voxelPoint);
                            if (voxDistance <= transformedDistance.magnitude)
                            {
                                voxelsToRemove.Add(curVoxel);
                            }
                        }


                        //Do not instantiate every voxel on large explosions to prevent frame drop
                        int particleStepper = 0;
                        if (maxParticleCount != 0)
                        {
                            particleStepper = Mathf.Max((int)(voxelsToRemove.Count / this.maxParticleCount), 1);
                        }

                        for (int i = 0; i < voxelsToRemove.Count; i++)
                        {
                            Voxel curVoxel = voxelsToRemove[i];
                            if ((particleStepper != 0) && ((i % particleStepper) == 0))
                            {
                                if (this.explosionParticle != null)
                                {
                                    GameObject newObject = Instantiate(this.explosionParticle);
                                    newObject.transform.parent     = rootObjectForParticles.transform;
                                    newObject.transform.localScale = voxelContainer.gameObject.transform.localScale;
                                    newObject.transform.position   = voxelContainer.gameObject.transform.TransformPoint(curVoxel.position);

                                    Renderer curRenderer = newObject.GetComponent <Renderer>();
                                    if (curRenderer != null)
                                    {
                                        if (curRenderer.material != null)
                                        {
                                            curRenderer.material.color = curVoxel.color;
                                        }
                                    }

                                    Rigidbody curRigidBody = newObject.GetComponent <Rigidbody>();
                                    if (curRigidBody != null)
                                    {
                                        curRigidBody.AddForce(voxelContainer.gameObject.transform.TransformDirection(curVoxel.position - voxelPoint).normalized *explosionForce);
                                        curRigidBody.AddRelativeTorque(torqueVector);
                                    }
                                }
                            }
                        }
                        voxelContainer.AddBuildTaskRemovedVoxels(voxelsToRemove);
                        VoxelPhysics vPhysics = this.GetComponent <VoxelPhysics>();
                        if ((vPhysics != null) && (vPhysics.enabled))
                        {
                            voxelContainer.rebuildCollider = false;
                        }


                        //Modification for Will
                        if (objectToInstantiate != null)
                        {
                            GameObject newGameObject = Instantiate <GameObject>(this.objectToInstantiate);
                            newGameObject.transform.position = this.gameObject.transform.position;
                            newGameObject.transform.parent   = rootObjectForParticles.transform;
                        }
                    }
                    else
                    {
                        Rigidbody curRigidBody = curCollider.gameObject.GetComponent <Rigidbody>();
                        if (curRigidBody != null)
                        {
                            Vector3 torqueVector = new Vector3(Random.Range(0f, 1f), Random.Range(0f, 1f), Random.Range(0f, 1f)) * this.explosionForce;
                            Vector3 voxelPoint   = curCollider.gameObject.transform.InverseTransformPoint(this.gameObject.transform.position);
                            curRigidBody.AddForce(curCollider.gameObject.transform.TransformDirection(voxelPoint - this.gameObject.transform.position).normalized *explosionForce);
                            curRigidBody.AddRelativeTorque(torqueVector);
                        }
                    }
                }
                Destroy(this.gameObject);
            }
        }