Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- namespace Assets.Scripts._CoreLibaryCode
- {
- public class Octree<GameObject>
- {
- private OctreeNode<GameObject> node;
- private int depth;
- public Octree(Vector3 position, float size, int depth)
- {
- node = new OctreeNode<GameObject>(position, size);
- node.Subdivide(depth);
- }
- public class OctreeNode<GameObject>
- {
- Vector3 position;
- float size;
- [SerializeField] OctreeNode<GameObject>[] subNodes;
- IList<GameObject> value;
- public OctreeNode(Vector3 pos, float size)
- {
- position = pos;
- this.size = size;
- }
- public IEnumerable<OctreeNode<GameObject>> Nodes
- {
- get { return subNodes; }
- }
- public Vector3 Position
- {
- get { return position; }
- }
- public float Size
- {
- get { return size; }
- }
- public void Subdivide(int depth = 0)
- {
- subNodes = new OctreeNode<GameObject>[8];
- for (int i = 0; i < subNodes.Length; i++)
- {
- Vector3 newPos = position;
- if ((i & 4) == 4)
- {
- newPos.y += size * 0.25f;
- }
- else
- {
- newPos.y -= size * 0.25f;
- }
- if ((i & 2) == 2)
- {
- newPos.x += size * 0.25f;
- }
- else
- {
- newPos.x -= size * 0.25f;
- }
- if ((i & 1) == 1)
- {
- newPos.z += size * 0.25f;
- }
- else
- {
- newPos.z -= size * 0.25f;
- }
- subNodes[i] = new OctreeNode<GameObject>(newPos, size * 0.5f);
- if (depth > 0)
- {
- subNodes[i].Subdivide(depth - 1);
- }
- }
- }
- public bool IsLeaf()
- {
- return subNodes == null;
- }
- }
- private int GetIndexOfPosition(Vector3 lookupPosition, Vector3 nodePosition)
- {
- int index = 0;
- index |= lookupPosition.y > nodePosition.y ? 4 : 0;
- index |= lookupPosition.x > nodePosition.x ? 2 : 0;
- index |= lookupPosition.z > nodePosition.z ? 1 : 0;
- return index;
- }
- public OctreeNode<GameObject> GetRoot()
- {
- return node;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement