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 all 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
2 changes: 1 addition & 1 deletion src/scene/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ export const shadowTypeInfo = new Map([
[SHADOW_PCF5_16F, { name: 'PCF5_16F', format: PIXELFORMAT_DEPTH16, pcf: true }],
[SHADOW_VSM_16F, { name: 'VSM_16F', format: PIXELFORMAT_RGBA16F, vsm: true }],
[SHADOW_VSM_32F, { name: 'VSM_32F', format: PIXELFORMAT_RGBA32F, vsm: true }],
[SHADOW_PCSS_32F, { name: 'PCSS_32F', format: PIXELFORMAT_R32F }]
[SHADOW_PCSS_32F, { name: 'PCSS_32F', format: PIXELFORMAT_R32F, pcss: true }]
]);

/**
Expand Down
1 change: 0 additions & 1 deletion src/scene/light.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,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
12 changes: 5 additions & 7 deletions src/scene/renderer/forward-renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,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 @@ -266,16 +266,14 @@ class ForwardRenderer extends Renderer {
this.shadowCascadeCountId[cnt].setValue(directional.numCascades);
this.lightShadowIntensity[cnt].setValue(directional.shadowIntensity);

const projectionCompensation = (50.0 / lightRenderData.projectionCompensation);
const shadowRT = lightRenderData.shadowCamera.renderTarget;
if (shadowRT) {
const pixelsPerMeter = directional.penumbraSize / shadowRT.width;
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 @@ -349,7 +347,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 @@ -417,7 +415,7 @@ class ForwardRenderer extends Renderer {

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 @@ -183,7 +183,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 @@ -59,8 +59,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
53 changes: 45 additions & 8 deletions src/scene/shader-lib/chunks/lit/frag/shadowPCSS.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ 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;
Expand All @@ -55,25 +55,26 @@ float PCSSBlockerDistance(TEXTURE_ACCEPT(shadowMap), vec2 sampleCoords[PCSS_SAMP
}

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;
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);
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 @@ -89,6 +90,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 @@ -103,7 +140,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 @@ -149,7 +186,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
9 changes: 7 additions & 2 deletions src/scene/shader-lib/programs/lit-shader.js
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,7 @@ class LitShader {
const shadowInfo = shadowTypeInfo.get(shadowType);
Debug.assert(shadowInfo);
const isVsm = shadowInfo?.vsm ?? false;
const isPcss = shadowInfo?.pcss ?? false;

let code = this._fsGetBeginCode();

Expand Down Expand Up @@ -472,19 +473,23 @@ class LitShader {
// 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 (isPcss) {
// 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;
}

if (!isVsm) {
const exportR32 = shadowType === SHADOW_PCSS_32F;
const exportR32 = isPcss;

if (exportR32) {
code += ' gl_FragColor.r = depth;\n';
Expand Down
Loading