Update shadow shaders

This commit is contained in:
Zach Pomerantz 2016-01-14 15:46:59 -08:00
parent bc379e920b
commit 2b27e48bf9
4 changed files with 59 additions and 72 deletions

View file

@ -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);
}
<!
vec3 debugShadowMap(float shadowAttenuation, vec4 shadowTexcoord) {
vec3 colorArray[4];
colorArray[0].xyz = vec3(1.0, 1.0, 1.0);
colorArray[1].xyz = vec3(1.0, 0.0, 0.0);
colorArray[2].xyz = vec3(0.0, 1.0, 0.0);
colorArray[3].xyz = vec3(0.0, 0.0, 1.0);
vec2 offsetArray[4];
offsetArray[0] = vec2(0.0, 0.0);
offsetArray[1] = vec2(0.5, 0.0);
offsetArray[2] = vec2(0.0, 0.5);
offsetArray[3] = vec2(0.5, 0.5);
return shadowAttenuation * colorArray[int(shadowTexcoord.w)];
// return shadowAttenuation * vec3(2.0*(shadowTexcoord.xy - offsetArray[int(shadowTexcoord.w)]), 0);
}
!>
<@endif@>

View file

@ -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,

View file

@ -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,

View file

@ -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,