瀏覽代碼

Add the Material.strictTransparencyMode property

Popov72 5 年之前
父節點
當前提交
6e5a148cad
共有 4 個文件被更改,包括 63 次插入1 次删除
  1. 15 0
      src/Materials/PBR/pbrBaseMaterial.ts
  2. 35 0
      src/Materials/material.ts
  3. 12 0
      src/Materials/standardMaterial.ts
  4. 1 1
      src/Shaders/pbr.fragment.fx

+ 15 - 0
src/Materials/PBR/pbrBaseMaterial.ts

@@ -80,6 +80,7 @@ export class PBRMaterialDefines extends MaterialDefines
     public ALPHAFRESNEL = false;
     public LINEARALPHAFRESNEL = false;
     public PREMULTIPLYALPHA = false;
+    public STRICTTRANSPARENCYMODE = false;
 
     public EMISSIVE = false;
     public EMISSIVEDIRECTUV = 0;
@@ -825,6 +826,10 @@ export abstract class PBRBaseMaterial extends PushMaterial {
      * Specifies whether or not this material should be rendered in alpha blend mode.
      */
     public needAlphaBlending(): boolean {
+        if (this.strictTransparencyMode) {
+            return this._transparencyMode === Material.MATERIAL_ALPHABLEND || this._transparencyMode === Material.MATERIAL_ALPHATESTANDBLEND;
+        }
+
         if (this._disableAlphaBlending) {
             return false;
         }
@@ -836,6 +841,10 @@ export abstract class PBRBaseMaterial extends PushMaterial {
      * Specifies whether or not this material should be rendered in alpha test mode.
      */
     public needAlphaTesting(): boolean {
+        if (this.strictTransparencyMode) {
+            return this._transparencyMode === Material.MATERIAL_ALPHATEST || this._transparencyMode === Material.MATERIAL_ALPHATESTANDBLEND;
+        }
+
         if (this._forceAlphaTest) {
             return true;
         }
@@ -851,6 +860,10 @@ export abstract class PBRBaseMaterial extends PushMaterial {
      * Specifies whether or not the alpha value of the albedo texture should be used for alpha blending.
      */
     protected _shouldUseAlphaFromAlbedoTexture(): boolean {
+        if (this.strictTransparencyMode) {
+            return this._useAlphaFromAlbedoTexture;
+        }
+
         return this._albedoTexture != null && this._albedoTexture.hasAlpha && this._useAlphaFromAlbedoTexture && this._transparencyMode !== PBRBaseMaterial.PBRMATERIAL_OPAQUE;
     }
 
@@ -1482,6 +1495,8 @@ export abstract class PBRBaseMaterial extends PushMaterial {
 
         defines.HORIZONOCCLUSION = this._useHorizonOcclusion;
 
+        defines.STRICTTRANSPARENCYMODE = this._strictTransparencyMode;
+
         // Misc.
         if (defines._areMiscDirty) {
             MaterialHelper.PrepareDefinesForMisc(mesh, scene, this._useLogarithmicDepth, this.pointsCloud, this.fogEnabled, this._shouldTurnAlphaTestOn(mesh) || this._forceAlphaTest, defines);

+ 35 - 0
src/Materials/material.ts

@@ -729,6 +729,25 @@ export class Material implements IAnimatable {
         this._markAllSubMeshesAsTexturesAndMiscDirty();
     }
 
+    protected _strictTransparencyMode: boolean = false;
+    /**
+     * Gets or sets the strict transparency mode
+     */
+    @serialize()
+    public get strictTransparencyMode(): boolean {
+        return this._strictTransparencyMode;
+    }
+
+    public set strictTransparencyMode(value: boolean) {
+        if (this._strictTransparencyMode === value) {
+            return;
+        }
+
+        this._strictTransparencyMode = value;
+
+        this._markAllSubMeshesAsTexturesAndMiscDirty();
+    }
+
     /**
      * Returns true if alpha blending should be disabled.
      */
@@ -742,6 +761,10 @@ export class Material implements IAnimatable {
      * @returns a boolean specifying if alpha blending is needed
      */
     public needAlphaBlending(): boolean {
+        if (this.strictTransparencyMode) {
+            return this._transparencyMode === Material.MATERIAL_ALPHABLEND || this._transparencyMode === Material.MATERIAL_ALPHATESTANDBLEND;
+        }
+
         if (this._disableAlphaBlending) {
             return false;
         }
@@ -755,6 +778,10 @@ export class Material implements IAnimatable {
      * @returns a boolean specifying if alpha blending is needed for the mesh
      */
     public needAlphaBlendingForMesh(mesh: AbstractMesh): boolean {
+        if (this.strictTransparencyMode) {
+            return this._transparencyMode === Material.MATERIAL_ALPHABLEND || this._transparencyMode === Material.MATERIAL_ALPHATESTANDBLEND;
+        }
+
         if (this._disableAlphaBlending && mesh.visibility >= 1.0) {
             return false;
         }
@@ -767,6 +794,10 @@ export class Material implements IAnimatable {
      * @returns a boolean specifying if an alpha test is needed.
      */
     public needAlphaTesting(): boolean {
+        if (this.strictTransparencyMode) {
+            return this._transparencyMode === Material.MATERIAL_ALPHATEST || this._transparencyMode === Material.MATERIAL_ALPHATESTANDBLEND;
+        }
+
         if (this._forceAlphaTest) {
             return true;
         }
@@ -779,6 +810,10 @@ export class Material implements IAnimatable {
      * @param mesh defines the mesh to check
      */
     protected _shouldTurnAlphaTestOn(mesh: AbstractMesh): boolean {
+        if (this.strictTransparencyMode) {
+            return this._transparencyMode === Material.MATERIAL_ALPHATEST || this._transparencyMode === Material.MATERIAL_ALPHATESTANDBLEND;
+        }
+
         return (!this.needAlphaBlendingForMesh(mesh) && this.needAlphaTesting());
     }
 

+ 12 - 0
src/Materials/standardMaterial.ts

@@ -747,6 +747,10 @@ export class StandardMaterial extends PushMaterial {
      * @returns a boolean specifying if alpha blending is needed
      */
     public needAlphaBlending(): boolean {
+        if (this.strictTransparencyMode) {
+            return this._transparencyMode === Material.MATERIAL_ALPHABLEND || this._transparencyMode === Material.MATERIAL_ALPHATESTANDBLEND;
+        }
+
         if (this._disableAlphaBlending) {
             return false;
         }
@@ -759,6 +763,10 @@ export class StandardMaterial extends PushMaterial {
      * @returns a boolean specifying if an alpha test is needed.
      */
     public needAlphaTesting(): boolean {
+        if (this.strictTransparencyMode) {
+            return this._transparencyMode === Material.MATERIAL_ALPHATEST || this._transparencyMode === Material.MATERIAL_ALPHATESTANDBLEND;
+        }
+
         if (this._forceAlphaTest) {
             return true;
         }
@@ -767,6 +775,10 @@ export class StandardMaterial extends PushMaterial {
     }
 
     protected _shouldUseAlphaFromDiffuseTexture(): boolean {
+        if (this.strictTransparencyMode) {
+            return this._useAlphaFromDiffuseTexture;
+        }
+
         return this._diffuseTexture != null && this._diffuseTexture.hasAlpha && this._useAlphaFromDiffuseTexture && this._transparencyMode !== Material.MATERIAL_OPAQUE;
     }
 

+ 1 - 1
src/Shaders/pbr.fragment.fx

@@ -93,7 +93,7 @@ void main(void) {
 
 #ifdef ALBEDO
     vec4 albedoTexture = texture2D(albedoSampler, vAlbedoUV + uvOffset);
-    #if defined(ALPHAFROMALBEDO) || defined(ALPHATEST)
+    #if defined(ALPHAFROMALBEDO) || defined(ALPHATEST) && !defined(STRICTTRANSPARENCYMODE)
         alpha *= albedoTexture.a;
     #endif