-
Notifications
You must be signed in to change notification settings - Fork 30
/
NativeQuadTreeDrawing.cs
77 lines (65 loc) · 2.8 KB
/
NativeQuadTreeDrawing.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;
using Unity.Mathematics;
using UnityEngine;
namespace NativeQuadTree
{
/// <summary>
/// Editor drawing of the NativeQuadTree
/// </summary>
public unsafe partial struct NativeQuadTree<T> where T : unmanaged
{
public static void Draw(NativeQuadTree<T> tree, NativeList<QuadElement<T>> results, AABB2D range,
Color[][] texture)
{
var widthMult = texture.Length / tree.bounds.Extents.x * 2 / 2 / 2;
var heightMult = texture[0].Length / tree.bounds.Extents.y * 2 / 2 / 2;
var widthAdd = tree.bounds.Center.x + tree.bounds.Extents.x;
var heightAdd = tree.bounds.Center.y + tree.bounds.Extents.y;
for (int i = 0; i < tree.nodes->Capacity; i++)
{
var node = UnsafeUtility.ReadArrayElement<QuadNode>(tree.nodes->Ptr, i);
if(node.count > 0)
{
for (int k = 0; k < node.count; k++)
{
var element =
UnsafeUtility.ReadArrayElement<QuadElement<T>>(tree.elements->Ptr, node.firstChildIndex + k);
texture[(int) ((element.pos.x + widthAdd) * widthMult)]
[(int) ((element.pos.y + heightAdd) * heightMult)] = Color.red;
}
}
}
if (results.IsCreated)
{
foreach (var element in results)
{
texture[(int)((element.pos.x + widthAdd) * widthMult)]
[(int)((element.pos.y + heightAdd) * heightMult)] = Color.green;
}
}
DrawBounds(texture, range, tree);
}
static void DrawBounds(Color[][] texture, AABB2D bounds, NativeQuadTree<T> tree)
{
var widthMult = texture.Length / tree.bounds.Extents.x * 2 / 2 / 2;
var heightMult = texture[0].Length / tree.bounds.Extents.y * 2 / 2 / 2;
var widthAdd = tree.bounds.Center.x + tree.bounds.Extents.x;
var heightAdd = tree.bounds.Center.y + tree.bounds.Extents.y;
var top = new float2(bounds.Center.x, bounds.Center.y - bounds.Extents.y);
var left = new float2(bounds.Center.x - bounds.Extents.x, bounds.Center.y);
for (int leftToRight = 0; leftToRight < bounds.Extents.x * 2; leftToRight++)
{
var poxX = left.x + leftToRight;
texture[(int) ((poxX + widthAdd) * widthMult)][(int) ((bounds.Center.y + heightAdd + bounds.Extents.y) * heightMult)] = Color.blue;
texture[(int) ((poxX + widthAdd) * widthMult)][(int) ((bounds.Center.y + heightAdd - bounds.Extents.y) * heightMult)] = Color.blue;
}
for (int topToBottom = 0; topToBottom < bounds.Extents.y * 2; topToBottom++)
{
var posY = top.y + topToBottom;
texture[(int) ((bounds.Center.x + widthAdd + bounds.Extents.x) * widthMult)][(int) ((posY + heightAdd) * heightMult)] = Color.blue;
texture[(int) ((bounds.Center.x + widthAdd - bounds.Extents.x) * widthMult)][(int) ((posY + heightAdd) * heightMult)] = Color.blue;
}
}
}
}