当前位置:   article > 正文

Unity Shader 流光 边缘光_unity ui外边框发光shader

unity ui外边框发光shader

前言

Unity2021.3.23

一、实现原理

Time控制UV的变化,再采样一张流光贴图.即可实现流光效果。

二、效果及源码展示

1.流光效果

效果描述:
1.边缘光(菲尼尔),
2.从上到下扫描光.

效果图如下:

代码如下:

  1. Shader "Unlit/ScanCode"
  2. {
  3. Properties
  4. {
  5. _MainTex ("Texture", 2D) = "white" {}
  6. _RimMin("RimMin",Range(-1,1)) = 0.0
  7. _RimMax("RimMax",Range(0,2)) = 1.0
  8. _InnerColor("InnerColor",Color) = (0.0, 0.0, 0.0 ,0.0)
  9. _RimColor("Rim Color", Color) = (1,1,1,1)
  10. _RimIntensity("Rim Intensity", Float) = 1.0
  11. _FlowTilling("Flow Tilling",Vector) = (1,1,0,0)
  12. _FlowSpeed("Flow Speed", Vector)= (1,1,0,0)
  13. _FlowTex("Flow Tex", 2D) = "white"{}
  14. _FlowIntensity("Flow Intensity",Float) = 0.5
  15. _InnerAlpha("Inner Alpha",Range(0.0,1.0)) = 0.0
  16. }
  17. SubShader
  18. {
  19. //Tags { "RenderType"="Opaque" }
  20. Tags { "Queue"="Transparent" }
  21. LOD 100
  22. Pass
  23. {
  24. ZWrite off
  25. Blend SrcAlpha One
  26. CGPROGRAM
  27. #pragma vertex vert
  28. #pragma fragment frag
  29. #include "UnityCG.cginc"
  30. struct appdata
  31. {
  32. float4 vertex : POSITION;
  33. float2 texcoord : TEXCOORD0;
  34. float3 normal : NORMAL;
  35. };
  36. struct v2f
  37. {
  38. float2 uv : TEXCOORD0;
  39. float4 vertex : SV_POSITION;
  40. float3 pos_world :TEXCOORD1;
  41. float3 normal_world : TEXCOORD2;
  42. //轴点的世界空间坐标
  43. float3 pivot_world :TEXCOORD3;
  44. };
  45. sampler2D _MainTex;
  46. float4 _MainTex_ST;
  47. float _RimMin;
  48. float _RimMax;
  49. float4 _InnerColor;
  50. float4 _RimColor;
  51. float4 _RimIntensity;
  52. float4 _FlowTilling;
  53. float4 _FlowSpeed;
  54. sampler2D _FlowTex;
  55. float _FlowIntensity;
  56. float _InnerAlpha;
  57. v2f vert (appdata v)
  58. {
  59. v2f o;
  60. o.vertex = UnityObjectToClipPos(v.vertex);
  61. //float4(v.normal,0.0)如果是向量,后面补0.0
  62. float3 normal_world = mul(float4(v.normal,0.0), unity_WorldToObject);
  63. float3 pos_world = mul(unity_ObjectToWorld,v.vertex).xyz;
  64. o.normal_world = normalize(normal_world);
  65. o.pos_world = pos_world;
  66. //三维向量变成一个四维向量,后面补1.0,
  67. o.pivot_world = mul(unity_ObjectToWorld,float4(0.0, 0.0, 0.0, 1.0));
  68. o.uv = v.texcoord;
  69. return o;
  70. }
  71. fixed4 frag (v2f i) : SV_Target
  72. {
  73. //1.边缘光
  74. half3 normal_world = normalize(i.normal_world);
  75. //视线方向的向量,先拿到世界空间上的摄像机的位置,然后归一化。
  76. //精度上,向量用half3就够了。片元阶段要非常注意性能
  77. half3 view_world = normalize(_WorldSpaceCameraPos.xyz -i.pos_world);
  78. //限制在0-1的范围内
  79. half NdotV = saturate(dot(normal_world, view_world));
  80. half fresnel = 1.0 - NdotV;
  81. fresnel = smoothstep(_RimMin,_RimMax, fresnel);
  82. //自发光
  83. half emiss = tex2D(_MainTex, i.uv).r;
  84. emiss = pow(emiss, 5.0);
  85. half final_fresnel = saturate(fresnel + emiss);
  86. //设置颜色
  87. half3 final_rim_color = lerp(_InnerColor.xyz, _RimColor.xyz *_RimIntensity, final_fresnel);
  88. half final_rim_alpha = final_fresnel;
  89. //2.流光
  90. half2 uv_flow = (i.pos_world.xy - i.pivot_world.xy) * _FlowTilling.xy;
  91. uv_flow =uv_flow + _Time.y * _FlowSpeed.xy;
  92. float4 flow_rgba = tex2D(_FlowTex, uv_flow) * _FlowIntensity;
  93. //整合
  94. float3 final_col = final_rim_color + flow_rgba.xyz;
  95. float final_alpha = saturate(final_rim_alpha + flow_rgba.a + _InnerAlpha);
  96. return float4(final_col,final_alpha);
  97. }
  98. ENDCG
  99. }
  100. }
  101. }

(i.pos_world.xy - i.pivot_world.xy)
用于计算UV位置,达到从上到下的扫描效果.

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

闽ICP备14008679号