AlphaCutoff-Alpha.shader 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. 
  2. Shader "Mirza Beig/Particles/Alpha Cutoff/Alpha Blended"
  3. {
  4. Properties
  5. {
  6. _TintColor("Tint Color", Color) = (0.5, 0.5, 0.5, 0.5)
  7. _MainTex("Particle Texture", 2D) = "white" {}
  8. _InvFade("Soft Particles Factor", Range(0.01, 8.0)) = 1.0
  9. _alphaCutoffThreshold("Alpha Cutoff Threshold", Range(-1.0, 1.0)) = 0.125
  10. [MaterialToggle] _textureBased("Texture Based", Float) = 0.0
  11. }
  12. Category
  13. {
  14. Tags { "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent" }
  15. Blend SrcAlpha OneMinusSrcAlpha
  16. AlphaTest Greater .01
  17. ColorMask RGB
  18. Cull Off Lighting Off ZWrite Off
  19. SubShader {
  20. Pass {
  21. CGPROGRAM
  22. #pragma vertex vert
  23. #pragma fragment frag
  24. #pragma multi_compile_particles
  25. #pragma multi_compile_fog
  26. #include "UnityCG.cginc"
  27. sampler2D _MainTex;
  28. fixed4 _TintColor;
  29. struct appdata_t
  30. {
  31. float4 vertex : POSITION;
  32. fixed4 color : COLOR;
  33. float2 texcoord : TEXCOORD0;
  34. };
  35. struct v2f
  36. {
  37. float4 vertex : SV_POSITION;
  38. fixed4 color : COLOR;
  39. float2 texcoord : TEXCOORD0;
  40. #ifdef SOFTPARTICLES_ON
  41. float4 projPos : TEXCOORD1;
  42. #endif
  43. UNITY_FOG_COORDS(2)
  44. };
  45. float4 _MainTex_ST;
  46. v2f vert(appdata_t v)
  47. {
  48. v2f o;
  49. o.vertex = UnityObjectToClipPos(v.vertex);
  50. #ifdef SOFTPARTICLES_ON
  51. o.projPos = ComputeScreenPos(o.vertex);
  52. COMPUTE_EYEDEPTH(o.projPos.z);
  53. #endif
  54. o.color = v.color;
  55. o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
  56. UNITY_TRANSFER_FOG(o, o.vertex);
  57. return o;
  58. }
  59. sampler2D_float _CameraDepthTexture;
  60. float _InvFade;
  61. uniform float _alphaCutoffThreshold;
  62. uniform float _textureBased;
  63. fixed4 frag(v2f i) : SV_Target
  64. {
  65. // Get texture colour.
  66. float4 texColour = tex2D(_MainTex, i.texcoord);
  67. // Tint.
  68. fixed4 colour = _TintColor * texColour;
  69. #ifdef SOFTPARTICLES_ON
  70. // Scene depth.
  71. //float sceneZ = LinearEyeDepth(tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(i.projPos)).r);
  72. float sceneZ = LinearEyeDepth(SAMPLE_DEPTH_TEXTURE_PROJ(_CameraDepthTexture, UNITY_PROJ_COORD(i.projPos)));
  73. // Distance to the camera.
  74. float partZ = i.projPos.z;
  75. // Soft particles (soft factor).
  76. // "Comparing depth values of the particle with depth values of world geometry (in view space)." - Special Effects with Depth (Siggraph, 2011).
  77. // float softFactor = saturate((depthEye - zEye) * fade)
  78. // Inverse fade: 0.0f = off.
  79. float fade = saturate(_InvFade * (sceneZ - partZ));
  80. colour.a *= fade;
  81. #endif
  82. // Input colour.
  83. colour *= i.color * 2.0f;
  84. // Alpha test. Automatic cutoff inversion detection.
  85. // Could use an if-statement, but I think that's slower?
  86. float alphaCutoffSign = sign(_alphaCutoffThreshold);
  87. clip(alphaCutoffSign * ((!_textureBased ? colour.a : texColour.a) - _alphaCutoffThreshold * alphaCutoffSign));
  88. // Fog towards nothing.
  89. UNITY_APPLY_FOG(i.fogCoord, colour);
  90. return colour;
  91. }
  92. ENDCG
  93. }
  94. }
  95. }
  96. }