Переглянути джерело

Add Automatic Premultiply Alpha

Sebastien Vandenberghe 7 роки тому
батько
коміт
d2a8f39e25

+ 1 - 7
src/Materials/PBR/babylon.pbrBaseMaterial.ts

@@ -376,12 +376,6 @@
         protected _forceAlphaTest = false;
 
         /**
-         * Specifies that the alpha is premultiplied before output (this enables alpha premultiplied blending).
-         * in your scene composition.
-         */
-        protected _preMultiplyAlpha = false;
-
-        /**
          * A fresnel is applied to the alpha of the model to ensure grazing angles edges are not alpha tested.
          * And/Or occlude the blended part.
          */
@@ -793,7 +787,7 @@
                 }
 
                 defines.ALPHATESTVALUE = this._alphaCutOff;
-                defines.PREMULTIPLYALPHA = this._preMultiplyAlpha;
+                defines.PREMULTIPLYALPHA = (this.alphaMode === Engine.ALPHA_PREMULTIPLIED || this.alphaMode === Engine.ALPHA_PREMULTIPLIED_PORTERDUFF);
                 defines.ALPHABLEND = this.needAlphaBlending();
                 defines.ALPHAFRESNEL = this._useAlphaFresnel;
             }

+ 0 - 8
src/Materials/PBR/babylon.pbrMaterial.ts

@@ -381,14 +381,6 @@
         public twoSidedLighting = false;
 
         /**
-         * Specifies that the alpha is premultiplied before output (this enables alpha premultiplied blending).
-         * in your scene composition.
-         */
-        @serialize()
-        @expandToProperty("_markAllSubMeshesAsTexturesDirty")
-        public preMultiplyAlpha = false;
-
-        /**
          * A fresnel is applied to the alpha of the model to ensure grazing angles edges are not alpha tested.
          * And/Or occlude the blended part.
          */

+ 12 - 3
src/Materials/babylon.material.ts

@@ -282,9 +282,18 @@
         */
         public onUnBindObservable = new Observable<Material>();
 
-
-        @serialize()
-        public alphaMode = Engine.ALPHA_COMBINE;
+        @serialize("alphaMode")
+        private _alphaMode: number = Engine.ALPHA_COMBINE;
+        public set alphaMode(value : number) {
+            if (this._alphaMode === value) {
+                return;
+            }
+            this._alphaMode = value;
+            this.markAsDirty(Material.TextureDirtyFlag);
+        }
+        public get alphaMode(): number {
+            return this._alphaMode;
+        }
 
         @serialize()
         private _needDepthPrePass = false;

+ 3 - 0
src/Materials/babylon.standardMaterial.ts

@@ -69,6 +69,7 @@ module BABYLON {
         public MORPHTARGETS_NORMAL = false;
         public MORPHTARGETS_TANGENT = false;
         public NUM_MORPH_INFLUENCERS = 0;
+        public PREMULTIPLYALPHA = false;
 
         public IMAGEPROCESSING = false;
         public VIGNETTE = false;
@@ -696,6 +697,8 @@ module BABYLON {
                 defines.LINKEMISSIVEWITHDIFFUSE = this._linkEmissiveWithDiffuse;       
 
                 defines.SPECULAROVERALPHA = this._useSpecularOverAlpha;
+
+                defines.PREMULTIPLYALPHA = (this.alphaMode === Engine.ALPHA_PREMULTIPLIED || this.alphaMode === Engine.ALPHA_PREMULTIPLIED_PORTERDUFF);
             }
 
             if (defines._areImageProcessingDirty) {

+ 5 - 0
src/Shaders/default.fragment.fx

@@ -412,5 +412,10 @@ void main(void) {
 	#endif
 #endif
 
+#ifdef PREMULTIPLYALPHA
+	// Convert to associative (premultiplied) format if needed.
+	color.rgb *= color.a;
+#endif
+
 	gl_FragColor = color;
 }