Ejemplo n.º 1
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();
                }
            }
        }
Ejemplo n.º 2
0
        private void MoveAggregatedVoxelsIntoNewContainers(object aBuildTask)
        {
            PhysicsBuildTask buildTask = (PhysicsBuildTask)aBuildTask;

            int gameObjectIndex = 0;

            try
            {
                if (buildTask.physicsAggregatedList != null)
                {
                    while (buildTask.physicsAggregatedList.Count > 1)
                    {
                        int            smallestIndex = 0;
                        VoxelContainer newContainer  = buildTask.voxelContainerList[gameObjectIndex];
                        newContainer.AssignUVDictionary(this.container);
                        foreach (Voxel curVoxel in buildTask.physicsAggregatedList[smallestIndex])
                        {
                            newContainer.AddVoxel(curVoxel, true);
                            lock (this.container.voxels)
                            {
                                this.container.RemoveVoxel(curVoxel, true);
                            }
                        }


                        Texture2D curTexture       = null;
                        int       curTextureWidth  = 0;
                        int       curTextureHeight = 0;
                        foreach (VoxelArea curArea in this.container.voxelAreas.Values)
                        {
                            curTexture       = curArea.GetTexture();
                            curTextureWidth  = curArea.GetTextureWidth();
                            curTextureHeight = curArea.GetTextureHeight();
                            if (curTexture != null)
                            {
                                break;
                            }
                        }

                        foreach (VoxelArea curArea in newContainer.voxelAreas.Values)
                        {
                            curArea.SetTexture(curTexture, curTextureWidth, curTextureHeight);
                        }

                        foreach (VoxelArea curArea in newContainer.voxelAreas.Values)
                        {
                            curArea.SetTexture(curTexture, curTextureWidth, curTextureHeight);
                        }

                        newContainer.GenerateAreaBuffers();

                        newContainer.rebuildCollider = false;
                        List <VoxelContainer.ColliderDescriptor> colliders = newContainer.PrepareColliderDescriptors();
                        buildTask.colliders.Add(colliders);

                        gameObjectIndex++;
                        buildTask.physicsAggregatedList.RemoveAt(smallestIndex);
                    }
                    buildTask.mainBodyCollider = this.container.PrepareColliderDescriptors();
                    lock (this.container.voxels)
                    {
                        this.container.rebuildCollider = false;
                        this.container.GenerateAreaBuffers();
                        this.container.mustRebuildBeforeUpdate = false;
                        this.container.ingameBuildTask         = null;
                    }

                    this.currentState = PhysicsState.Finalisation;
                    buildTask.isReady = true;
                }
            }
            catch (Exception e)
            {
                Debug.LogError("VoxelPhysics error: " + e.Message);
            }
        }