Files
SoundTool/Assets/Scripts/Debug/FlattenVoxelTreeDebugger.cs
2025-10-19 22:56:25 +02:00

103 lines
2.9 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using UnityEngine;
public class FlattenVoxelTreeDebugger : MonoBehaviour
{
public bool enableDraw = false;
[Header("Voxel Tree Data")]
public ComputeBuffer nodeBuffer; // buffer GPU (optionnel)
public LinearNode[] nodes; // ou data CPU (si tu las)
public Vector3 rootCenter = Vector3.zero;
public float rootHalfSize = 100f;
public bool showOnlyLeaves = false;
public bool useGPUBuffer = false;
[Header("Display Settings")]
public Color nodeColor = new Color(0f, 1f, 0f, 0.05f);
public Color leafColor = new Color(1f, 0f, 0f, 0.15f);
public Color borderColor = Color.yellow;
public int maxDepthToDraw = 6;
public VoxelTreeManager voxelTreeManager;
private void OnDrawGizmos()
{
if (enableDraw == false)
return;
if (nodes == null && voxelTreeManager.gpuRayCaster != null )
{
nodes = voxelTreeManager.gpuRayCaster.linearTree.nodes;
}
if (nodes == null || nodes.Length == 0)
return;
Gizmos.matrix = Matrix4x4.identity;
DrawNodeRecursive(0, rootCenter, rootHalfSize, 0);
}
private void DrawNodeRecursive(int nodeIndex, Vector3 center, float halfSize, int depth)
{
if (nodeIndex < 0 || nodeIndex >= nodes.Length) return;
if (depth > maxDepthToDraw) return;
var node = nodes[nodeIndex];
bool isLeaf = node.isLeaf == 1;
bool isOccupied = node.isOccupied == 1;
// Color based on node type
if (isOccupied)
Gizmos.color = leafColor;
else
Gizmos.color = nodeColor;
if (!showOnlyLeaves || isLeaf)
{
Gizmos.DrawCube(center, Vector3.one * (halfSize * 2f));
Gizmos.color = borderColor;
Gizmos.DrawWireCube(center, Vector3.one * (halfSize * 2f));
}
if (node.childMask != 0)
{
float childHalf = halfSize * 0.5f;
int realIndex = 0;
for (int i = 0; i < 8; i++)
{
bool childExists = (node.childMask & (1u << i)) != 0 ;
if (!childExists) continue;
Vector3 offset = childHalf * new Vector3(
(i & 4) != 0 ? 1f : -1f, // X
(i & 2) != 0 ? 1f : -1f, // Y
(i & 1) != 0 ? 1f : -1f // Z
);
Vector3 childCenter = center + offset;
int childIndex = node.childBase + realIndex;
DrawNodeRecursive(childIndex, childCenter, childHalf, depth + 1);
realIndex++;
}
}
}
// Optionnel : utilitaire pour extraire depuis un ComputeBuffer (GPU -> CPU)
public void SyncFromGPU()
{
if (nodeBuffer == null || !useGPUBuffer) return;
if (nodes == null || nodes.Length == 0)
nodes = new LinearNode[nodeBuffer.count];
nodeBuffer.GetData(nodes);
}
}