11namespace Hexa . NET . DebugDraw
22{
3- using Hexa . NET . Mathematics ;
43 using System ;
54 using System . Numerics ;
65 using System . Runtime . CompilerServices ;
@@ -17,6 +16,9 @@ public static unsafe class DebugDraw
1716 private const int COL32_B_SHIFT = 16 ;
1817 private const int COL32_A_SHIFT = 24 ;
1918 private const uint COL32_A_MASK = 0xFF000000 ;
19+ private const float PI = MathF . PI ;
20+ private const float PI2 = MathF . PI * 2.0f ;
21+ private const float PIDIV2 = MathF . PI / 2.0f ;
2022
2123 public static DebugDrawContext CreateContext ( )
2224 {
@@ -216,14 +218,14 @@ private static void DrawPreComputed(DebugDrawPrimitiveTopology topology, Vector3
216218 /// Sets the viewport for rendering debug shapes.
217219 /// </summary>
218220 /// <param name="viewport">The viewport to set.</param>
219- public static void SetViewport ( Viewport viewport )
221+ public static void SetViewport ( Vector2 offset , Vector2 size )
220222 {
221223 if ( currentContext == null )
222224 {
223225 throw new InvalidOperationException ( "DebugDraw context is not set. Call DebugDraw.SetContext() before drawing." ) ;
224226 }
225227
226- currentContext . SetViewport ( viewport ) ;
228+ currentContext . SetViewport ( new DebugDrawViewport ( offset , size ) ) ;
227229 }
228230
229231 /// <summary>
@@ -272,16 +274,63 @@ public static void ExecuteCommandList(DebugDrawCommandList commandList)
272274 /// <summary>
273275 /// Draws a bounding frustum in the specified color.
274276 /// </summary>
275- /// <param name="frustum">The bounding frustum to be drawn.</param>
277+ /// <param name="frustum">The bounding frustum to be drawn. Expects 9 corners </param>
276278 /// <param name="col">The color of the frustum.</param>
277279 ///
278- public static void DrawFrustum ( BoundingFrustum frustum , Vector4 col )
280+ public static void DrawFrustum ( Vector3 * frustumCorners , int cornerCount , Vector4 col )
279281 {
282+ if ( cornerCount != 9 )
283+ {
284+ throw new ArgumentException ( "Frustum must have 9 corners" , nameof ( frustumCorners ) ) ;
285+ }
286+ CurrentList . BeginDraw ( ) ;
287+
288+ uint color = ColorConvertFloat4ToU32 ( col ) ;
289+
290+ CurrentList . ReserveGeometry ( 9 , 24 ) ;
291+ var indices = CurrentList . Indices + CurrentList . IndexCount ;
292+ var vertices = CurrentList . Vertices + CurrentList . VertexCount ;
293+
294+ indices [ 0 ] = 0 ; indices [ 1 ] = 1 ;
295+ indices [ 2 ] = 1 ; indices [ 3 ] = 2 ;
296+ indices [ 4 ] = 2 ; indices [ 5 ] = 3 ;
297+ indices [ 6 ] = 3 ; indices [ 7 ] = 0 ;
298+ indices [ 8 ] = 0 ; indices [ 9 ] = 4 ;
299+ indices [ 10 ] = 1 ; indices [ 11 ] = 5 ;
300+ indices [ 12 ] = 2 ; indices [ 13 ] = 6 ;
301+ indices [ 14 ] = 3 ; indices [ 15 ] = 7 ;
302+ indices [ 16 ] = 4 ; indices [ 17 ] = 5 ;
303+ indices [ 18 ] = 5 ; indices [ 19 ] = 6 ;
304+ indices [ 20 ] = 6 ; indices [ 21 ] = 7 ;
305+ indices [ 22 ] = 7 ; indices [ 23 ] = 4 ;
306+
307+ for ( int i = 0 ; i < 9 ; i ++ )
308+ {
309+ vertices [ i ] . Color = color ;
310+ vertices [ i ] . Position = frustumCorners [ i ] ;
311+ vertices [ i ] . UV = WhiteUV ;
312+ }
313+
314+ CurrentList . RecordCmd ( DebugDrawPrimitiveTopology . LineList ) ;
315+ }
316+
317+ /// <summary>
318+ /// Draws a bounding frustum in the specified color.
319+ /// </summary>
320+ /// <param name="frustum">The bounding frustum to be drawn. Expects 9 corners</param>
321+ /// <param name="col">The color of the frustum.</param>
322+ ///
323+ public static void DrawFrustum ( Span < Vector3 > frustumCorners , Vector4 col )
324+ {
325+ if ( frustumCorners . Length < 9 )
326+ {
327+ throw new ArgumentException ( "Frustum must have 9 corners" , nameof ( frustumCorners ) ) ;
328+ }
280329 CurrentList . BeginDraw ( ) ;
281330
282331 uint color = ColorConvertFloat4ToU32 ( col ) ;
283332
284- CurrentList . ReserveGeometry ( BoundingFrustum . CornerCount , 24 ) ;
333+ CurrentList . ReserveGeometry ( 9 , 24 ) ;
285334 var indices = CurrentList . Indices + CurrentList . IndexCount ;
286335 var vertices = CurrentList . Vertices + CurrentList . VertexCount ;
287336
@@ -298,11 +347,10 @@ public static void DrawFrustum(BoundingFrustum frustum, Vector4 col)
298347 indices [ 20 ] = 6 ; indices [ 21 ] = 7 ;
299348 indices [ 22 ] = 7 ; indices [ 23 ] = 4 ;
300349
301- var corners = frustum . Corners ;
302- for ( int i = 0 ; i < BoundingFrustum . CornerCount ; i ++ )
350+ for ( int i = 0 ; i < 9 ; i ++ )
303351 {
304352 vertices [ i ] . Color = color ;
305- vertices [ i ] . Position = corners [ i ] ;
353+ vertices [ i ] . Position = frustumCorners [ i ] ;
306354 vertices [ i ] . UV = WhiteUV ;
307355 }
308356
@@ -315,7 +363,7 @@ public static void DrawFrustum(BoundingFrustum frustum, Vector4 col)
315363 /// <param name="box">The bounding box to be drawn.</param>
316364 /// <param name="col">The color of the box.</param>
317365 ///
318- public static void DrawBoundingBox ( BoundingBox box , Vector4 col )
366+ public static void DrawBoundingBox ( Vector3 min , Vector3 max , Vector4 col )
319367 {
320368 CurrentList . BeginDraw ( ) ;
321369
@@ -339,14 +387,14 @@ public static void DrawBoundingBox(BoundingBox box, Vector4 col)
339387 indices [ 20 ] = 6 ; indices [ 21 ] = 7 ;
340388 indices [ 22 ] = 7 ; indices [ 23 ] = 4 ;
341389
342- vertices [ 0 ] . Position = new Vector3 ( box . Min . X , box . Max . Y , box . Min . Z ) ;
343- vertices [ 1 ] . Position = new Vector3 ( box . Min . X , box . Min . Y , box . Min . Z ) ;
344- vertices [ 2 ] . Position = new Vector3 ( box . Max . X , box . Min . Y , box . Min . Z ) ;
345- vertices [ 3 ] . Position = new Vector3 ( box . Max . X , box . Max . Y , box . Min . Z ) ;
346- vertices [ 4 ] . Position = new Vector3 ( box . Min . X , box . Max . Y , box . Max . Z ) ;
347- vertices [ 5 ] . Position = new Vector3 ( box . Min . X , box . Min . Y , box . Max . Z ) ;
348- vertices [ 6 ] . Position = new Vector3 ( box . Max . X , box . Min . Y , box . Max . Z ) ;
349- vertices [ 7 ] . Position = new Vector3 ( box . Max . X , box . Max . Y , box . Max . Z ) ;
390+ vertices [ 0 ] . Position = new Vector3 ( max . X , max . Y , min . Z ) ;
391+ vertices [ 1 ] . Position = new Vector3 ( max . X , max . Y , min . Z ) ;
392+ vertices [ 2 ] . Position = new Vector3 ( max . X , max . Y , max . Z ) ;
393+ vertices [ 3 ] . Position = new Vector3 ( max . X , max . Y , max . Z ) ;
394+ vertices [ 4 ] . Position = new Vector3 ( max . X , max . Y , max . Z ) ;
395+ vertices [ 5 ] . Position = new Vector3 ( max . X , max . Y , max . Z ) ;
396+ vertices [ 6 ] . Position = new Vector3 ( max . X , max . Y , max . Z ) ;
397+ vertices [ 7 ] . Position = new Vector3 ( max . X , max . Y , max . Z ) ;
350398
351399 for ( int i = 0 ; i < vertexCount ; i ++ )
352400 {
@@ -561,9 +609,9 @@ public static void DrawBoundingBox(BoundingBox box, Vector4 col)
561609 /// <param name="sphere">The bounding sphere to be drawn.</param>
562610 /// <param name="col">The color of the sphere.</param>
563611 ///
564- public static void DrawBoundingSphere ( BoundingSphere sphere , Vector4 col )
612+ public static void DrawBoundingSphere ( Vector3 center , float radius , Vector4 col )
565613 {
566- DrawPreComputed ( DebugDrawPrimitiveTopology . LineList , spherePositions , sphereIndices , Matrix4x4 . CreateScale ( sphere . Radius ) * Matrix4x4 . CreateTranslation ( sphere . Center ) , col ) ;
614+ DrawPreComputed ( DebugDrawPrimitiveTopology . LineList , spherePositions , sphereIndices , Matrix4x4 . CreateScale ( radius ) * Matrix4x4 . CreateTranslation ( center ) , col ) ;
567615 }
568616
569617 /// <summary>
@@ -710,7 +758,7 @@ public static void DrawRing(Vector3 origin, Quaternion orientation, Vector3 majo
710758
711759 indices [ ( c_ringSegments - 1 ) * 2 + 1 ] = 0 ;
712760
713- float fAngleDelta = MathUtil . PI2 / c_ringSegments ;
761+ float fAngleDelta = PI2 / c_ringSegments ;
714762
715763 // Instead of calling cos/sin for each segment we calculate
716764 // the sign of the angle delta and then incrementally calculate sin
@@ -763,7 +811,7 @@ public static void DrawRing(Vector3 origin, Vector3 majorAxis, Vector3 minorAxis
763811
764812 indices [ ( c_ringSegments - 1 ) * 2 + 1 ] = 0 ;
765813
766- float fAngleDelta = MathUtil . PI2 / c_ringSegments ;
814+ float fAngleDelta = PI2 / c_ringSegments ;
767815
768816 // Instead of calling cos/sin for each segment we calculate
769817 // the sign of the angle delta and then incrementally calculate sin
@@ -818,7 +866,7 @@ public static void DrawRing(Vector3 origin, (Vector3 majorAxis, Vector3 minorAxi
818866
819867 Vector3 majorAxis = ellipse . majorAxis ;
820868 Vector3 minorAxis = ellipse . minorAxis ;
821- float fAngleDelta = MathUtil . PI2 / c_ringSegments ;
869+ float fAngleDelta = PI2 / c_ringSegments ;
822870
823871 // Instead of calling cos/sin for each segment we calculate
824872 // the sign of the angle delta and then incrementally calculate sin
@@ -876,7 +924,7 @@ public static void DrawRingBillboard(Vector3 origin, Vector3 camPos, Vector3 cam
876924
877925 Vector3 majorAxis = ellipse . majorAxis ;
878926 Vector3 minorAxis = ellipse . minorAxis ;
879- float fAngleDelta = MathUtil . PI2 / c_ringSegments ;
927+ float fAngleDelta = PI2 / c_ringSegments ;
880928
881929 var mat = MathUtil . BillboardLH ( origin , camPos , camUp , camForward ) ;
882930
0 commit comments