Search Unity

Resolved MaterialPropertyBlock.SetVectorArray not working properly with Color

Discussion in 'General Graphics' started by inSight01, May 24, 2023.

  1. inSight01

    inSight01

    Joined:
    Apr 18, 2017
    Posts:
    90
    MaterialPropertyBlock's SetVectorArray doesn't work as expected - Unity Forum

    MaterialPropertyBlock's SetVectorArray does not work as expected with Standard shader. - Unity Forum

    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.


    Code (CSharp):
    1.         UNITY_INSTANCING_BUFFER_START(Shader_Entity)
    2.             UNITY_DEFINE_INSTANCED_PROP(float4, _MainColour)
    3. #define _MainColour_arr Shader_Entity
    4.             UNITY_DEFINE_INSTANCED_PROP(float4, _IconColour)
    5. #define _IconColour_arr Shader_Entity
    6.         UNITY_INSTANCING_BUFFER_END(Shader_Entity)
    Full shader code:

    Code (CSharp):
    1. Shader "Shader_Entity"
    2. {
    3.     Properties
    4.     {
    5.         _Icon("Icon", 2D) = "white" {}
    6.         _MainColour("Main Colour", Color) = (0.9411765,0.2,0.2627451,1)
    7.         _IconColour("Icon Colour", Color) = (1,1,1,1)
    8.         [HideInInspector] _texcoord( "", 2D ) = "white" {}
    9.         [HideInInspector] __dirty( "", Int ) = 1
    10.     }
    11.  
    12.     SubShader
    13.     {
    14.         Tags{ "RenderType" = "Transparent"  "Queue" = "Transparent+0" "IgnoreProjector" = "True" "IsEmissive" = "true"  }
    15.         Cull Back
    16.         CGPROGRAM
    17.         #pragma target 3.0
    18.         #pragma multi_compile_instancing
    19.         #pragma surface surf Unlit alpha:fade keepalpha noshadow
    20.         struct Input
    21.         {
    22.             float2 uv_texcoord;
    23.         };
    24.  
    25.         uniform sampler2D _Icon;
    26.  
    27.         UNITY_INSTANCING_BUFFER_START(Shader_Entity)
    28.             UNITY_DEFINE_INSTANCED_PROP(float4, _MainColour)
    29. #define _MainColour_arr Shader_Entity
    30.             UNITY_DEFINE_INSTANCED_PROP(float4, _IconColour)
    31. #define _IconColour_arr Shader_Entity
    32.         UNITY_INSTANCING_BUFFER_END(Shader_Entity)
    33.  
    34.         inline half4 LightingUnlit( SurfaceOutput s, half3 lightDir, half atten )
    35.         {
    36.             return half4 ( 0, 0, 0, s.Alpha );
    37.         }
    38.  
    39.         void surf( Input i , inout SurfaceOutput o )
    40.         {
    41.             float4 _MainColour_Instance = UNITY_ACCESS_INSTANCED_PROP(_MainColour_arr, _MainColour);
    42.             float2 appendResult11_g2 = (float2(1.0 , 1.0));
    43.             float temp_output_17_0_g2 = length( ( (i.uv_texcoord*2.0 + -1.0) / appendResult11_g2 ) );
    44.             float temp_output_6_0 = saturate( ( ( 1.0 - temp_output_17_0_g2 ) / fwidth( temp_output_17_0_g2 ) ) );
    45.             float4 temp_cast_0 = (temp_output_6_0).xxxx;
    46.             float2 appendResult11_g1 = (float2(0.5 , 0.5));
    47.             float temp_output_17_0_g1 = length( ( (i.uv_texcoord*2.0 + -1.0) / appendResult11_g1 ) );
    48.             float2 uv_TexCoord2 = i.uv_texcoord * float2( 2,2 ) + float2( -0.5,-0.5 );
    49.             float4 temp_output_7_0 = saturate( ( saturate( ( ( 1.0 - temp_output_17_0_g1 ) / fwidth( temp_output_17_0_g1 ) ) ) * tex2D( _Icon, uv_TexCoord2 ) ) );
    50.             float4 _IconColour_Instance = UNITY_ACCESS_INSTANCED_PROP(_IconColour_arr, _IconColour);
    51.             o.Emission = ( ( _MainColour_Instance * saturate( ( temp_cast_0 - temp_output_7_0 ) ) ) + ( _IconColour_Instance * temp_output_7_0 ) ).rgb;
    52.             o.Alpha = temp_output_6_0;
    53.         }
    54.  
    55.         ENDCG
    56.     }
    57.     CustomEditor "ASEMaterialInspector"
    58. }
    EDIT: Another image comparison. Smaller circles above larger ones are rendered using Graphics.DrawMeshInstanced.

     
    Last edited: May 24, 2023
  2. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,105
    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.
     
  3. inSight01

    inSight01

    Joined:
    Apr 18, 2017
    Posts:
    90
    Wow. Such a simple and quick fix. Thank you.

     
    orionsyndrome likes this.