Skip to content

Commit

Permalink
Clipping for GridMaterial (#15866)
Browse files Browse the repository at this point in the history
  • Loading branch information
CedricGuillemet authored Nov 21, 2024
1 parent 52d08ee commit 0579881
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 26 deletions.
4 changes: 4 additions & 0 deletions packages/dev/materials/src/grid/grid.fragment.fx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ varying vec3 vNormal;
#extension GL_EXT_frag_depth : enable
#endif

#include<clipPlaneFragmentDeclaration>

#include<logDepthDeclaration>

#include<fogFragmentDeclaration>
Expand Down Expand Up @@ -90,6 +92,8 @@ void main(void) {

#define CUSTOM_FRAGMENT_MAIN_BEGIN

#include<clipPlaneFragment>

// Scale position to the requested ratio.
float gridRatio = gridControl.x;
vec3 gridPos = (vPosition + gridOffset.xyz) / gridRatio;
Expand Down
4 changes: 4 additions & 0 deletions packages/dev/materials/src/grid/grid.vertex.fx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ uniform mat4 opacityMatrix;
uniform vec2 vOpacityInfos;
#endif

#include<clipPlaneVertexDeclaration>

#define CUSTOM_VERTEX_DEFINITIONS

Expand Down Expand Up @@ -62,6 +63,9 @@ void main(void) {
}
#endif

// Clip plane
#include<clipPlaneVertex>

#include<logDepthVertex>

vPosition = position;
Expand Down
52 changes: 26 additions & 26 deletions packages/dev/materials/src/grid/gridMaterial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,15 @@ import {
PrepareDefinesForFrameBoundValues,
PrepareDefinesForMisc,
} from "core/Materials/materialHelper.functions";
import { addClipPlaneUniforms, bindClipPlane } from "core/Materials/clipPlaneMaterialHelper";

class GridMaterialDefines extends MaterialDefines {
public CLIPPLANE = false;
public CLIPPLANE2 = false;
public CLIPPLANE3 = false;
public CLIPPLANE4 = false;
public CLIPPLANE5 = false;
public CLIPPLANE6 = false;
public OPACITY = false;
public ANTIALIAS = false;
public TRANSPARENT = false;
Expand Down Expand Up @@ -222,35 +229,26 @@ export class GridMaterial extends PushMaterial {

PrepareAttributesForInstances(attribs, defines);

const uniforms = [
"projection",
"mainColor",
"lineColor",
"gridControl",
"gridOffset",
"vFogInfos",
"vFogColor",
"world",
"view",
"opacityMatrix",
"vOpacityInfos",
"visibility",
"logarithmicDepthConstant",
];
// Defines
const join = defines.toString();
addClipPlaneUniforms(uniforms);
subMesh.setEffect(
scene
.getEngine()
.createEffect(
"grid",
attribs,
[
"projection",
"mainColor",
"lineColor",
"gridControl",
"gridOffset",
"vFogInfos",
"vFogColor",
"world",
"view",
"opacityMatrix",
"vOpacityInfos",
"visibility",
"logarithmicDepthConstant",
],
["opacitySampler"],
join,
undefined,
this.onCompiled,
this.onError
),
scene.getEngine().createEffect("grid", attribs, uniforms, ["opacitySampler"], join, undefined, this.onCompiled, this.onError),
defines,
this._materialContext
);
Expand Down Expand Up @@ -309,6 +307,8 @@ export class GridMaterial extends PushMaterial {
this._activeEffect.setMatrix("opacityMatrix", this._opacityTexture.getTextureMatrix());
}

// Clip plane
bindClipPlane(effect, this, scene);
// Log. depth
if (this._useLogarithmicDepth) {
BindLogDepth(defines, effect, scene);
Expand Down

0 comments on commit 0579881

Please sign in to comment.