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

[Engine] Add a flag to use opacity instead of diffuse texture for transparent shadows. #12390

Merged
merged 1 commit into from
Apr 13, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions packages/dev/core/src/Lights/Shadows/shadowGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -671,10 +671,16 @@ export class ShadowGenerator implements IShadowGenerator {
* When it is enabled, the strength of the shadow is taken equal to mesh.visibility
* If you enabled an alpha texture on your material, the alpha value red from the texture is also combined to compute the strength:
* mesh.visibility * alphaTexture.a
* The texture used is the diffuse by default, but it can be set to the opacity by setting useOpacityTextureForTransparentShadow
* Note that by definition transparencyShadow must be set to true for enableSoftTransparentShadow to work!
*/
public enableSoftTransparentShadow: boolean = false;

/**
* If this is true, use the opacity texture's alpha channel for transparent shadows instead of the diffuse one
*/
public useOpacityTextureForTransparentShadow: boolean = false;

protected _shadowMap: Nullable<RenderTargetTexture>;
protected _shadowMap2: Nullable<RenderTargetTexture>;

Expand Down Expand Up @@ -1204,7 +1210,13 @@ export class ShadowGenerator implements IShadowGenerator {
subMesh._setMainDrawWrapperOverride(null);
} else {
// Alpha test
if (material && material.needAlphaTesting()) {
if (material && this.useOpacityTextureForTransparentShadow) {
const opacityTexture = (material as any).opacityTexture;
if (opacityTexture) {
effect.setTexture("diffuseSampler", opacityTexture);
effect.setMatrix("diffuseMatrix", opacityTexture.getTextureMatrix() || this._defaultTextureMatrix);
}
} else if (material && material.needAlphaTesting()) {
const alphaTexture = material.getAlphaTestTexture();
if (alphaTexture) {
effect.setTexture("diffuseSampler", alphaTexture);
Expand Down Expand Up @@ -1448,7 +1460,12 @@ export class ShadowGenerator implements IShadowGenerator {

// Alpha test
if (material && material.needAlphaTesting()) {
const alphaTexture = material.getAlphaTestTexture();
let alphaTexture = null;
if (this.useOpacityTextureForTransparentShadow) {
alphaTexture = (material as any).opacityTexture;
} else {
alphaTexture = material.getAlphaTestTexture();
}
if (alphaTexture) {
if (!alphaTexture.isReady()) {
return false;
Expand Down