颜色渐变

作者:追风剑情 发布于:2019-10-28 20:56 分类:Shader

  1. Shader "Custom/Gradient"
  2. {
  3. Properties
  4. {
  5. _MainTex("Texture", 2D) = "white" {}
  6. //渐变颜色
  7. _GradientColor("Gradient Color", Color) = (1, 0, 0, 0)
  8. //离底部距离
  9. _GradientBottom("Gradient Bottom", Range(0, 1)) = 0.5
  10. }
  11. SubShader
  12. {
  13. Tags { "Queue" = "Transparent" "IgnoreProjector" = "true" "RenderType" = "Transparent"}
  14. LOD 100
  15.  
  16. Pass
  17. {
  18. Tags {"LightMode" = "ForwardBase"}
  19.  
  20. ZWrite Off
  21. //开启Alpha混合才能调节透明度
  22. Blend SrcAlpha OneMinusSrcAlpha
  23.  
  24. CGPROGRAM
  25. #pragma vertex vert
  26. #pragma fragment frag
  27.  
  28. #include "UnityCG.cginc"
  29.  
  30. struct appdata
  31. {
  32. float4 vertex : POSITION;
  33. float2 uv : TEXCOORD0;
  34. };
  35.  
  36. struct v2f
  37. {
  38. float2 uv : TEXCOORD0;
  39. UNITY_FOG_COORDS(1)
  40. float4 vertex : SV_POSITION;
  41. };
  42.  
  43. sampler2D _MainTex;
  44. float4 _MainTex_ST;
  45. fixed4 _GradientColor;
  46. float _GradientBottom;
  47.  
  48. v2f vert(appdata v)
  49. {
  50. v2f o;
  51. o.vertex = UnityObjectToClipPos(v.vertex);
  52. o.uv = TRANSFORM_TEX(v.uv, _MainTex);
  53. return o;
  54. }
  55.  
  56. fixed4 frag(v2f i) : SV_Target
  57. {
  58. fixed4 col = tex2D(_MainTex, i.uv);
  59. //线性插值(y-y0)=k(x-x0)
  60. //+ step(_GradientBottom, 1) 是为了避免分母为0
  61. fixed t = max(0, 1 / (1 + step(_GradientBottom, 1) -_GradientBottom) * (i.uv.y - _GradientBottom));
  62. col = lerp(col, _GradientColor, t);
  63. return col;
  64. }
  65. ENDCG
  66. }
  67. }
  68. }

222.png

测试

111111.gif

标签: Shader

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号