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 Clip plane support in Geometry Buffer #13537

Merged
merged 1 commit into from Feb 15, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
256 changes: 132 additions & 124 deletions packages/dev/core/src/Rendering/geometryBufferRenderer.ts
Expand Up @@ -21,13 +21,39 @@ import { Material } from "../Materials/material";
import "../Shaders/geometry.fragment";
import "../Shaders/geometry.vertex";
import { MaterialFlags } from "../Materials/materialFlags";
import { addClipPlaneUniforms, bindClipPlane, prepareDefinesForClipPlanes } from "../Materials/clipPlaneMaterialHelper";

/** @internal */
interface ISavedTransformationMatrix {
world: Matrix;
viewProjection: Matrix;
}

/** list the uniforms used by the geometry renderer */
const uniforms = [
"world",
"mBones",
"viewProjection",
"diffuseMatrix",
"view",
"previousWorld",
"previousViewProjection",
"mPreviousBones",
"bumpMatrix",
"reflectivityMatrix",
"albedoMatrix",
"reflectivityColor",
"albedoColor",
"metallic",
"glossiness",
"vTangentSpaceParams",
"vBumpInfos",
"morphTargetInfluences",
"morphTargetTextureInfo",
"morphTargetTextureIndices",
];
addClipPlaneUniforms(uniforms);

/**
* This renderer is helpful to fill one of the render target with a geometry buffer.
*/
Expand Down Expand Up @@ -568,6 +594,8 @@ export class GeometryBufferRenderer {
defines.push("#define RENDER_TARGET_COUNT " + this._multiRenderTarget.textures.length);
}

prepareDefinesForClipPlanes(material, this._scene, defines);

// Get correct effect
const engine = this._scene.getEngine();
const drawWrapper = subMesh._getDrawWrapper(undefined, true)!;
Expand All @@ -579,28 +607,7 @@ export class GeometryBufferRenderer {
"geometry",
{
attributes: attribs,
uniformsNames: [
"world",
"mBones",
"viewProjection",
"diffuseMatrix",
"view",
"previousWorld",
"previousViewProjection",
"mPreviousBones",
"bumpMatrix",
"reflectivityMatrix",
"albedoMatrix",
"reflectivityColor",
"albedoColor",
"metallic",
"glossiness",
"vTangentSpaceParams",
"vBumpInfos",
"morphTargetInfluences",
"morphTargetTextureInfo",
"morphTargetTextureIndices",
],
uniformsNames: uniforms,
samplers: ["diffuseSampler", "bumpSampler", "reflectivitySampler", "albedoSampler", "morphTargets"],
defines: join,
onCompiled: null,
Expand Down Expand Up @@ -780,125 +787,126 @@ export class GeometryBufferRenderer {
this._scene.finalizeSceneUbo();
}

if (material) {
let sideOrientation: Nullable<number>;
const instanceDataStorage = (renderingMesh as Mesh)._instanceDataStorage;
let sideOrientation: Nullable<number>;
const instanceDataStorage = (renderingMesh as Mesh)._instanceDataStorage;

if (!instanceDataStorage.isFrozen && (material.backFaceCulling || renderingMesh.overrideMaterialSideOrientation !== null)) {
const mainDeterminant = effectiveMesh._getWorldMatrixDeterminant();
sideOrientation = renderingMesh.overrideMaterialSideOrientation;
if (sideOrientation === null) {
sideOrientation = material.sideOrientation;
}
if (mainDeterminant < 0) {
sideOrientation = sideOrientation === Material.ClockWiseSideOrientation ? Material.CounterClockWiseSideOrientation : Material.ClockWiseSideOrientation;
}
} else {
sideOrientation = instanceDataStorage.sideOrientation;
if (!instanceDataStorage.isFrozen && (material.backFaceCulling || renderingMesh.overrideMaterialSideOrientation !== null)) {
const mainDeterminant = effectiveMesh._getWorldMatrixDeterminant();
sideOrientation = renderingMesh.overrideMaterialSideOrientation;
if (sideOrientation === null) {
sideOrientation = material.sideOrientation;
}
if (mainDeterminant < 0) {
sideOrientation = sideOrientation === Material.ClockWiseSideOrientation ? Material.CounterClockWiseSideOrientation : Material.ClockWiseSideOrientation;
}
} else {
sideOrientation = instanceDataStorage.sideOrientation;
}

material._preBind(drawWrapper, sideOrientation);
material._preBind(drawWrapper, sideOrientation);

// Alpha test
if (material.needAlphaTesting()) {
const alphaTexture = material.getAlphaTestTexture();
if (alphaTexture) {
effect.setTexture("diffuseSampler", alphaTexture);
effect.setMatrix("diffuseMatrix", alphaTexture.getTextureMatrix());
}
// Alpha test
if (material.needAlphaTesting()) {
const alphaTexture = material.getAlphaTestTexture();
if (alphaTexture) {
effect.setTexture("diffuseSampler", alphaTexture);
effect.setMatrix("diffuseMatrix", alphaTexture.getTextureMatrix());
}
}

// Bump
if (material.bumpTexture && scene.getEngine().getCaps().standardDerivatives && MaterialFlags.BumpTextureEnabled) {
effect.setFloat3("vBumpInfos", material.bumpTexture.coordinatesIndex, 1.0 / material.bumpTexture.level, material.parallaxScaleBias);
effect.setMatrix("bumpMatrix", material.bumpTexture.getTextureMatrix());
effect.setTexture("bumpSampler", material.bumpTexture);
effect.setFloat2("vTangentSpaceParams", material.invertNormalMapX ? -1.0 : 1.0, material.invertNormalMapY ? -1.0 : 1.0);
}
// Bump
if (material.bumpTexture && scene.getEngine().getCaps().standardDerivatives && MaterialFlags.BumpTextureEnabled) {
effect.setFloat3("vBumpInfos", material.bumpTexture.coordinatesIndex, 1.0 / material.bumpTexture.level, material.parallaxScaleBias);
effect.setMatrix("bumpMatrix", material.bumpTexture.getTextureMatrix());
effect.setTexture("bumpSampler", material.bumpTexture);
effect.setFloat2("vTangentSpaceParams", material.invertNormalMapX ? -1.0 : 1.0, material.invertNormalMapY ? -1.0 : 1.0);
}

// Reflectivity
if (this._enableReflectivity) {
// for PBR materials: cf. https://doc.babylonjs.com/features/featuresDeepDive/materials/using/masterPBR
if (material.getClassName() === "PBRMetallicRoughnessMaterial") {
// if it is a PBR material in MetallicRoughness Mode:
if (material.metallicRoughnessTexture !== null) {
effect.setTexture("reflectivitySampler", material.metallicRoughnessTexture);
effect.setMatrix("reflectivityMatrix", material.metallicRoughnessTexture.getTextureMatrix());
}
if (material.metallic !== null) {
effect.setFloat("metallic", material.metallic);
}
if (material.roughness !== null) {
effect.setFloat("glossiness", 1.0 - material.roughness);
}
if (material.baseTexture !== null) {
effect.setTexture("albedoSampler", material.baseTexture);
effect.setMatrix("albedoMatrix", material.baseTexture.getTextureMatrix());
}
if (material.baseColor !== null) {
effect.setColor3("albedoColor", material.baseColor);
}
} else if (material.getClassName() === "PBRSpecularGlossinessMaterial") {
// if it is a PBR material in Specular/Glossiness Mode:
if (material.specularGlossinessTexture !== null) {
effect.setTexture("reflectivitySampler", material.specularGlossinessTexture);
effect.setMatrix("reflectivityMatrix", material.specularGlossinessTexture.getTextureMatrix());
} else {
if (material.specularColor !== null) {
effect.setColor3("reflectivityColor", material.specularColor);
}
}
if (material.glossiness !== null) {
effect.setFloat("glossiness", material.glossiness);
}
} else if (material.getClassName() === "PBRMaterial") {
// if it is the bigger PBRMaterial
if (material.metallicTexture !== null) {
effect.setTexture("reflectivitySampler", material.metallicTexture);
effect.setMatrix("reflectivityMatrix", material.metallicTexture.getTextureMatrix());
}
if (material.metallic !== null) {
effect.setFloat("metallic", material.metallic);
// Reflectivity
if (this._enableReflectivity) {
// for PBR materials: cf. https://doc.babylonjs.com/features/featuresDeepDive/materials/using/masterPBR
if (material.getClassName() === "PBRMetallicRoughnessMaterial") {
// if it is a PBR material in MetallicRoughness Mode:
if (material.metallicRoughnessTexture !== null) {
effect.setTexture("reflectivitySampler", material.metallicRoughnessTexture);
effect.setMatrix("reflectivityMatrix", material.metallicRoughnessTexture.getTextureMatrix());
}
if (material.metallic !== null) {
effect.setFloat("metallic", material.metallic);
}
if (material.roughness !== null) {
effect.setFloat("glossiness", 1.0 - material.roughness);
}
if (material.baseTexture !== null) {
effect.setTexture("albedoSampler", material.baseTexture);
effect.setMatrix("albedoMatrix", material.baseTexture.getTextureMatrix());
}
if (material.baseColor !== null) {
effect.setColor3("albedoColor", material.baseColor);
}
} else if (material.getClassName() === "PBRSpecularGlossinessMaterial") {
// if it is a PBR material in Specular/Glossiness Mode:
if (material.specularGlossinessTexture !== null) {
effect.setTexture("reflectivitySampler", material.specularGlossinessTexture);
effect.setMatrix("reflectivityMatrix", material.specularGlossinessTexture.getTextureMatrix());
} else {
if (material.specularColor !== null) {
effect.setColor3("reflectivityColor", material.specularColor);
}
}
if (material.glossiness !== null) {
effect.setFloat("glossiness", material.glossiness);
}
} else if (material.getClassName() === "PBRMaterial") {
// if it is the bigger PBRMaterial
if (material.metallicTexture !== null) {
effect.setTexture("reflectivitySampler", material.metallicTexture);
effect.setMatrix("reflectivityMatrix", material.metallicTexture.getTextureMatrix());
}
if (material.metallic !== null) {
effect.setFloat("metallic", material.metallic);
}

if (material.roughness !== null) {
effect.setFloat("glossiness", 1.0 - material.roughness);
}
if (material.roughness !== null) {
effect.setFloat("glossiness", 1.0 - material.roughness);
}

if (material.roughness !== null || material.metallic !== null || material.metallicTexture !== null) {
// MetallicRoughness Model
if (material.albedoTexture !== null) {
effect.setTexture("albedoSampler", material.albedoTexture);
effect.setMatrix("albedoMatrix", material.albedoTexture.getTextureMatrix());
}
if (material.albedoColor !== null) {
effect.setColor3("albedoColor", material.albedoColor);
}
} else {
// SpecularGlossiness Model
if (material.reflectivityTexture !== null) {
effect.setTexture("reflectivitySampler", material.reflectivityTexture);
effect.setMatrix("reflectivityMatrix", material.reflectivityTexture.getTextureMatrix());
} else if (material.reflectivityColor !== null) {
effect.setColor3("reflectivityColor", material.reflectivityColor);
}
if (material.microSurface !== null) {
effect.setFloat("glossiness", material.microSurface);
}
if (material.roughness !== null || material.metallic !== null || material.metallicTexture !== null) {
// MetallicRoughness Model
if (material.albedoTexture !== null) {
effect.setTexture("albedoSampler", material.albedoTexture);
effect.setMatrix("albedoMatrix", material.albedoTexture.getTextureMatrix());
}
} else if (material.getClassName() === "StandardMaterial") {
// if StandardMaterial:
if (material.specularTexture !== null) {
effect.setTexture("reflectivitySampler", material.specularTexture);
effect.setMatrix("reflectivityMatrix", material.specularTexture.getTextureMatrix());
if (material.albedoColor !== null) {
effect.setColor3("albedoColor", material.albedoColor);
}
if (material.specularColor !== null) {
effect.setColor3("reflectivityColor", material.specularColor);
} else {
// SpecularGlossiness Model
if (material.reflectivityTexture !== null) {
effect.setTexture("reflectivitySampler", material.reflectivityTexture);
effect.setMatrix("reflectivityMatrix", material.reflectivityTexture.getTextureMatrix());
} else if (material.reflectivityColor !== null) {
effect.setColor3("reflectivityColor", material.reflectivityColor);
}
if (material.microSurface !== null) {
effect.setFloat("glossiness", material.microSurface);
}
}
} else if (material.getClassName() === "StandardMaterial") {
// if StandardMaterial:
if (material.specularTexture !== null) {
effect.setTexture("reflectivitySampler", material.specularTexture);
effect.setMatrix("reflectivityMatrix", material.specularTexture.getTextureMatrix());
}
if (material.specularColor !== null) {
effect.setColor3("reflectivityColor", material.specularColor);
}
}
}

// Clip plane
bindClipPlane(effect, material, this._scene);

// Bones
if (renderingMesh.useBones && renderingMesh.computeBonesUsingShaders && renderingMesh.skeleton) {
effect.setMatrices("mBones", renderingMesh.skeleton.getTransformMatrices(renderingMesh));
Expand Down
4 changes: 4 additions & 0 deletions packages/dev/core/src/Shaders/geometry.fragment.fx
Expand Up @@ -60,12 +60,16 @@ uniform vec2 vTangentSpaceParams;
uniform sampler2D diffuseSampler;
#endif

#include<clipPlaneFragmentDeclaration>

#include<mrtFragmentDeclaration>[RENDER_TARGET_COUNT]
#include<bumpFragmentMainFunctions>
#include<bumpFragmentFunctions>
#include<helperFunctions>

void main() {
#include<clipPlaneFragment>

#ifdef ALPHATEST
if (texture2D(diffuseSampler, vUV).a < 0.4)
discard;
Expand Down
10 changes: 7 additions & 3 deletions packages/dev/core/src/Shaders/geometry.vertex.fx
Expand Up @@ -9,6 +9,8 @@ precision highp float;
#include<instancesDeclaration>
#include<__decl__geometryVertex>

#include<clipPlaneVertexDeclaration>

attribute vec3 position;
attribute vec3 normal;

Expand Down Expand Up @@ -85,7 +87,7 @@ void main(void)

#include<bonesVertex>
#include<bakedVertexAnimation>
vec4 pos = vec4(finalWorld * vec4(positionUpdated, 1.0));
vec4 worldPos = vec4(finalWorld * vec4(positionUpdated, 1.0));

#ifdef BUMP
vWorldView = view * finalWorld;
Expand All @@ -94,7 +96,7 @@ void main(void)
vNormalV = normalize(vec3((view * finalWorld) * vec4(normalUpdated, 0.0)));
#endif

vViewPos = view * pos;
vViewPos = view * worldPos;

#if defined(VELOCITY) && defined(BONES_VELOCITY_ENABLED)
vCurrentPosition = viewProjection * finalWorld * vec4(positionUpdated, 1.0);
Expand Down Expand Up @@ -132,11 +134,13 @@ void main(void)
#endif

#if defined(POSITION) || defined(BUMP)
vPositionW = pos.xyz / pos.w;
vPositionW = worldPos.xyz / worldPos.w;
#endif

gl_Position = viewProjection * finalWorld * vec4(positionUpdated, 1.0);

#include<clipPlaneVertex>

#ifdef NEED_UV
#ifdef UV1
#if defined(ALPHATEST) && defined(ALPHATEST_UV1)
Expand Down