Flowlight_.Shader 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. Shader "UI/Unlit/Flowlight_"
  2. {
  3. Properties
  4. {
  5. [PerRendererData] _MainTex("Sprite Texture", 2D) = "white" {}
  6. _Color("Tint", Color) = (1, 1, 1, 1)
  7. [MaterialToggle] PixelSnap("Pixel snap", float) = 0
  8. /* Flowlight */
  9. _FlowlightColor("Flowlight Color", Color) = (1, 0, 0, 1)
  10. _Lengthlitandlar("LangthofLittle and Large", range(0,0.5)) = 0.005
  11. _MoveSpeed("MoveSpeed", float) = 5
  12. _Power("Power", float) = 1
  13. _LargeWidth("LargeWidth", range(0,0.005)) = 0.0035
  14. _LittleWidth("LittleWidth", range(0,0.001)) = 0.002
  15. /* --------- */
  16. _WidthRate("WidthRate", float) = 0
  17. _XOffset("XOffset", float) = 0
  18. _HeightRate("HeightRate", float) = 0
  19. _YOffset("YOffset", float) = 0
  20. /* UI */
  21. _StencilComp("Stencil Comparison", Float) = 8
  22. _Stencil("Stencil ID", Float) = 0
  23. _StencilOp("Stencil Operation", Float) = 0
  24. _StencilWriteMask("Stencil Write Mask", Float) = 255
  25. _StencilReadMask("Stencil Read Mask", Float) = 255
  26. _ColorMask("Color Mask", Float) = 15
  27. /* -- */
  28. }
  29. SubShader
  30. {
  31. Tags
  32. {
  33. "Queue" = "Transparent"
  34. "IgnoreProjector" = "True"
  35. "RenderType" = "Transparent"
  36. "PreviewType" = "Plane"
  37. "CanUseSpriteAtlas" = "True"
  38. }
  39. Cull Off
  40. Lighting Off
  41. ZWrite Off
  42. Blend One OneMinusSrcAlpha
  43. ColorMask[_ColorMask]
  44. /* UI */
  45. Stencil
  46. {
  47. Ref[_Stencil]
  48. Comp[_StencilComp]
  49. Pass[_StencilOp]
  50. ReadMask[_StencilReadMask]
  51. WriteMask[_StencilWriteMask]
  52. }
  53. /* -- */
  54. Pass
  55. {
  56. CGPROGRAM
  57. #pragma vertex vert
  58. #pragma fragment frag
  59. #pragma multi_compile _ PIXELSNAP_ON
  60. #include "UnityCG.cginc"
  61. struct appdata_t
  62. {
  63. float4 vertex : POSITION;
  64. float4 color : COLOR;
  65. float2 texcoord : TEXCOORD0;
  66. };
  67. struct v2f
  68. {
  69. float4 vertex : SV_POSITION;
  70. fixed4 color : COLOR;
  71. half2 texcoord : TEXCOORD0;
  72. float4 worldPosition: TEXCOORD1;
  73. };
  74. fixed4 _Color;
  75. /* Flowlight */
  76. float _Power;
  77. float _LargeWidth;
  78. float _LittleWidth;
  79. float _Lengthlitandlar;
  80. float _MoveSpeed;
  81. fixed4 _FlowlightColor;
  82. /* --------- */
  83. float _UVPosX;
  84. v2f vert(appdata_t IN)
  85. {
  86. v2f OUT;
  87. OUT.worldPosition = IN.vertex;
  88. OUT.vertex = UnityObjectToClipPos(IN.vertex);
  89. OUT.texcoord = IN.texcoord;
  90. OUT.color = IN.color * _Color;
  91. # ifdef PIXELSNAP_ON
  92. OUT.vertex = UnityPixelSnap(OUT.vertex);
  93. #endif
  94. return OUT;
  95. }
  96. sampler2D _MainTex;
  97. float4 _MainTex_ST;
  98. float _WidthRate;
  99. float _XOffset;
  100. float _HeightRate;
  101. float _YOffset;
  102. bool _UseClipRect;
  103. float4 _ClipRect;
  104. float _ClipSoftX;
  105. float _ClipSoftY;
  106. fixed4 frag(v2f IN) : SV_Target
  107. {
  108. fixed4 c = tex2D(_MainTex, IN.texcoord);
  109. /*使用裁剪*/
  110. if (_UseClipRect)
  111. {
  112. float2 factor = float2(0.0, 0.0);
  113. float2 tempXY = (IN.worldPosition.xy - _ClipRect.xy) / float2(_ClipSoftX, _ClipSoftY) * step(_ClipRect.xy, IN.worldPosition.xy);
  114. factor = max(factor, tempXY);
  115. float2 tempZW = (_ClipRect.zw - IN.worldPosition.xy) / float2(_ClipSoftX, _ClipSoftY) * step(IN.worldPosition.xy, _ClipRect.zw);
  116. factor = min(factor, tempZW);
  117. c.a *= clamp(min(factor.x, factor.y), 0.0, 1.0);
  118. }
  119. /* --------- */
  120. /* Flowlight */
  121. //计算流动的标准uvX从-0.5到1.5范围
  122. _UVPosX = _XOffset +(fmod(_Time.x* _MoveSpeed, 1) * 2 -0.5)* _WidthRate;
  123. //标准uvX倾斜
  124. _UVPosX += (IN.texcoord.y- _HeightRate*0.5- _YOffset)*0.2;
  125. //以下是计算流光在区域内的强度,根据到标准点的距离的来确定强度,为了使变化更柔和非线性,使用距离平方或者sin函数也可以
  126. float lar = pow(1 - _LargeWidth * _WidthRate, 2);
  127. float lit = pow(1 - _LittleWidth * _WidthRate, 2);
  128. //第一道流光,可以累加任意条,如下
  129. fixed4 cadd = _FlowlightColor * saturate((1 - saturate(pow(_UVPosX - IN.texcoord.x, 2))) - lar) * _Power / (1 - lar);
  130. cadd += _FlowlightColor* saturate((1 - saturate(pow(_UVPosX - _Lengthlitandlar* _WidthRate - IN.texcoord.x, 2))) - lit)* _Power/ (1-lit);
  131. c.rgb += cadd.rgb;
  132. c.rgb *= c.a;
  133. /* --------- */
  134. return c;
  135. }
  136. ENDCG
  137. }
  138. }
  139. }