当前位置:   article > 正文

FairyGUI-Unity 自定义UIShader_fairygui line space

fairygui line space

FairyGUI中给组件更换Shader,最简单的方式就是找到组件中的Shader字段进行赋值。需要注意的是,对于自定的shader效果需要将目标图片进行单独发布,也就是一个目标图片占用一张图集。(应该会有更好的解决办法,但目前还是就先这样子)

本篇文章中的Shader源码来自于对官方的“FairyGUI/Image”修改,修改部分的源码来自于网络。

示例:加载Shader

代码中加载自定义Shader:

  1. local logoImg = this.GetChild("n2").asImage
  2. logoImg.shader = "FairyGUI/Image-Light"

一:扫光Shader

  1. // Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'
  2. // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
  3. Shader "FairyGUI/Image-Light"
  4. {
  5. Properties
  6. {
  7. _MainTex ("Base (RGB), Alpha (A)", 2D) = "black" {}
  8. _StencilComp ("Stencil Comparison", Float) = 8
  9. _Stencil ("Stencil ID", Float) = 0
  10. _StencilOp ("Stencil Operation", Float) = 0
  11. _StencilWriteMask ("Stencil Write Mask", Float) = 255
  12. _StencilReadMask ("Stencil Read Mask", Float) = 255
  13. _ColorMask ("Color Mask", Float) = 15
  14. _BlendSrcFactor ("Blend SrcFactor", Float) = 5
  15. _BlendDstFactor ("Blend DstFactor", Float) = 10
  16. [Header(Light)]
  17. //扫光时间
  18. _LightTime("Light Time", Float) = 1
  19. //扫光厚度
  20. _LightThick("Light Thick", Float) = 0.2
  21. //循环时间
  22. _LightInterval("Light Interval", Float) = 2
  23. //扫光角度
  24. _LightAngle("Light Angle", int) = 60
  25. //亮度
  26. _Brightness("Light Brightness",float) = 1
  27. //扫光颜色
  28. _LightColor ("Light Color", Color) = (1,1,1,1)
  29. }
  30. SubShader
  31. {
  32. LOD 100
  33. Tags
  34. {
  35. "Queue" = "Transparent"
  36. "IgnoreProjector" = "True"
  37. "RenderType" = "Transparent"
  38. }
  39. Stencil
  40. {
  41. Ref [_Stencil]
  42. Comp [_StencilComp]
  43. Pass [_StencilOp]
  44. ReadMask [_StencilReadMask]
  45. WriteMask [_StencilWriteMask]
  46. }
  47. Cull Off
  48. Lighting Off
  49. ZWrite Off
  50. Fog
  51. {
  52. Mode Off
  53. }
  54. Blend [_BlendSrcFactor] [_BlendDstFactor], One One
  55. ColorMask [_ColorMask]
  56. Pass
  57. {
  58. CGPROGRAM
  59. #pragma multi_compile NOT_COMBINED COMBINED
  60. #pragma multi_compile NOT_GRAYED GRAYED COLOR_FILTER
  61. #pragma multi_compile NOT_CLIPPED CLIPPED SOFT_CLIPPED ALPHA_MASK
  62. #pragma vertex vert
  63. #pragma fragment frag
  64. #include "UnityCG.cginc"
  65. struct appdata_t
  66. {
  67. float4 vertex : POSITION;
  68. fixed4 color : COLOR;
  69. float4 texcoord : TEXCOORD0;
  70. };
  71. struct v2f
  72. {
  73. float4 vertex : SV_POSITION;
  74. fixed4 color : COLOR;
  75. float4 texcoord : TEXCOORD0;
  76. #ifdef CLIPPED
  77. float2 clipPos : TEXCOORD1;
  78. #endif
  79. #ifdef SOFT_CLIPPED
  80. float2 clipPos : TEXCOORD1;
  81. #endif
  82. };
  83. sampler2D _MainTex;
  84. half _LightTime;
  85. float _LightThick;
  86. float _LightInterval;
  87. int _LightAngle;
  88. float _Brightness;
  89. float4 _LightColor;
  90. #ifdef COMBINED
  91. sampler2D _AlphaTex;
  92. #endif
  93. CBUFFER_START(UnityPerMaterial)
  94. #ifdef CLIPPED
  95. float4 _ClipBox = float4(-2, -2, 0, 0);
  96. #endif
  97. #ifdef SOFT_CLIPPED
  98. float4 _ClipBox = float4(-2, -2, 0, 0);
  99. float4 _ClipSoftness = float4(0, 0, 0, 0);
  100. #endif
  101. CBUFFER_END
  102. #ifdef COLOR_FILTER
  103. float4x4 _ColorMatrix;
  104. float4 _ColorOffset;
  105. float _ColorOption = 0;
  106. #endif
  107. v2f vert(appdata_t v)
  108. {
  109. v2f o;
  110. o.vertex = UnityObjectToClipPos(v.vertex);
  111. o.texcoord = v.texcoord;
  112. #if !defined(UNITY_COLORSPACE_GAMMA) && (UNITY_VERSION >= 550)
  113. o.color.rgb = GammaToLinearSpace(v.color.rgb);
  114. o.color.a = v.color.a;
  115. #else
  116. o.color = v.color;
  117. #endif
  118. #ifdef CLIPPED
  119. o.clipPos = mul(unity_ObjectToWorld, v.vertex).xy * _ClipBox.zw + _ClipBox.xy;
  120. #endif
  121. #ifdef SOFT_CLIPPED
  122. o.clipPos = mul(unity_ObjectToWorld, v.vertex).xy * _ClipBox.zw + _ClipBox.xy;
  123. #endif
  124. return o;
  125. }
  126. fixed4 frag(v2f i) : SV_Target
  127. {
  128. fixed4 col = tex2D(_MainTex, i.texcoord.xy / i.texcoord.w) * i.color;
  129. //扫光代码来源 https://blog.csdn.net/u014621871/article/details/122685044
  130. //光照的流逝时间
  131. fixed currentTimePassed = fmod(_Time.y, _LightTime + _LightInterval);
  132. fixed x = currentTimePassed / _LightTime;
  133. //倾斜角 1°≈0.0174444
  134. float angleInRad = 0.0174444 * _LightAngle;
  135. fixed tanX = tan(angleInRad);
  136. x += (x - 1) / tanX;
  137. fixed x1 = i.texcoord.y / tanX + x;
  138. fixed x2 = x1 + _LightThick;
  139. if (i.texcoord.x > x1 && i.texcoord.x < x2)
  140. {
  141. //差值计算,根据与中心的距离的比例来计算亮度
  142. float xMid = 0.5 * (x1 + x2);
  143. fixed dis = 1 - abs(i.texcoord.x - xMid) * 2 / _LightThick;
  144. half colorA = col.a;
  145. //扫光的颜色*强度+默认颜色输出
  146. col += col + _LightColor * (_Brightness * dis);
  147. col.a = colorA;
  148. }
  149. #ifdef COMBINED
  150. col.a *= tex2D(_AlphaTex, i.texcoord.xy / i.texcoord.w).g;
  151. #endif
  152. #ifdef GRAYED
  153. fixed grey = dot(col.rgb, fixed3(0.299, 0.587, 0.114));
  154. col.rgb = fixed3(grey, grey, grey);
  155. #endif
  156. #ifdef SOFT_CLIPPED
  157. float2 factor = float2(0,0);
  158. if(i.clipPos.x<0)
  159. factor.x = (1.0-abs(i.clipPos.x)) * _ClipSoftness.x;
  160. else
  161. factor.x = (1.0-i.clipPos.x) * _ClipSoftness.z;
  162. if(i.clipPos.y<0)
  163. factor.y = (1.0-abs(i.clipPos.y)) * _ClipSoftness.w;
  164. else
  165. factor.y = (1.0-i.clipPos.y) * _ClipSoftness.y;
  166. col.a *= clamp(min(factor.x, factor.y), 0.0, 1.0);
  167. #endif
  168. #ifdef CLIPPED
  169. float2 factor = abs(i.clipPos);
  170. col.a *= step(max(factor.x, factor.y), 1);
  171. #endif
  172. #ifdef COLOR_FILTER
  173. if (_ColorOption == 0)
  174. {
  175. fixed4 col2 = col;
  176. col2.r = dot(col, _ColorMatrix[0]) + _ColorOffset.x;
  177. col2.g = dot(col, _ColorMatrix[1]) + _ColorOffset.y;
  178. col2.b = dot(col, _ColorMatrix[2]) + _ColorOffset.z;
  179. col2.a = dot(col, _ColorMatrix[3]) + _ColorOffset.w;
  180. col = col2;
  181. }
  182. else //premultiply alpha
  183. col.rgb *= col.a;
  184. #endif
  185. #ifdef ALPHA_MASK
  186. clip(col.a - 0.001);
  187. #endif
  188. return col;
  189. }
  190. ENDCG
  191. }
  192. }
  193. }

二:循环背景图

  1. // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
  2. // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
  3. // Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'
  4. // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
  5. Shader "FairyGUI/Image-Loop"
  6. {
  7. Properties
  8. {
  9. _MainTex ("Base (RGB), Alpha (A)", 2D) = "black" {}
  10. _StencilComp ("Stencil Comparison", Float) = 8
  11. _Stencil ("Stencil ID", Float) = 0
  12. _StencilOp ("Stencil Operation", Float) = 0
  13. _StencilWriteMask ("Stencil Write Mask", Float) = 255
  14. _StencilReadMask ("Stencil Read Mask", Float) = 255
  15. _ColorMask ("Color Mask", Float) = 15
  16. _BlendSrcFactor ("Blend SrcFactor", Float) = 5
  17. _BlendDstFactor ("Blend DstFactor", Float) = 10
  18. _Speed("Speed", Range(0,3)) = 0.1
  19. _Rotation("Rotation", Range(0,360)) = 225 //为了方便直观,角度的值使用了0~360范围
  20. }
  21. SubShader
  22. {
  23. LOD 100
  24. Tags
  25. {
  26. "Queue" = "Transparent"
  27. "IgnoreProjector" = "True"
  28. "RenderType" = "Transparent"
  29. }
  30. Stencil
  31. {
  32. Ref [_Stencil]
  33. Comp [_StencilComp]
  34. Pass [_StencilOp]
  35. ReadMask [_StencilReadMask]
  36. WriteMask [_StencilWriteMask]
  37. }
  38. Cull Off
  39. Lighting Off
  40. ZWrite Off
  41. Fog
  42. {
  43. Mode Off
  44. }
  45. Blend [_BlendSrcFactor] [_BlendDstFactor], One One
  46. ColorMask [_ColorMask]
  47. Pass
  48. {
  49. Blend SrcAlpha OneMinusSrcAlpha
  50. CGPROGRAM
  51. #pragma multi_compile NOT_COMBINED COMBINED
  52. #pragma multi_compile NOT_GRAYED GRAYED COLOR_FILTER
  53. #pragma multi_compile NOT_CLIPPED CLIPPED SOFT_CLIPPED ALPHA_MASK
  54. #pragma vertex vert
  55. #pragma fragment frag
  56. #pragma shader_feature _FlipY
  57. #include "UnityCG.cginc"
  58. struct appdata_t
  59. {
  60. float4 vertex : POSITION;
  61. fixed4 color : COLOR;
  62. float4 texcoord : TEXCOORD0;
  63. };
  64. struct v2f
  65. {
  66. float4 vertex : SV_POSITION;
  67. fixed4 color : COLOR;
  68. float4 texcoord : TEXCOORD0;
  69. half2 uvTA: TEXCOORD2;
  70. #ifdef CLIPPED
  71. float2 clipPos : TEXCOORD1;
  72. #endif
  73. #ifdef SOFT_CLIPPED
  74. float2 clipPos : TEXCOORD1;
  75. #endif
  76. };
  77. sampler2D _MainTex;
  78. float _Speed;
  79. float _Rotation;
  80. uniform half4 _MainTex_ST;
  81. #ifdef COMBINED
  82. sampler2D _AlphaTex;
  83. #endif
  84. CBUFFER_START(UnityPerMaterial)
  85. #ifdef CLIPPED
  86. float4 _ClipBox = float4(-2, -2, 0, 0);
  87. #endif
  88. #ifdef SOFT_CLIPPED
  89. float4 _ClipBox = float4(-2, -2, 0, 0);
  90. float4 _ClipSoftness = float4(0, 0, 0, 0);
  91. #endif
  92. CBUFFER_END
  93. #ifdef COLOR_FILTER
  94. float4x4 _ColorMatrix;
  95. float4 _ColorOffset;
  96. float _ColorOption = 0;
  97. #endif
  98. v2f vert(appdata_t v)
  99. {
  100. v2f o;
  101. o.vertex = UnityObjectToClipPos(v.vertex);
  102. #if !defined(UNITY_COLORSPACE_GAMMA) && (UNITY_VERSION >= 550)
  103. o.color.rgb = GammaToLinearSpace(v.color.rgb);
  104. o.color.a = v.color.a;
  105. #else
  106. o.color = v.color;
  107. #endif
  108. #ifdef CLIPPED
  109. o.clipPos = mul(unity_ObjectToWorld, v.vertex).xy * _ClipBox.zw + _ClipBox.xy;
  110. #endif
  111. #ifdef SOFT_CLIPPED
  112. o.clipPos = mul(unity_ObjectToWorld, v.vertex).xy * _ClipBox.zw + _ClipBox.xy;
  113. #endif
  114. float Rot = _Rotation * (3.1415926f / 180.0f);
  115. float s = sin(Rot);
  116. float c = cos(Rot);
  117. o.texcoord = v.texcoord;
  118. o.uvTA = (v.texcoord) * _MainTex_ST.xy + fixed2(s, c) * (_Time.y * _Speed) - _MainTex_ST.zw;
  119. return o;
  120. }
  121. fixed4 frag(v2f i) : SV_Target
  122. {
  123. fixed4 col = tex2D(_MainTex, i.uvTA).rgba * i.color;
  124. #ifdef COMBINED
  125. col.a *= tex2D(_AlphaTex, i.texcoord.xy / i.texcoord.w).g;
  126. #endif
  127. #ifdef GRAYED
  128. fixed grey = dot(col.rgb, fixed3(0.299, 0.587, 0.114));
  129. col.rgb = fixed3(grey, grey, grey);
  130. #endif
  131. #ifdef SOFT_CLIPPED
  132. float2 factor = float2(0,0);
  133. if(i.clipPos.x<0)
  134. factor.x = (1.0-abs(i.clipPos.x)) * _ClipSoftness.x;
  135. else
  136. factor.x = (1.0-i.clipPos.x) * _ClipSoftness.z;
  137. if(i.clipPos.y<0)
  138. factor.y = (1.0-abs(i.clipPos.y)) * _ClipSoftness.w;
  139. else
  140. factor.y = (1.0-i.clipPos.y) * _ClipSoftness.y;
  141. col.a *= clamp(min(factor.x, factor.y), 0.0, 1.0);
  142. #endif
  143. #ifdef CLIPPED
  144. float2 factor = abs(i.clipPos);
  145. col.a *= step(max(factor.x, factor.y), 1);
  146. #endif
  147. #ifdef COLOR_FILTER
  148. if (_ColorOption == 0)
  149. {
  150. fixed4 col2 = col;
  151. col2.r = dot(col, _ColorMatrix[0]) + _ColorOffset.x;
  152. col2.g = dot(col, _ColorMatrix[1]) + _ColorOffset.y;
  153. col2.b = dot(col, _ColorMatrix[2]) + _ColorOffset.z;
  154. col2.a = dot(col, _ColorMatrix[3]) + _ColorOffset.w;
  155. col = col2;
  156. }
  157. else //premultiply alpha
  158. col.rgb *= col.a;
  159. #endif
  160. #ifdef ALPHA_MASK
  161. clip(col.a - 0.001);
  162. #endif
  163. return col;
  164. }
  165. ENDCG
  166. }
  167. }
  168. }

参考:

1、UGUI扫光shader

2、无限循环背景的制作

如果有问题可以留言指出!感谢!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/109459
推荐阅读
相关标签
  

闽ICP备14008679号