Search Unity

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

Discussion in 'Graphics for ECS' started by khalid_mightybear, Feb 10, 2020.

  1. khalid_mightybear

    khalid_mightybear

    Joined:
    Sep 10, 2017
    Posts:
    36
    Code (CSharp):
    1.            
    2. Entities.ForEach(
    3.    (
    4.       RenderMesh renderMesh
    5.    ) =>
    6.    {
    7.       renderMesh.material.color = Color.red
    8.    }
    9. );
    10.  
    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?
     
    Last edited: Feb 10, 2020
    eterlan likes this.
  2. julian-moschuering

    julian-moschuering

    Joined:
    Apr 15, 2014
    Posts:
    529
    Basically like this:
    Code (CSharp):
    1. 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.
     
  3. khalid_mightybear

    khalid_mightybear

    Joined:
    Sep 10, 2017
    Posts:
    36
    Thank you. Do you know if this works with URP? Judging from here, https://forum.unity.com/threads/per-instance-material-params-support-in-entities-0-2.782207/, it's yet to be supported
     
  4. julian-moschuering

    julian-moschuering

    Joined:
    Apr 15, 2014
    Posts:
    529
    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
    Code (CSharp):
    1. #if defined(UNITY_DOTS_INSTANCING_ENABLED)
    2.     float4 _Color_dummy;
    3. #else
    4.     float4 _Color;
    5. #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:
    Code (CSharp):
    1.  
    2. #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))
    3.     #define UNITY_SUPPORT_INSTANCING
    4. #endif
    5. #if defined(UNITY_SUPPORT_INSTANCING) && defined(INSTANCING_ON)
    6.     #define UNITY_DOTS_INSTANCING_ENABLED 1
    7. #endif
    8.  
    9. #if defined(UNITY_DOTS_INSTANCING_ENABLED)
    10.     #define SHADER_GRAPH_GENERATED
    11.     #define DOTS_CUSTOM_ADDITIONAL_MATERIAL_VARS UNITY_DEFINE_INSTANCED_PROP(float, _Color_Array)
    12.     #define _Color UNITY_ACCESS_INSTANCED_PROP(unity_Builtins0, _Color_Array)
    13. #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.
     
  5. WildMaN

    WildMaN

    Joined:
    Jan 24, 2013
    Posts:
    128
  6. WildMaN

    WildMaN

    Joined:
    Jan 24, 2013
    Posts:
    128
    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:
    https://issuetracker.unity3d.com/is...ering-on-entities-when-srp-batcher-is-enabled

    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.