Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Gradient shader not works?

Discussion in 'Shaders' started by Bongmo, Jun 11, 2017.

  1. Bongmo

    Bongmo

    Joined:
    Jan 18, 2014
    Posts:
    155
    Hi.

    Why this bottom shader code is not working? I want to make a vertical gradient shader for mobile phones. I see only one color when I use it e.g. on a quad object.

    Code (CSharp):
    1. Shader "Custom/BasicGradient"
    2. {
    3. Properties
    4. {
    5.      _TopColor ("Top Color", Color) = (1, 1, 1, 1)
    6.      _BottomColor ("Bottom Color", Color) = (1, 1, 1, 1)
    7.      _RampTex ("Ramp Texture", 2D) = "white" {}
    8. }
    9. SubShader
    10. {
    11.      Pass
    12.      {
    13.          Blend SrcAlpha OneMinusSrcAlpha
    14.          CGPROGRAM
    15.          #pragma vertex vert
    16.          #pragma fragment frag
    17.          struct vertexIn {
    18.              float4 pos : POSITION;
    19.              float2 uv : TEXCOORD0;
    20.          };
    21.          struct v2f {
    22.              float4 pos : SV_POSITION;
    23.              float2 uv : TEXCOORD0;
    24.          };
    25.          v2f vert(vertexIn input)
    26.          {
    27.              v2f output;
    28.              output.pos = mul(UNITY_MATRIX_MVP, input.pos);
    29.              output.uv = input.uv;
    30.              return output;
    31.          }
    32.          fixed4 _TopColor, _BottomColor;
    33.          sampler2D _RampTex;
    34.          fixed4 frag(v2f input) : COLOR
    35.          {
    36.              return lerp(_BottomColor, _TopColor, tex2D(_RampTex, input.uv).a);
    37.          }
    38.          ENDCG
    39.      }
    40. }
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    Nothing obviously wrong with the shader, what's the texture you're using for the ramp, and do you need it?

    Why not just:
    Code (CSharp):
    1. Shader "Custom/BasicGradient"
    2. {
    3. Properties
    4. {
    5.      _TopColor ("Top Color", Color) = (1, 1, 1, 1)
    6.      _BottomColor ("Bottom Color", Color) = (1, 1, 1, 1)
    7. }
    8. SubShader
    9. {
    10.      Pass
    11.      {
    12.          Blend SrcAlpha OneMinusSrcAlpha
    13.          CGPROGRAM
    14.          #pragma vertex vert
    15.          #pragma fragment frag
    16.          struct vertexIn {
    17.              float4 pos : POSITION;
    18.              float2 uv : TEXCOORD0;
    19.          };
    20.          struct v2f {
    21.              float4 pos : SV_POSITION;
    22.              float2 uv : TEXCOORD0;
    23.          };
    24.          v2f vert(vertexIn input)
    25.          {
    26.              v2f output;
    27.              output.pos = mul(UNITY_MATRIX_MVP, input.pos);
    28.              output.uv = input.uv;
    29.              return output;
    30.          }
    31.          fixed4 _TopColor, _BottomColor;
    32.          fixed4 frag(v2f input) : COLOR
    33.          {
    34.              return lerp(_BottomColor, _TopColor, input.uv.y);
    35.          }
    36.          ENDCG
    37.      }
    38. }