1
This commit is contained in:
86
Src/Unity/Scripts/Quadtree/MonoQuadtreeDrawer.cs
Normal file
86
Src/Unity/Scripts/Quadtree/MonoQuadtreeDrawer.cs
Normal file
@@ -0,0 +1,86 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using BITKit;
|
||||
using Net.BITKit.Quadtree;
|
||||
using Unity.Mathematics;
|
||||
using UnityEngine;
|
||||
using Random = UnityEngine.Random;
|
||||
|
||||
namespace Net.Project.B.Quadtree
|
||||
{
|
||||
public class MonoQuadtreeDrawer : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private int size;
|
||||
private BITKit.Quadtree.Quadtree _quadtree;
|
||||
|
||||
[BIT]
|
||||
private void Init()
|
||||
{
|
||||
_quadtree = new BITKit.Quadtree.Quadtree(default, new float2(512, 512));
|
||||
|
||||
for (var i = 0; i < 512*8; i++)
|
||||
{
|
||||
_quadtree.Insert(i,Random.insideUnitCircle * 512,Random.value>0.5f ? Random.insideUnitCircle * 8 :default);
|
||||
}
|
||||
|
||||
_quadtree.Query(default, default);
|
||||
}
|
||||
private void OnDrawGizmos()
|
||||
{
|
||||
if(_quadtree is null)return;
|
||||
|
||||
var repeatedObject = new HashSet<int>();
|
||||
Draw(_quadtree.Root);
|
||||
|
||||
{
|
||||
Gizmos.color = Color.red;
|
||||
var worldPos = transform.position;
|
||||
Gizmos.DrawWireSphere(worldPos, size);
|
||||
foreach (var id in _quadtree.Query(new float2(worldPos.x,worldPos.z),size))
|
||||
{
|
||||
var pos = _quadtree.Positions[id];
|
||||
Gizmos.DrawSphere(new Vector3(pos.x,0,pos.y),1);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return;
|
||||
|
||||
void Draw(QuadtreeNode quadtreeNode)
|
||||
{
|
||||
Gizmos.color = Color.white;
|
||||
|
||||
if(quadtreeNode.Size.x is 0)return;
|
||||
|
||||
var nodeCenter = quadtreeNode.Center;
|
||||
var nodeSize = quadtreeNode.Size;
|
||||
|
||||
foreach (var (id,rectangle) in _quadtree.Sizes)
|
||||
{
|
||||
var pos = _quadtree.Positions[id];
|
||||
Gizmos.DrawWireCube(new Vector3(pos.x,0,pos.y),new Vector3(rectangle.x,0,rectangle.y));
|
||||
}
|
||||
|
||||
foreach (var id in quadtreeNode.Objects)
|
||||
{
|
||||
if (repeatedObject.Add(id) is false)
|
||||
{
|
||||
Debug.Log("重复对象");
|
||||
}
|
||||
var pos = _quadtree.Positions[id];
|
||||
var worldPos = new Vector3(pos.x, 0, pos.y);
|
||||
Gizmos.DrawSphere(worldPos,1);
|
||||
}
|
||||
|
||||
Gizmos.DrawWireCube(new Vector3(nodeCenter.x,0,nodeCenter.y),new Vector3(nodeSize.x,0,nodeSize.y));
|
||||
|
||||
foreach (var child in quadtreeNode.Children)
|
||||
{
|
||||
Draw(child);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user