Version: Unity 6.3 Beta (6000.3)
LanguageEnglish
  • C#

SkinnedMeshRenderer.SetShaderUserValue

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Switch to Manual

Declaration

public void SetShaderUserValue(uint v);

Parameters

Parameter Description
v The integer to assign to the renderer as a custom value to be used in shaders.

Description

Assign a custom value to this renderer.

The SetShaderUserValue method assigns a custom integer value to a renderer. You can then access the value in your shaders as the unity_RendererUserValue variable. You can use this method to change how a shader draws each renderer, without using different materials or additional CPU time.

Note: The value of the unity_RendererUserValue shader variable will always be 0 in the following situations:

  • The shader is written for the Built-In Render Pipeline.
  • When baking lightmaps or light probes.

Note: The value is not serialized, so it is not saved to the asset and resets when the object is reloaded.

Additional resources: SkinnedMeshRenderer.GetShaderUserValue.

The following code sample creates a MonoBehaviour to assign a specific color value on a renderer by encoding it in a uint.

using UnityEngine;
using UnityEngine.Rendering;

[ExecuteAlways] public class PerRendererColor : MonoBehaviour { public Color color = Color.white;

private SkinnedMeshRenderer m_Renderer;

void Start() { m_Renderer = GetComponent<SkinnedMeshRenderer>(); }

void Update() { if (m_Renderer) { Color32 colorb = color; uint packedColor = (uint)colorb.r | ((uint)colorb.g << 8) | ((uint)colorb.b << 16); m_Renderer.SetShaderUserValue(packedColor); } } }

The corresponding shader code unpacks the unsigned integer into an RGB float triplet in the fragment shader.

// This shader fills the mesh shape with the color packed in the renderer's user value.
Shader "Example/URPUnlitUserValue"
{
    Properties
    { }

SubShader { Tags { "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline" }

Pass { HLSLPROGRAM #pragma vertex vert #pragma fragment frag

#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"

struct Attributes { float4 positionOS : POSITION; };

struct Varyings { float4 positionHCS : SV_POSITION; };

Varyings vert(Attributes IN) { Varyings OUT; OUT.positionHCS = TransformObjectToHClip(IN.positionOS.xyz); return OUT; }

half4 frag() : SV_Target { // Decoding the user value color and returning it. uint3 c = uint3(unity_RendererUserValue & 0xFF, (unity_RendererUserValue >> 8) & 0xFF, (unity_RendererUserValue >> 16) & 0xFF); return half4(half3(c) / 255.0f, 1.0f); } ENDHLSL } } }