public void divideFromCenter(Vector3 center)
    {
        QuadTree.RecursiveTree root = new QuadTree.RecursiveTree(true);
        root = this.mainChunk.getDividedChunksFromCenter(center, ref root);


        // Add to thread?
        this.mainChunk.killUnreferencedChunks(root);
    }
Пример #2
0
 public void killUnreferencedChunks(RecursiveTree parent)
 {
     if (parent.hasChildren)
     {
         for (int i = 0; i < this.cells.Length; i++)
         {
             this.cells[i].killUnreferencedChunks(parent.childrens[i]);
         }
     }
     else if (this.subdivided && !parent.hasChildren)
     {
         for (int i = 0; i < this.cells.Length; i++)
         {
             this.cells[i].killChildren();
         }
         this.subdivided = false;
         this.gameObject.GetComponent <MeshRenderer>().enabled = true;
         this.gameObject.GetComponent <BoxCollider>().enabled  = true;
     }
 }
Пример #3
0
/*      public void quadDivide(float playerDistance) {
 *                      int[] depthsTable = new int[] { 10, 100, 1000, 10000, 100000, 1000000 };
 *                      int computedTargetDepth = 15; // max value
 *
 *                      for (int i = 0; i < depthsTable.Length; i++) {
 *                              if (playerDistance < depthsTable[i]) {
 *                                      break;
 *                              }
 *                              computedTargetDepth -= 1;
 *                      }
 *
 *                      if (this.depth < computedTargetDepth) {
 *                              this.subdivide();
 *
 *                              for (int i = 0; i < this.cells.Length; i++) {
 *                                      if (this.cells[i] != null) {
 *                                              this.cells[i].quadDivide(playerDistance);
 *                                      }
 *                              }
 *                      }
 *                      else if (this.depth > computedTargetDepth) {
 *                              this.killChildren();
 *                      }
 *              } */


        public RecursiveTree getDividedChunksFromCenter(Vector3 playerCenterPosition, ref RecursiveTree parent)
        {
            float[] threshold = this.handler.planet.threshold;

            float   globalS = handler.getScale();
            Vector3 trans   = handler.getTranslation();
            Vector3 off     = new Vector3(globalS / 2, -globalS / 2, globalS / 2);

            float x = this.bounds.pos.x + this.bounds.dim / 2;
            float y = this.bounds.pos.y + this.bounds.dim / 2;

            Vector3 pos = this.handler.localRotation * ((new Vector3(x, 0, y) - off).normalized * globalS) + trans;

            // Test distance btw this chunk and projected player point
            float distancePlayerCenter = Vector3.Distance(playerCenterPosition, pos);

            if (depth < threshold.Length && distancePlayerCenter < threshold[depth] * this.handler.planet.scale)   // If < threshold
            // Divide
            {
                parent.divide();
                this.subdivide();

                for (int i = 0; i < this.cells.Length; i++)
                {
                    parent.childrens[i] = this.cells[i].getDividedChunksFromCenter(playerCenterPosition, ref parent.childrens[i]);
                }
            }
            return(parent);
        }