Sector Generation

Fixed Laser cancelling bug
Fixed Turrets not activating
Added ability to bind player perspective to orbital entities
Added Markov Chain name generator + Australian place name dataset
Modified old "galaxy" shader to serve as sector background
Implemented Cem Yuksel's Weighted Sample Elimination for Poisson Disk Sample Sets
Created sector generator which places sectors using WSE
This commit is contained in:
Reinier Vens 2021-03-10 09:18:07 +00:00
commit 3f9af29843
108 changed files with 139298 additions and 13543 deletions

View file

@ -0,0 +1,137 @@
/*MIT License
Copyright(c) 2018 Vili Volčini / viliwonka
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
/*
The object used for querying. This object should be persistent - re-used for querying.
Contains internal array for pooling, so that it doesn't generate (too much) garbage.
The array never down-sizes, only up-sizes, so the more you use this object, less garbage will it make over time.
Should be used only by 1 thread,
which means each thread should have it's own KDQuery object in order for querying to be thread safe.
KDQuery can query different KDTrees.
*/
using System.Collections.Generic;
using System;
using Unity.Mathematics;
using static Unity.Mathematics.math;
namespace DataStructures.ViliWonka.KDTree {
public partial class KDQuery {
protected KDQueryNode[] queueArray; // queue array
protected Heap.MinHeap<KDQueryNode> minHeap; //heap for k-nearest
protected int count = 0; // size of queue
protected int queryIndex = 0; // current index at stack
/// <summary>
/// Returns initialized node from stack that also acts as a pool
/// The returned reference to node stays in stack
/// </summary>
/// <returns>Reference to pooled node</returns>
private KDQueryNode PushGetQueue() {
KDQueryNode node = null;
if (count < queueArray.Length) {
if (queueArray[count] == null)
queueArray[count] = node = new KDQueryNode();
else
node = queueArray[count];
}
else {
// automatic resize of pool
Array.Resize(ref queueArray, queueArray.Length * 2);
node = queueArray[count] = new KDQueryNode();
}
count++;
return node;
}
protected void PushToQueue(KDNode node, float2 tempClosestPoint) {
var queryNode = PushGetQueue();
queryNode.node = node;
queryNode.tempClosestPoint = tempClosestPoint;
}
protected void PushToHeap(KDNode node, float2 tempClosestPoint, float2 queryPosition) {
var queryNode = PushGetQueue();
queryNode.node = node;
queryNode.tempClosestPoint = tempClosestPoint;
float sqrDist = lengthsq(tempClosestPoint - queryPosition);
queryNode.distance = sqrDist;
minHeap.PushObj(queryNode, sqrDist);
}
protected int LeftToProcess {
get {
return count - queryIndex;
}
}
// just gets unprocessed node from stack
// increases queryIndex
protected KDQueryNode PopFromQueue() {
var node = queueArray[queryIndex];
queryIndex++;
return node;
}
protected KDQueryNode PopFromHeap() {
KDQueryNode heapNode = minHeap.PopObj();
queueArray[queryIndex]= heapNode;
queryIndex++;
return heapNode;
}
protected void Reset() {
count = 0;
queryIndex = 0;
minHeap.Clear();
}
public KDQuery(int queryNodesContainersInitialSize = 2048) {
queueArray = new KDQueryNode[queryNodesContainersInitialSize];
minHeap = new Heap.MinHeap<KDQueryNode>(queryNodesContainersInitialSize);
}
}
}

View file

@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: 57b0c8c3d7b727847b235e82d2fbead0
timeCreated: 1521924674
licenseType: Free
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,44 @@
/*MIT License
Copyright(c) 2018 Vili Volčini / viliwonka
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
using Unity.Mathematics;
namespace DataStructures.ViliWonka.KDTree {
public class KDQueryNode {
public KDNode node;
public float2 tempClosestPoint;
public float distance;
public KDQueryNode() {
}
public KDQueryNode(KDNode node, float2 tempClosestPoint) {
this.node = node;
this.tempClosestPoint = tempClosestPoint;
}
}
}

View file

@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: cbe8b45a46d50254494605e93da9d3ed
timeCreated: 1521907406
licenseType: Free
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,145 @@
/*MIT License
Copyright(c) 2018 Vili Volčini / viliwonka
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
using System.Collections.Generic;
using System;
using Unity.Mathematics;
using static Unity.Mathematics.math;
namespace DataStructures.ViliWonka.KDTree {
using Heap;
public partial class KDQuery {
public void ClosestPoint(KDTree tree, float2 queryPosition, List<int> resultIndices, List<float> resultDistances = null) {
Reset();
float2[] points = tree.Points;
int[] permutation = tree.Permutation;
if (points.Length == 0) {
return;
}
int smallestIndex = 0;
/// Smallest Squared Radius
float SSR = Single.PositiveInfinity;
var rootNode = tree.RootNode;
float2 rootClosestPoint = rootNode.bounds.ClosestPoint(queryPosition);
PushToHeap(rootNode, rootClosestPoint, queryPosition);
KDQueryNode queryNode = null;
KDNode node = null;
int partitionAxis;
float partitionCoord;
float2 tempClosestPoint;
// searching
while(minHeap.Count > 0) {
queryNode = PopFromHeap();
if(queryNode.distance > SSR)
continue;
node = queryNode.node;
if(!node.Leaf) {
partitionAxis = node.partitionAxis;
partitionCoord = node.partitionCoordinate;
tempClosestPoint = queryNode.tempClosestPoint;
if((tempClosestPoint[partitionAxis] - partitionCoord) < 0) {
// we already know we are on the side of negative bound/node,
// so we don't need to test for distance
// push to stack for later querying
PushToHeap(node.negativeChild, tempClosestPoint, queryPosition);
// project the tempClosestPoint to other bound
tempClosestPoint[partitionAxis] = partitionCoord;
if(node.positiveChild.Count != 0) {
PushToHeap(node.positiveChild, tempClosestPoint, queryPosition);
}
}
else {
// we already know we are on the side of positive bound/node,
// so we don't need to test for distance
// push to stack for later querying
PushToHeap(node.positiveChild, tempClosestPoint, queryPosition);
// project the tempClosestPoint to other bound
tempClosestPoint[partitionAxis] = partitionCoord;
if(node.positiveChild.Count != 0) {
PushToHeap(node.negativeChild, tempClosestPoint, queryPosition);
}
}
}
else {
float sqrDist;
// LEAF
for(int i = node.start; i < node.end; i++) {
int index = permutation[i];
sqrDist = lengthsq(points[index] - queryPosition);
if(sqrDist <= SSR) {
SSR = sqrDist;
smallestIndex = index;
}
}
}
}
resultIndices.Add(smallestIndex);
if(resultDistances != null) {
resultDistances.Add(SSR);
}
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: a226a270de4df034986ad64328da4cc2
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,152 @@
/*MIT License
Copyright(c) 2018 Vili Volčini / viliwonka
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
using System.Collections.Generic;
using System;
using Unity.Mathematics;
using static Unity.Mathematics.math;
namespace DataStructures.ViliWonka.KDTree {
public partial class KDQuery {
public void Interval(KDTree tree, float2 min, float2 max, List<int> resultIndices) {
Reset();
float2[] points = tree.Points;
int[] permutation = tree.Permutation;
var rootNode = tree.RootNode;
PushToQueue(
rootNode,
rootNode.bounds.ClosestPoint((min + max) / 2)
);
KDQueryNode queryNode = null;
KDNode node = null;
// KD search with pruning (don't visit areas which distance is more away than range)
// Recursion done on Stack
while(LeftToProcess > 0) {
queryNode = PopFromQueue();
node = queryNode.node;
if(!node.Leaf) {
int partitionAxis = node.partitionAxis;
float partitionCoord = node.partitionCoordinate;
float2 tempClosestPoint = queryNode.tempClosestPoint;
if((tempClosestPoint[partitionAxis] - partitionCoord) < 0) {
// we already know we are inside negative bound/node,
// so we don't need to test for distance
// push to stack for later querying
// tempClosestPoint is inside negative side
// assign it to negativeChild
PushToQueue(node.negativeChild, tempClosestPoint);
tempClosestPoint[partitionAxis] = partitionCoord;
// testing other side
if(node.positiveChild.Count != 0
&& tempClosestPoint[partitionAxis] <= max[partitionAxis]) {
PushToQueue(node.positiveChild, tempClosestPoint);
}
}
else {
// we already know we are inside positive bound/node,
// so we don't need to test for distance
// push to stack for later querying
// tempClosestPoint is inside positive side
// assign it to positiveChild
PushToQueue(node.positiveChild, tempClosestPoint);
// project the tempClosestPoint to other bound
tempClosestPoint[partitionAxis] = partitionCoord;
// testing other side
if(node.negativeChild.Count != 0
&& tempClosestPoint[partitionAxis] >= min[partitionAxis]) {
PushToQueue(node.negativeChild, tempClosestPoint);
}
}
}
else {
// LEAF
// testing if node bounds are inside the query interval
if(node.bounds.min[0] >= min[0]
&& node.bounds.min[1] >= min[1]
&& node.bounds.min[2] >= min[2]
&& node.bounds.max[0] <= max[0]
&& node.bounds.max[1] <= max[1]
&& node.bounds.max[2] <= max[2]) {
for(int i = node.start; i < node.end; i++) {
resultIndices.Add(permutation[i]);
}
}
// node is not inside query interval, need to do test on each point separately
else {
for(int i = node.start; i < node.end; i++) {
int index = permutation[i];
float2 v = points[index];
if(v[0] >= min[0]
&& v[1] >= min[1]
&& v[2] >= min[2]
&& v[0] <= max[0]
&& v[1] <= max[1]
&& v[2] <= max[2]) {
resultIndices.Add(index);
}
}
}
}
}
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: ea62a128d2f9e7848ae1c116012e36e1
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,161 @@
/*MIT License
Copyright(c) 2018 Vili Volčini / viliwonka
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#define KDTREE_VISUAL_DEBUG
using System.Collections.Generic;
using System;
using Unity.Mathematics;
using static Unity.Mathematics.math;
namespace DataStructures.ViliWonka.KDTree {
using Heap;
public partial class KDQuery {
SortedList<int, KSmallestHeap<int>> _heaps = new SortedList<int, KSmallestHeap<int>>();
/// <summary>
/// Returns indices to k closest points, and optionaly can return distances
/// </summary>
/// <param name="tree">Tree to do search on</param>
/// <param name="queryPosition">Position</param>
/// <param name="k">Max number of points</param>
/// <param name="resultIndices">List where resulting indices will be stored</param>
/// <param name="resultDistances">Optional list where resulting distances will be stored</param>
public void KNearest(KDTree tree, float2 queryPosition, int k, List<int> resultIndices, List<float> resultDistances = null) {
// pooled heap arrays
KSmallestHeap<int> kHeap;
_heaps.TryGetValue(k, out kHeap);
if(kHeap == null) {
kHeap = new KSmallestHeap<int>(k);
_heaps.Add(k, kHeap);
}
kHeap.Clear();
Reset();
float2[] points = tree.Points;
int[] permutation = tree.Permutation;
///Biggest Smallest Squared Radius
float BSSR = Single.PositiveInfinity;
var rootNode = tree.RootNode;
float2 rootClosestPoint = rootNode.bounds.ClosestPoint(queryPosition);
PushToHeap(rootNode, rootClosestPoint, queryPosition);
KDQueryNode queryNode = null;
KDNode node = null;
int partitionAxis;
float partitionCoord;
float2 tempClosestPoint;
// searching
while(minHeap.Count > 0) {
queryNode = PopFromHeap();
if(queryNode.distance > BSSR)
continue;
node = queryNode.node;
if(!node.Leaf) {
partitionAxis = node.partitionAxis;
partitionCoord = node.partitionCoordinate;
tempClosestPoint = queryNode.tempClosestPoint;
if((tempClosestPoint[partitionAxis] - partitionCoord) < 0) {
// we already know we are on the side of negative bound/node,
// so we don't need to test for distance
// push to stack for later querying
PushToHeap(node.negativeChild, tempClosestPoint, queryPosition);
// project the tempClosestPoint to other bound
tempClosestPoint[partitionAxis] = partitionCoord;
if(node.positiveChild.Count != 0) {
PushToHeap(node.positiveChild, tempClosestPoint, queryPosition);
}
}
else {
// we already know we are on the side of positive bound/node,
// so we don't need to test for distance
// push to stack for later querying
PushToHeap(node.positiveChild, tempClosestPoint, queryPosition);
// project the tempClosestPoint to other bound
tempClosestPoint[partitionAxis] = partitionCoord;
if(node.positiveChild.Count != 0) {
PushToHeap(node.negativeChild, tempClosestPoint, queryPosition);
}
}
}
else {
float sqrDist;
// LEAF
for(int i = node.start; i < node.end; i++) {
int index = permutation[i];
sqrDist = lengthsq(points[index] - queryPosition);
if(sqrDist <= BSSR) {
kHeap.PushObj(index, sqrDist);
if(kHeap.Full) {
BSSR = kHeap.HeadValue;
}
}
}
}
}
kHeap.FlushResult(resultIndices, resultDistances);
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: ded2e8ab341672849898f46a7719a7a0
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,231 @@
/*MIT License
Copyright(c) 2018 Vili Volčini / viliwonka
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
using System.Collections.Generic;
using System;
using Unity.Mathematics;
using static Unity.Mathematics.math;
namespace DataStructures.ViliWonka.KDTree {
public partial class KDQuery {
/// <summary>
/// Search by radius method.
/// </summary>
/// <param name="tree">Tree to do search on</param>
/// <param name="queryPosition">Position</param>
/// <param name="queryRadius">Radius</param>
/// <param name="resultIndices">Initialized list, cleared.</param>
public void Radius(KDTree tree, float2 queryPosition, float queryRadius, List<int> resultIndices, List<float> resultSqrDistances) {
Reset();
float2[] points = tree.Points;
int[] permutation = tree.Permutation;
float squaredRadius = queryRadius * queryRadius;
var rootNode = tree.RootNode;
PushToQueue(rootNode, rootNode.bounds.ClosestPoint(queryPosition));
KDQueryNode queryNode = null;
KDNode node = null;
// KD search with pruning (don't visit areas which distance is more away than range)
// Recursion done on Stack
while(LeftToProcess > 0) {
queryNode = PopFromQueue();
node = queryNode.node;
if(!node.Leaf) {
int partitionAxis = node.partitionAxis;
float partitionCoord = node.partitionCoordinate;
float2 tempClosestPoint = queryNode.tempClosestPoint;
if((tempClosestPoint[partitionAxis] - partitionCoord) < 0) {
// we already know we are inside negative bound/node,
// so we don't need to test for distance
// push to stack for later querying
// tempClosestPoint is inside negative side
// assign it to negativeChild
PushToQueue(node.negativeChild, tempClosestPoint);
tempClosestPoint[partitionAxis] = partitionCoord;
float sqrDist = lengthsq(tempClosestPoint - queryPosition);
// testing other side
if(node.positiveChild.Count != 0
&& sqrDist <= squaredRadius) {
PushToQueue(node.positiveChild, tempClosestPoint);
}
}
else {
// we already know we are inside positive bound/node,
// so we don't need to test for distance
// push to stack for later querying
// tempClosestPoint is inside positive side
// assign it to positiveChild
PushToQueue(node.positiveChild, tempClosestPoint);
// project the tempClosestPoint to other bound
tempClosestPoint[partitionAxis] = partitionCoord;
float sqrDist = lengthsq(tempClosestPoint - queryPosition);
// testing other side
if(node.negativeChild.Count != 0
&& sqrDist <= squaredRadius) {
PushToQueue(node.negativeChild, tempClosestPoint);
}
}
}
else {
// LEAF
for(int i = node.start; i < node.end; i++) {
int index = permutation[i];
if(lengthsq(points[index] - queryPosition) <= squaredRadius) {
resultIndices.Add(index);
}
}
}
}
}
/// <summary>
/// Search by radius method.
/// </summary>
/// <param name="tree">Tree to do search on</param>
/// <param name="queryPosition">Position</param>
/// <param name="queryRadius">Radius</param>
/// <param name="resultIndices">Initialized list, cleared.</param>
public IEnumerable<(int index, float sqrDistance)> Radius(KDTree tree, float2 queryPosition, float queryRadius) {
Reset();
float2[] points = tree.Points;
int[] permutation = tree.Permutation;
float squaredRadius = queryRadius * queryRadius;
var rootNode = tree.RootNode;
PushToQueue(rootNode, rootNode.bounds.ClosestPoint(queryPosition));
KDQueryNode queryNode = null;
KDNode node = null;
// KD search with pruning (don't visit areas which distance is more away than range)
// Recursion done on Stack
while(LeftToProcess > 0) {
queryNode = PopFromQueue();
node = queryNode.node;
if(!node.Leaf) {
int partitionAxis = node.partitionAxis;
float partitionCoord = node.partitionCoordinate;
float2 tempClosestPoint = queryNode.tempClosestPoint;
if((tempClosestPoint[partitionAxis] - partitionCoord) < 0) {
// we already know we are inside negative bound/node,
// so we don't need to test for distance
// push to stack for later querying
// tempClosestPoint is inside negative side
// assign it to negativeChild
PushToQueue(node.negativeChild, tempClosestPoint);
tempClosestPoint[partitionAxis] = partitionCoord;
float sqrDist = lengthsq(tempClosestPoint - queryPosition);
// testing other side
if(node.positiveChild.Count != 0
&& sqrDist <= squaredRadius) {
PushToQueue(node.positiveChild, tempClosestPoint);
}
}
else {
// we already know we are inside positive bound/node,
// so we don't need to test for distance
// push to stack for later querying
// tempClosestPoint is inside positive side
// assign it to positiveChild
PushToQueue(node.positiveChild, tempClosestPoint);
// project the tempClosestPoint to other bound
tempClosestPoint[partitionAxis] = partitionCoord;
float sqrDist = lengthsq(tempClosestPoint - queryPosition);
// testing other side
if(node.negativeChild.Count != 0
&& sqrDist <= squaredRadius) {
PushToQueue(node.negativeChild, tempClosestPoint);
}
}
}
else {
// LEAF
for(int i = node.start; i < node.end; i++) {
int index = permutation[i];
var lengthSq = lengthsq(points[index] - queryPosition);
if(lengthSq <= squaredRadius) {
yield return (index, lengthSq);
}
}
}
}
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 17086d61d13921a4ba22e206ef32e74f
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: