From 5b28c2d6dd69310650c22dec8665ac55dbd86dba Mon Sep 17 00:00:00 2001 From: Gary Hsu Date: Fri, 30 Aug 2024 08:57:43 -0700 Subject: [PATCH 1/2] Enable alpha test when using vertex alpha in some cases --- packages/dev/core/src/Materials/material.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/packages/dev/core/src/Materials/material.ts b/packages/dev/core/src/Materials/material.ts index 352e92ad9fa..86bd80bc731 100644 --- a/packages/dev/core/src/Materials/material.ts +++ b/packages/dev/core/src/Materials/material.ts @@ -1147,13 +1147,22 @@ export class Material implements IAnimatable, IClipPlanesHolder { return false; } + /** + * Specifies whether or not this material should be rendered in alpha test mode when used with the given mesh. + * @param mesh defines the mesh to check + * @returns a boolean specifying if an alpha test is needed. + */ + public needAlphaTestingForMesh(mesh: AbstractMesh): boolean { + return mesh.hasVertexAlpha || this.needAlphaTesting(); + } + /** * Specifies if material alpha testing should be turned on for the mesh * @param mesh defines the mesh to check * @returns a boolean specifying if alpha testing should be turned on for the mesh */ protected _shouldTurnAlphaTestOn(mesh: AbstractMesh): boolean { - return !this.needAlphaBlendingForMesh(mesh) && this.needAlphaTesting(); + return !this.needAlphaBlendingForMesh(mesh) && this.needAlphaTestingForMesh(mesh); } /** From 52395923af80ec1b86ef562476d37403a14b8c4c Mon Sep 17 00:00:00 2001 From: Gary Hsu Date: Wed, 4 Sep 2024 14:48:45 -0700 Subject: [PATCH 2/2] Use needAlphTestingForMesh where possible --- packages/dev/core/src/Layers/effectLayer.ts | 4 ++-- packages/dev/core/src/Lights/Shadows/shadowGenerator.ts | 2 +- .../src/PostProcesses/volumetricLightScatteringPostProcess.ts | 4 ++-- packages/dev/core/src/Rendering/depthRenderer.ts | 4 ++-- packages/dev/core/src/Rendering/geometryBufferRenderer.ts | 4 ++-- packages/dev/core/src/Rendering/outlineRenderer.ts | 4 ++-- packages/dev/core/src/Rendering/renderingGroup.ts | 2 +- 7 files changed, 12 insertions(+), 12 deletions(-) diff --git a/packages/dev/core/src/Layers/effectLayer.ts b/packages/dev/core/src/Layers/effectLayer.ts index 61a38abe126..88da978974b 100644 --- a/packages/dev/core/src/Layers/effectLayer.ts +++ b/packages/dev/core/src/Layers/effectLayer.ts @@ -595,7 +595,7 @@ export abstract class EffectLayer { // Diffuse if (material) { - const needAlphaTest = material.needAlphaTesting(); + const needAlphaTest = material.needAlphaTestingForMesh(mesh); const diffuseTexture = material.getAlphaTestTexture(); const needAlphaBlendFromDiffuse = @@ -986,7 +986,7 @@ export abstract class EffectLayer { } if (!renderingMaterial) { - const needAlphaTest = material.needAlphaTesting(); + const needAlphaTest = material.needAlphaTestingForMesh(effectiveMesh); const diffuseTexture = material.getAlphaTestTexture(); const needAlphaBlendFromDiffuse = diff --git a/packages/dev/core/src/Lights/Shadows/shadowGenerator.ts b/packages/dev/core/src/Lights/Shadows/shadowGenerator.ts index 16f5605abc2..f6e9643cc83 100644 --- a/packages/dev/core/src/Lights/Shadows/shadowGenerator.ts +++ b/packages/dev/core/src/Lights/Shadows/shadowGenerator.ts @@ -1563,7 +1563,7 @@ export class ShadowGenerator implements IShadowGenerator { } // Alpha test - const needAlphaTesting = material.needAlphaTesting(); + const needAlphaTesting = material.needAlphaTestingForMesh(mesh); if (needAlphaTesting || material.needAlphaBlending()) { if (this.useOpacityTextureForTransparentShadow) { diff --git a/packages/dev/core/src/PostProcesses/volumetricLightScatteringPostProcess.ts b/packages/dev/core/src/PostProcesses/volumetricLightScatteringPostProcess.ts index 7a812024306..5716618229b 100644 --- a/packages/dev/core/src/PostProcesses/volumetricLightScatteringPostProcess.ts +++ b/packages/dev/core/src/PostProcesses/volumetricLightScatteringPostProcess.ts @@ -213,7 +213,7 @@ export class VolumetricLightScatteringPostProcess extends PostProcess { // Alpha test if (material) { - if (material.needAlphaTesting()) { + if (material.needAlphaTestingForMesh(mesh)) { defines.push("#define ALPHATEST"); } @@ -463,7 +463,7 @@ export class VolumetricLightScatteringPostProcess extends PostProcess { effect.setMatrix("viewProjection", scene.getTransformMatrix()); // Alpha test - if (material.needAlphaTesting()) { + if (material.needAlphaTestingForMesh(effectiveMesh)) { const alphaTexture = material.getAlphaTestTexture(); if (alphaTexture) { diff --git a/packages/dev/core/src/Rendering/depthRenderer.ts b/packages/dev/core/src/Rendering/depthRenderer.ts index 7ae6085e2ce..5412d8b0c97 100644 --- a/packages/dev/core/src/Rendering/depthRenderer.ts +++ b/packages/dev/core/src/Rendering/depthRenderer.ts @@ -284,7 +284,7 @@ export class DepthRenderer { if (!renderingMaterial) { // Alpha test - if (material.needAlphaTesting()) { + if (material.needAlphaTestingForMesh(effectiveMesh)) { const alphaTexture = material.getAlphaTestTexture(); if (alphaTexture) { @@ -404,7 +404,7 @@ export class DepthRenderer { const attribs = [VertexBuffer.PositionKind]; // Alpha test - if (material.needAlphaTesting() && material.getAlphaTestTexture()) { + if (material.needAlphaTestingForMesh(mesh) && material.getAlphaTestTexture()) { defines.push("#define ALPHATEST"); if (mesh.isVerticesDataPresent(VertexBuffer.UVKind)) { attribs.push(VertexBuffer.UVKind); diff --git a/packages/dev/core/src/Rendering/geometryBufferRenderer.ts b/packages/dev/core/src/Rendering/geometryBufferRenderer.ts index 41e018ee659..3a13f05f81e 100644 --- a/packages/dev/core/src/Rendering/geometryBufferRenderer.ts +++ b/packages/dev/core/src/Rendering/geometryBufferRenderer.ts @@ -450,7 +450,7 @@ export class GeometryBufferRenderer { if (material) { let needUv = false; // Alpha test - if (material.needAlphaTesting() && material.getAlphaTestTexture()) { + if (material.needAlphaTestingForMesh(mesh) && material.getAlphaTestTexture()) { defines.push("#define ALPHATEST"); defines.push(`#define ALPHATEST_UV${material.getAlphaTestTexture().coordinatesIndex + 1}`); needUv = true; @@ -951,7 +951,7 @@ export class GeometryBufferRenderer { material._preBind(drawWrapper, sideOrientation); // Alpha test - if (material.needAlphaTesting()) { + if (material.needAlphaTestingForMesh(effectiveMesh)) { const alphaTexture = material.getAlphaTestTexture(); if (alphaTexture) { effect.setTexture("diffuseSampler", alphaTexture); diff --git a/packages/dev/core/src/Rendering/outlineRenderer.ts b/packages/dev/core/src/Rendering/outlineRenderer.ts index 3dde8450679..c3cde9070ec 100644 --- a/packages/dev/core/src/Rendering/outlineRenderer.ts +++ b/packages/dev/core/src/Rendering/outlineRenderer.ts @@ -242,7 +242,7 @@ export class OutlineRenderer implements ISceneComponent { } // Alpha test - if (material && material.needAlphaTesting()) { + if (material && material.needAlphaTestingForMesh(effectiveMesh)) { const alphaTexture = material.getAlphaTestTexture(); if (alphaTexture) { effect.setTexture("diffuseSampler", alphaTexture); @@ -288,7 +288,7 @@ export class OutlineRenderer implements ISceneComponent { const scene = mesh.getScene(); // Alpha test - if (material.needAlphaTesting()) { + if (material.needAlphaTestingForMesh(mesh)) { defines.push("#define ALPHATEST"); if (mesh.isVerticesDataPresent(VertexBuffer.UVKind)) { attribs.push(VertexBuffer.UVKind); diff --git a/packages/dev/core/src/Rendering/renderingGroup.ts b/packages/dev/core/src/Rendering/renderingGroup.ts index a147d5a5bbb..98aa1c9b567 100644 --- a/packages/dev/core/src/Rendering/renderingGroup.ts +++ b/packages/dev/core/src/Rendering/renderingGroup.ts @@ -411,7 +411,7 @@ export class RenderingGroup { if (material.needAlphaBlendingForMesh(mesh)) { // Transparent this._transparentSubMeshes.push(subMesh); - } else if (material.needAlphaTesting()) { + } else if (material.needAlphaTestingForMesh(mesh)) { // Alpha test if (material.needDepthPrePass) { this._depthOnlySubMeshes.push(subMesh);