All Projects → viliwonka → Kdtree

viliwonka / Kdtree

Licence: mit
Fast KDTree for Unity, with thread-safe querying.

Labels

Projects that are alternatives of or similar to Kdtree

Opengraphic
Graphic Engine & Game Engine lists
Stars: ✭ 772 (+220.33%)
Mutual labels:  unity-3d
Playfabgameserver
Out of the Box Game Server for PlayFab.com
Stars: ✭ 89 (-63.07%)
Mutual labels:  unity-3d
Unity resources
A list of resources and tutorials for those doing programming in Unity.
Stars: ✭ 170 (-29.46%)
Mutual labels:  unity-3d
Beaverandfairies
Stars: ✭ 14 (-94.19%)
Mutual labels:  unity-3d
Voxelframework
An awesome Voxel framework for Unity (Game Engine)
Stars: ✭ 57 (-76.35%)
Mutual labels:  unity-3d
Lowpolyshaders
Unity shaders optimized for Low Poly models.
Stars: ✭ 157 (-34.85%)
Mutual labels:  unity-3d
Unity Game Hacking
A guide for hacking unity games
Stars: ✭ 710 (+194.61%)
Mutual labels:  unity-3d
Json
A really simple C# JSON Parser in 350 lines
Stars: ✭ 202 (-16.18%)
Mutual labels:  unity-3d
Dlibfacelandmarkdetector
FaceLandmark Detector using Dlib (Unity Asset Plugin)
Stars: ✭ 80 (-66.8%)
Mutual labels:  unity-3d
Unityskidmarks
A simple skidmark effect generator for Unity3D
Stars: ✭ 165 (-31.54%)
Mutual labels:  unity-3d
Savegamepro
A Complete and Powerful Save Game Solution for Unity (Game Engine)
Stars: ✭ 30 (-87.55%)
Mutual labels:  unity-3d
Energysuite
Simple real-time energy based system for your Unity3d game
Stars: ✭ 31 (-87.14%)
Mutual labels:  unity-3d
Unitysdk
Unity C# SDKs for PlayFab
Stars: ✭ 161 (-33.2%)
Mutual labels:  unity-3d
Com.unity.multiplayer.mlapi
A game networking framework built for the Unity Engine to abstract game networking concepts.
Stars: ✭ 781 (+224.07%)
Mutual labels:  unity-3d
Rimlight
Customizable rimlight shader for Unity that includes pulsation and noise scrolling. Give your scenes that extra oomph!
Stars: ✭ 170 (-29.46%)
Mutual labels:  unity-3d
Radialprogressbar
Customizable radial progress bar shader for Unity3D. Allows you to set arc range, minimum and maximum colors, textures, radius, and a few more things. Create HP Bars, Speedometers, rank progress, etc!
Stars: ✭ 714 (+196.27%)
Mutual labels:  unity-3d
Unitylibrary
📚 Library of all kind of scripts, snippets & shaders for Unity
Stars: ✭ 1,968 (+716.6%)
Mutual labels:  unity-3d
Apple Signin Unity
Unity plugin to support Sign In With Apple Id
Stars: ✭ 228 (-5.39%)
Mutual labels:  unity-3d
Unity Ecs Rts
Trying to recreate a simple RTS game using Unity and pure ECS
Stars: ✭ 189 (-21.58%)
Mutual labels:  unity-3d
Litenetlib
Lite reliable UDP library for Mono and .NET
Stars: ✭ 2,179 (+804.15%)
Mutual labels:  unity-3d

KDTree

Description

3D KDTree for Unity, with fast construction and fast & thread-safe querying, with minimal memory garbage.

It was designed:

  • to be working with Unity Vector3 structs, but can be modified to work with any other 3D (or 2D & 4D or higher) struct/arrays
  • for speedy & light Construction & Reconstruction,
  • to be light on memory, everything is pooled,
  • for fast querying,
  • queryable from multiple threads (thread-safe),

Query modes:

  • K-Nearest
  • Closest point query
  • Radius query
  • Interval query

How to use

Construction

First you need some array of points.

Example:

Vector3[] pointCloud = new Vector3[10000];

for(int i = 0; i < pointCloud.Length; i++)
	pointCloud[i] = Random.insideUnitSphere;

Then build the tree out of it. Note that original pointCloud shouldn't change, since tree is referencing it!

Note: Higher maxPointsPerLeafNode makes construction of tree faster, but querying slower. And true is inverse: Lower maxPointsPerLeafNode makes construction of tree slower, but querying faster.

int maxPointsPerLeafNode = 32;
KDTree tree = new KDTree(pointCloud, maxPointsPerLeafNode);

Reconstruction

If you wish to update points and reconstruct tree, you do it like this:

for(int i = 0; i < tree.Count; i++) {
    tree.Points[i] += Func(tree.Points[i]);
}

tree.Rebuild();

Such rebuilding will be with zero GC impact.

Other functions for rebuilding (data will be copied from array/list, not reference!).

public void Build(Vector3[] newPoints, int maxPointsPerLeafNode = -1);
public void Build(List<Vector3> newPoints, int maxPointsPerLeafNode = -1);

Querying

Now that tree has been constructed, make a KDQuery object.

Note: if you wish to do querying from multiple threads, then each own thread should have it's own KDQuery object.

Query.KDQuery query = new Query.KDQuery();

For most query methods you need pre-initialized results list & reference to tree that you wish to query. Results list will contain indexes for pointCloud array.

List should be cleared; but it's not necesary to clear it (if you wish to do multiple queries), but this way you will have duplicate indexes.

Query objects should be re-used, since it pools everything - to avoid unnecesarry allocations and deallocations.

List<int> results = new List<int>();

// spherical query
query.Radius(tree, position, radius, results);

// returns k nearest points         
query.KNearest(tree, position, k, results);

// bounds query
query.Interval(tree, min, max, results);

// closest point query
query.ClosestPoint(tree, position, results);

Post Query

If you wish to do something with query results, then use it like this:

for(int i = 0; i < results.Count; i++) {
	
	Vector3 p = pointCloud[results[i]];
	Draw(p);
}

Demos

Those demos show rebuilding tree & querying of Live Lorenz Attractor point cloud.

Drawing traversal nodes of KNearest Query

alt-text

KNearest Query

alt-text

Radius Query

alt-text

Interval/Bounds Query

alt-text

How it works?

Construction

Uses internal permutation array, so it doesn't modify original data array. Permutation is identity array at first (arr[i] = i), then gets sorted down the line. Hoare partitioning enables to sort permutation array inplace. (Quicksort uses hoare partitioning, too). Mid-point rule is used for node splitting - not the most optimal split but makes construction much faster.

KDQuery

All traversal nodes are pooled in internal queue. Uses binary heap for KNearest query. Heaps for all sizes are pooled inside KDQuery object.

Sources

https://www.cs.umd.edu/~mount/Papers/cgc99-smpack.pdf - Paper about slidding mid-point rule for node splitting.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].