Search Unity

Simple Unlit/Frag Shader Problem

Discussion in 'Shaders' started by DelphicMind, Nov 13, 2019.

  1. DelphicMind

    DelphicMind

    Joined:
    Apr 23, 2018
    Posts:
    30
    Hi, i have a simple shader-coding question.

    I dont understand why i dont get the desired Color and just a grey tone when i pass the RGBA values in seperated manner. ->

    fixed4 mainColor = _MainColor;
    fixed4 col = (mainColor.r, mainColor.g, mainColor.b, mainColor.a);

    If i would return only mainColor is would get my desired Color but if i return col i just get grey.
    Whats the problem?




    Code (HLSL):
    1. Shader "Unlit/test"
    2. {
    3.     Properties
    4.     {
    5.         _MainColor("MainColor", Color) = (1,1,1,1)
    6.         _MainTex ("Texture", 2D) = "white" {}
    7.     }
    8.     SubShader
    9.     {
    10.         Tags { "RenderType"="Opaque" }
    11.         LOD 100
    12.  
    13.         Pass
    14.         {
    15.             CGPROGRAM
    16.             #pragma vertex vert
    17.             #pragma fragment frag
    18.             // make fog work
    19.             #pragma multi_compile_fog
    20.  
    21.             #include "UnityCG.cginc"
    22.  
    23.             struct appdata
    24.             {
    25.                 float4 vertex : POSITION;
    26.                 float2 uv : TEXCOORD0;
    27.             };
    28.  
    29.             struct v2f
    30.             {
    31.                 float2 uv : TEXCOORD0;
    32.                 UNITY_FOG_COORDS(1)
    33.                 float4 vertex : SV_POSITION;
    34.             };
    35.  
    36.             float4 _MainColor;
    37.             sampler2D _MainTex;
    38.             float4 _MainTex_ST;
    39.  
    40.             v2f vert (appdata v)
    41.             {
    42.                 v2f o;
    43.                 o.vertex = UnityObjectToClipPos(v.vertex);
    44.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    45.                 UNITY_TRANSFER_FOG(o,o.vertex);
    46.                 return o;
    47.             }
    48.  
    49.             fixed4 frag (v2f i) : SV_Target
    50.             {
    51.                 fixed4 mainColor = _MainColor;
    52.                 // sample the texture
    53.                 fixed4 texColor = tex2D(_MainTex, i.uv);
    54.  
    55.                 fixed4 col = (mainColor.r,mainColor.g,mainColor.b,mainColor.a);
    56.  
    57.                 // apply fog
    58.                 UNITY_APPLY_FOG(i.fogCoord, col);
    59.                 return col;
    60.             }
    61.             ENDCG
    62.         }
    63.     }
    64. }
    65.  
     
    Last edited: Nov 13, 2019
  2. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    637
    Hi Chris,

    Your syntax is just slightly wrong. Use:
    Code (HLSL):
    1. float4(mainColor.r,mainColor.g,mainColor.b,mainColor.a);
    and you can use also this kind of variable declaration, similar to setting values to an array:
    Code (HLSL):
    1. fixed4 col = { mainColor.r,mainColor.g,mainColor.b,mainColor.a };
    I suggest you open the Microsoft HLSL reference. It's a dry but good source for exact information. Helps a lot when you run into this kind of things especially when starting with shaders.
     
    DelphicMind likes this.
  3. DelphicMind

    DelphicMind

    Joined:
    Apr 23, 2018
    Posts:
    30
    ah thank you ! so simple xD
     
    Olmi likes this.
  4. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    637
    Yeah but not so simple when you don't get an error :). Happens to me a lot from time to time with different things when trying to write shaders/HLSL.

    P.S. also notice that you're mixing two data types there, fixed and float.
     
    DelphicMind likes this.
  5. DelphicMind

    DelphicMind

    Joined:
    Apr 23, 2018
    Posts:
    30
    yeb thats the thing, i thought it was accepted. :)
    im just starting out with this... thx
     
  6. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    6,298
    This is orthogonal to what the OP asked, but avoid using custom names for things unless you have a specific reason. For example using _MainColor instead of the more standard _Color might cause problems, since there are things in Unity that rely on things being named a certain way.
     
  7. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    8,105
    Or, you know, just
    fixed4 col = mainColor;


    Specifically
    material.color = myColor;
    expects the shader to be using
    _Color
    , as it's just an alias for
    material.SetColor("_Color", myColor);
    . Really, just don't ever use
    material.color
    and it won't be a problem.

    The other place it's expected is with fallback shadowcaster passes and replacement shaders. This is really only an issue with an alpha tested shader, where it's expecting the alpha of the
    _MainTex
    is multiplied by
    _Color.a
    before doing the
    clip()
    . If you're not using alpha testing, or not using a fallback for the shadowcaster and not using the _CameraDepthNormalsTexture, this can also be safely ignored.
     
  8. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    6,298
    Also lightmapping expects certain things to be named in certain ways, although if you are writing completely custom Lightmapped shaders, you might as well write a Meta pass too.
     
  9. DelphicMind

    DelphicMind

    Joined:
    Apr 23, 2018
    Posts:
    30
    thanks everybody... i just wanted to understand why it didnt work when i seperate the RGBA and make a new float. all good now :)
     
  10. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    637
    @bgolus I thought there might be something going to happen with those values after that specific issue was resolved by the author, so I tried to suggest a fix it with minimal changes. But of course you're absolutely correct.
     
unityunity