Skip to content

Game.Rendering.Debug.RenderPrefabRenderer

Assembly: Assembly-CSharp
Namespace: Game.Rendering.Debug

Type: class

Base: UnityEngine.MonoBehaviour

Summary:
RenderPrefabRenderer is a debug/helper MonoBehaviour used to instantiate and preview RenderPrefab assets (including LOD variants) in the scene. It builds GameObject hierarchies from RenderPrefab meshes and materials, handles virtual texturing (VT) requests, emissive/light buffers, procedural animation bone transforms, color/decal/window material property overrides, and simple per-bone animation for previewing. It contains an inner Instance class that encapsulates a single prefab instance (including LOD variants) and an AnimationState class used to animate/replicate bone transforms across instances. This component is primarily intended for debugging or previewing prefabs in-editor or in debug scenes.


Fields

  • private const bool kRefreshPrefabDataEveryFrame = true
    Controls whether prefab data should be refreshed every frame in this debug renderer. Kept as a constant flag in the original implementation.

  • public bool m_NoVT
    Public toggle to disable virtual texturing (VT) when creating preview instances. When true, VT features/keywords are disabled for materials.

  • public RenderPrefab m_Prefab
    The RenderPrefab asset to preview. This is the base prefab used to create Instance hierarchies (including LODs if present).

  • [Range(0f, 1f)] public float m_WindowsLight
    Controls a "window light" property passed into materials (varies per-instance via a MaterialPropertyBlock). Range attribute indicates editor slider.

  • [Range(-1f, 255f)] public int m_EmissiveLight = -1
    Index used to select emissive light preset for emissive materials. -1 disables or uses default combined lights behaviour.

  • [Range(-1f, 10f)] public int m_ColorIndex
    Index used for color masks (building color swapping) applied via ColorProperties on the prefab. -1 disables.

  • [Range(0f, 3f)] public int m_LODIndex
    Selected LOD index to show (0 = base prefab, higher values show LOD prefab instances if available).

  • public bool m_Animate = true
    Toggle to enable procedural bone animation in the debug preview.

  • private List<Instance> m_Hierarchies
    Internal list of Instance objects (one per LOD + base) created for the prefab preview.

  • private MaterialPropertyBlock m_MaterialPropertyBlock
    Shared MaterialPropertyBlock reused to set per-instance material properties (colors, emissive buffers, procedural buffers, etc).

  • private Dictionary<string, List<AnimationState>> m_AnimationStates = new Dictionary<string, List<AnimationState>>()
    Map of bone name -> list of AnimationState objects. Used to animate a single "master" bone and transfer transforms to duplicates across instances.

Properties

  • public IReadOnlyList<Instance> hierarchies { get; }
    Returns the internal list of Instance objects (read-only view). Each Instance corresponds to a prefab/LOD hierarchy created under this renderer.

{{ YOUR_INFO }}
Use this to inspect created hierarchies from other code (read-only). The list will be null until OnEnable creates instances; it will be populated with the base prefab instance at index 0 and any LOD instances after that.

Constructors

  • public RenderPrefabRenderer()
    Default MonoBehaviour constructor (no explicit initialization beyond field defaults). Initialization and instance creation occur in OnEnable.

{{ YOUR_INFO }}
Because this is a Unity MonoBehaviour, add it to a GameObject (or instantiate from prefab) to use it; do not rely on the constructor for initialization — use OnEnable / Awake instead.

Methods

  • public GameObject GetActiveRoot()
    Returns the active root GameObject for the currently selected LOD index (m_LODIndex). Returns null if no hierarchies exist or index out of range.

{{ YOUR_INFO }}
Useful to retrieve the GameObject parent that contains meshes for the currently visible LOD.

  • private void OnEnable()
    Unity callback. Initializes the MaterialPropertyBlock and creates Instance objects for the base prefab and any LOD prefabs (if the prefab contains LodProperties). The created instances are parented under this component's transform.

{{ YOUR_INFO }}
Instances are created with VT enabled unless m_NoVT is true. LOD instances are created but disabled by default (enabled only when m_LODIndex equals their index).

  • private void OnDisable()
    Unity callback. Disposes of all created Instance objects and releases resources (ComputeBuffers, VTTextureRequester, etc) and destroys created GameObjects.

  • private void RegisterForAnimation(string boneName, Transform target, ProceduralAnimationProperties.BoneInfo boneInfo)
    Internal helper that registers a Transform for procedural animation under a given bone name. Ensures duplicates are not registered twice.

{{ YOUR_INFO }}
This method is used by Instance objects while building bone transforms so that there is a single master animation target and clones that receive transfer of transforms.

  • private void UpdateAnimations()
    Iterates registered AnimationState groups, calls Animate() on the first state (master), then transfers the resulting transform to the remaining states in the group.

{{ YOUR_INFO }}
Called from Update when m_Animate is true. Provides simple per-frame procedural movement for previewing animated parts (wheels, propellers, steering, etc).

  • private void Update()
    Unity callback (per-frame). If m_Animate is enabled, updates animations. Then iterates all Instance hierarchies: clears the shared MaterialPropertyBlock, calls Instance.Update(), then sets window/color/decal/emissive/procedural/character material properties and toggles which Instance is enabled based on m_LODIndex.

{{ YOUR_INFO }}
This is the main per-frame loop for the debug renderer. It updates VT texture requests, animation/skeleton buffers, emissive buffers and applies MaterialPropertyBlock settings to each mesh renderer.

  • private void OnDrawGizmosSelected()
    Unity editor callback. Draws gizmos (wire cubes) around child Renderers when this component is selected in the editor. Wire colors are chosen with a deterministic "nice random" color for each renderer index.

{{ YOUR_INFO }}
Handy for visualizing bounds of the created mesh parts when debugging in the scene view.

Nested / Supporting Types (overview)

  • Instance (public nested class)
    Encapsulates a single prefab instance (base prefab or an LOD prefab). Responsibilities:
  • Constructs a parent GameObject and per-mesh child GameObjects with MeshFilter/MeshRenderer components using the prefab's meshes and materials.
  • Handles setting up/disposing VT texture requests via VTTextureRequester.
  • Handles procedural animation setup (compute buffer), emissive light buffer creation, and creation/maintenance of skin matrices.
  • Exposes public members:
    • string name { get; } — returns prefab name
    • GameObject root { get; } — root GameObject for the instance
    • bool enabled { get; set; } — controls active state (maps to root.SetActive)
    • string GetStats() — returns "Vertices/Triangles" string based on prefab counts
    • void Update() — updates VT requests (per-instance)
    • void Dispose() — releases buffers, requests and destroys GameObjects
    • several Set*Properties(ref MaterialPropertyBlock) methods:
    • SetWindowProperties(float randomWin, ref MaterialPropertyBlock block)
    • SetColorProperties(int index, ref MaterialPropertyBlock block)
    • SetDecalProperties(ref MaterialPropertyBlock block)
    • SetProceduralAnimationProperties(ref MaterialPropertyBlock block)
    • SetEmissiveProperties(int lightIndex, ref MaterialPropertyBlock block)
    • SetCharacterProperties(ref MaterialPropertyBlock block)

{{ YOUR_INFO }}
Instance handles many internal details: registering and updating VT textures, enabling/disabling shader keywords (ENABLE_VT), binding VT param blocks to materials, creating compute buffers for emissive/light info and procedural bone transforms, and creating simple bone GameObjects used to animate procedural bones in the preview.

  • ShaderIDs (public static nested class)
    Contains static readonly shader property IDs used by this renderer to set MaterialPropertyBlock entries:
  • _BuildingState, _ColorMask0/1/2, _TextureArea, _MeshSize, _DecalLayerMask, _LodFade, _LodParameters

{{ YOUR_INFO }}
Use these constants when setting the same shader properties elsewhere to avoid mismatched property names / allocation costs.

  • AnimationState (private nested class)
    Holds a Transform and a BoneType (from ProceduralAnimationProperties.BoneInfo) and implements:
  • void Animate() — applies simple per-frame rotations for specific bone types (tires, propellers, steering, etc).
  • void Transfer(AnimationState state) — copies the local transform from this state to another (used to replicate master animation to duplicates).

{{ YOUR_INFO }}
AnimationState is intentionally simple and only meant for debug/preview motions. For runtime gameplay animation, use the game's official animation systems.

Usage Example

// Example: create a debug renderer in code and assign a RenderPrefab to preview
GameObject go = new GameObject("PrefabPreview");
var preview = go.AddComponent<Game.Rendering.Debug.RenderPrefabRenderer>();
preview.m_Prefab = myRenderPrefab; // assign a valid RenderPrefab asset
preview.m_NoVT = false;            // enable virtual texturing
preview.m_WindowsLight = 0.5f;
preview.m_ColorIndex = 0;
preview.m_LODIndex = 0;
preview.m_Animate = true;

{{ YOUR_INFO }}
Typical workflow: attach this component to a temporary GameObject in the scene (or add it to an editor preview window), assign the RenderPrefab asset, and tune properties in the inspector to preview different LODs, color masks, emissive setups or procedural animations. Remember to remove the preview GameObject when done — the component will release its created resources on OnDisable / destruction.