Distortion-Add.shader 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. Shader "Mirza Beig/Particles/Distortion/Add"
  2. {
  3. Properties
  4. {
  5. _opacity ("Opacity", range(0.0, 2.0)) = 0.5
  6. _MainTex ("Distortion Texture", 2D) = "white" {}
  7. _InvFade ("Soft Particles Factor", Range(0.01, 8.0)) = 1.0
  8. _distortion ("Distortion", range(0.0, 1000.0)) = 10.0
  9. }
  10. Category
  11. {
  12. // Transparent = other objects drawn before this one.
  13. Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }
  14. Blend SrcAlpha One
  15. AlphaTest Greater .01
  16. ColorMask RGB
  17. Cull Off Lighting Off ZWrite Off
  18. SubShader {
  19. // Grab screen behind object into a texture.
  20. // Accessed in next pass in _GrabTexture.
  21. GrabPass
  22. {
  23. Name "BASE"
  24. Tags { "LightMode" = "Always" }
  25. }
  26. // Preturb texture from above using bump map.
  27. Pass
  28. {
  29. Name "BASE"
  30. Tags { "LightMode" = "Always"
  31. }
  32. CGPROGRAM
  33. #pragma vertex vert
  34. #pragma fragment frag
  35. #pragma multi_compile_particles
  36. #pragma multi_compile_fog
  37. #include "UnityCG.cginc"
  38. sampler2D _MainTex;
  39. // ...
  40. struct appdata_t
  41. {
  42. fixed4 color : COLOR;
  43. float4 vertex : POSITION;
  44. float2 uv : TEXCOORD0;
  45. };
  46. // ...
  47. struct v2f
  48. {
  49. fixed4 color : COLOR;
  50. float4 vertex : SV_POSITION;
  51. float2 uv : TEXCOORD0;
  52. #ifdef SOFTPARTICLES_ON
  53. float4 uv_depth : TEXCOORD1;
  54. #endif
  55. float4 uv_grab : TEXCOORD2;
  56. UNITY_FOG_COORDS(4)
  57. };
  58. // ...
  59. float4 _MainTex_ST;
  60. v2f vert (appdata_t v)
  61. {
  62. v2f o;
  63. o.vertex = UnityObjectToClipPos(v.vertex);
  64. #ifdef SOFTPARTICLES_ON
  65. o.uv_depth = ComputeScreenPos(o.vertex);
  66. COMPUTE_EYEDEPTH(o.uv_depth.z);
  67. #endif
  68. o.color = v.color;
  69. o.uv = TRANSFORM_TEX(v.uv, _MainTex);
  70. #if UNITY_UV_STARTS_AT_TOP
  71. float scale = -1.0;
  72. #else
  73. float scale = 1.0;
  74. #endif
  75. o.uv_grab.zw = o.vertex.zw;
  76. o.uv_grab.xy = (float2(o.vertex.x, o.vertex.y * scale) + o.vertex.w) * 0.5f;
  77. UNITY_TRANSFER_FOG(o, o.vertex);
  78. return o;
  79. }
  80. sampler2D_float _CameraDepthTexture;
  81. float _InvFade;
  82. float _distortion;
  83. sampler2D _GrabTexture;
  84. float4 _GrabTexture_TexelSize;
  85. float _opacity;
  86. // ...
  87. fixed4 frag (v2f i) : SV_Target
  88. {
  89. // Get texture alpha mask.
  90. float4 texColour = tex2D(_MainTex, i.uv);
  91. float opacity = texColour.a * _opacity;
  92. // Soft particles.
  93. #ifdef SOFTPARTICLES_ON
  94. // Scene depth.
  95. //float sceneZ = LinearEyeDepth(tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(i.uv_depth)).r);
  96. float sceneZ = LinearEyeDepth(SAMPLE_DEPTH_TEXTURE_PROJ(_CameraDepthTexture, UNITY_PROJ_COORD(i.uv_depth)));
  97. // Distance to the camera.
  98. float partZ = i.uv_depth.z;
  99. // Soft particles (soft factor).
  100. // "Comparing depth values of the particle with depth values of world geometry (in view space)." - Special Effects with Depth (Siggraph, 2011).
  101. // float softFactor = saturate((depthEye - zEye) * fade)
  102. // Inverse fade: 0.0f = off.
  103. opacity *= saturate(_InvFade * (sceneZ - partZ));
  104. #endif
  105. // Input colour.
  106. opacity *= i.color.a;
  107. //opacity *= i.color.a * 2.0f;
  108. // Distortion.
  109. texColour.rg *= texColour.b * 5;
  110. half2 bump = UnpackNormal(texColour).rg;
  111. float2 offset = bump * _distortion * _GrabTexture_TexelSize.xy;
  112. i.uv_grab.xy = offset * i.uv_grab.z + i.uv_grab.xy;
  113. half4 distortionColour = tex2Dproj(_GrabTexture, UNITY_PROJ_COORD(i.uv_grab));
  114. distortionColour.a = opacity;
  115. // Fog towards black (additive black = nothing).
  116. //UNITY_APPLY_FOG_COLOR(i.fogCoord, distortionColour, fixed4(0.0f, 0.0f, 0.0f, 0.0f));
  117. return distortionColour;
  118. }
  119. ENDCG
  120. }
  121. }
  122. }
  123. }