| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- /* light.glsl -- Contains everything you need to manage lights
- *
- * Copyright (c) 2025-2026 Le Juez Victor
- *
- * This software is provided 'as-is', without any express or implied warranty.
- * For conditions of distribution and use, see accompanying LICENSE file.
- */
- /* === Includes === */
- #include "../math.glsl"
- #include "../pbr.glsl"
- /* === Defines === */
- // Should be defined in client side
- //#define NUM_FORWARD_LIGHTS 8
- #define LIGHT_DIR 0
- #define LIGHT_SPOT 1
- #define LIGHT_OMNI 2
- /* === Structures === */
- struct Light {
- mat4 viewProj;
- vec3 color;
- vec3 position;
- vec3 direction;
- float specular;
- float energy;
- float range;
- float near;
- float far;
- float attenuation;
- float innerCutOff;
- float outerCutOff;
- float shadowSoftness;
- float shadowDepthBias;
- float shadowSlopeBias;
- int shadowLayer; //< less than zero if no shadows
- int type;
- };
- /* === Uniform Block === */
- #ifdef NUM_FORWARD_LIGHTS
- layout(std140) uniform LightArrayBlock {
- Light uLights[NUM_FORWARD_LIGHTS];
- int uNumLights;
- };
- #else
- layout(std140) uniform LightBlock {
- Light uLight;
- };
- #endif
- /* === Functions === */
- vec3 L_Diffuse(float cLdotH, float cNdotV, float cNdotL, float roughness)
- {
- float FD90_minus_1 = 2.0 * cLdotH * cLdotH * roughness - 0.5;
- float FdV = 1.0 + FD90_minus_1 * PBR_SchlickFresnel(cNdotV);
- float FdL = 1.0 + FD90_minus_1 * PBR_SchlickFresnel(cNdotL);
- return vec3(M_INV_PI * (FdV * FdL * cNdotL)); // Diffuse BRDF (Burley)
- }
- vec3 L_Specular(vec3 F0, float cLdotH, float cNdotH, float cNdotV, float cNdotL, float roughness)
- {
- roughness = max(roughness, 0.02); // >0.01 to avoid FP16 overflow after GGX distribution
- float alphaGGX = roughness * roughness;
- float D = PBR_DistributionGGX(cNdotH, alphaGGX);
- float G = PBR_GeometryGGX(cNdotL, cNdotV, alphaGGX);
- float cLdotH5 = PBR_SchlickFresnel(cLdotH);
- float F90 = clamp(50.0 * F0.g, 0.0, 1.0);
- vec3 F = F0 + (F90 - F0) * cLdotH5;
- return cNdotL * D * F * G; // Specular BRDF (Schlick GGX)
- }
|