Skip to content

Commit

Permalink
Merge pull request #13197 from Popov72/shadow-fix-blend-transp
Browse files Browse the repository at this point in the history
Shadows: Fix transparent shadows with ALPHABLEND transparency mode
  • Loading branch information
sebavan authored Nov 3, 2022
2 parents a8d4f52 + 8aa8dbe commit 8e0e555
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
13 changes: 9 additions & 4 deletions packages/dev/core/src/Lights/Shadows/shadowGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1231,7 +1231,7 @@ export class ShadowGenerator implements IShadowGenerator {
effect.setTexture("diffuseSampler", opacityTexture);
effect.setMatrix("diffuseMatrix", opacityTexture.getTextureMatrix() || this._defaultTextureMatrix);
}
} else if (material && material.needAlphaTesting()) {
} else if (material && (material.needAlphaTesting() || material.needAlphaBlending())) {
const alphaTexture = material.getAlphaTestTexture();
if (alphaTexture) {
effect.setTexture("diffuseSampler", alphaTexture);
Expand Down Expand Up @@ -1474,7 +1474,10 @@ export class ShadowGenerator implements IShadowGenerator {
}

// Alpha test
if (material && material.needAlphaTesting()) {
const needAlphaTesting = material?.needAlphaTesting();
const needAlphaBlending = material?.needAlphaBlending();

if (material && (needAlphaTesting || needAlphaBlending)) {
let alphaTexture = null;
if (this.useOpacityTextureForTransparentShadow) {
alphaTexture = (material as any).opacityTexture;
Expand All @@ -1488,8 +1491,10 @@ export class ShadowGenerator implements IShadowGenerator {

const alphaCutOff = (material as any).alphaCutOff ?? ShadowGenerator.DEFAULT_ALPHA_CUTOFF;

defines.push("#define ALPHATEST");
defines.push(`#define ALPHATESTVALUE ${alphaCutOff}${alphaCutOff % 1 === 0 ? "." : ""}`);
defines.push("#define ALPHATEXTURE");
if (needAlphaTesting) {
defines.push(`#define ALPHATESTVALUE ${alphaCutOff}${alphaCutOff % 1 === 0 ? "." : ""}`);
}
if (mesh.isVerticesDataPresent(VertexBuffer.UVKind)) {
attribs.push(VertexBuffer.UVKind);
defines.push("#define UV1");
Expand Down
12 changes: 7 additions & 5 deletions packages/dev/core/src/Shaders/shadowMap.fragment.fx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include<shadowMapFragmentExtraDeclaration>

#ifdef ALPHATEST
#ifdef ALPHATEXTURE
varying vec2 vUV;
uniform sampler2D diffuseSampler;
#endif
Expand All @@ -14,14 +14,16 @@ void main(void)
{
#include<clipPlaneFragment>

#ifdef ALPHATEST
#ifdef ALPHATEXTURE
float alphaFromAlphaTexture = texture2D(diffuseSampler, vUV).a;
if (alphaFromAlphaTexture < ALPHATESTVALUE)
discard;
#ifdef ALPHATESTVALUE
if (alphaFromAlphaTexture < ALPHATESTVALUE)
discard;
#endif
#endif

#if SM_SOFTTRANSPARENTSHADOW == 1
#ifdef ALPHATEST
#ifdef ALPHATEXTURE
if ((bayerDither8(floor(mod(gl_FragCoord.xy, 8.0)))) / 64.0 >= softTransparentShadowSM * alphaFromAlphaTexture) discard;
#else
if ((bayerDither8(floor(mod(gl_FragCoord.xy, 8.0)))) / 64.0 >= softTransparentShadowSM) discard;
Expand Down
4 changes: 2 additions & 2 deletions packages/dev/core/src/Shaders/shadowMap.vertex.fx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ attribute vec3 position;

#include<__decl__shadowMapVertex>

#ifdef ALPHATEST
#ifdef ALPHATEXTURE
varying vec2 vUV;
uniform mat4 diffuseMatrix;
#ifdef UV1
Expand Down Expand Up @@ -83,7 +83,7 @@ gl_Position = viewProjection * worldPos;

#include<shadowMapVertexMetric>

#ifdef ALPHATEST
#ifdef ALPHATEXTURE
#ifdef UV1
vUV = vec2(diffuseMatrix * vec4(uvUpdated, 1.0, 0.0));
#endif
Expand Down

0 comments on commit 8e0e555

Please sign in to comment.