LitParticle.glsl 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. varying vec4 vWorldPos;
  8. #ifdef VERTEXCOLOR
  9. varying vec4 vColor;
  10. #endif
  11. #ifdef PERPIXEL
  12. #ifdef SHADOW
  13. varying vec4 vShadowPos[NUMCASCADES];
  14. #endif
  15. #ifdef SPOTLIGHT
  16. varying vec4 vSpotPos;
  17. #endif
  18. #ifdef POINTLIGHT
  19. varying vec3 vCubeMaskVec;
  20. #endif
  21. #else
  22. varying vec3 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. vWorldPos = vec4(worldPos, GetDepth(gl_Position));
  31. #ifdef VERTEXCOLOR
  32. vColor = iColor;
  33. #endif
  34. #ifdef PERPIXEL
  35. // Per-pixel forward lighting
  36. vec4 projWorldPos = vec4(worldPos, 1.0);
  37. #ifdef SHADOW
  38. // Shadow projection: transform from world space to shadow space
  39. for (int i = 0; i < NUMCASCADES; i++)
  40. vShadowPos[i] = GetShadowPos(i, projWorldPos);
  41. #endif
  42. #ifdef SPOTLIGHT
  43. // Spotlight projection: transform from world space to projector texture coordinates
  44. vSpotPos = cLightMatrices[0] * projWorldPos;
  45. #endif
  46. #ifdef POINTLIGHT
  47. vCubeMaskVec = mat3(cLightMatrices[0][0].xyz, cLightMatrices[0][1].xyz, cLightMatrices[0][2].xyz) * (worldPos - cLightPos.xyz);
  48. #endif
  49. #else
  50. // Ambient & per-vertex lighting
  51. vVertexLight = GetAmbient(GetZonePos(worldPos));
  52. #ifdef NUMVERTEXLIGHTS
  53. for (int i = 0; i < NUMVERTEXLIGHTS; ++i)
  54. vVertexLight += GetVertexLightVolumetric(i, worldPos) * cVertexLights[i * 3].rgb;
  55. #endif
  56. #endif
  57. }
  58. void PS()
  59. {
  60. // Get material diffuse albedo
  61. #ifdef DIFFMAP
  62. vec4 diffInput = texture2D(sDiffMap, vTexCoord);
  63. #ifdef ALPHAMASK
  64. if (diffInput.a < 0.5)
  65. discard;
  66. #endif
  67. vec4 diffColor = cMatDiffColor * diffInput;
  68. #else
  69. vec4 diffColor = cMatDiffColor;
  70. #endif
  71. #ifdef VERTEXCOLOR
  72. diffColor *= vColor;
  73. #endif
  74. // Get fog factor
  75. #ifdef HEIGHTFOG
  76. float fogFactor = GetHeightFogFactor(vWorldPos.w, vWorldPos.y);
  77. #else
  78. float fogFactor = GetFogFactor(vWorldPos.w);
  79. #endif
  80. #ifdef PERPIXEL
  81. // Per-pixel forward lighting
  82. vec3 lightColor;
  83. vec3 lightDir;
  84. vec3 finalColor;
  85. float diff = GetDiffuseVolumetric(vWorldPos.xyz);
  86. #ifdef SHADOW
  87. diff *= GetShadow(vShadowPos, vWorldPos.w);
  88. #endif
  89. #if defined(SPOTLIGHT)
  90. lightColor = vSpotPos.w > 0.0 ? texture2DProj(sLightSpotMap, vSpotPos).rgb * cLightColor.rgb : vec3(0.0, 0.0, 0.0);
  91. #elif defined(CUBEMASK)
  92. lightColor = textureCube(sLightCubeMap, vCubeMaskVec).rgb * cLightColor.rgb;
  93. #else
  94. lightColor = cLightColor.rgb;
  95. #endif
  96. finalColor = diff * lightColor * diffColor.rgb;
  97. gl_FragColor = vec4(GetLitFog(finalColor, fogFactor), diffColor.a);
  98. #else
  99. // Ambient & per-vertex lighting
  100. vec3 finalColor = vVertexLight * diffColor.rgb;
  101. gl_FragColor = vec4(GetFog(finalColor, fogFactor), diffColor.a);
  102. #endif
  103. }