Advertisement
Rakshalpha

Octree

Oct 14th, 2019
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.97 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. namespace Assets.Scripts._CoreLibaryCode
  6. {
  7.     public class Octree<GameObject>
  8.     {
  9.         private OctreeNode<GameObject> node;
  10.         private int depth;
  11.  
  12.         public Octree(Vector3 position, float size, int depth)
  13.         {
  14.             node = new OctreeNode<GameObject>(position, size);
  15.             node.Subdivide(depth);
  16.         }
  17.  
  18.         public class OctreeNode<GameObject>
  19.         {
  20.             Vector3 position;
  21.             float size;
  22.             [SerializeField] OctreeNode<GameObject>[] subNodes;
  23.             IList<GameObject> value;
  24.  
  25.             public OctreeNode(Vector3 pos, float size)
  26.             {
  27.                 position = pos;
  28.                 this.size = size;
  29.             }
  30.  
  31.             public IEnumerable<OctreeNode<GameObject>> Nodes
  32.             {
  33.                 get { return subNodes; }
  34.             }
  35.  
  36.             public Vector3 Position
  37.             {
  38.                 get { return position; }
  39.             }
  40.  
  41.             public float Size
  42.             {
  43.                 get { return size; }
  44.             }
  45.  
  46.             public void Subdivide(int depth = 0)
  47.             {
  48.                 subNodes = new OctreeNode<GameObject>[8];
  49.                 for (int i = 0; i < subNodes.Length; i++)
  50.                 {
  51.                     Vector3 newPos = position;
  52.                     if ((i & 4) == 4)
  53.                     {
  54.                         newPos.y += size * 0.25f;
  55.                     }
  56.                     else
  57.                     {
  58.                         newPos.y -= size * 0.25f;
  59.                     }
  60.  
  61.                     if ((i & 2) == 2)
  62.                     {
  63.                         newPos.x += size * 0.25f;
  64.                     }
  65.                     else
  66.                     {
  67.                         newPos.x -= size * 0.25f;
  68.                     }
  69.  
  70.                     if ((i & 1) == 1)
  71.                     {
  72.                         newPos.z += size * 0.25f;
  73.                     }
  74.                     else
  75.                     {
  76.                         newPos.z -= size * 0.25f;
  77.                     }
  78.  
  79.                     subNodes[i] = new OctreeNode<GameObject>(newPos, size * 0.5f);
  80.                     if (depth > 0)
  81.                     {
  82.                         subNodes[i].Subdivide(depth - 1);
  83.                     }
  84.                 }
  85.             }
  86.             public bool IsLeaf()
  87.             {
  88.                 return subNodes == null;
  89.             }
  90.         }
  91.  
  92.         private int GetIndexOfPosition(Vector3 lookupPosition, Vector3 nodePosition)
  93.         {
  94.             int index = 0;
  95.  
  96.             index |= lookupPosition.y > nodePosition.y ? 4 : 0;
  97.             index |= lookupPosition.x > nodePosition.x ? 2 : 0;
  98.             index |= lookupPosition.z > nodePosition.z ? 1 : 0;
  99.  
  100.             return index;
  101.         }
  102.  
  103.         public OctreeNode<GameObject> GetRoot()
  104.         {
  105.             return node;
  106.         }
  107.     }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement