<@if not DEFERRED_GLOBAL_LIGHT_SLH@> <@def DEFERRED_GLOBAL_LIGHT_SLH@> <@include DeferredLighting.slh@> struct SphericalHarmonics { vec4 L00; vec4 L1m1; vec4 L10; vec4 L11; vec4 L2m2; vec4 L2m1; vec4 L20; vec4 L21; vec4 L22; }; vec4 evalSphericalLight(SphericalHarmonics sh, vec3 direction ) { const float C1 = 0.429043; const float C2 = 0.511664; const float C3 = 0.743125; const float C4 = 0.886227; const float C5 = 0.247708; vec4 value = C1 * sh.L22 * (direction.x * direction.x - direction.y * direction.y) + C3 * sh.L20 * direction.z * direction.z + C4 * sh.L00 - C5 * sh.L20 + 2.0 * C1 * ( sh.L2m2 * direction.x * direction.y + sh.L21 * direction.x * direction.z + sh.L2m1 * direction.y * direction.z ) + 2.0 * C2 * ( sh.L11 * direction.x + sh.L1m1 * direction.y + sh.L10 * direction.z ) ; return value; } // Need one SH uniform SphericalHarmonics ambientSphere; // Everything about light <@include Light.slh@> // The view Matrix uniform mat4 invViewMat; vec3 evalAmbienGlobalColor(float shadowAttenuation, vec3 position, vec3 normal, vec3 diffuse, vec3 specular, float gloss) { // Need the light now Light light = getLight(); vec3 fragNormal = vec3(invViewMat * vec4(normal, 0.0)); vec4 fragEyeVector = invViewMat * vec4(-position, 0.0); vec3 fragEyeDir = normalize(fragEyeVector.xyz); vec3 color = diffuse.rgb * getLightColor(light) * 0.5; vec4 shading = evalFragShading(fragNormal, -getLightDirection(light), fragEyeDir, specular, gloss); color += vec3(diffuse + shading.rgb) * shading.w * shadowAttenuation * getLightColor(light) * getLightIntensity(light); return color; } vec3 evalAmbienSphereGlobalColor(float shadowAttenuation, vec3 position, vec3 normal, vec3 diffuse, vec3 specular, float gloss) { // Need the light now Light light = getLight(); vec3 fragNormal = vec3(invViewMat * vec4(normal, 0.0)); vec4 fragEyeVector = invViewMat * vec4(-position, 0.0); vec3 fragEyeDir = normalize(fragEyeVector.xyz); // TODO: The world space normal doesn;t seem to work properly with the current SH definitions // FoOr now, we use the normal in view space vec3 ambientNormal = normal; vec3 color = diffuse.rgb * 0.5 * evalSphericalLight(ambientSphere, ambientNormal).xyz; vec4 shading = evalFragShading(fragNormal, -getLightDirection(light), fragEyeDir, specular, gloss); color += vec3(diffuse + shading.rgb) * shading.w * shadowAttenuation * getLightColor(light) * getLightIntensity(light); return color; } vec3 evalLightmappedColor(float shadowAttenuation, vec3 normal, vec3 diffuse, vec4 lightmap) { float diffuseDot = dot(normal, gl_LightSource[0].position.xyz); // need to catch normals perpendicular to the projection plane hence the magic number for the threshold // it should be just 0, but we have innacurracy so we need to overshoot const float PERPENDICULAR_THRESHOLD = -0.005; float facingLight = step(PERPENDICULAR_THRESHOLD, diffuseDot); //float normalMapContrib = (lightmap.w != 0.0 ? diffuseDot * abs(2*lightmap.w - 1.0) : 1.0); // float normalMapContrib = (lightmap.w != 0.0 ? diffuseDot * abs(2*lightmap.w - 1.0) : 1.0); float normalMapContrib = 2.0 * (lightmap.w - 0.5); // evaluate the shadow test but only relevant for light facing fragments float lightAttenuation = (1 - facingLight) + facingLight * shadowAttenuation; // lightAttenuation *= (abs(normalMapContrib) * max(0, diffuseDot) + (1.0 - abs(normalMapContrib))); lightAttenuation *= (abs(normalMapContrib) * max(0, diffuseDot)); // diffuse light is the lightmap dimmed by shadow vec3 diffuseLight = lightAttenuation * lightmap.rgb; // ambient is a tiny percentage of the lightmap and only when in the shadow vec3 ambientLight = (1 - lightAttenuation) * 0.5 * lightmap.rgb; // return normal; return diffuse * (ambientLight + diffuseLight); // return lightmap.rgb; } <@endif@>