Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Address PCSS softness #5483

Merged
merged 5 commits into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/scene/light.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ class LightRenderData {
this.shadowScissor = new Vec4(0, 0, 1, 1);

// depth range compensation for PCSS with directional lights
this.depthRangeCompensation = 0;
this.projectionCompensation = 0;

// face index, value is based on light type:
Expand Down
14 changes: 6 additions & 8 deletions src/scene/renderer/forward-renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function vogelSpherePrecalculationSamples(numSamples) {
const samples = [];
for (let i = 0; i < numSamples; i++) {
const weight = i / numSamples;
const radius = Math.sqrt(1.0 - weight * weight);
const radius = Math.sqrt(weight * weight);
samples.push(radius);
}
return samples;
Expand Down Expand Up @@ -258,13 +258,11 @@ class ForwardRenderer extends Renderer {
this.shadowCascadeCountId[cnt].setValue(directional.numCascades);
this.lightShadowIntensity[cnt].setValue(directional.shadowIntensity);

const projectionCompensation = (50.0 / lightRenderData.projectionCompensation);
const pixelsPerMeter = 1.0 / (lightRenderData.shadowCamera.renderTarget.width / directional.penumbraSize);
this.lightShadowSearchAreaId[cnt].setValue(pixelsPerMeter * projectionCompensation);
this.lightShadowSearchAreaId[cnt].setValue(directional.penumbraSize / lightRenderData.shadowCamera.renderTarget.width * lightRenderData.projectionCompensation);

const cameraParams = directional._shadowCameraParams;
cameraParams.length = 4;
cameraParams[0] = lightRenderData.depthRangeCompensation;
cameraParams[0] = 3.0; // unused
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mvaligursky Need to figure out what to do here, presumably. 😄

cameraParams[1] = lightRenderData.shadowCamera._farClip;
cameraParams[2] = lightRenderData.shadowCamera._nearClip;
cameraParams[3] = 1;
Expand Down Expand Up @@ -338,7 +336,7 @@ class ForwardRenderer extends Renderer {
const cameraParams = omni._shadowCameraParams;

cameraParams.length = 4;
cameraParams[0] = lightRenderData.depthRangeCompensation;
cameraParams[0] = 0; // unused
cameraParams[1] = lightRenderData.shadowCamera._farClip;
cameraParams[2] = lightRenderData.shadowCamera._nearClip;
cameraParams[3] = 0;
Expand Down Expand Up @@ -399,14 +397,14 @@ class ForwardRenderer extends Renderer {
this.lightShadowParamsId[cnt].setValue(params);
this.lightShadowIntensity[cnt].setValue(spot.shadowIntensity);

const pixelsPerMeter = 1.0 / (lightRenderData.shadowCamera.renderTarget.width / spot.penumbraSize);
const pixelsPerMeter = spot.penumbraSize / lightRenderData.shadowCamera.renderTarget.width;
const fov = lightRenderData.shadowCamera._fov * Math.PI / 180.0;
const fovRatio = 1.0 / Math.tan(fov / 2.0);
this.lightShadowSearchAreaId[cnt].setValue(pixelsPerMeter * fovRatio);

const cameraParams = spot._shadowCameraParams;
cameraParams.length = 4;
cameraParams[0] = lightRenderData.depthRangeCompensation;
cameraParams[0] = 0; // unused
cameraParams[1] = lightRenderData.shadowCamera._farClip;
cameraParams[2] = lightRenderData.shadowCamera._nearClip;
cameraParams[3] = 0;
Expand Down
1 change: 0 additions & 1 deletion src/scene/renderer/shadow-renderer-directional.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,6 @@ class ShadowRendererDirectional {
shadowCamNode.translateLocal(0, 0, depthRange.max + 0.1);
shadowCam.farClip = depthRange.max - depthRange.min + 0.2;

lightRenderData.depthRangeCompensation = shadowCam.farClip;
lightRenderData.projectionCompensation = radius;
}
}
Expand Down
2 changes: 0 additions & 2 deletions src/scene/renderer/shadow-renderer-local.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ class ShadowRendererLocal {
shadowCam.nearClip = light.attenuationEnd / 1000;
shadowCam.farClip = light.attenuationEnd;

lightRenderData.depthRangeCompensation = shadowCam.farClip - shadowCam.nearClip;

const shadowCamNode = shadowCam._node;
const lightNode = light._node;
shadowCamNode.setPosition(lightNode.getPosition());
Expand Down
59 changes: 46 additions & 13 deletions src/scene/shader-lib/chunks/lit/frag/shadowPCSS.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,11 @@ float viewSpaceDepth(float depth, mat4 invProjection) {
return viewSpace.z;
}

float PCSSBlockerDistance(TEXTURE_ACCEPT(shadowMap), vec2 sampleCoords[PCSS_SAMPLE_COUNT], vec2 shadowCoords, vec2 searchSize, float z) {
float PCSSBlockerDistance(TEXTURE_ACCEPT(shadowMap), vec2 sampleCoords[PCSS_SAMPLE_COUNT], vec2 shadowCoords, vec2 searchSize, float z, vec4 cameraParams) {

float blockers = 0.0;
float averageBlocker = 0.0;
//float radius = searchSize * ( z - )
mvaligursky marked this conversation as resolved.
Show resolved Hide resolved
for (int i = 0; i < PCSS_SAMPLE_COUNT; i++) {
vec2 offset = sampleCoords[i] * searchSize;
vec2 sampleUV = shadowCoords + offset;
Expand All @@ -64,20 +65,16 @@ float PCSSBlockerDistance(TEXTURE_ACCEPT(shadowMap), vec2 sampleCoords[PCSS_SAMP
#endif
float isBlocking = step(blocker, z);
blockers += isBlocking;
averageBlocker += blocker * isBlocking;
averageBlocker += blocker * isBlocking;
willeastcott marked this conversation as resolved.
Show resolved Hide resolved
}

if (blockers > 0.0)
return averageBlocker /= blockers;
return averageBlocker / blockers;
return -1.0;
}

float PCSS(TEXTURE_ACCEPT(shadowMap), vec3 shadowCoords, vec4 cameraParams, vec2 shadowSearchArea) {
float receiverDepth = shadowCoords.z;
#ifndef GL2
// If using packed depth on GL1, we need to normalize to get the correct receiver depth
receiverDepth *= 1.0 / (cameraParams.y - cameraParams.z);
#endif
float receiverDepth = linearizeDepth(shadowCoords.z, cameraParams);

vec2 samplePoints[PCSS_SAMPLE_COUNT];
float noise = noise( gl_FragCoord.xy ) * 2.0 * PI;
Expand All @@ -86,12 +83,12 @@ float PCSS(TEXTURE_ACCEPT(shadowMap), vec3 shadowCoords, vec4 cameraParams, vec2
samplePoints[i] = vogelDisk(i, float(PCSS_SAMPLE_COUNT), noise, pcssPresample);
}

float averageBlocker = PCSSBlockerDistance(TEXTURE_PASS(shadowMap), samplePoints, shadowCoords.xy, shadowSearchArea, receiverDepth);
float averageBlocker = PCSSBlockerDistance(TEXTURE_PASS(shadowMap), samplePoints, shadowCoords.xy, shadowSearchArea, receiverDepth, cameraParams);
if (averageBlocker == -1.0) {
return 1.0;
} else {

vec2 filterRadius = ((receiverDepth - averageBlocker) / averageBlocker) * shadowSearchArea * cameraParams.x;
float depthDifference = (receiverDepth - averageBlocker) / 3.0;
vec2 filterRadius = depthDifference * shadowSearchArea;

float shadow = 0.0;

Expand All @@ -111,6 +108,42 @@ float PCSS(TEXTURE_ACCEPT(shadowMap), vec3 shadowCoords, vec4 cameraParams, vec2
}
}

float PCSSDirectional(TEXTURE_ACCEPT(shadowMap), vec3 shadowCoords, vec4 cameraParams, vec2 shadowSearchArea) {
float receiverDepth = linearizeDepth(shadowCoords.z, cameraParams);

vec2 samplePoints[PCSS_SAMPLE_COUNT];
float noise = noise( gl_FragCoord.xy ) * 2.0 * PI;
for (int i = 0; i < PCSS_SAMPLE_COUNT; i++) {
float pcssPresample = pcssDiskSamples[i];
samplePoints[i] = vogelDisk(i, float(PCSS_SAMPLE_COUNT), noise, pcssPresample);
}

float averageBlocker = PCSSBlockerDistance(TEXTURE_PASS(shadowMap), samplePoints, shadowCoords.xy, shadowSearchArea, receiverDepth, cameraParams);
if (averageBlocker == -1.0) {
return 1.0;
} else {
float depthDifference = saturate((receiverDepth - averageBlocker) / cameraParams.x);
vec2 filterRadius = depthDifference * shadowSearchArea;

float shadow = 0.0;

for (int i = 0; i < PCSS_SAMPLE_COUNT; i ++)
{
vec2 sampleUV = samplePoints[i] * filterRadius;
sampleUV = shadowCoords.xy + sampleUV;

#ifdef GL2
float depth = texture(shadowMap, sampleUV).r;
#else // GL1
float depth = unpackFloat(texture2D(shadowMap, sampleUV));
#endif
shadow += step(receiverDepth, depth);
}
return shadow / float(PCSS_SAMPLE_COUNT);
}
}


float PCSSCubeBlockerDistance(samplerCube shadowMap, vec3 lightDirNorm, vec3 samplePoints[PCSS_SAMPLE_COUNT], float z, float shadowSearchArea) {
float blockers = 0.0;
float averageBlocker = 0.0;
Expand All @@ -129,7 +162,7 @@ float PCSSCubeBlockerDistance(samplerCube shadowMap, vec3 lightDirNorm, vec3 sam
}

if (blockers > 0.0)
return averageBlocker /= float(blockers);
return averageBlocker / blockers;
return -1.0;
}

Expand Down Expand Up @@ -179,7 +212,7 @@ float getShadowSpotPCSS(TEXTURE_ACCEPT(shadowMap), vec3 shadowCoord, vec4 shadow
}

float getShadowPCSS(TEXTURE_ACCEPT(shadowMap), vec3 shadowCoord, vec4 shadowParams, vec4 cameraParams, vec2 shadowSearchArea, vec3 lightDir) {
return PCSS(TEXTURE_PASS(shadowMap), shadowCoord, cameraParams, shadowSearchArea);
return PCSSDirectional(TEXTURE_PASS(shadowMap), shadowCoord, cameraParams, shadowSearchArea);
}

`;
6 changes: 3 additions & 3 deletions src/scene/shader-lib/chunks/lit/frag/shadowSampleCoord.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ vec3 getShadowSampleCoord$LIGHT(mat4 shadowTransform, vec4 shadowParams, vec3 wo
#ifdef SHADOW_SAMPLE_POINT
#ifdef SHADOW_SAMPLE_NORMAL_OFFSET
float distScale = length(lightDir);
surfacePosition = worldPosition + normal * shadowParams.y * clamp(1.0 - dot(normal, -lightDirNorm), 0.0, 1.0) * distScale;
surfacePosition = surfacePosition + normal * shadowParams.y * clamp(1.0 - dot(normal, -lightDirNorm), 0.0, 1.0) * distScale;
lightDir = surfacePosition - lightPos;
return lightDir;
#endif
#else
#ifdef SHADOW_SAMPLE_SOURCE_ZBUFFER
#ifdef SHADOW_SAMPLE_NORMAL_OFFSET
surfacePosition = worldPosition + normal * shadowParams.y;
surfacePosition = surfacePosition + normal * shadowParams.y;
#endif
#else
#ifdef SHADOW_SAMPLE_NORMAL_OFFSET
Expand All @@ -23,7 +23,7 @@ vec3 getShadowSampleCoord$LIGHT(mat4 shadowTransform, vec4 shadowParams, vec3 wo
#else
float distScale = abs(dot(vPositionW - lightPos, lightDirNorm));
#endif
surfacePosition = worldPosition + normal * shadowParams.y * clamp(1.0 - dot(normal, -lightDirNorm), 0.0, 1.0) * distScale;
surfacePosition = surfacePosition + normal * shadowParams.y * clamp(1.0 - dot(normal, -lightDirNorm), 0.0, 1.0) * distScale;
#endif
#endif

Expand Down
11 changes: 10 additions & 1 deletion src/scene/shader-lib/programs/lit-shader.js
Original file line number Diff line number Diff line change
Expand Up @@ -505,17 +505,23 @@ class LitShader {

const isVsm = shadowType === SHADOW_VSM8 || shadowType === SHADOW_VSM16 || shadowType === SHADOW_VSM32;
const applySlopeScaleBias = !device.webgl2 && device.extStandardDerivatives && !device.isWebGPU;
const useAbsoluteDistance = shadowType === SHADOW_PCSS;

// Use perspective depth for:
// Directional: Always since light has no position
// Spot: If not using VSM
// Point: Never
const usePerspectiveDepth = lightType === LIGHTTYPE_DIRECTIONAL || (!isVsm && lightType === LIGHTTYPE_SPOT);
const usePerspectiveDepth = (lightType === LIGHTTYPE_DIRECTIONAL || (!isVsm && lightType === LIGHTTYPE_SPOT));


// Flag if we are using non-standard depth, i.e gl_FragCoord.z
let hasModifiedDepth = false;
if (usePerspectiveDepth) {
code += " float depth = gl_FragCoord.z;\n";
if (useAbsoluteDistance) {
// Transform depth values to world space
code += " depth = linearizeDepth(depth, camera_params);\n";
}
} else {
code += " float depth = min(distance(view_position, vPositionW) / light_radius, 0.99999);\n";
hasModifiedDepth = true;
Expand All @@ -528,6 +534,9 @@ class LitShader {
}

if (usePackedDepth) {
if (useAbsoluteDistance) {
code += " depth *= 1.0 / (camera_params.y - camera_params.z);\n";
}
code += " gl_FragColor = packFloat(depth);\n";
} else if (!isVsm) {
const exportR32 = shadowType === SHADOW_PCSS;
Expand Down