14
14
15
15
using System ;
16
16
using System . Collections . Generic ;
17
+ using System . Linq ;
17
18
using com . google . apps . peltzer . client . model . core ;
18
19
using com . google . apps . peltzer . client . model . util ;
19
20
using NativeTrees ;
21
+ using Unity . Collections ;
20
22
using UnityEngine ;
21
23
22
24
/// <summary>
23
25
/// Class wrapping NativeTrees functions which implement our collision system.
24
26
/// </summary>
25
27
public static class BurstSpatialFunction
26
28
{
27
- private static List < NativeOctree < int > > _indices = new ( ) ;
29
+ private static List < NativeOctree < int > > _octrees = new ( ) ;
28
30
29
31
public static int AllocSpatialPartitioner ( )
30
32
{
31
33
var octree = new NativeOctree < int > ( ) ;
32
- _indices . Add ( octree ) ;
33
- return _indices . Count - 1 ;
34
+ _octrees . Add ( octree ) ;
35
+ return _octrees . Count - 1 ;
34
36
}
35
37
36
38
public static void SpatialPartitionerAddItem ( int idx , int itemId , Vector3 center , Vector3 extents )
37
39
{
38
- _indices [ idx ] . InsertPoint ( itemId , center ) ;
40
+ _octrees [ idx ] . InsertPoint ( itemId , center ) ;
39
41
}
40
42
41
43
public static void SpatialPartitionerUpdateAll ( int idx , object itemIds )
42
44
{
43
45
var octree = new NativeOctree < int > ( ) ;
44
- _indices [ idx ] = _indices [ idx ] ;
46
+ octree = _octrees [ idx ] ;
47
+ jgtuu
45
48
}
46
49
47
- public static void SpatialPartitionerRemoveItem ( int idx , int itemId )
50
+ public static int SpatialPartitionerIntersectedBy ( int idx , Vector3 testCenter , Vector3 testExtents , int [ ] returnArray , int returnArrayMaxSize )
48
51
{
49
- throw new NotImplementedException ( ) ;
52
+ AABB range = new AABB ( testCenter , testExtents ) ;
53
+ var results = new NativeParallelHashSet < int > ( returnArrayMaxSize , Allocator . TempJob ) ;
54
+ RangeAABBUnique ( _octrees [ idx ] , range , results ) ;
55
+ returnArray = results . ToArray ( ) ;
50
56
}
51
57
52
- public static int SpatialPartitionerContainedBy ( int idx , Vector3 testCenter , Vector3 testExtents , int [ ] returnArray , int returnArrayMaxSize )
58
+ public static void RangeAABBUnique < T > ( this NativeOctree < T > octree , AABB range , NativeParallelHashSet < T > results )
59
+ where T : unmanaged, IEquatable < T >
53
60
{
54
- throw new NotImplementedException ( ) ;
55
- }
61
+ var vistor = new RangeAABBUniqueVisitor < T > ( )
62
+ {
63
+ results = results
64
+ } ;
56
65
57
- public static int SpatialPartitionerIntersectedBy ( int idx , Vector3 testCenter , Vector3 testExtents , int [ ] returnArray , int returnArrayMaxSize )
58
- {
59
- throw new NotImplementedException ( ) ;
66
+ octree . Range ( range , ref vistor ) ;
60
67
}
61
68
62
- public static int HasItem ( int idx , int itemHandle )
69
+ struct RangeAABBUniqueVisitor < T > : IOctreeRangeVisitor < T > where T : unmanaged , IEquatable < T >
63
70
{
64
- throw new NotImplementedException ( ) ;
71
+ public NativeParallelHashSet < T > results ;
72
+
73
+ public bool OnVisit ( T obj , AABB objBounds , AABB queryRange )
74
+ {
75
+ // check if our object's AABB overlaps with the query AABB
76
+ if ( objBounds . Overlaps ( queryRange ) )
77
+ results . Add ( obj ) ;
78
+
79
+ return true ; // always keep iterating, we want to catch all objects
80
+ }
65
81
}
66
82
}
67
83
@@ -97,19 +113,6 @@ public void Add(T item, Bounds bounds)
97
113
itemBounds [ item ] = bounds ;
98
114
BurstSpatialFunction . SpatialPartitionerAddItem ( spatialPartitionId , id , bounds . center , bounds . extents ) ;
99
115
}
100
- public void UpdateItemBounds ( T item , Bounds bounds )
101
- {
102
- throw new NotImplementedException ( ) ;
103
- }
104
-
105
- /// <summary>
106
- /// Updates the bounding box of an item already in the collision system.
107
- /// </summary>
108
- // public void UpdateItemBounds(T item, Bounds bounds)
109
- // {
110
- // itemIds[item].b
111
- // BurstSpatialFunction.SpatialPartitionerUpdateAll(spatialPartitionId, itemIds);
112
- // }
113
116
114
117
/// <summary>
115
118
/// Remove an item from the system.
@@ -126,29 +129,6 @@ public void Remove(T item)
126
129
BurstSpatialFunction . SpatialPartitionerUpdateAll ( spatialPartitionId , itemIds ) ;
127
130
}
128
131
129
- /// <summary>
130
- /// Find items contained entirely within the given bounds.
131
- /// This method will create a set when the number of items
132
- /// is greater than zero.
133
- /// </summary>
134
- /// <param name="bounds">Containing bounds.</param>
135
- /// <param name="items">Set of items found. Null when this
136
- /// method returns false.</param>
137
- /// <param name="limit">Maximum number of items to find.</param>
138
- /// <returns>true if any items are found.</returns>
139
- public bool ContainedBy ( Bounds bounds , out HashSet < T > items ,
140
- int limit = SpatialIndex . MAX_INTERSECT_RESULTS )
141
- {
142
- int numResults = BurstSpatialFunction . SpatialPartitionerContainedBy ( spatialPartitionId , bounds . center ,
143
- bounds . extents , results , limit ) ;
144
- items = new HashSet < T > ( ) ;
145
- for ( int i = 0 ; i < numResults ; i ++ )
146
- {
147
- items . Add ( idsToItems [ results [ i ] ] ) ;
148
- }
149
- return items . Count > 0 ;
150
- }
151
-
152
132
/// <summary>
153
133
/// Returns whether the item is tracked in this system.
154
134
/// </summary>
0 commit comments