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

Addons: WebGPU CSM shadows - using shadowNode #29610

Open
wants to merge 8 commits into
base: dev
Choose a base branch
from

Conversation

aardgoose
Copy link
Contributor

Related issue: #29295

CSM implemented using shadowNode.

This uses dummy light objects to use the existing mechanism for updating the cascades shadow cameras, without injecting additional lights into the scene.

The original csm.lightDirection is replaced by the DirectionalLights direction and shadow parameters are inherited from the lights shadow object.

This issue where switching between fade and non-fade modes requires triggering a rebuild by disabling and reenabling shadows remains.

Copy link

github-actions bot commented Oct 10, 2024

📦 Bundle size

Full ESM build, minified and gzipped.

Before After Diff
WebGL 690.8
171.12
690.8
171.12
+0 B
+0 B
WebGPU 816.46
220.09
816.51
220.1
+54 B
+11 B
WebGPU Nodes 815.97
219.96
816.02
219.97
+54 B
+11 B

🌳 Bundle size after tree-shaking

Minimal build including a renderer, camera, empty scene, and dependencies.

Before After Diff
WebGL 463.45
111.9
463.45
111.9
+0 B
+0 B
WebGPU 538.64
145.42
538.69
145.44
+54 B
+20 B
WebGPU Nodes 494.75
135.28
494.81
135.31
+54 B
+20 B

@Mugen87 Mugen87 added this to the r170 milestone Oct 10, 2024
@Mugen87
Copy link
Collaborator

Mugen87 commented Oct 10, 2024

This issue where switching between fade and non-fade modes requires triggering a rebuild by disabling and reenabling shadows remains.

It seems toggling the fade option in webgl_shadowmap_csm has a different effect than in webgpu_shadowmap_csm where I can barely see any change. Is the mentioned rebuild missing?

@Mugen87
Copy link
Collaborator

Mugen87 commented Oct 10, 2024

webgpu_shadowmap_csm does not run in Firefox/WebGL. I just see the blue/gray background. The following WebGL warnings are logged:

WebGL warning: drawElementsInstanced: Buffer for uniform block is smaller than UNIFORM_BLOCK_DATA_SIZE.
After reporting 32, no further warnings will be reported for this WebGL context.

@aardgoose
Copy link
Contributor Author

I get the same errors on Chrome WebGL. Appears to be an issue with vec2[4] cascades array. Hardcoding a vec2() rather than cascades.element( n ) in the shader removes the error and is equivalent to how the original PR worked.

Looking at the issue at the moment.

@Mugen87
Copy link
Collaborator

Mugen87 commented Oct 12, 2024

The WebGL version works now 🎉 .

During testing, I have noticed that when using a WebGL backend and an orthographic camera, the shadows start with an offset:

WebGPURenderer(WebGL):

image

WebGLRenderer:

image

Fix main frustum with WebGL backend.
@Mugen87
Copy link
Collaborator

Mugen87 commented Oct 15, 2024

I have fixed the issue with the WebGL backend and orthographic cameras via b441f99.

@Mugen87
Copy link
Collaborator

Mugen87 commented Oct 15, 2024

Fading is still broken though. Not sure where the root cause is yet...

The far shadow distances also do not match with the original.

@aardgoose
Copy link
Contributor Author

What is the exact issue with fading? Apart from requiring toggling shadow on and off to get the material to rebuild.

@Mugen87
Copy link
Collaborator

Mugen87 commented Oct 16, 2024

When you turn fade one, try to move the camera away. Below are two screenshot from webgl_shadowmap_csm.

Without fading:

image

With fading:

image

In the webgpu_shadowmap_csm, the fading is missing. Even toggling shadows does not seem to have an effect.

@aardgoose
Copy link
Contributor Author

Fixed up the fading now

import { reference, uniform, int, float, vec4, vec2, If, Fn, min, renderGroup, positionView, Node, NodeUpdateType, shadow } from 'three/tsl';

const _cameraToLightMatrix = new Matrix4();
const _lightSpaceFrustum = new CSMFrustum();
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think you must create CSMFrustum depending on the backend. Otherwise the result is incorrect for WebGL since the internal vertices are not correctly configured.

@@ -8,6 +8,8 @@ class CSMFrustum {

data = data || {};

this.zNear = data.webGL === true ? - 1 : 0;
Copy link
Collaborator

@Mugen87 Mugen87 Oct 18, 2024

Choose a reason for hiding this comment

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

CSMFrustum creates internal instances of CSMFrustum but without honoring the data parameter (see line 87).

It seems data must be a property so you can pass it later to the ctor.

csmDirectionalLight.shadow.mapSize.width = 1024;
csmDirectionalLight.shadow.mapSize.height = 1024;
csmDirectionalLight.shadow.camera.near = 1;
csmDirectionalLight.shadow.camera.far = 20000;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why did you pick this extreme far value? I would expect using 1000 (params.far).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants