当前位置:   article > 正文

Unity Shader:闪烁_unity 闪烁shader

unity 闪烁shader

还是一样的分为UI闪烁和物体闪烁,其中具体可分为:UI闪烁、物体闪烁与半透明闪烁

1,UI闪烁

对于UI 还是一样的,改写UI本身的shader

  1. Shader "UI/YydUIShanShder"
  2. {
  3. Properties
  4. {
  5. [PerRendererData] _MainTex("Sprite Texture", 2D) = "white" {}
  6. _Color("Tint", Color) = (1,1,1,1)
  7. _StencilComp("Stencil Comparison", Float) = 8
  8. _Stencil("Stencil ID", Float) = 0
  9. _StencilOp("Stencil Operation", Float) = 0
  10. _StencilWriteMask("Stencil Write Mask", Float) = 255
  11. _StencilReadMask("Stencil Read Mask", Float) = 255
  12. _ColorMask("Color Mask", Float) = 15
  13. [Toggle(UNITY_UI_ALPHACLIP)] _UseUIAlphaClip("Use Alpha Clip", Float) = 0
  14. [Toggle]_Switch("Switch", Float) = 0
  15. _value("Speed",Range(1,3)) = 1
  16. _overlayCol("OtherColor",Color) = (0.2146,1,0,0.6039)
  17. }
  18. SubShader
  19. {
  20. Tags
  21. {
  22. "Queue" = "Transparent"
  23. "IgnoreProjector" = "True"
  24. "RenderType" = "Transparent"
  25. "PreviewType" = "Plane"
  26. "CanUseSpriteAtlas" = "True"
  27. }
  28. Stencil
  29. {
  30. Ref[_Stencil]
  31. Comp[_StencilComp]
  32. Pass[_StencilOp]
  33. ReadMask[_StencilReadMask]
  34. WriteMask[_StencilWriteMask]
  35. }
  36. Cull Off
  37. Lighting Off
  38. ZWrite Off
  39. ZTest[unity_GUIZTestMode]
  40. Blend SrcAlpha OneMinusSrcAlpha
  41. ColorMask[_ColorMask]
  42. Pass
  43. {
  44. Name "Default"
  45. CGPROGRAM
  46. #pragma vertex vert
  47. #pragma fragment frag
  48. #pragma target 2.0
  49. #include "UnityCG.cginc"
  50. #include "UnityUI.cginc"
  51. #pragma multi_compile_local _ UNITY_UI_CLIP_RECT
  52. #pragma multi_compile_local _ UNITY_UI_ALPHACLIP
  53. struct appdata_t
  54. {
  55. float4 vertex : POSITION;
  56. float4 color : COLOR;
  57. float2 texcoord : TEXCOORD0;
  58. UNITY_VERTEX_INPUT_INSTANCE_ID
  59. };
  60. struct v2f
  61. {
  62. float4 vertex : SV_POSITION;
  63. fixed4 color : COLOR;
  64. float2 texcoord : TEXCOORD0;
  65. float4 worldPosition : TEXCOORD1;
  66. half4 mask : TEXCOORD2;
  67. UNITY_VERTEX_OUTPUT_STEREO
  68. };
  69. sampler2D _MainTex;
  70. fixed4 _Color;
  71. fixed4 _TextureSampleAdd;
  72. float4 _ClipRect;
  73. float4 _MainTex_ST;
  74. float _UIMaskSoftnessX;
  75. float _UIMaskSoftnessY;
  76. float _value;
  77. float4 _overlayCol;
  78. half _Switch;
  79. v2f vert(appdata_t v)
  80. {
  81. v2f OUT;
  82. UNITY_SETUP_INSTANCE_ID(v);
  83. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(OUT);
  84. float4 vPosition = UnityObjectToClipPos(v.vertex);
  85. OUT.worldPosition = v.vertex;
  86. OUT.vertex = vPosition;
  87. float2 pixelSize = vPosition.w;
  88. pixelSize /= float2(1, 1) * abs(mul((float2x2)UNITY_MATRIX_P, _ScreenParams.xy));
  89. float4 clampedRect = clamp(_ClipRect, -2e10, 2e10);
  90. float2 maskUV = (v.vertex.xy - clampedRect.xy) / (clampedRect.zw - clampedRect.xy);
  91. OUT.texcoord = TRANSFORM_TEX(v.texcoord.xy, _MainTex);
  92. OUT.mask = half4(v.vertex.xy * 2 - clampedRect.xy - clampedRect.zw, 0.25 / (0.25 * half2(_UIMaskSoftnessX, _UIMaskSoftnessY) + abs(pixelSize.xy)));
  93. OUT.color = v.color * _Color;
  94. return OUT;
  95. }
  96. fixed4 frag(v2f IN) : SV_Target
  97. {
  98. half4 color = IN.color * (tex2D(_MainTex, IN.texcoord) + _TextureSampleAdd);
  99. #ifdef UNITY_UI_CLIP_RECT
  100. half2 m = saturate((_ClipRect.zw - _ClipRect.xy - abs(IN.mask.xy)) * IN.mask.zw);
  101. color.a *= m.x * m.y;
  102. #endif
  103. #ifdef UNITY_UI_ALPHACLIP
  104. clip(color.a - 0.001);
  105. #endif
  106. half _g = dot(color.rgb, unity_ColorSpaceLuminance);
  107. //这部分是自定义处理的
  108. if (_Switch == 1)
  109. {
  110. fixed4 col = _overlayCol;
  111. float t = abs(sin(_Time.w * _value));
  112. col.a = lerp(0, _overlayCol.a, t);
  113. color = lerp(color, col, col.a);//将col叠加在color上
  114. }
  115. return color;
  116. }
  117. ENDCG
  118. }
  119. }
  120. }

  实现原理我们可以理解为,在原有的图片上,叠加了一层会随着时间变化而变化透明度的纯色。

  和原image的shader相比,基本没做太大改动。只是为了实现闪烁效果,自定义了如下部分:

  1. //这部分是自定义处理的
  2. if (_Switch == 1)
  3. {
  4. fixed4 col = _overlayCol;
  5. float t = abs(sin(_Time.w * _value));
  6. col.a = lerp(0, _overlayCol.a, t);
  7. color = lerp(color, col, col.a);//将col叠加在color上
  8. }

2,物体闪烁

  原理相同,实现代码如下:

  1. Shader "YaDong/YydShanShader"
  2. {
  3. Properties
  4. {
  5. _MainTex("Texture", 2D) = "white" {}
  6. [Header(twinkle)]
  7. _twinkleCol("闪光色",Color) = (0.7735,0.7735,0.7735,1)
  8. _twinkleValue("闪烁 Speed",float) = 1
  9. _middleValue("中间值",float) = 0.5
  10. _volatilityValue("波动",float) = 0.6
  11. }
  12. SubShader
  13. {
  14. Tags { "RenderType" = "Opaque" }
  15. LOD 100
  16. Pass
  17. {
  18. CGPROGRAM
  19. #pragma vertex vert
  20. #pragma fragment frag
  21. // make fog work
  22. //#pragma multi_compile_fog
  23. #include "UnityCG.cginc"
  24. struct appdata
  25. {
  26. float4 vertex : POSITION;
  27. float2 uv : TEXCOORD0;
  28. };
  29. struct v2f
  30. {
  31. float2 uv : TEXCOORD0;
  32. //UNITY_FOG_COORDS(1)
  33. float4 vertex : SV_POSITION;
  34. };
  35. sampler2D _MainTex, _YydChangeInterval;
  36. float4 _MainTex_ST;
  37. float4 _twinkleCol;
  38. float _twinkleValue;
  39. float _middleValue;
  40. float _volatilityValue;
  41. v2f vert(appdata v)
  42. {
  43. v2f o;
  44. o.vertex = UnityObjectToClipPos(v.vertex);
  45. o.uv = TRANSFORM_TEX(v.uv, _MainTex);
  46. // UNITY_TRANSFER_FOG(o,o.vertex);
  47. return o;
  48. }
  49. fixed4 frag(v2f i) : SV_Target
  50. {
  51. // sample the texture
  52. fixed4 col = tex2D(_MainTex, i.uv);
  53. float t = abs(sin(_Time.w * _twinkleValue)* _volatilityValue)+ _middleValue;// +0.5;
  54. col.rgb = lerp(col.rgb, saturate(col.rgb + _twinkleCol.rgb), t);
  55. return col;
  56. }
  57. ENDCG
  58. }
  59. }
  60. }

3,纯色半透明闪烁

  与上述物体闪烁不同的是,这个只是在颜色基础上加了半透明闪烁,实现代码如下:

  1. Shader "YaDong/YydGreenShader"
  2. {
  3. Properties
  4. {
  5. _MainTex("Texture", 2D) = "white" {}
  6. _value("速度",Range(1,3)) = 1
  7. _overlayCol("叠加色",Color) = (0.2146,1,0,0.6039)
  8. }
  9. SubShader
  10. {
  11. //Tags { "RenderType" = "Opaque" }
  12. Tags { "Queue" = "Transparent" "RenderType" = "Transparent" }
  13. zWrite off
  14. blend srcAlpha one
  15. //LOD 100
  16. Pass
  17. {
  18. CGPROGRAM
  19. #pragma vertex vert
  20. #pragma fragment frag
  21. // make fog work
  22. //#pragma multi_compile_fog
  23. #include "UnityCG.cginc"
  24. struct appdata
  25. {
  26. float4 vertex : POSITION;
  27. float2 uv : TEXCOORD0;
  28. };
  29. struct v2f
  30. {
  31. float2 uv : TEXCOORD0;
  32. //UNITY_FOG_COORDS(1)
  33. float4 vertex : SV_POSITION;
  34. };
  35. sampler2D _MainTex, _YydChangeInterval;
  36. float4 _MainTex_ST;
  37. float _value;
  38. float4 _overlayCol;
  39. v2f vert(appdata v)
  40. {
  41. v2f o;
  42. o.vertex = UnityObjectToClipPos(v.vertex);
  43. o.uv = TRANSFORM_TEX(v.uv, _MainTex);
  44. // UNITY_TRANSFER_FOG(o,o.vertex);
  45. return o;
  46. }
  47. fixed4 frag(v2f i) : SV_Target
  48. {
  49. fixed4 col = _overlayCol;
  50. float t = abs(sin(_Time.w * _value));
  51. col.a = lerp(0.3, _overlayCol.a, t);
  52. return col;
  53. }
  54. ENDCG
  55. }
  56. }
  57. }

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

闽ICP备14008679号