How do you change color of the material using the hybrid renderer?

Entities.ForEach(
   (
      RenderMesh renderMesh
   ) =>
   {
      renderMesh.material.color = Color.red
   }
);

This doesn’t seem to change anything

Edit:Nvm, I got the color keyword wrong. How would I set it for a specific entity though?

1 Like

Basically like this:

EntityManager.AddComponentData(entity, new MaterialColor { Value = new float4(1,0,0,1) });

MaterialColor is a sample component provided by unity, you can simple add more.
This feature is pretty new. You have to set the property to ‘Hybrid Instanced (experimental)’ in shader graph. Using this feature in manually written shaders or other shader graphs like Amplify is possible but currently requires some extra work.

Thank you. Do you know if this works with URP? Judging from here, https://discussions.unity.com/t/766602 , it’s yet to be supported

You can get it to work but it doesn’t work out of the box. Shader graph templates are not yet adjusted. The shader code for dots instancing is included in core.

To get it to work in custom shaders you basically have to do these steps:

  1. Property has to be in Properties section
  2. You need to specify this in the UnityPerMaterial block
#if defined(UNITY_DOTS_INSTANCING_ENABLED)
    float4 _Color_dummy;
#else
    float4 _Color;
#endif

This ensures that the UnityPerMaterial block format and size is the same for the non-instanced and instanced variation making the shader SRP batcher compatible.
3. You basically have to define this to add the property to the DOTS property block:

#if SHADER_TARGET >= 35 && (defined(SHADER_API_D3D11) || defined(SHADER_API_GLES3) || defined(SHADER_API_GLCORE) || defined(SHADER_API_XBOXONE) || defined(SHADER_API_PSSL) || defined(SHADER_API_VULKAN) || defined(SHADER_API_METAL))
    #define UNITY_SUPPORT_INSTANCING
#endif
#if defined(UNITY_SUPPORT_INSTANCING) && defined(INSTANCING_ON)
    #define UNITY_DOTS_INSTANCING_ENABLED 1
#endif

#if defined(UNITY_DOTS_INSTANCING_ENABLED)
    #define SHADER_GRAPH_GENERATED
    #define DOTS_CUSTOM_ADDITIONAL_MATERIAL_VARS UNITY_DEFINE_INSTANCED_PROP(float, _Color_Array)
    #define _Color UNITY_ACCESS_INSTANCED_PROP(unity_Builtins0, _Color_Array)
#endif

Adding it to the Shader Graph templates is possible too. I don’t know if you should do this stuff, it will probably get official support soon. 2020.1 will use this feature for DOTS skinning and URP support is worked on.

@julian-moschuering Could you please share a working shader code?

So I managed to get it working, kinda - not on Android, unfortunately. The code julian-moschuering mentioned is actually from the default HDRP shader, you can grab it from the Shader Graph node. It’s not HDRP specific and compiles with URP.

The problem is the tinted texture flicker. It might be related to this bug:

Lit shader definitely flickers. I took an Unlit one and stripped away pretty much all the pizzas. Still no luck.

The problem is that if I use a) albedo texture and b) multiply it by a tint color, then c) moving entities with this material will flicker. Any two of this work: albedo+tint on static, albedo OR tint on dynamic.

float3 Color1 = _TintColor_Instance.rgb;
float3 Color2 = tex2D( _Albedo, uv_Albedo ).rgb;
// float3 Color = Color1; <-- OK
// float3 Color = Color1 / 4; <-- OK
// float3 Color = Color2; <-- OK
// float3 Color = Color2 * float3(0,1,0) ; <-- OK
float3 Color = Color1 * Color2; // <-- FLICKER ((((((

I don’t get why it causes flickering on dynamic objects on Android. PC is rock solid though.