131 lines
4.1 KiB
C#
Raw Normal View History

2024-08-17 14:27:18 +08:00
// 定义BspTree的Unity可视化扩展
//#define SHOW_BSP_TREE_GIZOMS
#if UNITY_EDITOR // && SHOW_BSP_TREE_GIZOMS
using System.Collections.Generic;
using JNGame.Math;
using UnityEngine;
namespace JNGame.PathFinding
{
// BspTreeNode的调试信息
public partial class BspTreeNode
{
public static int s_AutoIncretID = 0;
public int debugNodeId;
public BspTreeNode()
{
debugNodeId = ++s_AutoIncretID;
}
}
// BspTree的可视化绘制
public partial class BspTree
{
#region Debug Gizoms
public static Transform s_DebugTrans;
public static Material s_DebugMat;
public void DebugDraw()
{
var tran = DrawNode(m_RootNode, 0, s_DebugTrans);
tran.transform.position += Vector3.up * 0.05f;
}
private Vector3 Hash13(int id)
{
var val = (Mathf.Sin(15741.254f * id) + 1) / 2;
var val2 = (Mathf.Sin(7331.5147f * id) + 1) / 2;
var val3 = (Mathf.Sin(24317.433f * id) + 1) / 2;
return new Vector3(val, val2, val3);
}
private Transform DrawNode(BspTreeNode node, int depth, Transform parent)
{
var val = Hash13(node.debugNodeId);
Color color = new Color(val.x, val.y, val.z, 1);
if (node.IsLeaf)
{
var tran = CreateGo(node.debugNodeId.ToString(), node.tris, color).transform;
tran.SetParent(parent, true);
return tran;
}
else
{
var tran = CreateGo(node.debugNodeId.ToString(), node.splitLine, color).transform;
tran.SetParent(parent, true);
DrawNode(node.LeftChild, depth + 1, tran);
DrawNode(node.RightChild, depth + 1, tran);
return tran;
}
}
private GameObject CreateGo(string name, in SplitLine plane, in Color color)
{
return new GameObject(name);
#if false
var dir = plane.Direction.Normalized;
var or = plane.vertexA;
var s = or - dir * worldSize;
var e = or + dir * worldSize;
var perp = new LVector2(-dir.y, dir.x);
const float lineSize = 0.3f;
var s1 = perp * lineSize.ToLFloat() + s;
var e1 = perp * lineSize.ToLFloat() + e;
List<Vector3> vertices = new List<Vector3>();
List<int> tirs = new List<int>();
vertices.Add(s.ToVector3XZ());
vertices.Add(e.ToVector3XZ());
vertices.Add(e1.ToVector3XZ());
vertices.Add(s1.ToVector3XZ());
int triIdx = 0;
tirs.Add(0);
tirs.Add(1);
tirs.Add(2);
tirs.Add(0);
tirs.Add(2);
tirs.Add(3);
return CreateGo(name, vertices, tirs, color);
#endif
}
private GameObject CreateGo(string name, List<TriangleRef> allRawTriangle, in Color color)
{
List<Vector3> vertices = new List<Vector3>();
List<int> tirs = new List<int>();
int triIdx = 0;
foreach (var tri in allRawTriangle)
{
vertices.Add(tri.vertexA.ToVector3XZ());
vertices.Add(tri.vertexB.ToVector3XZ());
vertices.Add(tri.vertexC.ToVector3XZ());
tirs.Add(triIdx++);
tirs.Add(triIdx++);
tirs.Add(triIdx++);
}
return CreateGo(name, vertices, tirs, color);
}
private GameObject CreateGo(string name, List<Vector3> vertices, List<int> tirs, in Color color)
{
var go = new GameObject(name);
var render = go.AddComponent<MeshRenderer>();
var mat = new Material(s_DebugMat) { color = color };
render.material = mat;
var tempMesh = new Mesh();
tempMesh.vertices = vertices.ToArray();
tempMesh.triangles = tirs.ToArray();
go.AddComponent<MeshFilter>().mesh = tempMesh;
return go;
}
#endregion
}
}
#endif