Distortion-Alpha.shader 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. Shader "Mirza Beig/Particles/Distortion/Alpha Blended"
  2. {
  3. Properties
  4. {
  5. _Opacity("Opacity", range(0.0, 2.0)) = 0.5
  6. _Intensity("Intensity", range(0.0, 10.0)) = 1.0
  7. _Distortion("Distortion", range(0.0, 2.0)) = 0.05
  8. _MainTex("Particle Texture", 2D) = "white" {}
  9. _DistTex("Distortion Texture", 2D) = "white" {}
  10. _InvFade("Soft Particles Factor", Range(0.01, 8.0)) = 1.0
  11. }
  12. Category
  13. {
  14. // Transparent = other objects drawn before this one.
  15. Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" "PreviewType" = "Plane" }
  16. Blend SrcAlpha OneMinusSrcAlpha
  17. ColorMask RGB
  18. Cull Off Lighting Off ZWrite Off
  19. SubShader {
  20. // Grab screen behind object into a texture.
  21. // Accessed in next pass in _GrabTexture.
  22. GrabPass
  23. {
  24. Name "BASE"
  25. Tags { "LightMode" = "Always" }
  26. }
  27. // Preturb texture from above using bump map.
  28. Pass
  29. {
  30. Name "BASE"
  31. Tags { "LightMode" = "Always"
  32. }
  33. CGPROGRAM
  34. #pragma vertex vert
  35. #pragma fragment frag
  36. #pragma target 2.0
  37. #pragma multi_compile_particles
  38. #pragma multi_compile_fog
  39. #include "UnityCG.cginc"
  40. sampler2D _MainTex;
  41. sampler2D _DistTex;
  42. // ...
  43. struct appdata_t
  44. {
  45. float4 vertex : POSITION;
  46. fixed4 color : COLOR;
  47. float2 texcoord : TEXCOORD0;
  48. float2 texcoord_dist : TEXCOORD3;
  49. UNITY_VERTEX_INPUT_INSTANCE_ID
  50. };
  51. // ...
  52. struct v2f
  53. {
  54. float4 vertex : SV_POSITION;
  55. fixed4 color : COLOR;
  56. float2 texcoord : TEXCOORD0;
  57. UNITY_FOG_COORDS(1)
  58. #ifdef SOFTPARTICLES_ON
  59. float4 projPos : TEXCOORD2;
  60. #endif
  61. float2 texcoord_dist : TEXCOORD3;
  62. // Grabpass texture coordinates.
  63. float4 uvgrab : TEXCOORD4;
  64. UNITY_VERTEX_OUTPUT_STEREO
  65. };
  66. // ...
  67. float4 _MainTex_ST;
  68. float4 _DistTex_ST;
  69. float _Intensity;
  70. v2f vert (appdata_t v)
  71. {
  72. v2f o;
  73. UNITY_SETUP_INSTANCE_ID(v);
  74. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
  75. o.vertex = UnityObjectToClipPos(v.vertex);
  76. #ifdef SOFTPARTICLES_ON
  77. o.projPos = ComputeScreenPos(o.vertex);
  78. COMPUTE_EYEDEPTH(o.projPos.z);
  79. #endif
  80. // 0.5 since Unity's regular shaders are gray-tinted?
  81. // Meh, just make it the full colour. This isn't supposed
  82. // to be the same end-result. I'll take out the x2.0f in
  83. // the fragment part near the end.
  84. //o.color = v.color;
  85. o.color = v.color * _Intensity;
  86. o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
  87. o.texcoord_dist = TRANSFORM_TEX(v.texcoord_dist, _DistTex);
  88. UNITY_TRANSFER_FOG(o, o.vertex);
  89. // Grabpass stuff.
  90. o.uvgrab = ComputeGrabScreenPos(o.vertex);
  91. return o;
  92. }
  93. sampler2D_float _CameraDepthTexture;
  94. float _InvFade;
  95. float _Opacity;
  96. float _Distortion;
  97. sampler2D _GrabTexture;
  98. // ...
  99. fixed4 frag (v2f i) : SV_Target
  100. {
  101. float softParticleAlpha = 1.0;
  102. #ifdef SOFTPARTICLES_ON
  103. float sceneZ = LinearEyeDepth(SAMPLE_DEPTH_TEXTURE_PROJ(_CameraDepthTexture, UNITY_PROJ_COORD(i.projPos)));
  104. float partZ = i.projPos.z;
  105. softParticleAlpha = saturate(_InvFade * (sceneZ - partZ));
  106. #endif
  107. float inputAlpha = i.color.a;
  108. half mainTextureAlpha = tex2D(_MainTex, i.texcoord).a;
  109. half4 distortionTextureColour = tex2D(_DistTex, i.texcoord);
  110. //distortionTextureColour.rg *= distortionTextureColour.b;
  111. half2 distortion = UnpackNormal(distortionTextureColour).rg;
  112. i.uvgrab.xy += distortion * _Distortion;
  113. fixed4 grabPassTextureColourDistorted = tex2Dproj(_GrabTexture, UNITY_PROJ_COORD(i.uvgrab));
  114. fixed4 col = grabPassTextureColourDistorted;
  115. col.a = inputAlpha * mainTextureAlpha * softParticleAlpha * _Opacity;
  116. UNITY_APPLY_FOG(i.fogCoord, col);
  117. return col;
  118. }
  119. ENDCG
  120. }
  121. }
  122. }
  123. }