Learn how to set and use the Renderer Shader User Value (RSUV) through an example.
To set the value, you have to write a script.
The following example sets a random color to all MeshRenderer of the scene, encoded as a 24-bit integer:
MeshRenderer[] allMeshRenderers = FindObjectsByType<MeshRenderer>(FindObjectsSortMode.InstanceID);
foreach (MeshRenderer meshRenderer in allMeshRenderers)
{
// compute a random color
Color32 c = Color.HSVToRGB(UnityEngine.Random.Range(0f, 1f), UnityEngine.Random.Range(0.6f, 1.0f), UnityEngine.Random.Range(0.6f, 1.0f));
// set it as a LDR color in the RSUV value of the renderer
uint cc = ((uint)c.b << 16) | ((uint)c.g << 8) | ((uint)c.r << 0);
meshRenderer.SetShaderUserValue(cc);
}
Once you’ve set the value, you can use it in shader code or in a shader graph. You can also use it with Entities Graphics.
In shader code, you can decode the 24-bit integer as a float4 color. For example:
uint c = unity_RendererUserValue;
float4 mycolor = float4((float)((c >> 0) & 255) * (1.f / 255.f),
(float)((c >> 8) & 255) * (1.f / 255.f),
(float)((c >> 16) & 255) * (1.f / 255.f),
(float)1.f);
To retrieve the RSUV in a shader graph, use a Custom Function node.
For example:
In your project, create an HLSL file with the following content:
``` lang-hlsl
void DecodeRSUVColor_float(out float4 Out) { uint c = unity_RendererUserValue; Out = float4((float)((c >> 0) & 255) * (1.f / 255.f), (float)((c >> 8) & 255) * (1.f / 255.f), (float)((c >> 16) & 255) * (1.f / 255.f), 1.f); }
#endif // DECODE_RSUV_INCLUDED
```
Name the file DecodeRSUV.hlsl, for example.
In the Shader Graph workspace, add a Custom Function node and select it.
In the Graph Inspector, in the Custom Function node properties:
Add an Output of type Vector 4 and call it Out.
Set the Type to File.
Set the Name to DecodeRSUVColor.
In the Source field, select the DecodeRSUV.hlsl you previously created.
In the Shader Graph workspace, connect the Custom Function node’s Out port to the Fragment’s Base Color input.
You can also use the RSUV feature along with Unity’s Entities Graphics package.
The following code example assigns a distinct RSUV value to all entities that include a MaterialMeshInfo component.
using Unity.Entities;
using UnityEngine;
using Unity.Rendering;
using Unity.Burst;
[MaterialProperty("unity_RendererUserValuesPropertyEntry")]
public struct RendererUserValue_BaseValue : IComponentData
{
public uint Value;
}
[BurstCompile]
public partial struct MaterialMeshInfoDebugSystem : ISystem
{
private bool m_done;
public void OnCreate(ref SystemState state)
{
m_done = false;
}
public void OnUpdate(ref SystemState state)
{
if ( !m_done )
{
var entityManager = state.EntityManager;
var mQuery = entityManager.CreateEntityQuery(ComponentType.ReadOnly<MaterialMeshInfo>());
// subscene may not be fully loaded, so wait for some entities to be here
if (!mQuery.IsEmpty)
{
using (var entities = mQuery.ToEntityArray(Unity.Collections.Allocator.Temp))
{
foreach (var entity in entities)
{
Color32 c = Color.HSVToRGB(UnityEngine.Random.Range(0f, 1f), UnityEngine.Random.Range(0.6f, 1.0f), UnityEngine.Random.Range(0.6f, 1.0f));
uint cc = ((uint)255<<24) | ((uint)c.b << 16) | ((uint)c.g << 8) | ((uint)c.r << 0);
entityManager.AddComponentData(entity, new RendererUserValue_BaseValue { Value = cc });
}
}
m_done = true;
}
}
}
}