Quadtree and Octree implementation for unity based on the genericTree project.
Import the unityPackage from the release tab. Dependencies are included.
This is a MonoBehaviour
independend implementation. There is no constraint to use the API within a MonoBehaviour
. Only UnityEngine.Vector3
and UnityEngine.Vector2
is required from Unity.
public class Example : MonoBehaviour
{
public Vector3 size;
public int maxDepth;
public int maxLeafsPerNode;
public Octree tree;
void Start()
{
tree = new Octree(transform.position, size, maxDepth, maxLeafsPerNode);
}
}
public class Example : MonoBehaviour
{
public Octree tree;
void Update()
{
// ExampleItem have to implement the ILeaf<T> interface
var leaf = new ExampleItem();
// add a leaf to the structure
tree.Add(leaf);
// search for leafs nearby
var leafs = tree.FindBySphere(transform.position, searchRadius);
// removes a leaf from the structure
tree.Remove(leaf);
}
}
public class BoxLeaf : MonoBehaviour, ILeaf<Vector3>
{
public Vector3 size;
private Box boundingBox;
void Awake()
{
boundingBox = new Box(transform.position, size);
}
public bool IntersectionTest(Volume<Vector3> volume)
=> boundingBox.TestIntersection(volume);
}