Mask-Alpha.shader 3.2 KB

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