Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by Chiri · Dec 07, 2015 at 12:13 AM · uishaderlerpshader programmingcolor change

3 Color linear gradient shader

Hi all,

Im trying to adapt this shader to take 3 colors and create a gradient from them. But I just can't seem to figure out why its not working. The idea is to check whether we are passed the mid point of the image or not and change the transition of the image but the middle color does not look solid and the transitions can sometimes change color. Any guidance is appreciated.

 Shader "Custom/Gradient_3Color"
 {
  Properties {
      [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
      _Color ("Top Color", Color) = (1,1,1,1)
      _Color2 ("Mid Color", Color) = (1,1,1,1)
      _Color3 ("Bot Color", Color) = (1,1,1,1)
      _Scale ("Middle", Range(0, 1)) = 1
  }
   
  SubShader {
      Tags {"Queue"="Background"  "IgnoreProjector"="True"}
      LOD 100
   
      ZWrite On
   
      Pass {
          CGPROGRAM
          #pragma vertex vert  
          #pragma fragment frag
          #include "UnityCG.cginc"
   
          fixed4 _Color;
          fixed4 _Color2;
          fixed4 _Color3;
          fixed  _Scale;
   
          struct v2f {
              float4 pos : SV_POSITION;
              fixed4 col : COLOR;
          };
   
          v2f vert (appdata_full v)
          {
              v2f o;
              o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
              o.col = step(_Scale, v.texcoord.y) * lerp(_Color2, _Color, v.texcoord.y) + step(v.texcoord.y, _Scale) * lerp(_Color3, _Color2, v.texcoord.y );
 //             o.col = lerp(_Color, _Color2, v.texcoord.y );
 //             o.col = half4( v.vertex.y, 0, 0, 1);
              return o;
          }
         
   
          float4 frag (v2f i) : COLOR {
              float4 c = i.col;
              c.a = 1;
              return c;
          }
              ENDCG
          }
      }
  }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
12
Best Answer

Answer by JonathanCzeck · Dec 07, 2015 at 03:28 AM

The main issue is that this is not something that you want to do in the vertex shader. You've only got 4 vertices on a sprite, so those are the only data points the fragment shader has to work with. The alternative of adding vertices in the correct locations is probably not what you want to do either.

I took the liberty of sticking it in a fragment shader and cleaning it up a tad for you:

 Shader "Custom/Gradient_3Color" {
     Properties {
         [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
         _ColorTop ("Top Color", Color) = (1,1,1,1)
         _ColorMid ("Mid Color", Color) = (1,1,1,1)
         _ColorBot ("Bot Color", Color) = (1,1,1,1)
         _Middle ("Middle", Range(0.001, 0.999)) = 1
     }
 
     SubShader {
         Tags {"Queue"="Background"  "IgnoreProjector"="True"}
         LOD 100
 
         ZWrite On
 
         Pass {
         CGPROGRAM
         #pragma vertex vert  
         #pragma fragment frag
         #include "UnityCG.cginc"
 
         fixed4 _ColorTop;
         fixed4 _ColorMid;
         fixed4 _ColorBot;
         float  _Middle;
 
         struct v2f {
             float4 pos : SV_POSITION;
             float4 texcoord : TEXCOORD0;
         };
 
         v2f vert (appdata_full v) {
             v2f o;
             o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
             o.texcoord = v.texcoord;
             return o;
         }
 
         fixed4 frag (v2f i) : COLOR {
             fixed4 c = lerp(_ColorBot, _ColorMid, i.texcoord.y / _Middle) * step(i.texcoord.y, _Middle);
             c += lerp(_ColorMid, _ColorTop, (i.texcoord.y - _Middle) / (1 - _Middle)) * step(_Middle, i.texcoord.y);
             c.a = 1;
             return c;
         }
         ENDCG
         }
     }
 }

It helps to call the variables more what they are instead of _Color2 etc. It'll save you headache :)

Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Chiri · Dec 07, 2015 at 04:02 AM 0
Share

Wow, that worked like a charm. I wasn't aware of the differences between frag and vert. Thank you very much :D

avatar image JonathanCzeck Chiri · Dec 07, 2015 at 04:07 AM 0
Share

You're welcome

avatar image felixpk JonathanCzeck · Jul 18, 2016 at 05:37 PM 0
Share

Do you know how to make the gradient "move with the Object"? I mean if I move the mesh the gradient stays in the same place.

avatar image fredrik7001 · Oct 21, 2016 at 07:50 PM 0
Share

Avoiding names like _Color2 is a good idea, still everyone insist on calling vertex vert and fragment frag tho ;)

avatar image unity_51HJ2jkrzmHJdQ · Jan 17, 2018 at 06:30 PM 0
Share

Do you know how to apply this gradient just toward z direction? https://answers.unity.com/questions/1456021/shader-for-pixel-colour-just-based-on-z-position.html

avatar image muhammadtahiriqbal · Mar 19, 2018 at 07:37 AM 0
Share

sir thanks you are hero :p

avatar image LessTergy · Jun 20, 2018 at 08:53 AM 0
Share

I found bug in formula. What happening if i.texcoord.y is equal _Middle? It is equal _ColorMid += _ColorMid

This formula prevent from this situation

c += lerp(_ColorMid, _ColorTop, (i.texcoord.y - _Middle) / (1 - _Middle)) * (1 - step(i.texcoord.y, _Middle));

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

50 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Fog of War overlay for overhead map. 1 Answer

Unity crashes when trying to run depth shader with color lerp. 1 Answer

UI Triangle Color picker 0 Answers

Getting 3D Meshes to render with UI according to heirarchy 0 Answers

Need Mask Shader . 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges