LitParticle.glsl 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #include "Uniforms.glsl"
  2. #include "Samplers.glsl"
  3. #include "Transform.glsl"
  4. #include "Lighting.glsl"
  5. #include "Fog.glsl"
  6. varying vec2 vTexCoord;
  7. #ifdef HEIGHTFOG
  8. varying vec3 vWorldPos;
  9. #endif
  10. #ifdef VERTEXCOLOR
  11. varying vec4 vColor;
  12. #endif
  13. #ifdef PERPIXEL
  14. varying vec4 vLightVec;
  15. #ifdef SPOTLIGHT
  16. varying vec4 vSpotPos;
  17. #endif
  18. #ifdef POINTLIGHT
  19. varying vec3 vCubeMaskVec;
  20. #endif
  21. #else
  22. varying vec4 vVertexLight;
  23. #endif
  24. void VS()
  25. {
  26. mat4 modelMatrix = iModelMatrix;
  27. vec3 worldPos = GetWorldPos(modelMatrix);
  28. gl_Position = GetClipPos(worldPos);
  29. vTexCoord = GetTexCoord(iTexCoord);
  30. #ifdef HEIGHTFOG
  31. vWorldPos = worldPos;
  32. #endif
  33. #ifdef VERTEXCOLOR
  34. vColor = iColor;
  35. #endif
  36. #ifdef PERPIXEL
  37. // Per-pixel forward lighting
  38. vec4 projWorldPos = vec4(worldPos, 1.0);
  39. #ifdef DIRLIGHT
  40. vLightVec = vec4(cLightDir, GetDepth(gl_Position));
  41. #else
  42. vLightVec = vec4((cLightPos.xyz - worldPos) * cLightPos.w, GetDepth(gl_Position));
  43. #endif
  44. #ifdef SPOTLIGHT
  45. // Spotlight projection: transform from world space to projector texture coordinates
  46. vSpotPos = cLightMatrices[0] * projWorldPos;
  47. #endif
  48. #ifdef POINTLIGHT
  49. vCubeMaskVec = mat3(cLightMatrices[0][0].xyz, cLightMatrices[0][1].xyz, cLightMatrices[0][2].xyz) * vLightVec.xyz;
  50. #endif
  51. #else
  52. // Ambient & per-vertex lighting
  53. vVertexLight = vec4(GetAmbient(GetZonePos(worldPos)), GetDepth(gl_Position));
  54. #ifdef NUMVERTEXLIGHTS
  55. for (int i = 0; i < NUMVERTEXLIGHTS; ++i)
  56. vVertexLight.rgb += GetVertexLightVolumetric(i, worldPos) * cVertexLights[i * 3].rgb;
  57. #endif
  58. #endif
  59. }
  60. void PS()
  61. {
  62. // Get material diffuse albedo
  63. #ifdef DIFFMAP
  64. vec4 diffInput = texture2D(sDiffMap, vTexCoord);
  65. #ifdef ALPHAMASK
  66. if (diffInput.a < 0.5)
  67. discard;
  68. #endif
  69. vec4 diffColor = cMatDiffColor * diffInput;
  70. #else
  71. vec4 diffColor = cMatDiffColor;
  72. #endif
  73. #ifdef VERTEXCOLOR
  74. diffColor *= vColor;
  75. #endif
  76. #ifdef PERPIXEL
  77. // Per-pixel forward lighting
  78. vec3 lightColor;
  79. vec3 finalColor;
  80. float diff;
  81. diff = GetDiffuseVolumetric(vLightVec.xyz);
  82. #if defined(SPOTLIGHT)
  83. lightColor = vSpotPos.w > 0.0 ? texture2DProj(sLightSpotMap, vSpotPos).rgb * cLightColor.rgb : vec3(0.0, 0.0, 0.0);
  84. #elif defined(CUBEMASK)
  85. lightColor = textureCube(sLightCubeMap, vCubeMaskVec).rgb * cLightColor.rgb;
  86. #else
  87. lightColor = cLightColor.rgb;
  88. #endif
  89. finalColor = diff * lightColor * diffColor.rgb;
  90. #ifdef HEIGHTFOG
  91. float fogFactor = GetHeightFogFactor(vLightVec.w, vWorldPos.y);
  92. #else
  93. float fogFactor = GetFogFactor(vLightVec.w);
  94. #endif
  95. #ifdef AMBIENT
  96. finalColor += cAmbientColor * diffColor.rgb;
  97. gl_FragColor = vec4(GetFog(finalColor, fogFactor), diffColor.a);
  98. #else
  99. gl_FragColor = vec4(GetLitFog(finalColor, fogFactor), diffColor.a);
  100. #endif
  101. #else
  102. // Ambient & per-vertex lighting
  103. vec3 finalColor = vVertexLight.rgb * diffColor.rgb;
  104. #ifdef HEIGHTFOG
  105. float fogFactor = GetHeightFogFactor(vVertexLight.a, vWorldPos.y);
  106. #else
  107. float fogFactor = GetFogFactor(vVertexLight.a);
  108. #endif
  109. gl_FragColor = vec4(GetFog(finalColor, fogFactor), diffColor.a);
  110. #endif
  111. }