diff --git a/libraries/render-utils/src/DeferredLighting.slh b/libraries/render-utils/src/DeferredLighting.slh index 62d2e1e00f..1c9f77c2e0 100755 --- a/libraries/render-utils/src/DeferredLighting.slh +++ b/libraries/render-utils/src/DeferredLighting.slh @@ -45,6 +45,9 @@ vec4 evalSphericalLight(SphericalHarmonics sh, vec3 direction ) { uniform SphericalHarmonics ambientSphere; +// Everything about light +<@include Light.slh@> + vec3 evalAmbientColor(vec3 normal, vec3 diffuse, vec3 specular, float gloss) { return diffuse.rgb * gl_FrontLightProduct[0].ambient.rgb; } @@ -55,19 +58,27 @@ vec3 evalAmbientSphereColor(vec3 normal, vec3 diffuse, vec3 specular, float glos return diffuse.rgb * ambientLight; } -vec3 evalDirectionalColor(float shadowAttenuation, vec3 position, vec3 normal, vec3 diffuse, vec3 specular, float gloss) { +// Frag Shading returns the diffuse amount as W and the specular rgb as xyz +vec4 evalFragShading(vec3 fragNormal, vec3 fragLightDir, vec3 fragEyeDir, vec3 specular, float gloss) { // Diffuse Lighting - float diffuseDot = dot(normal, gl_LightSource[0].position.xyz); - float facingLight = step(0.0, diffuseDot) * shadowAttenuation; - vec3 diffuseColor = diffuse * (gl_FrontLightProduct[0].diffuse.rgb * (diffuseDot * facingLight)); - - // compute the specular multiplier (sans exponent) - float specularPower = facingLight * max(0.0, - dot(normalize(gl_LightSource[0].position.xyz - normalize(position)), normal)); - vec3 specularColor = pow(specularPower, gloss * 128.0) * specular; + float diffuseDot = dot(fragNormal, fragLightDir); + float facingLight = step(0.0, diffuseDot); + float diffuse = diffuseDot * facingLight; + + // Specular Lighting depends on the half vector and the gloss + vec3 halfDir = normalize(fragEyeDir + fragLightDir); - // add specular contribution - return vec3(diffuseColor + specularColor); + float specularPower = facingLight * max(0.0, dot(fragEyeDir, halfDir)); + vec3 reflect = pow(specularPower, gloss * 128.0) * specular; + + return vec4(reflect, diffuse); +} + +vec3 evalDirectionalColor(float shadowAttenuation, vec3 position, vec3 normal, vec3 diffuse, vec3 specular, float gloss) { + + vec4 shading = evalFragShading(normal, gl_LightSource[0].position.xyz, normalize(position), specular, gloss); + + return vec3(diffuse + shading.rgb) * shading.w * shadowAttenuation * gl_FrontLightProduct[0].diffuse.rgb; } diff --git a/libraries/render-utils/src/directional_ambient_light.slf b/libraries/render-utils/src/directional_ambient_light.slf index 059c0e150f..a0b850e925 100755 --- a/libraries/render-utils/src/directional_ambient_light.slf +++ b/libraries/render-utils/src/directional_ambient_light.slf @@ -20,6 +20,7 @@ void main(void) { DeferredFragment frag = unpackDeferredFragment(gl_TexCoord[0].st); + // Light mapped or not ? if ((frag.normalVal.a >= 0.45) && (frag.normalVal.a <= 0.55)) { vec3 color = evalLightmappedColor( diff --git a/libraries/render-utils/src/spot_light.slf b/libraries/render-utils/src/spot_light.slf index d8bedc2bea..75c09508ae 100644 --- a/libraries/render-utils/src/spot_light.slf +++ b/libraries/render-utils/src/spot_light.slf @@ -57,10 +57,14 @@ void main(void) { discard; } - vec4 wNor = invViewMat * vec4(frag.normal, 0.0); + vec4 fragNormal = invViewMat * vec4(frag.normal, 0.0); vec4 fragEyeVector = invViewMat * vec4(-frag.position.xyz, 0.0); vec3 fragEyeDir = normalize(fragEyeVector.xyz); - vec3 wHalfDir = normalize(fragEyeDir + fragLightDir); + + vec4 shading = evalFragShading(fragNormal, fragLightDir, fragEyeDir, frag.specular, frag.gloss); + + vec3 fragColor = shading.w * (frag.diffuse + shading.xyz); + /* vec3 wHalfDir = normalize(fragEyeDir + fragLightDir); // Diffuse Lighting float diffuseDot = dot(wNor.xyz, fragLightDir); @@ -70,7 +74,7 @@ void main(void) { // compute the specular multiplier (sans exponent) float specularPower = facingLight * max(0.0, dot(fragEyeDir, wHalfDir)); vec3 specularColor = pow(specularPower, frag.gloss * 128.0) * frag.specular; - + */ // Eval angular attenuation float lightAngularAttenuation = clamp((cosSpotAngle - getLightSpotAngleCos(light)) / (1.0 - getLightSpotAngleCos(light)), 0.0, 1.0); @@ -84,7 +88,7 @@ void main(void) { float edgeCoord = exp2(-8.0*edge*edge); gl_FragColor = vec4(edgeCoord * edgeCoord * getLightColor(light), 0.0); } else { - gl_FragColor = vec4((diffuseColor + specularColor) * lightAngularAttenuation * lightAttenuation * getLightColor(light) * getLightIntensity(light), 0.0); + gl_FragColor = vec4(fragColor * lightAngularAttenuation * lightAttenuation * getLightColor(light) * getLightIntensity(light), 0.0); } }