Skip to content

Commit ea8c7df

Browse files
committed
Initial commit
0 parents  commit ea8c7df

31 files changed

+2143
-0
lines changed

.gitignore

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
2+
# Created by https://www.gitignore.io/api/unity
3+
# Edit at https://www.gitignore.io/?templates=unity
4+
5+
### Unity ###
6+
# This .gitignore file should be placed at the root of your Unity project directory
7+
#
8+
# Get latest from https://github.com/github/gitignore/blob/master/Unity.gitignore
9+
/[Ll]ibrary/
10+
/[Tt]emp/
11+
/[Oo]bj/
12+
/[Bb]uild/
13+
/[Bb]uilds/
14+
/[Ll]ogs/
15+
/[Mm]emoryCaptures/
16+
17+
# Never ignore Asset meta data
18+
!/[Aa]ssets/**/*.meta
19+
20+
# Uncomment this line if you wish to ignore the asset store tools plugin
21+
# /[Aa]ssets/AssetStoreTools*
22+
23+
# TextMesh Pro files
24+
[Aa]ssets/TextMesh*Pro/
25+
26+
# Autogenerated Jetbrains Rider plugin
27+
[Aa]ssets/Plugins/Editor/JetBrains*
28+
29+
# Visual Studio cache directory
30+
.vs/
31+
32+
# Gradle cache directory
33+
.gradle/
34+
35+
# Rider
36+
.idea/
37+
38+
# Autogenerated VS/MD/Consulo solution and project files
39+
ExportedObj/
40+
.consulo/
41+
*.csproj
42+
*.unityproj
43+
*.sln
44+
*.suo
45+
*.tmp
46+
*.user
47+
*.userprefs
48+
*.pidb
49+
*.booproj
50+
*.svd
51+
*.pdb
52+
*.mdb
53+
*.opendb
54+
*.VC.db
55+
56+
# Unity3D generated meta files
57+
*.pidb.meta
58+
*.pdb.meta
59+
*.mdb.meta
60+
61+
# Unity3D generated file on crash reports
62+
sysinfo.txt
63+
64+
# Builds
65+
*.apk
66+
*.unitypackage
67+
*.symbols.zip
68+
69+
# Crashlytics generated file
70+
crashlytics-build.properties
71+
72+
73+
# End of https://www.gitignore.io/api/unity

Assets/AABB2D.cs

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System;
2+
using Unity.Mathematics;
3+
4+
namespace QuadTree
5+
{
6+
[Serializable]
7+
public struct AABB2D {
8+
public float2 Center;
9+
public float2 Extents;
10+
11+
public float2 Size => Extents * 2;
12+
public float2 Min => Center - Extents;
13+
public float2 Max => Center + Extents;
14+
15+
public AABB2D(float2 center, float2 extents)
16+
{
17+
Center = center;
18+
Extents = extents;
19+
}
20+
21+
public bool Contains(float2 point) {
22+
if (point[0] < Center[0] - Extents[0]) {
23+
return false;
24+
}
25+
if (point[0] > Center[0] + Extents[0]) {
26+
return false;
27+
}
28+
29+
if (point[1] < Center[1] - Extents[1]) {
30+
return false;
31+
}
32+
if (point[1] > Center[1] + Extents[1]) {
33+
return false;
34+
}
35+
36+
return true;
37+
}
38+
39+
public bool Contains(AABB2D b) {
40+
return Contains(b.Center + new float2(-b.Extents.x, -b.Extents.y)) &&
41+
Contains(b.Center + new float2(-b.Extents.x, b.Extents.y)) &&
42+
Contains(b.Center + new float2(b.Extents.x, -b.Extents.y)) &&
43+
Contains(b.Center + new float2(b.Extents.x, b.Extents.y));
44+
}
45+
}
46+
}

Assets/AABB2D.cs.meta

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Editor.meta

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Editor/QuadTreeDrawer.cs

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using QuadTree;
2+
using UnityEditor;
3+
using UnityEngine;
4+
5+
public class QuadTreeDrawer : EditorWindow
6+
{
7+
[MenuItem("Window/QuadTreeDrawer")]
8+
static void Init()
9+
{
10+
GetWindow(typeof(QuadTreeDrawer)).Show();
11+
}
12+
13+
public static void Draw<T>(NativeQuadTree<T> quadTree) where T : unmanaged
14+
{
15+
QuadTreeDrawer window = (QuadTreeDrawer)GetWindow(typeof(QuadTreeDrawer));
16+
window.DoDraw(quadTree);
17+
}
18+
19+
[SerializeField]
20+
Color[][] pixels;
21+
22+
void DoDraw<T>(NativeQuadTree<T> quadTree) where T : unmanaged
23+
{
24+
pixels = new Color[512][];
25+
for (var i = 0; i < pixels.Length; i++)
26+
{
27+
pixels[i] = new Color[512];
28+
}
29+
NativeQuadTree<T>.Draw(quadTree, pixels);
30+
}
31+
32+
void OnGUI()
33+
{
34+
if(pixels != null)
35+
{
36+
var texture = new Texture2D(512, 512);
37+
for (var x = 0; x < pixels.Length; x++)
38+
{
39+
for (int y = 0; y < pixels[x].Length; y++)
40+
{
41+
texture.SetPixel(x, y, pixels[x][y]);
42+
}
43+
}
44+
texture.Apply();
45+
46+
GUI.DrawTexture(new Rect(0, 0, position.width, position.height), texture);
47+
}
48+
}
49+
}

Assets/Editor/QuadTreeDrawer.cs.meta

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Editor/QuadTreeTests.cs

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using System.Diagnostics;
2+
using NUnit.Framework;
3+
using QuadTree;
4+
using Unity.Burst;
5+
using Unity.Jobs;
6+
using Debug = UnityEngine.Debug;
7+
8+
public class QuadTreeTests
9+
{
10+
[Test]
11+
public void Insert()
12+
{
13+
var quadTree = new NativeQuadTree<int>(new AABB2D(0, 50));
14+
quadTree.InsertElement(new QuadElement<int>
15+
{
16+
pos = 5,
17+
element = 53
18+
});
19+
}
20+
21+
[Test]
22+
public void InsertTriggerDivide()
23+
{
24+
var s = Stopwatch.StartNew();
25+
var job = new AddJob()
26+
{
27+
QuadTree = new NativeQuadTree<int>(new AABB2D(0, 50))
28+
};
29+
job.Run();
30+
s.Stop();
31+
Debug.Log(s.Elapsed.TotalMilliseconds);
32+
33+
QuadTreeDrawer.Draw(job.QuadTree);
34+
job.QuadTree.Dispose();
35+
}
36+
37+
[BurstCompile]
38+
struct AddJob : IJob
39+
{
40+
public NativeQuadTree<int> QuadTree;
41+
42+
public void Execute()
43+
{
44+
var rnd = new Unity.Mathematics.Random(1);
45+
46+
for (int i = 0; i < 2000; i++)
47+
{
48+
QuadTree.InsertElement(new QuadElement<int>
49+
{
50+
pos = rnd.NextFloat2(-40, 40),
51+
element = 53
52+
});
53+
}
54+
}
55+
}
56+
}

Assets/Editor/QuadTreeTests.cs.meta

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)