Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Surface Shader - Invalid RGB Input

Discussion in 'Shaders' started by Mooyee85, May 18, 2024.

  1. Mooyee85

    Mooyee85

    Joined:
    Aug 23, 2019
    Posts:
    3
    Hello, I created a simple shader, based on Sprites/Diffuse, that replaces Input Color to Ouput Color but it doesn't work well.

    I've a single color texture (R=247, G=25, B=255):
    upload_2024-5-18_23-43-4.png

    Here is my shader:
    Code (CSharp):
    1. Shader "Custom/Test"
    2. {
    3.     Properties
    4.     {
    5.         [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
    6.         _InputColor("Input Color", Color) = (1,1,1,1)
    7.         _Color ("Tint", Color) = (1,1,1,1)
    8.         [MaterialToggle] PixelSnap ("Pixel snap", Float) = 0
    9.         [HideInInspector] _RendererColor ("RendererColor", Color) = (1,1,1,1)
    10.         [HideInInspector] _Flip ("Flip", Vector) = (1,1,1,1)
    11.         [PerRendererData] _AlphaTex ("External Alpha", 2D) = "white" {}
    12.         [PerRendererData] _EnableExternalAlpha ("Enable External Alpha", Float) = 0
    13.     }
    14.  
    15.     SubShader
    16.     {
    17.         Tags
    18.         {
    19.             "Queue"="Transparent"
    20.             "IgnoreProjector"="True"
    21.             "RenderType"="Transparent"
    22.             "PreviewType"="Plane"
    23.             "CanUseSpriteAtlas"="True"
    24.         }
    25.  
    26.         Cull Off
    27.         Lighting Off
    28.         ZWrite Off
    29.         Blend One OneMinusSrcAlpha
    30.  
    31.         CGPROGRAM
    32.         #pragma surface surf Lambert vertex:vert nofog nolightmap nodynlightmap keepalpha noinstancing
    33.         #pragma multi_compile_local _ PIXELSNAP_ON
    34.         #pragma multi_compile _ ETC1_EXTERNAL_ALPHA
    35.         #include "UnitySprites.cginc"
    36.  
    37.         struct Input
    38.         {
    39.             float2 uv_MainTex;
    40.             fixed4 color;
    41.         };
    42.  
    43.         fixed4 _InputColor;
    44.  
    45.         void vert (inout appdata_full v, out Input o)
    46.         {
    47.             v.vertex = UnityFlipSprite(v.vertex, _Flip);
    48.  
    49.             UNITY_INITIALIZE_OUTPUT(Input, o);
    50.             o.color = v.color;
    51.         }
    52.  
    53.         void surf (Input IN, inout SurfaceOutput o)
    54.         {
    55.             fixed4 mainTextRgba = tex2D(_MainTex, IN.uv_MainTex);
    56.  
    57.             fixed4 destRgba;
    58.             if (all(mainTextRgba == _InputColor))
    59.             {
    60.                 // Never
    61.                 destRgba.r = 1;
    62.                 destRgba.g = 0;
    63.                 destRgba.b = 0;
    64.                 destRgba.a = 1;
    65.             }
    66.             else if (mainTextRgba.r == 0.9296875298023224 &&
    67.                 mainTextRgba.g == 0.00970459030941128817 &&
    68.                 mainTextRgba.b == 1)
    69.             {
    70.                 // Always
    71.                 destRgba.r = 0;
    72.                 destRgba.g = 1;
    73.                 destRgba.b = 0;
    74.                 destRgba.a = 1;
    75.             }
    76.             else
    77.             {
    78.                 // Never
    79.                 destRgba.r = 0;
    80.                 destRgba.g = 0;
    81.                 destRgba.b = 1;
    82.                 destRgba.a = 1;
    83.             }
    84.  
    85.             o.Albedo = destRgba.rgb * destRgba.a;
    86.             o.Alpha = destRgba.a;
    87.         }
    88.         ENDCG
    89.     }
    90.  
    91. Fallback "Transparent/VertexLit"
    92. }
    I set _inputColor to (R=247, G=25, B=255, A=255). The result should be red with _inputColor set to the texture's color or blue when _inputColor is different but it's always green, I don't know why. Looks like RGB Input is:
    R = 0.9296875298023224f ≈ 237
    G = 0.00970459030941128817f ≈ 2
    B = 1.0f = 255
    upload_2024-5-18_23-52-57.png

    Any idea why is it happening?
    Unity 6000.0.1f1
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,396
    What’s happening is gamma correction.

    When you are defining a color in the editor, you’re doing it in sRGB space. If you’re using linear color space for your project, which Unity defaults to, then Unity will convert that sRGB color value to a linear color value.

    Any texture and all other color values are also converted to linear color values during rendering when using linear color space.