Local project added
This commit is contained in:
102
Assets/Scripts/Debug/FlattenVoxelTreeDebugger.cs
Normal file
102
Assets/Scripts/Debug/FlattenVoxelTreeDebugger.cs
Normal file
@ -0,0 +1,102 @@
|
||||
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 l’as)
|
||||
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);
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/Debug/FlattenVoxelTreeDebugger.cs.meta
Normal file
2
Assets/Scripts/Debug/FlattenVoxelTreeDebugger.cs.meta
Normal file
@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7af577c689fa40b4a9291ff83dbde469
|
||||
154
Assets/Scripts/Debug/Player.cs
Normal file
154
Assets/Scripts/Debug/Player.cs
Normal file
@ -0,0 +1,154 @@
|
||||
using UnityEngine;
|
||||
using System.Diagnostics;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Collections.Generic;
|
||||
|
||||
|
||||
public class Player : MonoBehaviour
|
||||
{
|
||||
public VoxelTreeManager voxelManager;
|
||||
|
||||
public bool EnableDebug = false;
|
||||
|
||||
int totalCastDone = 0;
|
||||
|
||||
public float longitudeStep = 45f;
|
||||
public float lateralStep = 45f;
|
||||
|
||||
int rayCount;
|
||||
|
||||
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
||||
void Start()
|
||||
{
|
||||
InitPrebuildArray();
|
||||
|
||||
VoxelRaycastGPU.Ray[] rays = new VoxelRaycastGPU.Ray[rayCount];
|
||||
FillRaysArray(rays);
|
||||
voxelManager.gpuRayCaster.Init(rayCount, rays);
|
||||
}
|
||||
|
||||
void Cast( ref VoxelRaycastGPU.BatchData[] batchData, int batchCount, int iIteration )
|
||||
{
|
||||
ComputeBuffer hitBuffer = new ComputeBuffer(rayCount * batchCount, Marshal.SizeOf(typeof(VoxelRaycastGPU.Hit)), ComputeBufferType.Append);
|
||||
|
||||
Stopwatch sw = Stopwatch.StartNew();
|
||||
totalCastDone += batchCount * rayCount;
|
||||
VoxelRaycastGPU.BatchData[] hits = voxelManager.CastGpuRay(in batchData, batchCount);
|
||||
|
||||
/*for( int i = 0; i < hits.Length; i++ )
|
||||
{
|
||||
GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
|
||||
sphere.transform.position = hits[i].origin;
|
||||
sphere.transform.localScale = Vector3.one * 0.5f;
|
||||
}*/
|
||||
|
||||
sw.Stop();
|
||||
}
|
||||
|
||||
Vector3[] prebuildArrayDirection;
|
||||
|
||||
void InitPrebuildArray()
|
||||
{
|
||||
var dirs = new List<Vector3>();
|
||||
|
||||
const float EPS = 1e-6f;
|
||||
|
||||
// Elevation (longitude in your naming): from -90 to +90 inclusive
|
||||
for (float lonDeg = -90f; lonDeg <= 90f + 1e-6f; lonDeg += longitudeStep)
|
||||
{
|
||||
float lonRad = lonDeg * Mathf.Deg2Rad;
|
||||
float cosLon = Mathf.Cos(lonRad);
|
||||
float sinLon = Mathf.Sin(lonRad);
|
||||
|
||||
// If cosLon is ~0, we are at a pole: all azimuths collapse to the same vector.
|
||||
if (Mathf.Abs(cosLon) < EPS)
|
||||
{
|
||||
// Add a single pole direction (north or south)
|
||||
dirs.Add(new Vector3(0f, sinLon, 0f));
|
||||
continue; // skip azimuth loop for this elevation
|
||||
}
|
||||
|
||||
// Otherwise loop over azimuth (lateral)
|
||||
for (float latDeg = 0f; latDeg < 360f - 1e-6f; latDeg += lateralStep)
|
||||
{
|
||||
float latRad = latDeg * Mathf.Deg2Rad;
|
||||
float cosLat = Mathf.Cos(latRad);
|
||||
float sinLat = Mathf.Sin(latRad);
|
||||
|
||||
Vector3 dir = new Vector3(
|
||||
cosLon * cosLat,
|
||||
sinLon,
|
||||
cosLon * sinLat
|
||||
);
|
||||
|
||||
dirs.Add(dir.normalized);
|
||||
}
|
||||
}
|
||||
|
||||
prebuildArrayDirection = dirs.ToArray();
|
||||
rayCount = dirs.Count;
|
||||
}
|
||||
|
||||
void FillRaysArray( VoxelRaycastGPU.Ray[] rays )
|
||||
{
|
||||
for (int i = 0; i < prebuildArrayDirection.Length; i++)
|
||||
{
|
||||
rays[i].direction = prebuildArrayDirection[i];
|
||||
}
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
if (Input.GetKeyDown(KeyCode.R))
|
||||
{
|
||||
//VoxelTreeRaycaster.HitInfo outHit;
|
||||
|
||||
Vector3 origin = GetComponent<Transform>().position;
|
||||
|
||||
UnityEngine.Debug.Log(origin);
|
||||
|
||||
//int iNbCast = 0;
|
||||
|
||||
/*Stopwatch sw = Stopwatch.StartNew();
|
||||
|
||||
for (float el = -90f; el <= 90f; el += elStep)
|
||||
{
|
||||
for (float az = 0f; az < 360f; az += azStep)
|
||||
{
|
||||
float elRad = el * Mathf.Deg2Rad;
|
||||
float azRad = az * Mathf.Deg2Rad;
|
||||
|
||||
Vector3 dir = new Vector3(
|
||||
Mathf.Cos(elRad) * Mathf.Cos(azRad),
|
||||
Mathf.Sin(elRad),
|
||||
Mathf.Cos(elRad) * Mathf.Sin(azRad)
|
||||
).normalized;
|
||||
|
||||
iNbCast++;
|
||||
bool hit = voxelManager.CastRay(origin, dir, 100f, out outHit);
|
||||
}
|
||||
}
|
||||
|
||||
sw.Stop();
|
||||
|
||||
UnityEngine.Debug.Log($"RayCast done in {sw.Elapsed.TotalMilliseconds}ms for {iNbCast} casts");*/
|
||||
|
||||
Stopwatch sw = Stopwatch.StartNew();
|
||||
|
||||
totalCastDone = 0;
|
||||
|
||||
VoxelRaycastGPU.Ray[] rays = new VoxelRaycastGPU.Ray[rayCount];
|
||||
VoxelRaycastGPU.BatchData[] batchDatas = new VoxelRaycastGPU.BatchData[1];
|
||||
|
||||
batchDatas[0].origin = origin;
|
||||
batchDatas[0].maxDistance = 100;
|
||||
|
||||
Cast(ref batchDatas, 1, 0);
|
||||
sw.Stop();
|
||||
|
||||
UnityEngine.Debug.Log($"Gpu RayCast done in {sw.Elapsed.TotalMilliseconds}ms for {totalCastDone} casts");
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/Debug/Player.cs.meta
Normal file
2
Assets/Scripts/Debug/Player.cs.meta
Normal file
@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 98b63d0a77a360e4d94b27528bc3000d
|
||||
52
Assets/Scripts/Debug/VoxelTreeDebugger.cs
Normal file
52
Assets/Scripts/Debug/VoxelTreeDebugger.cs
Normal file
@ -0,0 +1,52 @@
|
||||
using UnityEngine;
|
||||
|
||||
[ExecuteAlways]
|
||||
public class VoxelTreeDebugger : MonoBehaviour
|
||||
{
|
||||
public bool enableDraw = false;
|
||||
|
||||
[Header("Tree reference")]
|
||||
public OctreeNode root; // Reference to your voxel tree root
|
||||
|
||||
[Header("Debug settings")]
|
||||
public bool drawLeavesOnly = true; // Draw only leaf nodes
|
||||
public int maxDepthToDraw = 6; // Limit drawing depth
|
||||
public Color emptyColor = new Color(0f, 0.5f, 1f, 0.2f);
|
||||
public Color solidColor = new Color(1f, 0f, 0f, 0.5f);
|
||||
public Color mixedColor = new Color(1f, 1f, 0f, 0.4f);
|
||||
|
||||
void OnDrawGizmos()
|
||||
{
|
||||
if (root != null && enableDraw)
|
||||
{
|
||||
DrawNode(root, 0);
|
||||
}
|
||||
}
|
||||
|
||||
private void DrawNode(OctreeNode node, int depth)
|
||||
{
|
||||
if (node == null || depth > maxDepthToDraw)
|
||||
return;
|
||||
|
||||
// Set color based on node type
|
||||
if (node.isLeaf)
|
||||
{
|
||||
Gizmos.color = node.isOccupied ? solidColor : emptyColor;
|
||||
Gizmos.DrawWireCube(node.bounds.center, node.bounds.size);
|
||||
if (node.isOccupied)
|
||||
Gizmos.DrawCube(node.bounds.center, node.bounds.size * 0.95f);
|
||||
}
|
||||
else
|
||||
{
|
||||
Gizmos.color = mixedColor;
|
||||
if (!drawLeavesOnly)
|
||||
Gizmos.DrawWireCube(node.bounds.center, node.bounds.size);
|
||||
|
||||
// Recursively draw children
|
||||
foreach (var child in node.children)
|
||||
{
|
||||
DrawNode(child, depth + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/Debug/VoxelTreeDebugger.cs.meta
Normal file
2
Assets/Scripts/Debug/VoxelTreeDebugger.cs.meta
Normal file
@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 31dcad4d65772e349b756df7096c55d0
|
||||
Reference in New Issue
Block a user