Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

problem with using color in fragment shader

Discussion in 'Shaders' started by pareylook, Feb 9, 2020.

  1. pareylook

    pareylook

    Joined:
    Mar 4, 2016
    Posts:
    10
    HI all! Could you help me with my question about shaders? I trying write vertex fragment shader. If use i.color in fragment shader, my shader looks just white, but if i use _Color. Shader works correctly.

    Code (CSharp):
    1. Shader "custom/green"
    2. {
    3.     Properties
    4.     {
    5.         _Color ("Color", Color) = (1,1,1,1)
    6.         _MainTex ("Color Texture", 2D) = "white" {}
    7.     }
    8.     SubShader
    9.     {
    10.         Tags
    11.         {
    12.             "Queue"="Transparent"
    13.             "IgnoreProjector"="True"
    14.             "RenderType"="Transparent"
    15.             "PreviewType"="Plane"
    16.             "CanUseSpriteAtlas"="True"
    17.         }
    18.  
    19.         Cull Off
    20.         Lighting Off
    21.         ZWrite Off
    22.         Blend One OneMinusSrcAlpha
    23.         Pass
    24.         {
    25.             CGPROGRAM
    26.             #pragma vertex vert
    27.             #pragma fragment frag
    28.  
    29.  
    30.             #include "UnityCG.cginc"
    31.  
    32.             struct appdata
    33.             {
    34.                 float4 vertex : POSITION;
    35.                 float2 uv : TEXCOORD0;
    36.                 float4 color : COLOR0;
    37.             };
    38.  
    39.             struct v2f
    40.             {
    41.                 float4 vertex : SV_POSITION;
    42.                 float2 uv : TEXCOORD0;
    43.                 float4 color : COLOR0;
    44.             };
    45.            
    46.             float4 _Color;
    47.             sampler2D _MainTex;
    48.             float4 _MainTex_ST;
    49.  
    50.             v2f vert (appdata v)
    51.             {
    52.                 v2f o;
    53.                 o.vertex = UnityObjectToClipPos(v.vertex);              
    54.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    55.                 o.color = v.color;
    56.                 return o;
    57.             }
    58.  
    59.             fixed4 frag (v2f i) : SV_Target
    60.             {
    61.                 fixed4 col_tex = tex2D(_MainTex, i.uv);
    62.                 // fixed4 col = i.color; // not work
    63.                 fixed4 col = _Color; // work!
    64.                 return col;
    65.             }
    66.             ENDCG
    67.         }
    68.     }
    69. }
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,238
    _Color
    is the name of the color property that you see on the material.

    i.color
    is the value passed to the fragment shader from the vertex shader, which the vertex shader got from the mesh’s vertex color data. If you don’t have any color data set on your mesh (either colored in an external program, via a custom script, or some kind of vertex painting tool you’re using in the editor) then it’ll default to white.
     
    pareylook likes this.
  3. pareylook

    pareylook

    Joined:
    Mar 4, 2016
    Posts:
    10
    thank you! I understood