RimLighting.shader 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. Shader "Custom/ShaderFX" {
  2. Properties
  3. {
  4. _MainColor("MainColor", Color) = (1,1,1,1)
  5. _MainTex("Texture", 2D) = "white" {}
  6. _Fresnel("Fresnel Intensity", Range(0,200)) = 3.0
  7. _FresnelWidth("Fresnel Width", Range(0,2)) = 3.0
  8. _Distort("Distort", Range(0, 100)) = 1.0
  9. _IntersectionThreshold("Highlight of intersection threshold", range(0,1)) = .1 //Max difference for intersections
  10. _ScrollSpeedU("Scroll U Speed",float) = 2
  11. _ScrollSpeedV("Scroll V Speed",float) = 0
  12. }
  13. SubShader
  14. {
  15. Tags{ "Queue" = "Overlay" "IgnoreProjector" = "True" "RenderType" = "Transparent" }
  16. GrabPass{ "_GrabTexture" }
  17. Pass
  18. {
  19. Lighting Off ZWrite On
  20. Blend SrcAlpha OneMinusSrcAlpha
  21. Cull Off
  22. CGPROGRAM
  23. #pragma vertex vert
  24. #pragma fragment frag
  25. #include "UnityCG.cginc"
  26. struct appdata
  27. {
  28. fixed4 vertex : POSITION;
  29. fixed4 normal : NORMAL;
  30. fixed3 uv : TEXCOORD0;
  31. };
  32. struct v2f
  33. {
  34. fixed2 uv : TEXCOORD0;
  35. fixed4 vertex : SV_POSITION;
  36. fixed3 rimColor : TEXCOORD1;
  37. fixed4 screenPos : TEXCOORD2;
  38. };
  39. sampler2D _MainTex, _CameraDepthTexture, _GrabTexture;
  40. fixed4 _MainTex_ST,_MainColor,_GrabTexture_ST, _GrabTexture_TexelSize;
  41. fixed _Fresnel, _FresnelWidth, _Distort, _IntersectionThreshold, _ScrollSpeedU, _ScrollSpeedV;
  42. v2f vert(appdata v)
  43. {
  44. v2f o;
  45. o.vertex = UnityObjectToClipPos(v.vertex);
  46. o.uv = TRANSFORM_TEX(v.uv, _MainTex);
  47. //scroll uv
  48. o.uv.x += _Time * _ScrollSpeedU;
  49. o.uv.y += _Time * _ScrollSpeedV;
  50. //fresnel
  51. fixed3 viewDir = normalize(ObjSpaceViewDir(v.vertex));
  52. fixed dotProduct = 1 - saturate(dot(v.normal, viewDir));
  53. o.rimColor = smoothstep(1 - _FresnelWidth, 1.0, dotProduct) * .5f;
  54. o.screenPos = ComputeScreenPos(o.vertex);
  55. COMPUTE_EYEDEPTH(o.screenPos.z);//eye space depth of the vertex
  56. return o;
  57. }
  58. fixed4 frag(v2f i,fixed face : VFACE) : SV_Target
  59. {
  60. //intersection
  61. fixed intersect = saturate((abs(LinearEyeDepth(tex2Dproj(_CameraDepthTexture,i.screenPos).r) - i.screenPos.z)) / _IntersectionThreshold);
  62. fixed3 main = tex2D(_MainTex, i.uv);
  63. //distortion
  64. i.screenPos.xy += (main.rg * 2 - 1) * _Distort * _GrabTexture_TexelSize.xy;
  65. fixed3 distortColor = tex2Dproj(_GrabTexture, i.screenPos);
  66. distortColor *= _MainColor * _MainColor.a + 1;
  67. //intersect hightlight
  68. i.rimColor *= intersect * clamp(0,1,face);
  69. main *= _MainColor * pow(_Fresnel,i.rimColor);
  70. //lerp distort color & fresnel color
  71. main = lerp(distortColor, main, i.rimColor.r);
  72. main += (1 - intersect) * (face > 0 ? .03 : .3) * _MainColor * _Fresnel;
  73. return fixed4(main,.9);
  74. }
  75. ENDCG
  76. }
  77. }
  78. }