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

Add support for clip planes to the depth renderer #12685

Merged
merged 1 commit into from
Jun 28, 2022
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
35 changes: 35 additions & 0 deletions packages/dev/core/src/Rendering/depthRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,9 @@ export class DepthRenderer {
}
}

// Clip planes
MaterialHelper.BindClipPlane(effect, scene);

// Morph targets
MaterialHelper.BindMorphTargetParameters(renderingMesh, effect);
if (renderingMesh.morphTargetManager && renderingMesh.morphTargetManager.isUsingTextureForTargets) {
Expand Down Expand Up @@ -332,6 +335,7 @@ export class DepthRenderer {
public isReady(subMesh: SubMesh, useInstances: boolean): boolean {
const engine = this._scene.getEngine();
const mesh = subMesh.getMesh();
const scene = mesh.getScene();

const renderingMaterial = mesh._internalAbstractMeshDataInfo._materialForRenderPass?.[engine.currentRenderPassId];

Expand Down Expand Up @@ -418,6 +422,31 @@ export class DepthRenderer {
defines.push("#define PACKED");
}

// Clip planes
if (scene.clipPlane) {
defines.push("#define CLIPPLANE");
}

if (scene.clipPlane2) {
defines.push("#define CLIPPLANE2");
}

if (scene.clipPlane3) {
defines.push("#define CLIPPLANE3");
}

if (scene.clipPlane4) {
defines.push("#define CLIPPLANE4");
}

if (scene.clipPlane5) {
defines.push("#define CLIPPLANE5");
}

if (scene.clipPlane6) {
defines.push("#define CLIPPLANE6");
}

// Get correct effect
const drawWrapper = subMesh._getDrawWrapper(undefined, true)!;
const cachedDefines = drawWrapper.defines;
Expand All @@ -437,6 +466,12 @@ export class DepthRenderer {
"morphTargetInfluences",
"morphTargetTextureInfo",
"morphTargetTextureIndices",
"vClipPlane",
"vClipPlane2",
"vClipPlane3",
"vClipPlane4",
"vClipPlane5",
"vClipPlane6",
],
["diffuseSampler", "morphTargets", "boneSampler"],
join,
Expand Down
3 changes: 3 additions & 0 deletions packages/dev/core/src/Shaders/depth.fragment.fx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
varying vec2 vUV;
uniform sampler2D diffuseSampler;
#endif
#include<clipPlaneFragmentDeclaration>

varying float vDepthMetric;

Expand All @@ -14,6 +15,8 @@ varying float vDepthMetric;

void main(void)
{
#include<clipPlaneFragment>

#ifdef ALPHATEST
if (texture2D(diffuseSampler, vUV).a < 0.4)
discard;
Expand Down
6 changes: 5 additions & 1 deletion packages/dev/core/src/Shaders/depth.vertex.fx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ attribute vec3 position;
#include<morphTargetsVertexGlobalDeclaration>
#include<morphTargetsVertexDeclaration>[0..maxSimultaneousMorphTargets]

#include<clipPlaneVertexDeclaration>

// Uniform
#include<instancesDeclaration>

Expand Down Expand Up @@ -43,7 +45,9 @@ void main(void)
#include<bonesVertex>
#include<bakedVertexAnimation>

gl_Position = viewProjection * finalWorld * vec4(positionUpdated, 1.0);
vec4 worldPos = finalWorld * vec4(positionUpdated, 1.0);
#include<clipPlaneVertex>
gl_Position = viewProjection * worldPos;

#ifdef USE_REVERSE_DEPTHBUFFER
vDepthMetric = ((-gl_Position.z + depthValues.x) / (depthValues.y));
Expand Down