Bias mat for shadowmaps

This commit is contained in:
Lubos Lenco 2017-02-28 14:13:50 +01:00
parent 707daf8da3
commit c25868fa85
6 changed files with 56 additions and 59 deletions

View file

@ -105,7 +105,6 @@ vec3 SSSSTransmittance(float translucency, float sssWidth, vec3 worldPosition, v
#ifndef _NoShadows
float shadowTest(vec4 lPos) {
lPos.xyz /= lPos.w;
lPos.xy = lPos.xy * 0.5 + 0.5;
#ifdef _Clampstc
// Filtering out of bounds, remove

View file

@ -29,7 +29,7 @@
"links": [
{
"name": "LWVP",
"link": "_lampWorldViewProjectionMatrix"
"link": "_biasLampWorldViewProjectionMatrix"
}
],
"texture_params": [],

View file

@ -3,7 +3,7 @@
uniform sampler2D shadowMap;
float texture2DCompare(const vec2 uv, const float compare){
float depth = texture(shadowMap, uv).r; // * 2.0 - 1.0; // - mult compare instead
float depth = texture(shadowMap, uv).r;
return step(compare, depth);
}
@ -27,7 +27,6 @@ float PCF(const vec2 uv, float compare) {
// for(int y = -1; y <= 1; y++){
// vec2 off = vec2(x, y) / shadowmapSize;
// result += texture2DShadowLerp(shadowmapSize, uv + off, compare);
compare = compare * 0.5 + 0.5;
float result = texture2DShadowLerp(uv + (vec2(-1.0, -1.0) / shadowmapSize), compare);
result += texture2DShadowLerp(uv + (vec2(-1.0, 0.0) / shadowmapSize), compare);
result += texture2DShadowLerp(uv + (vec2(-1.0, 1.0) / shadowmapSize), compare);

View file

@ -64,39 +64,39 @@ float findBlocker(const vec2 uv, const float zReceiver) {
float blockerDepthSum = 0.0;
int numBlockers = 0;
// for (int i = 0; i < NUM_SAMPLES; i++) {
float shadowMapDepth = texture(shadowMap, uv + poissonDisk0 * searchRadius).r * 2.0 - 1.0;
float shadowMapDepth = texture(shadowMap, uv + poissonDisk0 * searchRadius).r;
if (shadowMapDepth < zReceiver) { blockerDepthSum += shadowMapDepth; numBlockers++; }
shadowMapDepth = texture(shadowMap, uv + poissonDisk1 * searchRadius).r * 2.0 - 1.0;
shadowMapDepth = texture(shadowMap, uv + poissonDisk1 * searchRadius).r;
if (shadowMapDepth < zReceiver) { blockerDepthSum += shadowMapDepth; numBlockers++; }
shadowMapDepth = texture(shadowMap, uv + poissonDisk2 * searchRadius).r * 2.0 - 1.0;
shadowMapDepth = texture(shadowMap, uv + poissonDisk2 * searchRadius).r;
if (shadowMapDepth < zReceiver) { blockerDepthSum += shadowMapDepth; numBlockers++; }
shadowMapDepth = texture(shadowMap, uv + poissonDisk3 * searchRadius).r * 2.0 - 1.0;
shadowMapDepth = texture(shadowMap, uv + poissonDisk3 * searchRadius).r;
if (shadowMapDepth < zReceiver) { blockerDepthSum += shadowMapDepth; numBlockers++; }
shadowMapDepth = texture(shadowMap, uv + poissonDisk4 * searchRadius).r * 2.0 - 1.0;
shadowMapDepth = texture(shadowMap, uv + poissonDisk4 * searchRadius).r;
if (shadowMapDepth < zReceiver) { blockerDepthSum += shadowMapDepth; numBlockers++; }
shadowMapDepth = texture(shadowMap, uv + poissonDisk5 * searchRadius).r * 2.0 - 1.0;
shadowMapDepth = texture(shadowMap, uv + poissonDisk5 * searchRadius).r;
if (shadowMapDepth < zReceiver) { blockerDepthSum += shadowMapDepth; numBlockers++; }
shadowMapDepth = texture(shadowMap, uv + poissonDisk6 * searchRadius).r * 2.0 - 1.0;
shadowMapDepth = texture(shadowMap, uv + poissonDisk6 * searchRadius).r;
if (shadowMapDepth < zReceiver) { blockerDepthSum += shadowMapDepth; numBlockers++; }
shadowMapDepth = texture(shadowMap, uv + poissonDisk7 * searchRadius).r * 2.0 - 1.0;
shadowMapDepth = texture(shadowMap, uv + poissonDisk7 * searchRadius).r;
if (shadowMapDepth < zReceiver) { blockerDepthSum += shadowMapDepth; numBlockers++; }
shadowMapDepth = texture(shadowMap, uv + poissonDisk8 * searchRadius).r * 2.0 - 1.0;
shadowMapDepth = texture(shadowMap, uv + poissonDisk8 * searchRadius).r;
if (shadowMapDepth < zReceiver) { blockerDepthSum += shadowMapDepth; numBlockers++; }
shadowMapDepth = texture(shadowMap, uv + poissonDisk9 * searchRadius).r * 2.0 - 1.0;
shadowMapDepth = texture(shadowMap, uv + poissonDisk9 * searchRadius).r;
if (shadowMapDepth < zReceiver) { blockerDepthSum += shadowMapDepth; numBlockers++; }
shadowMapDepth = texture(shadowMap, uv + poissonDisk10 * searchRadius).r * 2.0 - 1.0;
shadowMapDepth = texture(shadowMap, uv + poissonDisk10 * searchRadius).r;
if (shadowMapDepth < zReceiver) { blockerDepthSum += shadowMapDepth; numBlockers++; }
shadowMapDepth = texture(shadowMap, uv + poissonDisk11 * searchRadius).r * 2.0 - 1.0;
shadowMapDepth = texture(shadowMap, uv + poissonDisk11 * searchRadius).r;
if (shadowMapDepth < zReceiver) { blockerDepthSum += shadowMapDepth; numBlockers++; }
shadowMapDepth = texture(shadowMap, uv + poissonDisk12 * searchRadius).r * 2.0 - 1.0;
shadowMapDepth = texture(shadowMap, uv + poissonDisk12 * searchRadius).r;
if (shadowMapDepth < zReceiver) { blockerDepthSum += shadowMapDepth; numBlockers++; }
shadowMapDepth = texture(shadowMap, uv + poissonDisk13 * searchRadius).r * 2.0 - 1.0;
shadowMapDepth = texture(shadowMap, uv + poissonDisk13 * searchRadius).r;
if (shadowMapDepth < zReceiver) { blockerDepthSum += shadowMapDepth; numBlockers++; }
shadowMapDepth = texture(shadowMap, uv + poissonDisk14 * searchRadius).r * 2.0 - 1.0;
shadowMapDepth = texture(shadowMap, uv + poissonDisk14 * searchRadius).r;
if (shadowMapDepth < zReceiver) { blockerDepthSum += shadowMapDepth; numBlockers++; }
shadowMapDepth = texture(shadowMap, uv + poissonDisk15 * searchRadius).r * 2.0 - 1.0;
shadowMapDepth = texture(shadowMap, uv + poissonDisk15 * searchRadius).r;
if (shadowMapDepth < zReceiver) { blockerDepthSum += shadowMapDepth; numBlockers++; }
shadowMapDepth = texture(shadowMap, uv + poissonDisk16 * searchRadius).r * 2.0 - 1.0;
shadowMapDepth = texture(shadowMap, uv + poissonDisk16 * searchRadius).r;
if (shadowMapDepth < zReceiver) { blockerDepthSum += shadowMapDepth; numBlockers++; }
// }
if (numBlockers == 0) return -1.0;
@ -106,75 +106,75 @@ float findBlocker(const vec2 uv, const float zReceiver) {
float filterPCF(const vec2 uv, const float zReceiver, const float filterRadius) {
float sum = 0.0;
// for (int i = 0; i < NUM_SAMPLES; i++) {
float depth = texture(shadowMap, uv + poissonDisk0 * filterRadius).r * 2.0 - 1.0;
float depth = texture(shadowMap, uv + poissonDisk0 * filterRadius).r;
if (zReceiver <= depth) sum += 1.0;
depth = texture(shadowMap, uv + poissonDisk1 * filterRadius).r * 2.0 - 1.0;
depth = texture(shadowMap, uv + poissonDisk1 * filterRadius).r;
if (zReceiver <= depth) sum += 1.0;
depth = texture(shadowMap, uv + poissonDisk2 * filterRadius).r * 2.0 - 1.0;
depth = texture(shadowMap, uv + poissonDisk2 * filterRadius).r;
if (zReceiver <= depth) sum += 1.0;
depth = texture(shadowMap, uv + poissonDisk3 * filterRadius).r * 2.0 - 1.0;
depth = texture(shadowMap, uv + poissonDisk3 * filterRadius).r;
if (zReceiver <= depth) sum += 1.0;
depth = texture(shadowMap, uv + poissonDisk4 * filterRadius).r * 2.0 - 1.0;
depth = texture(shadowMap, uv + poissonDisk4 * filterRadius).r;
if (zReceiver <= depth) sum += 1.0;
depth = texture(shadowMap, uv + poissonDisk5 * filterRadius).r * 2.0 - 1.0;
depth = texture(shadowMap, uv + poissonDisk5 * filterRadius).r;
if (zReceiver <= depth) sum += 1.0;
depth = texture(shadowMap, uv + poissonDisk6 * filterRadius).r * 2.0 - 1.0;
depth = texture(shadowMap, uv + poissonDisk6 * filterRadius).r;
if (zReceiver <= depth) sum += 1.0;
depth = texture(shadowMap, uv + poissonDisk7 * filterRadius).r * 2.0 - 1.0;
depth = texture(shadowMap, uv + poissonDisk7 * filterRadius).r;
if (zReceiver <= depth) sum += 1.0;
depth = texture(shadowMap, uv + poissonDisk8 * filterRadius).r * 2.0 - 1.0;
depth = texture(shadowMap, uv + poissonDisk8 * filterRadius).r;
if (zReceiver <= depth) sum += 1.0;
depth = texture(shadowMap, uv + poissonDisk9 * filterRadius).r * 2.0 - 1.0;
depth = texture(shadowMap, uv + poissonDisk9 * filterRadius).r;
if (zReceiver <= depth) sum += 1.0;
depth = texture(shadowMap, uv + poissonDisk10 * filterRadius).r * 2.0 - 1.0;
depth = texture(shadowMap, uv + poissonDisk10 * filterRadius).r;
if (zReceiver <= depth) sum += 1.0;
depth = texture(shadowMap, uv + poissonDisk11 * filterRadius).r * 2.0 - 1.0;
depth = texture(shadowMap, uv + poissonDisk11 * filterRadius).r;
if (zReceiver <= depth) sum += 1.0;
depth = texture(shadowMap, uv + poissonDisk12 * filterRadius).r * 2.0 - 1.0;
depth = texture(shadowMap, uv + poissonDisk12 * filterRadius).r;
if (zReceiver <= depth) sum += 1.0;
depth = texture(shadowMap, uv + poissonDisk13 * filterRadius).r * 2.0 - 1.0;
depth = texture(shadowMap, uv + poissonDisk13 * filterRadius).r;
if (zReceiver <= depth) sum += 1.0;
depth = texture(shadowMap, uv + poissonDisk14 * filterRadius).r * 2.0 - 1.0;
depth = texture(shadowMap, uv + poissonDisk14 * filterRadius).r;
if (zReceiver <= depth) sum += 1.0;
depth = texture(shadowMap, uv + poissonDisk15 * filterRadius).r * 2.0 - 1.0;
depth = texture(shadowMap, uv + poissonDisk15 * filterRadius).r;
if (zReceiver <= depth) sum += 1.0;
depth = texture(shadowMap, uv + poissonDisk16 * filterRadius).r * 2.0 - 1.0;
depth = texture(shadowMap, uv + poissonDisk16 * filterRadius).r;
if (zReceiver <= depth) sum += 1.0;
// }
// for (int i = 0; i < NUM_SAMPLES; i++) {
depth = texture(shadowMap, uv + -poissonDisk0.yx * filterRadius).r * 2.0 - 1.0;
depth = texture(shadowMap, uv + -poissonDisk0.yx * filterRadius).r;
if (zReceiver <= depth) sum += 1.0;
depth = texture(shadowMap, uv + -poissonDisk1.yx * filterRadius).r * 2.0 - 1.0;
depth = texture(shadowMap, uv + -poissonDisk1.yx * filterRadius).r;
if (zReceiver <= depth) sum += 1.0;
depth = texture(shadowMap, uv + -poissonDisk2.yx * filterRadius).r * 2.0 - 1.0;
depth = texture(shadowMap, uv + -poissonDisk2.yx * filterRadius).r;
if (zReceiver <= depth) sum += 1.0;
depth = texture(shadowMap, uv + -poissonDisk3.yx * filterRadius).r * 2.0 - 1.0;
depth = texture(shadowMap, uv + -poissonDisk3.yx * filterRadius).r;
if (zReceiver <= depth) sum += 1.0;
depth = texture(shadowMap, uv + -poissonDisk4.yx * filterRadius).r * 2.0 - 1.0;
depth = texture(shadowMap, uv + -poissonDisk4.yx * filterRadius).r;
if (zReceiver <= depth) sum += 1.0;
depth = texture(shadowMap, uv + -poissonDisk5.yx * filterRadius).r * 2.0 - 1.0;
depth = texture(shadowMap, uv + -poissonDisk5.yx * filterRadius).r;
if (zReceiver <= depth) sum += 1.0;
depth = texture(shadowMap, uv + -poissonDisk6.yx * filterRadius).r * 2.0 - 1.0;
depth = texture(shadowMap, uv + -poissonDisk6.yx * filterRadius).r;
if (zReceiver <= depth) sum += 1.0;
depth = texture(shadowMap, uv + -poissonDisk7.yx * filterRadius).r * 2.0 - 1.0;
depth = texture(shadowMap, uv + -poissonDisk7.yx * filterRadius).r;
if (zReceiver <= depth) sum += 1.0;
depth = texture(shadowMap, uv + -poissonDisk8.yx * filterRadius).r * 2.0 - 1.0;
depth = texture(shadowMap, uv + -poissonDisk8.yx * filterRadius).r;
if (zReceiver <= depth) sum += 1.0;
depth = texture(shadowMap, uv + -poissonDisk9.yx * filterRadius).r * 2.0 - 1.0;
depth = texture(shadowMap, uv + -poissonDisk9.yx * filterRadius).r;
if (zReceiver <= depth) sum += 1.0;
depth = texture(shadowMap, uv + -poissonDisk10.yx * filterRadius).r * 2.0 - 1.0;
depth = texture(shadowMap, uv + -poissonDisk10.yx * filterRadius).r;
if (zReceiver <= depth) sum += 1.0;
depth = texture(shadowMap, uv + -poissonDisk11.yx * filterRadius).r * 2.0 - 1.0;
depth = texture(shadowMap, uv + -poissonDisk11.yx * filterRadius).r;
if (zReceiver <= depth) sum += 1.0;
depth = texture(shadowMap, uv + -poissonDisk12.yx * filterRadius).r * 2.0 - 1.0;
depth = texture(shadowMap, uv + -poissonDisk12.yx * filterRadius).r;
if (zReceiver <= depth) sum += 1.0;
depth = texture(shadowMap, uv + -poissonDisk13.yx * filterRadius).r * 2.0 - 1.0;
depth = texture(shadowMap, uv + -poissonDisk13.yx * filterRadius).r;
if (zReceiver <= depth) sum += 1.0;
depth = texture(shadowMap, uv + -poissonDisk14.yx * filterRadius).r * 2.0 - 1.0;
depth = texture(shadowMap, uv + -poissonDisk14.yx * filterRadius).r;
if (zReceiver <= depth) sum += 1.0;
depth = texture(shadowMap, uv + -poissonDisk15.yx * filterRadius).r * 2.0 - 1.0;
depth = texture(shadowMap, uv + -poissonDisk15.yx * filterRadius).r;
if (zReceiver <= depth) sum += 1.0;
depth = texture(shadowMap, uv + -poissonDisk16.yx * filterRadius).r * 2.0 - 1.0;
depth = texture(shadowMap, uv + -poissonDisk16.yx * filterRadius).r;
if (zReceiver <= depth) sum += 1.0;
// }
return sum / (2.0 * float(NUM_SAMPLES));

View file

@ -237,11 +237,11 @@ def make_forward_base(con_mesh, parse_opacity=False):
if is_shadows:
if tese != None:
tese.add_out('vec4 lampPos')
tese.add_uniform('mat4 LVP', '_lampViewProjectionMatrix')
tese.add_uniform('mat4 LVP', '_biasLampViewProjectionMatrix')
tese.write('lampPos = LVP * vec4(wposition, 1.0);')
else:
vert.add_out('vec4 lampPos')
vert.add_uniform('mat4 LWVP', '_lampWorldViewProjectionMatrix')
vert.add_uniform('mat4 LWVP', '_biasLampWorldViewProjectionMatrix')
vert.write('lampPos = LWVP * spos;')
if is_pcss:
@ -257,7 +257,6 @@ def make_forward_base(con_mesh, parse_opacity=False):
frag.write('if (receiveShadow && lampPos.w > 0.0) {')
frag.tab += 1
frag.write('vec3 lpos = lampPos.xyz / lampPos.w;')
frag.write('lpos.xy = lpos.xy * 0.5 + 0.5;')
if is_pcss:
frag.write('visibility = PCSS(lpos.xy, lpos.z - shadowsBias);')
else:

View file

@ -493,7 +493,7 @@ def init_properties():
bpy.types.Lamp.lamp_clip_start = bpy.props.FloatProperty(name="Clip Start", default=0.1)
bpy.types.Lamp.lamp_clip_end = bpy.props.FloatProperty(name="Clip End", default=50.0)
bpy.types.Lamp.lamp_fov = bpy.props.FloatProperty(name="Field of View", default=0.84)
bpy.types.Lamp.lamp_shadows_bias = bpy.props.FloatProperty(name="Bias", description="Depth offset for shadow acne", default=0.0002)
bpy.types.Lamp.lamp_shadows_bias = bpy.props.FloatProperty(name="Bias", description="Depth offset for shadow acne", default=0.0001)
bpy.types.Lamp.lamp_omni_shadows = bpy.props.BoolProperty(name="Omnidirectional Shadows", description="Fakes omnidirectional shadows by creating 6 directional lights - will result in preformance loss - usable for deferred renderers only", default=False)
if not 'Arm' in bpy.data.worlds: