ClipPlane and shadow

Hi,
I run in the issue with clipPlane and shadows.
I cut off part a mesh with clipPlane and this mesh is shadow caster.
And shadow generator creates shadow for full mesh not only visible part (ignores clipPlane).

Shadow generator (light position is updated later)

const light = new SpotLight(
        Light.Shadow,
        Vector3.Zero(),
        Vector3.Zero(),
        Tools.ToRadians(35),
        0,
        scene,
    );
    light.specular = new Color3(0, 0, 0);
    light.intensity = 0;
    const generator = new ShadowGenerator(1024, light);
    generator.transparencyShadow = true;
    if (pcfFilter && scene.getEngine().webGLVersion > 1) {
        generator.filter = ShadowGenerator.FILTER_PCF;
    } else {
        generator.filter = ShadowGenerator.FILTER_BLUREXPONENTIALSHADOWMAP;
    }

Clip plane is set / updated on mesh

this.onAfterWorldMatrixUpdateObservable.add(this.updateClipPlane);
this.mesh.onBeforeRenderObservable.add(this.setPlane);
this.mesh.onAfterRenderObservable.add(this.removePlane);

Missing something, or its desired behaviour?

Thank you

Could you create a Playground so that it’s easier for us to have a look?

Thanks in advance.

Hi Hersir

The clipPlane is property of the scene , not the mesh and so you might want to use another approach.

CSG https://www.babylonjs-playground.com/#T6NP3F#0 is one way that will get you the results you want.

You are right, clipplanes are not supported in the shadow generator so far.

Anyone fancy doing a PR ? it consists in adding the

  • #include<clipPlaneVertexDeclaration>

  • #include<clipPlaneVertex>
    In the shadowMap.vertex.fx

  • #include<clipPlaneFragmentDeclaration>

  • #include<clipPlaneFragment>
    In the shadowMap.fragment.fx

as well as the Binding code in the ShadowGenerator and CSMShadowGenerator.

I am proposing only as it might be a nice and easy first contrib if somebody wants to Jump in ???

Else I ll do it tomorrow :slight_smile:

Ok seems that there are no takers, so I ll implement it :slight_smile:

PR is up and running it will be in the next nightly: Add clip planes support in shadow generation. by sebavan · Pull Request #7519 · BabylonJS/Babylon.js · GitHub

3 Likes

Thank you @sebavan, sorry for late response, was on tight deadlines :smiley:

1 Like

@sebavan, as a note - the specific case described by @Hersir doesn’t seem to be fixed (please check this PG).

@Michael_Prosser CSG wouldn’t work as the clipping has to be animated - like here https://www.babylonjs-playground.com/#XQ61ID#1.

Hi, @sebavan
sorry for bringing up old thread, but we faced this issue again :slight_smile:
and PG from @alvov is still breaking in latest version.
Is there any way to fix it ?

Thank you

1 Like

See Getting shadows to update with clip planes - #2 by Evgeni_Popov

However, onBeforeShadowMapRenderObservable and onBeforeShadowMapRenderMeshObservable are not needed, only _isReadyCustomDefines needs to be overriden:

1 Like