Skip to content

DebugRender Module

Technical Docs / DebugRender Module
Editor Only Gizmos GPU Instancing URP

DEV TOOLS · PROCEDURAL RENDERING

DebugRender

메시 없는 절차적 GPU 인스턴싱 기반의 에디터 전용 디버그 시각화 시스템. Quad, Segment, Cube, Sphere, Capsule을 Graphics.RenderPrimitives로 일괄 렌더링합니다.

모든 코드는 #if UNITY_EDITOR 로 감싸져 있어 빌드에 포함되지 않습니다
PRIMITIVES 5
PIPELINE URP
INIT CAPACITY 32 instances
GROW STRATEGY 2x doubling
USER CALL
RenderXxx()

인스턴스 데이터를 NativeArray에 추가

CPU QUEUE
NativeArray<T>

Persistent 할당, 필요 시 2배 확장

PREPARE BATCHES
beginCameraRendering

바운드 계산 + GPU 버퍼 업로드
프레임당 1회 보장 (_preparedFrame)

GPU DRAW
RenderPrimitives()

GraphicsBuffer → Shader → Screen

CLEAR
endContextRendering

큐 카운트 초기화

Quad

6 verts 64 bytes Multiplicative
void RenderQuad()
void RenderQuad(float3 a, float3 b, float3 c, float3 d, Color color)
float3x4 Vertices Color Color

4개 정점으로 QuadVertexMap 인덱싱을 통해 삼각형 2개 구성. 블렌드 모드: DstColor Zero (곱셈)

Segment

6 verts 48 bytes Multiplicative
void RenderSegment(float3 a, float3 b, float width, Color color)
void RenderSegment(segment seg, float width, Color color)
float4 A, B float Width Color Color

버텍스 셰이더에서 카메라 향 쿼드로 확장. 두 점 + 두께로 선 세그먼트 표현.

Cube

36 verts 64 bytes Alpha
void RenderCube(float3 position, quaternion rotation, float3 extents, Color color)
quaternion Rotation float4 Position float4 Extents Color Color

정적 배열(CubeVertices 36개)에 회전·크기 적용. 6면 × 2삼각형 구성.

Sphere

24 verts 32 bytes Alpha
void RenderSphere(float3 position, float radius, Color color)
float4 PositionRadius xyz=pos, w=r Color Color

옥타헤드론(SphereVertices 24개)으로 구 근사. position+radius를 float4 하나에 패킹하여 최소 구조체 크기(32 bytes).

Capsule

24 verts 64 bytes Alpha
void RenderCapsule(float3 a, float3 b, float radius, Color color)
float4 A, B float Radius Color Color

OctahedronPos 함수로 두 반구 + 원통 구간을 단일 셰이더에서 생성. 물리 콜라이더 시각화에 적합.

CPU Side — NativeArray

  • Allocator.Persistent — GC 없는 장기 보유
  • 초기 용량 32개, 초과 시 ×2 더블링
  • 각 프리미티브 타입별 독립 배열
  • QueuedXxxCount / Capacity 로 진단 가능

GPU Side — GraphicsBuffer

  • Structured Buffer — HLSL에서 _DebugRenderInstances로 접근
  • 용량 부족 시 재생성 (기존 버퍼 Dispose → 새 버퍼 할당)
  • SetData()로 CPU → GPU 매 프레임 업로드
  • 어셈블리 리로드·에디터 종료 시 자동 해제
PIPELINE UniversalPipeline (URP)
QUEUE Transparent
DEPTH ZWrite Off
CULLING Cull Back
셰이더 정점 수 블렌드 생성 방식
Quad 6 Multiplicative QuadVertexMap — 4 정점 인덱스 매핑으로 삼각형 2개
Segment 6 Multiplicative 카메라 향 쿼드로 확장, 두께는 width 파라미터
Cube 36 Alpha CubeVertices 정적 배열 + 회전·크기 적용 (6면 × 6verts)
Sphere 24 Alpha SphereVertices 옥타헤드론 근사 + 스케일
Capsule 24 Alpha OctahedronPos — 반구 2개 + 원통 구간
절차적 지오메트리: 메시 없이 SV_VertexID / SV_InstanceID만으로 정점 좌표를 계산합니다.
초기화
PlayMode 진입 [RuntimeInitializeOnLoadMethod]
어셈블리 로드 (에디터) [InitializeOnLoadMethod]

NativeArray 할당 → 머터리얼 로드 → 렌더 콜백 등록

리소스 해제
어셈블리 리로드 AssemblyReloadEvents
에디터 종료 EditorApplication.quitting
GraphicsBuffer.Dispose() NativeArray.Dispose() DestroyImmediate(material) 렌더 콜백 구독 해제
테스트 커버리지
큐 카운트 증가 확인
32 → 64 더블링 확인
미초기화 InvalidOperationException
C# ↔ HLSL 구조체 레이아웃 검증

Reflection으로 내부 상태 접근, Compute Shader로 GPU 메모리 레이아웃 검증.