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

Support shadow fade #1960

Merged
merged 14 commits into from
Jan 17, 2024
Prev Previous commit
Next Next commit
refactor: move code
zhuxudong committed Jan 17, 2024
commit 5d0aee963f1e775bb001e01e24b1cac40680d7dd
18 changes: 10 additions & 8 deletions packages/core/src/shadow/CascadedShadowCasterPass.ts
Original file line number Diff line number Diff line change
@@ -75,9 +75,9 @@ export class CascadedShadowCasterPass extends PipelinePass {
*/
override onRender(context: RenderContext): void {
const light = this._camera.scene._lightManager._sunlight;
this._updateShadowSettings(light);
this._updateShadowSettings();
this._renderDirectShadowMap(context, light);
this._updateReceiversShaderData();
this._updateReceiversShaderData(light);
}

private _renderDirectShadowMap(context: RenderContext, light: DirectLight): void {
@@ -234,12 +234,17 @@ export class CascadedShadowCasterPass extends PipelinePass {
}
}

private _updateReceiversShaderData(): void {
const scene = this._camera.scene;
private _updateReceiversShaderData(light: DirectLight): void {
const camera = this._camera;
const scene = camera.scene;
const splitBoundSpheres = this._splitBoundSpheres;
const shadowMatrices = this._shadowMatrices;
const shadowCascades = scene.shadowCascades;

const shadowFar = Math.min(scene.shadowDistance, camera.farClipPlane);
ShadowUtils.getScaleAndBiasForLinearDistanceFade(Math.pow(shadowFar, 2), scene.shadowFadeBorder, this._shadowInfos);
this._shadowInfos.x = light.shadowStrength;

// set zero matrix to project the index out of max cascade
if (shadowCascades > 1) {
for (let i = shadowCascades * 4, n = splitBoundSpheres.length; i < n; i++) {
@@ -302,7 +307,7 @@ export class CascadedShadowCasterPass extends PipelinePass {
return Math.sqrt((radius * radius) / denominator);
}

private _updateShadowSettings(light: DirectLight): void {
private _updateShadowSettings(): void {
const camera = this._camera;
const scene = camera.scene;
const shadowFormat = ShadowUtils.shadowDepthFormat(scene.shadowResolution, this._supportDepthTexture);
@@ -312,9 +317,6 @@ export class CascadedShadowCasterPass extends PipelinePass {

this._getCascadesSplitDistance(shadowFar);

ShadowUtils.getScaleAndBiasForLinearDistanceFade(Math.pow(shadowFar, 2), scene.shadowFadeBorder, this._shadowInfos);
this._shadowInfos.x = light.shadowStrength;

if (
shadowFormat !== this._shadowMapFormat ||
shadowResolution !== this._shadowMapResolution ||
2 changes: 1 addition & 1 deletion packages/core/src/shadow/ShadowUtils.ts
Original file line number Diff line number Diff line change
@@ -444,7 +444,7 @@ export class ShadowUtils {
/**
* Extract scale and bias from a fade distance to achieve a linear fading of the fade distance.
*/
static getScaleAndBiasForLinearDistanceFade(fadeDistance: number, border: number, outInfo: Vector4) {
static getScaleAndBiasForLinearDistanceFade(fadeDistance: number, border: number, outInfo: Vector4): void {
Copy link
Member

Choose a reason for hiding this comment

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

Add comments

 (P^2-N^2)/(F^2-N^2)

// To avoid division from zero
// This values ensure that fade within cascade will be 0 and outside 1
if (border < 0.0001) {