Game.Rendering.Debug.DecalLayerSetter
Assembly:
Assembly-CSharp (Assembly-CSharp.dll)
Namespace:
Game.Rendering.Debug
Type:
public class DecalLayerSetter : MonoBehaviour
Base:
UnityEngine.MonoBehaviour
Summary:
DecalLayerSetter is a small MonoBehaviour used to drive a shader property that encodes which decal layers a mesh belongs to. It caches the MeshRenderer on Awake and, each frame in Update, writes a float value to the material property identified by RenderPrefabRenderer.ShaderIDs._DecalLayerMask. The float is produced by converting the DecalLayers enum value to an int and then to a float (math.asfloat((int)m_LayerMask)), which matches the shader's expected representation of a bitmask. The component exposes m_LayerMask so it can be set in the inspector or via script. Use with care: this uses sharedMaterial (modifies the shared material asset) and updates every frame.
Fields
-
private Renderer m_MeshRenderer
Caches the MeshRenderer component on the same GameObject. Populated in Awake via GetComponent(). May be null if no MeshRenderer is present — consider adding [RequireComponent(typeof(MeshRenderer))] to avoid that. -
public DecalLayers m_LayerMask = DecalLayers.Terrain
Public enum field (likely a flags enum) that selects which decal layers apply to this mesh. Defaults to DecalLayers.Terrain. The value is converted to an int and then to a float for the shader.
Properties
- None
This class does not expose any C# properties.
Constructors
public DecalLayerSetter()
No explicit constructor is defined in the source; the default MonoBehaviour constructor is used. Initialization is performed in Awake.
Methods
-
private void Awake()
Caches the MeshRenderer component: m_MeshRenderer = GetComponent(); If the component is missing, subsequent Update calls will throw a NullReferenceException. Consider requiring the component or adding null checks. -
public void Update()
Every frame, writes the decal layer mask to the material: m_MeshRenderer.sharedMaterial.SetFloat(RenderPrefabRenderer.ShaderIDs._DecalLayerMask, math.asfloat((int)m_LayerMask)); Notes: - Uses sharedMaterial, which modifies the shared material asset used by all instances. If you want per-instance material changes, use material instead (but that creates instances).
- Continually setting the same value every frame can be wasteful; consider updating only when m_LayerMask changes.
- math.asfloat((int)m_LayerMask) packs the integer bitmask into a float for the shader to read.
Usage Example
// Simple usage: attach DecalLayerSetter to a GameObject with a MeshRenderer.
// Set the layer mask in the inspector or from code.
[RequireComponent(typeof(MeshRenderer))]
public class DecalLayerExample : MonoBehaviour
{
void Start()
{
var setter = gameObject.AddComponent<Game.Rendering.Debug.DecalLayerSetter>();
// Example: assume DecalLayers is a flags enum; combine layers as needed.
setter.m_LayerMask = DecalLayers.Terrain; // or DecalLayers.Terrain | DecalLayers.Roads
}
}
Additional notes: - The shader property ID used (RenderPrefabRenderer.ShaderIDs._DecalLayerMask) must exist and be expected to receive the packed float bitmask. If you control the shader, ensure it decodes the bitmask appropriately. - For safety and clarity in Unity editor usage, consider adding [RequireComponent(typeof(MeshRenderer))] to DecalLayerSetter and/or performing a null-check on m_MeshRenderer before using it.