diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 4465899262..2c510c4980 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -2907,7 +2907,7 @@ void Application::updateShadowMap() { { PerformanceTimer perfTimer("entities"); - _entities.render(RenderArgs::SHADOW_RENDER_MODE); + // _entities.render(RenderArgs::SHADOW_RENDER_MODE); } // render JS/scriptable overlays diff --git a/libraries/render-utils/src/directional_light.slf b/libraries/render-utils/src/directional_light.slf index 70020320c5..7a02e3c3a8 100644 --- a/libraries/render-utils/src/directional_light.slf +++ b/libraries/render-utils/src/directional_light.slf @@ -45,15 +45,13 @@ void main(void) { // compute the view space position using the depth float z = near / (depthVal * depthScale - 1.0); vec4 position = vec4((depthTexCoordOffset + gl_TexCoord[0].st * depthTexCoordScale) * z, z, 0.0); - - // get the normal from the map - vec4 normal = normalVal; + + // Light mapped or not ? if ((normalVal.a >= 0.45) && (normalVal.a <= 0.55)) { - normal.a = 1.0; - normalVal.a = 0.0; gl_FragColor = vec4(diffuseVal.rgb * specularVal.rgb, 1.0); } else { - vec3 normalizedNormal = normalize(normal.xyz * 2.0 - vec3(1.0)); + // get the normal from the map + vec3 normalizedNormal = normalize(normalVal.xyz * 2.0 - vec3(1.0)); // compute the base color based on OpenGL lighting model float diffuse = dot(normalizedNormal, gl_LightSource[0].position.xyz); @@ -67,6 +65,6 @@ void main(void) { // add specular contribution vec4 specularColor = specularVal; - gl_FragColor = vec4(baseColor.rgb + pow(specular, specularColor.a * 128.0) * specularColor.rgb, normal.a); + gl_FragColor = vec4(baseColor.rgb + pow(specular, specularColor.a * 128.0) * specularColor.rgb, normalVal.a); } } diff --git a/libraries/render-utils/src/directional_light_cascaded_shadow_map.slf b/libraries/render-utils/src/directional_light_cascaded_shadow_map.slf index c47ac7121d..b314beb6dc 100644 --- a/libraries/render-utils/src/directional_light_cascaded_shadow_map.slf +++ b/libraries/render-utils/src/directional_light_cascaded_shadow_map.slf @@ -46,36 +46,66 @@ uniform vec2 depthTexCoordOffset; uniform vec2 depthTexCoordScale; void main(void) { + float depthVal = texture2D(depthMap, gl_TexCoord[0].st).r; + vec4 normalVal = texture2D(normalMap, gl_TexCoord[0].st); + vec4 diffuseVal = texture2D(diffuseMap, gl_TexCoord[0].st); + vec4 specularVal = texture2D(specularMap, gl_TexCoord[0].st); + // compute the view space position using the depth - float z = near / (texture2D(depthMap, gl_TexCoord[0].st).r * depthScale - 1.0); + float z = near / (depthVal * depthScale - 1.0); vec4 position = vec4((depthTexCoordOffset + gl_TexCoord[0].st * depthTexCoordScale) * z, z, 1.0); // compute the index of the cascade to use and the corresponding texture coordinates int shadowIndex = int(dot(step(vec3(position.z), shadowDistances), vec3(1.0, 1.0, 1.0))); vec3 shadowTexCoord = vec3(dot(gl_EyePlaneS[shadowIndex], position), dot(gl_EyePlaneT[shadowIndex], position), dot(gl_EyePlaneR[shadowIndex], position)); - - // get the normal from the map - vec4 normal = texture2D(normalMap, gl_TexCoord[0].st); - vec4 normalizedNormal = normalize(normal * 2.0 - vec4(1.0, 1.0, 1.0, 2.0)); - - // average values from the shadow map - float diffuse = dot(normalizedNormal, gl_LightSource[0].position); - float facingLight = step(0.0, diffuse) * 0.25 * + + // evaluate the shadow test but only relevant for light facing fragments + float shadowAttenuation = (0.25 * (shadow2D(shadowMap, shadowTexCoord + vec3(-shadowScale, -shadowScale, 0.0)).r + shadow2D(shadowMap, shadowTexCoord + vec3(-shadowScale, shadowScale, 0.0)).r + shadow2D(shadowMap, shadowTexCoord + vec3(shadowScale, -shadowScale, 0.0)).r + - shadow2D(shadowMap, shadowTexCoord + vec3(shadowScale, shadowScale, 0.0)).r); - - // compute the base color based on OpenGL lighting model - vec4 baseColor = texture2D(diffuseMap, gl_TexCoord[0].st) * (gl_FrontLightModelProduct.sceneColor + - gl_FrontLightProduct[0].ambient + gl_FrontLightProduct[0].diffuse * (diffuse * facingLight)); - - // compute the specular multiplier (sans exponent) - float specular = facingLight * max(0.0, dot(normalize(gl_LightSource[0].position - normalize(vec4(position.xyz, 0.0))), - normalizedNormal)); - - // add specular contribution - vec4 specularColor = texture2D(specularMap, gl_TexCoord[0].st); - gl_FragColor = vec4(baseColor.rgb + pow(specular, specularColor.a * 128.0) * specularColor.rgb, normal.a); + shadow2D(shadowMap, shadowTexCoord + vec3(shadowScale, shadowScale, 0.0)).r)); + + // get the normal from the map + vec3 normalizedNormal = normalize(normalVal.xyz * 2.0 - vec3(1.0)); + + // how much this fragment faces the light direction + float diffuse = dot(normalizedNormal, gl_LightSource[0].position.xyz); + + // Light mapped or not ? + if ((normalVal.a >= 0.45) && (normalVal.a <= 0.55)) { + normalVal.a = 0.0; + + // 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, diffuse); + + // evaluate the shadow test but only relevant for light facing fragments + float lightAttenuation = (1 - facingLight) + facingLight * shadowAttenuation; + + // diffuse light is the lightmap dimmed by shadow + vec3 diffuseLight = lightAttenuation * specularVal.rgb; + // ambient is a tiny percentage of the lightmap and only when in the shadow + vec3 ambientLight = (1 - lightAttenuation) * 0.5 * specularVal.rgb; + + gl_FragColor = vec4(diffuseVal.rgb * (ambientLight + diffuseLight), 1.0); + } else { + + // average values from the shadow map + float facingLight = step(0.0, diffuse) * shadowAttenuation; + + // compute the base color based on OpenGL lighting model + vec3 baseColor = diffuseVal.rgb * (gl_FrontLightModelProduct.sceneColor.rgb + + gl_FrontLightProduct[0].ambient.rgb + gl_FrontLightProduct[0].diffuse.rgb * (diffuse * facingLight)); + + // compute the specular multiplier (sans exponent) + float specular = facingLight * max(0.0, dot(normalize(gl_LightSource[0].position.xyz - normalize(position.xyz)), + normalizedNormal)); + + // add specular contribution + vec4 specularColor = specularVal; + gl_FragColor = vec4(baseColor.rgb + pow(specular, specularColor.a * 128.0) * specularColor.rgb, normalVal.a); + } } diff --git a/libraries/render-utils/src/directional_light_shadow_map.slf b/libraries/render-utils/src/directional_light_shadow_map.slf index fd3d5f161f..056e866699 100644 --- a/libraries/render-utils/src/directional_light_shadow_map.slf +++ b/libraries/render-utils/src/directional_light_shadow_map.slf @@ -43,34 +43,63 @@ uniform vec2 depthTexCoordOffset; uniform vec2 depthTexCoordScale; void main(void) { + float depthVal = texture2D(depthMap, gl_TexCoord[0].st).r; + vec4 normalVal = texture2D(normalMap, gl_TexCoord[0].st); + vec4 diffuseVal = texture2D(diffuseMap, gl_TexCoord[0].st); + vec4 specularVal = texture2D(specularMap, gl_TexCoord[0].st); + // compute the view space position using the depth - float z = near / (texture2D(depthMap, gl_TexCoord[0].st).r * depthScale - 1.0); + float z = near / (depthVal * depthScale - 1.0); vec4 position = vec4((depthTexCoordOffset + gl_TexCoord[0].st * depthTexCoordScale) * z, z, 1.0); - + // compute the corresponding texture coordinates vec3 shadowTexCoord = vec3(dot(gl_EyePlaneS[0], position), dot(gl_EyePlaneT[0], position), dot(gl_EyePlaneR[0], position)); - - // get the normal from the map - vec4 normal = texture2D(normalMap, gl_TexCoord[0].st); - vec4 normalizedNormal = normalize(normal * 2.0 - vec4(1.0, 1.0, 1.0, 2.0)); - - // average values from the shadow map - float diffuse = dot(normalizedNormal, gl_LightSource[0].position); - float facingLight = step(0.0, diffuse) * 0.25 * + + // evaluate the shadow test but only relevant for light facing fragments + float shadowAttenuation = (0.25 * (shadow2D(shadowMap, shadowTexCoord + vec3(-shadowScale, -shadowScale, 0.0)).r + shadow2D(shadowMap, shadowTexCoord + vec3(-shadowScale, shadowScale, 0.0)).r + shadow2D(shadowMap, shadowTexCoord + vec3(shadowScale, -shadowScale, 0.0)).r + - shadow2D(shadowMap, shadowTexCoord + vec3(shadowScale, shadowScale, 0.0)).r); + shadow2D(shadowMap, shadowTexCoord + vec3(shadowScale, shadowScale, 0.0)).r)); + + // get the normal from the map + vec3 normalizedNormal = normalize(normalVal.xyz * 2.0 - vec3(1.0)); + + // how much this fragment faces the light direction + float diffuse = dot(normalizedNormal, gl_LightSource[0].position.xyz); + + // Light mapped or not ? + if ((normalVal.a >= 0.45) && (normalVal.a <= 0.55)) { + normalVal.a = 0.0; + + // need to catch normals perpendicular to the projection plane hence the magic number for the threshold + // it should be just 0, be we have innacurracy so we need to overshoot + const float PERPENDICULAR_THRESHOLD = -0.005; + float facingLight = step(PERPENDICULAR_THRESHOLD, diffuse); - // compute the base color based on OpenGL lighting model - vec4 baseColor = texture2D(diffuseMap, gl_TexCoord[0].st) * (gl_FrontLightModelProduct.sceneColor + - gl_FrontLightProduct[0].ambient + gl_FrontLightProduct[0].diffuse * (diffuse * facingLight)); - - // compute the specular multiplier (sans exponent) - float specular = facingLight * max(0.0, dot(normalize(gl_LightSource[0].position - normalize(vec4(position.xyz, 0.0))), - normalizedNormal)); - - // add specular contribution - vec4 specularColor = texture2D(specularMap, gl_TexCoord[0].st); - gl_FragColor = vec4(baseColor.rgb + pow(specular, specularColor.a * 128.0) * specularColor.rgb, normal.a); + // evaluate the shadow test but only relevant for light facing fragments + float lightAttenuation = (1 - facingLight) + facingLight * shadowAttenuation; + + // diffuse light is the lightmap dimmed by shadow + vec3 diffuseLight = lightAttenuation * specularVal.rgb; + // ambient is a tiny percentage of the lightmap and only when in the shadow + vec3 ambientLight = (1 - lightAttenuation) * 0.5 * specularVal.rgb; + + gl_FragColor = vec4(diffuseVal.rgb * (ambientLight + diffuseLight), 1.0); + } else { + // average values from the shadow map + float facingLight = step(0.0, diffuse) * shadowAttenuation; + + // compute the base color based on OpenGL lighting model + vec3 baseColor = diffuseVal.rgb * (gl_FrontLightModelProduct.sceneColor.rgb + + gl_FrontLightProduct[0].ambient.rgb + gl_FrontLightProduct[0].diffuse.rgb * (diffuse * facingLight)); + + // compute the specular multiplier (sans exponent) + float specular = facingLight * max(0.0, dot(normalize(gl_LightSource[0].position.xyz - normalize(position.xyz)), + normalizedNormal)); + + // add specular contribution + vec4 specularColor = specularVal; + gl_FragColor = vec4(baseColor.rgb + pow(specular, specularColor.a * 128.0) * specularColor.rgb, normalVal.a); + } }