UnlitParticle.glsl 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #include "Uniforms.glsl"
  2. #include "Samplers.glsl"
  3. #include "Transform.glsl"
  4. #include "ScreenPos.glsl"
  5. #include "Fog.glsl"
  6. varying vec2 vTexCoord;
  7. varying vec4 vWorldPos;
  8. #ifdef VERTEXCOLOR
  9. varying vec4 vColor;
  10. #endif
  11. #ifdef SOFTPARTICLES
  12. varying vec4 vScreenPos;
  13. uniform float cSoftParticleFadeScale;
  14. #endif
  15. void VS()
  16. {
  17. mat4 modelMatrix = iModelMatrix;
  18. vec3 worldPos = GetWorldPos(modelMatrix);
  19. gl_Position = GetClipPos(worldPos);
  20. vTexCoord = GetTexCoord(iTexCoord);
  21. vWorldPos = vec4(worldPos, GetDepth(gl_Position));
  22. #ifdef SOFTPARTICLES
  23. vScreenPos = GetScreenPos(gl_Position);
  24. #endif
  25. #ifdef VERTEXCOLOR
  26. vColor = iColor;
  27. #endif
  28. }
  29. void PS()
  30. {
  31. // Get material diffuse albedo
  32. #ifdef DIFFMAP
  33. vec4 diffColor = cMatDiffColor * texture2D(sDiffMap, vTexCoord);
  34. #ifdef ALPHAMASK
  35. if (diffColor.a < 0.5)
  36. discard;
  37. #endif
  38. #else
  39. vec4 diffColor = cMatDiffColor;
  40. #endif
  41. #ifdef VERTEXCOLOR
  42. diffColor *= vColor;
  43. #endif
  44. // Get fog factor
  45. #ifdef HEIGHTFOG
  46. float fogFactor = GetHeightFogFactor(vWorldPos.w, vWorldPos.y);
  47. #else
  48. float fogFactor = GetFogFactor(vWorldPos.w);
  49. #endif
  50. // Soft particle fade
  51. // In expand mode depth test should be off. In that case do manual alpha discard test first to reduce fill rate
  52. #ifdef SOFTPARTICLES
  53. #ifdef EXPAND
  54. if (diffColor.a < 0.01)
  55. discard;
  56. #endif
  57. float particleDepth = vWorldPos.w;
  58. #ifdef HWDEPTH
  59. float depth = ReconstructDepth(texture2DProj(sDepthBuffer, vScreenPos).r);
  60. #else
  61. float depth = DecodeDepth(texture2DProj(sDepthBuffer, vScreenPos).rgb);
  62. #endif
  63. #ifdef EXPAND
  64. float diffZ = max(particleDepth - depth, 0.0) * (cFarClipPS - cNearClipPS);
  65. float fade = clamp(diffZ * cSoftParticleFadeScale, 0.0, 1.0);
  66. #else
  67. float diffZ = (depth - particleDepth) * (cFarClipPS - cNearClipPS);
  68. float fade = clamp(1.0 - diffZ * cSoftParticleFadeScale, 0.0, 1.0);
  69. #endif
  70. #ifndef ADDITIVE
  71. diffColor.a = max(diffColor.a - fade, 0.0);
  72. #else
  73. diffColor.rgb = max(diffColor.rgb - fade, vec3(0.0, 0.0, 0.0));
  74. #endif
  75. #endif
  76. gl_FragColor = vec4(GetFog(diffColor.rgb, fogFactor), diffColor.a);
  77. }