diff --git a/libraries/render-utils/src/Shadow.slh b/libraries/render-utils/src/Shadow.slh index 525abf60b7..850be95cde 100755 --- a/libraries/render-utils/src/Shadow.slh +++ b/libraries/render-utils/src/Shadow.slh @@ -14,35 +14,46 @@ // the shadow texture uniform sampler2DShadow shadowMap; -struct EyePlanes { - vec4 _S[1]; - vec4 _T[1]; - vec4 _R[1]; - vec4 _Q[1]; +struct ShadowTransform { + mat4 projection; + mat4 view; + + float bias; + float scale; }; -uniform eyePlanes { - EyePlanes _eyePlanes; +uniform shadowTransformBuffer { + ShadowTransform _shadowTransform; }; -EyePlanes getEyePlanes() { - return _eyePlanes; +mat4 getShadowView() { + return _shadowTransform.view; } +mat4 getShadowProjection() { + return _shadowTransform.projection; +} + +float getShadowScale() { + return 1.0; + //_shadowTransform.scale; +} + +// Compute the texture coordinates from world coordinates +vec4 evalShadowTexcoord(vec4 position) { + mat4 bias = mat4( + 0.5, 0.0, 0.0, 0.0, + 0.0, 0.5, 0.0, 0.0, + 0.0, 0.0, 0.5, 0.0, + 0.5, 0.5, 0.5, 1.0); + return bias * getShadowProjection() * inverse(getShadowView()) * position; +} // Fetching it -float fetchShadow(vec3 texcoord) { - return texture(shadowMap, texcoord); +float fetchShadow(vec3 shadowTexcoord) { + return texture(shadowMap, shadowTexcoord); } -// the distances to the cascade sections -uniform vec3 shadowDistances; - -// the inverse of the size of the shadow map -uniform float shadowScale; - -uniform mat4 shadowMatrices[4]; - vec2 samples[8] = vec2[8]( vec2(-2.0, -2.0), vec2(2.0, -2.0), @@ -54,28 +65,10 @@ vec2 samples[8] = vec2[8]( vec2(0.0, -1.0) ); -vec4 evalShadowTexcoord(vec4 position) { - // compute the corresponding texture coordinates - EyePlanes eyePlanes = getEyePlanes(); - vec3 shadowTexcoord = vec3(dot(eyePlanes._S[0], position), dot(eyePlanes._T[0], position), dot(eyePlanes._R[0], position)); - return vec4(shadowTexcoord, 0.0); -} - -vec4 evalCascadedShadowTexcoord(vec4 position) { - EyePlanes eyePlanes = getEyePlanes(); - - // 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(eyePlanes._S[shadowIndex], position), - dot(eyePlanes._T[shadowIndex], position), - dot(eyePlanes._R[shadowIndex], position)); - - return vec4(shadowTexcoord, shadowIndex); -} - float evalShadowAttenuationPCF(vec4 shadowTexcoord) { float radiusScale = (shadowTexcoord.w + 1.0); + float shadowScale = getShadowScale(); + float shadowAttenuation = (0.25 * ( fetchShadow(shadowTexcoord.xyz + radiusScale * shadowScale * vec3(samples[0], 0.0)) + fetchShadow(shadowTexcoord.xyz + radiusScale * shadowScale * vec3(samples[1], 0.0)) + @@ -98,36 +91,24 @@ float evalShadowAttenuationPCF(vec4 shadowTexcoord) { float evalShadowAttenuationBasic(vec4 shadowTexcoord) { float radiusScale = 0.5; + float shadowScale = getShadowScale(); + float shadowAttenuation = (0.25 * ( fetchShadow(shadowTexcoord.xyz + radiusScale * shadowScale * vec3(samples[0], 0.0)) + fetchShadow(shadowTexcoord.xyz + radiusScale * shadowScale * vec3(samples[1], 0.0)) + fetchShadow(shadowTexcoord.xyz + radiusScale * shadowScale * vec3(samples[2], 0.0)) + fetchShadow(shadowTexcoord.xyz + radiusScale * shadowScale * vec3(samples[3], 0.0)) )); + return shadowAttenuation; } -float evalShadowAttenuation(vec4 shadowTexcoord) { - return evalShadowAttenuationBasic(shadowTexcoord); +float evalShadowAttenuation(vec4 position) { + vec4 shadowTexcoord = evalShadowTexcoord(position); + + return fetchShadow(shadowTexcoord.xyz); + // return evalShadowAttenuationBasic(shadowTexcoord); + // return evalShadowAttenuationPCF(shadowTexcoord); } - - <@endif@> diff --git a/libraries/render-utils/src/directional_ambient_light.slf b/libraries/render-utils/src/directional_ambient_light.slf index a905dbdfd5..5b936f566f 100755 --- a/libraries/render-utils/src/directional_ambient_light.slf +++ b/libraries/render-utils/src/directional_ambient_light.slf @@ -12,9 +12,8 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // -// Everything about deferred buffer +<@include Shadow.slh@> <@include DeferredBuffer.slh@> - <@include DeferredGlobalLight.slh@> <$declareEvalLightmappedColor()$> @@ -27,10 +26,13 @@ void main(void) { DeferredTransform deferredTransform = getDeferredTransform(); DeferredFragment frag = unpackDeferredFragment(deferredTransform, _texCoord0); + vec4 worldPos = deferredTransform.viewInverse * vec4(frag.position.xyz, 1.0); + float shadowAttenuation = evalShadowAttenuation(worldPos); + if ((frag.normalVal.a >= 0.45) && (frag.normalVal.a <= 0.55)) { vec3 color = evalLightmappedColor( deferredTransform.viewInverse, - 1.0, + shadowAttenuation, frag.normal, frag.diffuse, frag.specularVal.xyz); @@ -38,7 +40,7 @@ void main(void) { } else { vec3 color = evalAmbientSphereGlobalColor( deferredTransform.viewInverse, - 1.0, + shadowAttenuation, frag.position.xyz, frag.normal, frag.diffuse, diff --git a/libraries/render-utils/src/directional_light.slf b/libraries/render-utils/src/directional_light.slf index eff40df255..f057f9cc35 100644 --- a/libraries/render-utils/src/directional_light.slf +++ b/libraries/render-utils/src/directional_light.slf @@ -12,9 +12,8 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // -// Everything about deferred buffer +<@include Shadow.slh@> <@include DeferredBuffer.slh@> - <@include DeferredGlobalLight.slh@> <$declareEvalLightmappedColor()$> @@ -27,11 +26,14 @@ void main(void) { DeferredTransform deferredTransform = getDeferredTransform(); DeferredFragment frag = unpackDeferredFragment(deferredTransform, _texCoord0); + vec4 worldPos = deferredTransform.viewInverse * vec4(frag.position.xyz, 1.0); + float shadowAttenuation = evalShadowAttenuation(worldPos); + // Light mapped or not ? if ((frag.normalVal.a >= 0.45) && (frag.normalVal.a <= 0.55)) { vec3 color = evalLightmappedColor( deferredTransform.viewInverse, - 1.0, + shadowAttenuation, frag.normal, frag.diffuse, frag.specularVal.xyz); @@ -39,7 +41,7 @@ void main(void) { } else { vec3 color = evalAmbientGlobalColor( deferredTransform.viewInverse, - 1.0, + shadowAttenuation, frag.position.xyz, frag.normal, frag.diffuse, diff --git a/libraries/render-utils/src/directional_skybox_light.slf b/libraries/render-utils/src/directional_skybox_light.slf index ca3efef047..871cb32337 100755 --- a/libraries/render-utils/src/directional_skybox_light.slf +++ b/libraries/render-utils/src/directional_skybox_light.slf @@ -12,9 +12,8 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html //!> -// Everything about deferred buffer +<@include Shadow.slh@> <@include DeferredBuffer.slh@> - <@include DeferredGlobalLight.slh@> <$declareEvalLightmappedColor()$> @@ -27,11 +26,14 @@ void main(void) { DeferredTransform deferredTransform = getDeferredTransform(); DeferredFragment frag = unpackDeferredFragment(deferredTransform, _texCoord0); + vec4 worldPos = deferredTransform.viewInverse * vec4(frag.position.xyz, 1.0); + float shadowAttenuation = evalShadowAttenuation(worldPos); + // Light mapped or not ? if ((frag.normalVal.a >= 0.45) && (frag.normalVal.a <= 0.55)) { vec3 color = evalLightmappedColor( deferredTransform.viewInverse, - 1.0, + shadowAttenuation, frag.normal, frag.diffuse, frag.specularVal.xyz); @@ -39,7 +41,7 @@ void main(void) { } else { vec3 color = evalSkyboxGlobalColor( deferredTransform.viewInverse, - 1.0, + shadowAttenuation, frag.position.xyz, frag.normal, frag.diffuse,