Game.Rendering.Debug.CharacterGroupRenderer
Assembly: Assembly-CSharp
Namespace: Game.Rendering.Debug
Type: class
Base: UnityEngine.MonoBehaviour
Summary:
A debug helper MonoBehaviour that instantiates and renders a single character from a CharacterGroup prefab for previewing/testing. It creates child GameObjects with RenderPrefabRenderer components, allocates GPU ComputeBuffers for bone and meta data, and provides a method to bind those buffers to a MaterialPropertyBlock for rendering shaders.
Fields
-
public CharacterGroup m_Prefab
The CharacterGroup asset to preview. The component will pick a character from this group using m_CharacterIndex. If null, no renderer is created. -
public bool m_NoVT
Flag forwarded to each created RenderPrefabRenderer indicating whether virtual texturing (VT) should be disabled for the preview meshes. -
public int m_CharacterIndex
Index into m_Prefab.m_Characters to select which character to instantiate. Clamped to valid range when CreateRenderer runs. -
[Range(0f, 16f)] public int m_OverlayColorIndex
Index used to select a color variant from Character overlay color palettes when computing blended overlay colors. -
private GameObject m_Root
Root GameObject parent for instantiated per-mesh preview objects. Created during CreateRenderer and destroyed by ReleaseRenderer. -
private ComputeBuffer m_BoneBuffer
GPU structured buffer that stores current bone transforms (BoneElement[]) for the character. Allocated in SetupBuffers and released in ReleaseRenderer. -
private ComputeBuffer m_BoneHistoryBuffer
GPU structured buffer that stores previous-frame bone transforms for motion/blend effects. Allocated and released alongside m_BoneBuffer. -
private ComputeBuffer m_MetaBuffer
GPU structured buffer that stores meta information (MetaBufferData) such as bone count, shape/texture/overlay weights and overlay colors used by shaders.
Properties
- None.
Constructors
public CharacterGroupRenderer()
Default MonoBehaviour constructor. Initialization happens in OnEnable/CreateRenderer rather than the constructor.
Methods
-
private void OnEnable()
Called by Unity when the component is enabled. Calls CreateRenderer to set up child GameObjects and GPU buffers. -
public void Recreate()
Convenience method to fully rebuild the preview: releases current renderer and buffers and then recreates them. Use after changing the assigned prefab or character index at runtime. -
private void CreateRenderer()
Builds the preview hierarchy and allocates buffers if m_Prefab is set. It: - Clamps m_CharacterIndex to the available characters.
- Calls SetupBuffers to allocate and initialize ComputeBuffers.
-
Creates m_Root and for each RenderPrefab in the selected character creates a child GameObject with a RenderPrefabRenderer, sets its m_NoVT and m_Prefab, and activates it.
-
private Color GetBlendColor(RenderPrefab[] renderPrefabs, BlendWeight weight)
Iterates over provided RenderPrefabs, extracts CharacterProperties, and finds overlays that match the provided BlendWeight index. It accumulates and blends ColorProperties according to the blend weight and returns the final resulting Color (linear space). Returns white if no overlay was found. -
private BlendColors GetBlendColors(RenderPrefab[] renderPrefabs, BlendWeights weights)
Builds a BlendColors struct by calling GetBlendColor for each weight slot (m_Weight0..m_Weight7). Used to populate overlay color data in the meta buffer. -
private unsafe void SetupBuffers(CharacterGroup.Character character)
Allocates and initializes m_BoneBuffer, m_BoneHistoryBuffer and m_MetaBuffer based on the character style (bone and shape counts). Initializes bone arrays to identity matrices and sets a MetaBufferData entry containing bone/shape/texture/mask/overlay weights and computed overlay colors. Uses RenderingUtils to convert weight data. Marked unsafe because it sets up native GPU buffers and uses sizeof on unmanaged types. -
public void SetCharacterProperties(ref MaterialPropertyBlock block)
If buffers are allocated, binds the ComputeBuffers to the provided MaterialPropertyBlock using the shader buffer names "boneBuffer", "boneHistoryBuffer" and "metaBuffer". Call this before drawing or passing the block to a renderer so shaders can access the buffer data. -
private void ReleaseRenderer()
Destroys m_Root GameObject and releases all allocated ComputeBuffers. Used to clean up resources when the component is disabled, destroyed, or when Recreate is invoked. Important to avoid GPU memory leaks. -
private void OnDisable()
Called by Unity when the component is disabled. Calls ReleaseRenderer to free child GameObjects and GPU buffers.
Usage Example
// Create a GameObject and add the debug preview component:
var go = new GameObject("CharacterDebugPreview");
var preview = go.AddComponent<Game.Rendering.Debug.CharacterGroupRenderer>();
// Assign the CharacterGroup asset (set in inspector or via code):
preview.m_Prefab = myCharacterGroupPrefab; // CharacterGroup reference
preview.m_CharacterIndex = 0;
preview.m_OverlayColorIndex = 2;
preview.m_NoVT = true;
// When enabled, the component will create child GameObjects and allocate GPU buffers.
// To bind the buffers to a material before drawing manually:
var mpb = new MaterialPropertyBlock();
preview.SetCharacterProperties(ref mpb);
// Use mpb when calling Graphics.DrawMesh or set it on a Renderer with renderer.SetPropertyBlock(mpb);
// If you change the prefab or character index at runtime:
preview.Recreate(); // safely rebuilds the preview and buffers
Additional notes: - This component is intended for debugging/preview in editor/runtime; it creates GPU resources (ComputeBuffers) — ensure ReleaseRenderer runs (via OnDisable) to avoid leaks. - It relies on several game-specific types (CharacterGroup, RenderPrefab, RenderingUtils, BlendWeights/BlendColors, BoneElement, MetaBufferData) and shader buffer names ("boneBuffer", "boneHistoryBuffer", "metaBuffer") used by the game's rendering shaders. - m_NoVT likely disables virtual texturing for the preview mesh renderers; pass false to use normal material behavior.