LitParticle.glsl 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #include "Uniforms.glsl"
  2. #include "Samplers.glsl"
  3. #include "Transform.glsl"
  4. #include "ScreenPos.glsl"
  5. #include "Lighting.glsl"
  6. #include "Fog.glsl"
  7. varying vec2 vTexCoord;
  8. varying vec4 vWorldPos;
  9. #ifdef VERTEXCOLOR
  10. varying vec4 vColor;
  11. #endif
  12. #ifdef SOFTPARTICLES
  13. varying vec4 vScreenPos;
  14. uniform float cSoftParticleFadeScale;
  15. #endif
  16. #ifdef PERPIXEL
  17. #ifdef SHADOW
  18. #ifndef GL_ES
  19. varying vec4 vShadowPos[NUMCASCADES];
  20. #else
  21. varying highp vec4 vShadowPos[NUMCASCADES];
  22. #endif
  23. #endif
  24. #ifdef SPOTLIGHT
  25. varying vec4 vSpotPos;
  26. #endif
  27. #ifdef POINTLIGHT
  28. varying vec3 vCubeMaskVec;
  29. #endif
  30. #else
  31. varying vec3 vVertexLight;
  32. #endif
  33. void VS()
  34. {
  35. mat4 modelMatrix = iModelMatrix;
  36. vec3 worldPos = GetWorldPos(modelMatrix);
  37. gl_Position = GetClipPos(worldPos);
  38. vTexCoord = GetTexCoord(iTexCoord);
  39. vWorldPos = vec4(worldPos, GetDepth(gl_Position));
  40. #ifdef SOFTPARTICLES
  41. vScreenPos = GetScreenPos(gl_Position);
  42. #endif
  43. #ifdef VERTEXCOLOR
  44. vColor = iColor;
  45. #endif
  46. #ifdef PERPIXEL
  47. // Per-pixel forward lighting
  48. vec4 projWorldPos = vec4(worldPos, 1.0);
  49. #ifdef SHADOW
  50. // Shadow projection: transform from world space to shadow space
  51. for (int i = 0; i < NUMCASCADES; i++)
  52. vShadowPos[i] = GetShadowPos(i, vec3(0, 0, 0), projWorldPos);
  53. #endif
  54. #ifdef SPOTLIGHT
  55. // Spotlight projection: transform from world space to projector texture coordinates
  56. vSpotPos = projWorldPos * cLightMatrices[0];
  57. #endif
  58. #ifdef POINTLIGHT
  59. vCubeMaskVec = (worldPos - cLightPos.xyz) * mat3(cLightMatrices[0][0].xyz, cLightMatrices[0][1].xyz, cLightMatrices[0][2].xyz);
  60. #endif
  61. #else
  62. // Ambient & per-vertex lighting
  63. vVertexLight = GetAmbient(GetZonePos(worldPos));
  64. #ifdef NUMVERTEXLIGHTS
  65. for (int i = 0; i < NUMVERTEXLIGHTS; ++i)
  66. vVertexLight += GetVertexLightVolumetric(i, worldPos) * cVertexLights[i * 3].rgb;
  67. #endif
  68. #endif
  69. }
  70. void PS()
  71. {
  72. // Get material diffuse albedo
  73. #ifdef DIFFMAP
  74. vec4 diffInput = texture2D(sDiffMap, vTexCoord);
  75. #ifdef ALPHAMASK
  76. if (diffInput.a < 0.5)
  77. discard;
  78. #endif
  79. vec4 diffColor = cMatDiffColor * diffInput;
  80. #else
  81. vec4 diffColor = cMatDiffColor;
  82. #endif
  83. #ifdef VERTEXCOLOR
  84. diffColor *= vColor;
  85. #endif
  86. // Get fog factor
  87. #ifdef HEIGHTFOG
  88. float fogFactor = GetHeightFogFactor(vWorldPos.w, vWorldPos.y);
  89. #else
  90. float fogFactor = GetFogFactor(vWorldPos.w);
  91. #endif
  92. // Soft particle fade
  93. // In expand mode depth test should be off. In that case do manual alpha discard test first to reduce fill rate
  94. #ifdef SOFTPARTICLES
  95. #ifdef EXPAND
  96. if (diffColor.a < 0.01)
  97. discard;
  98. #endif
  99. float particleDepth = vWorldPos.w;
  100. #ifdef HWDEPTH
  101. float depth = ReconstructDepth(texture2DProj(sDepthBuffer, vScreenPos).r);
  102. #else
  103. float depth = DecodeDepth(texture2DProj(sDepthBuffer, vScreenPos).rgb);
  104. #endif
  105. #ifdef EXPAND
  106. float diffZ = max(particleDepth - depth, 0.0) * (cFarClipPS - cNearClipPS);
  107. float fade = clamp(diffZ * cSoftParticleFadeScale, 0.0, 1.0);
  108. #else
  109. float diffZ = (depth - particleDepth) * (cFarClipPS - cNearClipPS);
  110. float fade = clamp(1.0 - diffZ * cSoftParticleFadeScale, 0.0, 1.0);
  111. #endif
  112. diffColor.a = max(diffColor.a - fade, 0.0);
  113. #endif
  114. #ifdef PERPIXEL
  115. // Per-pixel forward lighting
  116. vec3 lightColor;
  117. vec3 lightDir;
  118. vec3 finalColor;
  119. float diff = GetDiffuseVolumetric(vWorldPos.xyz);
  120. #ifdef SHADOW
  121. diff *= GetShadow(vShadowPos, vWorldPos.w);
  122. #endif
  123. #if defined(SPOTLIGHT)
  124. lightColor = vSpotPos.w > 0.0 ? texture2DProj(sLightSpotMap, vSpotPos).rgb * cLightColor.rgb : vec3(0.0, 0.0, 0.0);
  125. #elif defined(CUBEMASK)
  126. lightColor = textureCube(sLightCubeMap, vCubeMaskVec).rgb * cLightColor.rgb;
  127. #else
  128. lightColor = cLightColor.rgb;
  129. #endif
  130. finalColor = diff * lightColor * diffColor.rgb;
  131. gl_FragColor = vec4(GetLitFog(finalColor, fogFactor), diffColor.a);
  132. #else
  133. // Ambient & per-vertex lighting
  134. vec3 finalColor = vVertexLight * diffColor.rgb;
  135. gl_FragColor = vec4(GetFog(finalColor, fogFactor), diffColor.a);
  136. #endif
  137. }