MaterialPropertyBlock.SetVectorArray not working properly with Color

MaterialPropertyBlock’s SetVectorArray doesn’t work as expected - Unity Forum

[MaterialPropertyBlock’s SetVectorArray does not work as expected with Standard shader. - Unity Forum]( MaterialPropertyBlock's SetVectorArray does not work as expected with Standard shader. #:~:text=The%20problem%20is%20exactly%20the%20same%20as%20the,to%20write%20a%20custom%20shader%20to%20get%20that.)

I guess it’s a similar issue as the above threads.

I’m using MaterialPropertyBlock.SetVectorArray to set the colour of each circle. Each colour is obtained from an array of colours. For some reason, the colours seem a lot softer when rendered using Graphics.DrawMeshInstanced (colours look right if using normal GameObjects).

I have GPU Instancing enabled. I am also using Amplify Shader and when I have a look at the shader file I can see that the following is applied.

        UNITY_INSTANCING_BUFFER_START(Shader_Entity)
            UNITY_DEFINE_INSTANCED_PROP(float4, _MainColour)
#define _MainColour_arr Shader_Entity
            UNITY_DEFINE_INSTANCED_PROP(float4, _IconColour)
#define _IconColour_arr Shader_Entity
        UNITY_INSTANCING_BUFFER_END(Shader_Entity)

Full shader code:

Shader "Shader_Entity"
{
    Properties
    {
        _Icon("Icon", 2D) = "white" {}
        _MainColour("Main Colour", Color) = (0.9411765,0.2,0.2627451,1)
        _IconColour("Icon Colour", Color) = (1,1,1,1)
        [HideInInspector] _texcoord( "", 2D ) = "white" {}
        [HideInInspector] __dirty( "", Int ) = 1
    }

    SubShader
    {
        Tags{ "RenderType" = "Transparent"  "Queue" = "Transparent+0" "IgnoreProjector" = "True" "IsEmissive" = "true"  }
        Cull Back
        CGPROGRAM
        #pragma target 3.0
        #pragma multi_compile_instancing
        #pragma surface surf Unlit alpha:fade keepalpha noshadow
        struct Input
        {
            float2 uv_texcoord;
        };

        uniform sampler2D _Icon;

        UNITY_INSTANCING_BUFFER_START(Shader_Entity)
            UNITY_DEFINE_INSTANCED_PROP(float4, _MainColour)
#define _MainColour_arr Shader_Entity
            UNITY_DEFINE_INSTANCED_PROP(float4, _IconColour)
#define _IconColour_arr Shader_Entity
        UNITY_INSTANCING_BUFFER_END(Shader_Entity)

        inline half4 LightingUnlit( SurfaceOutput s, half3 lightDir, half atten )
        {
            return half4 ( 0, 0, 0, s.Alpha );
        }

        void surf( Input i , inout SurfaceOutput o )
        {
            float4 _MainColour_Instance = UNITY_ACCESS_INSTANCED_PROP(_MainColour_arr, _MainColour);
            float2 appendResult11_g2 = (float2(1.0 , 1.0));
            float temp_output_17_0_g2 = length( ( (i.uv_texcoord*2.0 + -1.0) / appendResult11_g2 ) );
            float temp_output_6_0 = saturate( ( ( 1.0 - temp_output_17_0_g2 ) / fwidth( temp_output_17_0_g2 ) ) );
            float4 temp_cast_0 = (temp_output_6_0).xxxx;
            float2 appendResult11_g1 = (float2(0.5 , 0.5));
            float temp_output_17_0_g1 = length( ( (i.uv_texcoord*2.0 + -1.0) / appendResult11_g1 ) );
            float2 uv_TexCoord2 = i.uv_texcoord * float2( 2,2 ) + float2( -0.5,-0.5 );
            float4 temp_output_7_0 = saturate( ( saturate( ( ( 1.0 - temp_output_17_0_g1 ) / fwidth( temp_output_17_0_g1 ) ) ) * tex2D( _Icon, uv_TexCoord2 ) ) );
            float4 _IconColour_Instance = UNITY_ACCESS_INSTANCED_PROP(_IconColour_arr, _IconColour);
            o.Emission = ( ( _MainColour_Instance * saturate( ( temp_cast_0 - temp_output_7_0 ) ) ) + ( _IconColour_Instance * temp_output_7_0 ) ).rgb;
            o.Alpha = temp_output_6_0;
        }

        ENDCG
    }
    CustomEditor "ASEMaterialInspector"
}

EDIT: Another image comparison. Smaller circles above larger ones are rendered using Graphics.DrawMeshInstanced.

Try checking if the colors you’re seeing correspond to any of the following
Color.linear or Color.gamma

If they do, the solution is very simple: Unity forgets to compensate for gamma curve, do it manually.

Wow. Such a simple and quick fix. Thank you.

1 Like