1
- ![ NativeTrees] ( NativeTrees.png " NativeTrees ")
1
+ ![ NativeTrees] ( ./Packages/NativeTrees/ NativeTrees.png " NativeTrees ")
2
2
3
- Generic sparse octree and quadtree
3
+ Generic sparse octree and quadtree
4
4
that store objects together with their axis aligned bounding boxes (AABB's)
5
5
6
6
Written in C# for Unity's burst compiler and the ECS/DOTS framework.
@@ -11,7 +11,6 @@ Tested with Unity 2021.1.3.11f1
11
11
- Range (AABB overlap)
12
12
- K-Nearest neighbours
13
13
14
-
15
14
### Other features:
16
15
- Implemented as a sparse tree, so only stores nodes that are occupied. Memory usage is therefore relatively low.
17
16
The octree has a max depth of 10 and the quad tree a max depth of 15.
@@ -22,30 +21,36 @@ Tested with Unity 2021.1.3.11f1
22
21
### Limitations:
23
22
- Objects must be structs (Burst limitation)
24
23
- No remove or update. Tried several approaches but they either left an unbalanced tree or doing a
25
- full clear and re-insert was faster.
24
+ full clear and re-insert was faster.
26
25
- No foreach support, we leverage the stack and "struct" delegates, which suits the recursive
27
- nature of the tree better.
28
-
26
+ nature of the tree better.
27
+
29
28
- Insertion can only be done on a single thread. Queries ofcourse can be across multiple threads.
30
29
31
30
### Future todo's:
32
31
- Frustrum query
33
32
- 'Fat' raycast (virtually expand AABB's of nodes and objects when testing for ray intersections)
34
33
34
+ ## Installation
35
+
36
+ Using the Unity package manager, choose Addd Package from git URL and enter:
37
+
38
+ https://github.com/bartofzo/NativeTrees.git?path=/Packages/NativeTrees
39
+
35
40
## Performance
36
41
37
- The trees are heaviliy optimized to make use of SIMD instructions.
42
+ The trees are heaviliy optimized to make use of SIMD instructions.
38
43
Therefore performance is best when used in burst compiled code.
39
44
40
- Queries are * very* fast.
45
+ Queries are * very* fast.
41
46
The raycast never visits more nodes than absolutely neccessary.
42
47
The overlap (and insertion) use a technique where to test in which child nodes AABB's should go, only
43
48
two comparisons are made followed up by some bitwise operations. (See the source for an explanation).
44
49
45
50
Nearest neighbour is the slowest of the bunch (but still fast) as it has some overhead in keeping track of a priority queue.
46
51
47
52
Actual performance can vary wildly depending on the structure of your tree and it's
48
- settings. For the sake of cool stats, here are some numbers on insertion times and raycasts for random points and AABB's,
53
+ settings. For the sake of cool stats, here are some numbers on insertion times and raycasts for random points and AABB's,
49
54
Single thread burst compiled, maxDepth of 8. These numbers should not be taken too seriously because it's random data
50
55
and the tree will be divided almost equally everywhere, which in most realistic scenarios is not the case.
51
56
@@ -63,15 +68,139 @@ Run on a MBP 16" i9 2.3 GHz from 2019.
63
68
|500K |51.00ms |131.58ms |60.08ms |164.17ms |0.84ms |0.45ms
64
69
|1M |129.03ms |260.55ms |152.50ms |301.99ms |1.04ms |0.55ms
65
70
71
+ ## Usage
72
+
73
+ There are two samples included that show how to use the octree and quadtree.
74
+ The extension classes provide readymade solutions for AABB only checking. For more complicated
75
+ shapes you must provide your own ray/overlap/distance calculations.
76
+
77
+ ### Insertion
78
+ The objects can be of any unmanaged type, when inserting, an AABB must be provided:
79
+
80
+ // Insert a bunch of triangles
81
+ for (int i = 0; i < tris.Length; i++)
82
+ {
83
+ var triangle = tris[i];
84
+ octree.Insert(triangle, triangle.GetAABB());
85
+ }
86
+
87
+ Often times however, it's more efficient to insert Id's that map to something outside of
88
+ the tree (like DOTS entities).
89
+
90
+ If you know your objects are points, you can insert them faster by using:
91
+
92
+ // Insert entities that are 'points'
93
+ for (int i = 0; i < entities.Length; i++)
94
+ {
95
+ var entity = entities[i];
96
+ octree.InsertPoint(entities[i], positions[i]);
97
+ }
98
+
99
+ Note that objects inserted as points only support range and nearest neighbour queries.
100
+
101
+ ### Queries
102
+
103
+ All of the supported queries use the same pattern which is
104
+ to (ab)use structs as a sort of delegate. This separates collision/intersection
105
+ code from the type of objects, allowing you to insert even primitive types or types from another assembly.
106
+ This turned out to be the most efficent and easiest to implement while keeping things fully compatibly with Unity's Burst compiler.
107
+
108
+ ### Raycast
109
+
110
+ A raycast query for example, requires you to implement IOctreeRayIntersecter which
111
+ acts as a delegate to determine if a ray intersects with an object that's in the tree.
112
+
113
+ public static bool RaycastAABB<T>(this NativeOctree<T> octree, Ray ray, out OctreeRaycastHit<T> hit) where T : unmanaged
114
+ {
115
+ return octree.Raycast<RayAABBIntersecter<T>>(ray, out hit);
116
+ }
117
+
118
+ struct RayAABBIntersecter<T> : IOctreeRayIntersecter<T>
119
+ {
120
+ public bool IntersectRay(in PrecomputedRay ray, T obj, AABB objBounds, out float distance)
121
+ {
122
+ return objBounds.IntersectsRay(ray, out distance);
123
+ }
124
+ }
125
+
126
+ The example above just tests the ray against the object's bounds. (See NativeOctreeExtensions) But you could go a step further
127
+ and test it against a triangle, a collider and so forth. Note that the tree itself
128
+ does not automatically test for Ray-AABB intersections on the objects, so it's usually a good decision to early exit
129
+ if the ray doesn't exit with the object's bounds since those checks are cheap.
130
+
131
+ ### Nearest Neighbour
132
+ NativeTrees support nearest neighbour queries. You should implement IOctreeNearestVisitor and IOctreeDistanceProvider.
133
+
134
+ struct AABBDistanceSquaredProvider<T> : IOctreeDistanceProvider<T>
135
+ {
136
+ // Just return the distance squared to our bounds
137
+ public float DistanceSquared(float3 point, T obj, AABB bounds) => bounds.DistanceSquared(point);
138
+ }
139
+
140
+ struct OctreeNearestAABBVisitor<T> : IOctreeNearestVisitor<T>
141
+ {
142
+ public T nearest;
143
+ public bool found;
144
+
145
+ public bool OnVist(T obj)
146
+ {
147
+ this.found = true;
148
+ this.nearest = obj;
149
+
150
+ return false; // immediately stop iterating at first hit
151
+ // if we want the 2nd or 3rd neighbour, we could iterate on and keep track of the count!
152
+ }
153
+ }
154
+
155
+ The extensions classes show an example of these implementation. But only for AABB's.
156
+ If you need more detail on your distance, you can implement your type specific behaviour using these interfaces.
157
+
158
+ To get the nearest neighbour:
159
+
160
+ var visitor = new OctreeNearestAABBVisitor<Entity>();
161
+ octree.Nearest(point, maxDistance, ref visitor, default(AABBDistanceSquaredProvider<Entity>));
162
+ Entity nearestEntity = visitor.nearest;
163
+
164
+ ### Range
165
+
166
+ Here's an example that adds unique objects that overlap with a range to a hashset:
167
+
168
+ public static void RangeAABBUnique<T>(this NativeOctree<T> octree, AABB range, NativeParallelHashSet<T> results)
169
+ where T : unmanaged, IEquatable<T>
170
+ {
171
+ var vistor = new RangeAABBUniqueVisitor<T>()
172
+ {
173
+ results = results
174
+ };
175
+
176
+ octree.Range(range, ref vistor);
177
+ }
178
+
179
+ struct RangeAABBUniqueVisitor<T> : IOctreeRangeVisitor<T> where T : unmanaged, IEquatable<T>
180
+ {
181
+ public NativeParallelHashSet<T> results;
182
+
183
+ public bool OnVisit(T obj, AABB objBounds, AABB queryRange)
184
+ {
185
+ // check if our object's AABB overlaps with the query AABB
186
+ if (objBounds.Overlaps(queryRange))
187
+ results.Add(obj);
188
+
189
+ return true; // always keep iterating, we want to catch all objects
190
+ }
191
+ }
192
+
193
+ It's important to note that the query itself iterates all of the objects that are in nodes that overlap with
194
+ the input range. An extra check should be performed to test if the object overlaps.
195
+ Also, if the objects aren't points, it's possible for them to be visited multiple times as they reside in more than one node.
196
+ A hashset can be used to only visit each object once.
197
+
66
198
### Support
67
199
Feel free to raise an issue or contact me for any questions.
200
+ The code is free to use in your project(s).
201
+ If this was helpful to you, consider buying me a coffee ;)
68
202
69
- ### Donate
70
- The code is free to use in your project(s).
71
- If this was helpful to you and you're feeling generous, you can support me with a donation to help me pay for my energy bills and making
72
- more of these projects in the future ;)
73
-
74
- [ ![ paypal] ( https://www.paypalobjects.com/en_US/i/btn/btn_donateCC_LG.gif )] ( https://paypal.me/bartofzo?country.x=NL&locale.x=nl_NL )
203
+ https://ko-fi.com/bartofzo
75
204
76
205
Thank you!
77
206
0 commit comments