Unmultiply-AlphaFromLuminance.shader 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. Shader "Mirza Beig/Particles/Unmultiply/Alpha from Luminance"
  2. {
  3. Properties
  4. {
  5. _TintColor("Tint Color", Color) = (0.5, 0.5, 0.5, 0.5)
  6. _MainTex("Particle Texture", 2D) = "white" {}
  7. _InvFade("Soft Particles Factor", Range(0.01, 8.0)) = 1.0
  8. }
  9. Category
  10. {
  11. Tags { "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent" }
  12. Blend One OneMinusSrcAlpha
  13. Blend SrcAlpha OneMinusSrcAlpha
  14. ColorMask RGB
  15. Cull Off Lighting Off ZWrite Off
  16. SubShader
  17. {
  18. Pass
  19. {
  20. CGPROGRAM
  21. #pragma vertex vert
  22. #pragma fragment frag
  23. #pragma multi_compile_particles
  24. #pragma multi_compile_fog
  25. #include "UnityCG.cginc"
  26. sampler2D _MainTex;
  27. fixed4 _TintColor;
  28. struct appdata_t
  29. {
  30. float4 vertex : POSITION;
  31. fixed4 color : COLOR;
  32. float2 texcoord : TEXCOORD0;
  33. };
  34. struct v2f
  35. {
  36. float4 vertex : SV_POSITION;
  37. fixed4 color : COLOR;
  38. float2 texcoord : TEXCOORD0;
  39. #ifdef SOFTPARTICLES_ON
  40. float4 projPos : TEXCOORD1;
  41. #endif
  42. UNITY_FOG_COORDS(2)
  43. };
  44. float4 _MainTex_ST;
  45. v2f vert(appdata_t v)
  46. {
  47. v2f o;
  48. o.vertex = UnityObjectToClipPos(v.vertex);
  49. #ifdef SOFTPARTICLES_ON
  50. o.projPos = ComputeScreenPos(o.vertex);
  51. COMPUTE_EYEDEPTH(o.projPos.z);
  52. #endif
  53. o.color = v.color;
  54. o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
  55. UNITY_TRANSFER_FOG(o, o.vertex);
  56. return o;
  57. }
  58. sampler2D_float _CameraDepthTexture;
  59. float _InvFade;
  60. // ...
  61. fixed getLuminance(fixed4 colour)
  62. {
  63. float lumR = 0.2126f;
  64. float lumG = 0.7152f;
  65. float lumB = 0.0722f;
  66. float luminance =
  67. colour.r * lumR +
  68. colour.g * lumG +
  69. colour.b * lumB;
  70. return luminance;
  71. }
  72. fixed getLuminance2(fixed4 colour)
  73. {
  74. float lumR = 0.2990f;
  75. float lumG = 0.5870f;
  76. float lumB = 0.1140f;
  77. float luminance =
  78. colour.r * lumR +
  79. colour.g * lumG +
  80. colour.b * lumB;
  81. return luminance;
  82. }
  83. // Most accurate, but also slower.
  84. fixed getLuminance3(fixed4 colour)
  85. {
  86. float lumR = pow(0.2990f, 2.0f);
  87. float lumG = pow(0.5870f, 2.0f);
  88. float lumB = pow(0.1140f, 2.0f);
  89. float luminance =
  90. colour.r * lumR +
  91. colour.g * lumG +
  92. colour.b * lumB;
  93. luminance = sqrt(luminance);
  94. return luminance;
  95. }
  96. // ...
  97. fixed4 frag(v2f i) : SV_Target
  98. {
  99. // Get texture colour.
  100. float4 texColour = tex2D(_MainTex, i.texcoord);
  101. // Tint.
  102. fixed4 colour = _TintColor * texColour;
  103. #ifdef SOFTPARTICLES_ON
  104. // Scene depth.
  105. //float sceneZ = LinearEyeDepth(tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(i.projPos)).r);
  106. float sceneZ = LinearEyeDepth(SAMPLE_DEPTH_TEXTURE_PROJ(_CameraDepthTexture, UNITY_PROJ_COORD(i.projPos)));
  107. // Distance to the camera.
  108. float partZ = i.projPos.z;
  109. // Soft particles (soft factor).
  110. // "Comparing depth values of the particle with depth values of world geometry (in view space)." - Special Effects with Depth (Siggraph, 2011).
  111. // float softFactor = saturate((depthEye - zEye) * fade)
  112. // Inverse fade: 0.0f = off.
  113. float fade = saturate(_InvFade * (sceneZ - partZ));
  114. colour.a *= fade;
  115. #endif
  116. // Input colour.
  117. //colour *= i.color * 2.0f;
  118. colour *= i.color * 2.0f;
  119. // Alpha channel from luminance.
  120. //colour.a = getLuminance(colour);
  121. colour.a = getLuminance2(colour);
  122. //colour.a = getLuminance3(colour);
  123. colour.a *= i.color.a;
  124. // Fog towards nothing.
  125. UNITY_APPLY_FOG(i.fogCoord, colour);
  126. return colour;
  127. }
  128. ENDCG
  129. }
  130. }
  131. }
  132. }