diff --git a/packages/dev/core/src/Rendering/geometryBufferRenderer.ts b/packages/dev/core/src/Rendering/geometryBufferRenderer.ts index f63bfa47eb7..c8fce63ad5d 100644 --- a/packages/dev/core/src/Rendering/geometryBufferRenderer.ts +++ b/packages/dev/core/src/Rendering/geometryBufferRenderer.ts @@ -21,6 +21,7 @@ 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 { @@ -28,6 +29,31 @@ interface ISavedTransformationMatrix { 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. */ @@ -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)!; @@ -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, @@ -780,125 +787,126 @@ export class GeometryBufferRenderer { this._scene.finalizeSceneUbo(); } - if (material) { - let sideOrientation: Nullable; - const instanceDataStorage = (renderingMesh as Mesh)._instanceDataStorage; + let sideOrientation: Nullable; + 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)); diff --git a/packages/dev/core/src/Shaders/geometry.fragment.fx b/packages/dev/core/src/Shaders/geometry.fragment.fx index aa702976dbd..cd6f73fb44a 100644 --- a/packages/dev/core/src/Shaders/geometry.fragment.fx +++ b/packages/dev/core/src/Shaders/geometry.fragment.fx @@ -60,12 +60,16 @@ uniform vec2 vTangentSpaceParams; uniform sampler2D diffuseSampler; #endif +#include + #include[RENDER_TARGET_COUNT] #include #include #include void main() { + #include + #ifdef ALPHATEST if (texture2D(diffuseSampler, vUV).a < 0.4) discard; diff --git a/packages/dev/core/src/Shaders/geometry.vertex.fx b/packages/dev/core/src/Shaders/geometry.vertex.fx index b5e2c6ecbf0..e4b6a310c51 100644 --- a/packages/dev/core/src/Shaders/geometry.vertex.fx +++ b/packages/dev/core/src/Shaders/geometry.vertex.fx @@ -9,6 +9,8 @@ precision highp float; #include #include<__decl__geometryVertex> +#include + attribute vec3 position; attribute vec3 normal; @@ -85,7 +87,7 @@ void main(void) #include #include - vec4 pos = vec4(finalWorld * vec4(positionUpdated, 1.0)); + vec4 worldPos = vec4(finalWorld * vec4(positionUpdated, 1.0)); #ifdef BUMP vWorldView = view * finalWorld; @@ -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); @@ -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 + #ifdef NEED_UV #ifdef UV1 #if defined(ALPHATEST) && defined(ALPHATEST_UV1)