瀏覽代碼

Merge pull request #194 from Platane/master

useAlphaFromDiffuseTexture option for standard material
deltakosh 11 年之前
父節點
當前提交
d55fa703af

+ 10 - 1
Babylon/Materials/babylon.standardMaterial.js

@@ -18,6 +18,7 @@ var BABYLON;
             this.specularColor = new BABYLON.Color3(1, 1, 1);
             this.specularPower = 64;
             this.emissiveColor = new BABYLON.Color3(0, 0, 0);
+            this.useAlphaFromDiffuseTexture = false;
             this._cachedDefines = null;
             this._renderTargets = new BABYLON.SmartArray(16);
             this._worldViewProjectionMatrix = BABYLON.Matrix.Zero();
@@ -38,13 +39,17 @@ var BABYLON;
             };
         }
         StandardMaterial.prototype.needAlphaBlending = function () {
-            return (this.alpha < 1.0) || (this.opacityTexture != null);
+            return (this.alpha < 1.0) || (this.opacityTexture != null) || this._shouldUseAlphaFromDiffuseTexture();
         };
 
         StandardMaterial.prototype.needAlphaTesting = function () {
             return this.diffuseTexture != null && this.diffuseTexture.hasAlpha;
         };
 
+        StandardMaterial.prototype._shouldUseAlphaFromDiffuseTexture = function(){
+            return this.diffuseTexture != null && this.diffuseTexture.hasAlpha && this.useAlphaFromDiffuseTexture;
+        };
+
         // Methods
         StandardMaterial.prototype.isReady = function (mesh) {
             if (this.checkReadyOnlyOnce) {
@@ -135,6 +140,10 @@ var BABYLON;
                 defines.push("#define ALPHATEST");
             }
 
+            if(this._shouldUseAlphaFromDiffuseTexture()) {
+                defines.push("#define ALPHAFROMDIFFUSE");
+            }
+
             // Fog
             if (scene.fogMode !== BABYLON.Scene.FOGMODE_NONE) {
                 defines.push("#define FOG");

+ 10 - 1
Babylon/Materials/babylon.standardMaterial.ts

@@ -15,6 +15,7 @@
         public specularColor = new BABYLON.Color3(1, 1, 1);
         public specularPower = 64;
         public emissiveColor = new BABYLON.Color3(0, 0, 0);
+        public useAlphaFromDiffuseTexture = false;
 
         private _cachedDefines = null;
         private _renderTargets = new BABYLON.SmartArray(16);
@@ -41,13 +42,17 @@
         }
 
         public needAlphaBlending(): boolean {
-            return (this.alpha < 1.0) || (this.opacityTexture != null);
+            return (this.alpha < 1.0) || (this.opacityTexture != null) || this._shouldUseAlphaFromDiffuseTexture();
         }
 
         public needAlphaTesting(): boolean {
             return this.diffuseTexture != null && this.diffuseTexture.hasAlpha;
         }
 
+        private _shouldUseAlphaFromDiffuseTexture(): boolean {
+            return this.diffuseTexture != null && this.diffuseTexture.hasAlpha && this.useAlphaFromDiffuseTexture;
+        }
+
         // Methods   
         public isReady(mesh?: Mesh): boolean {
             if (this.checkReadyOnlyOnce) {
@@ -138,6 +143,10 @@
                 defines.push("#define ALPHATEST");
             }
 
+            if (this._shouldUseAlphaFromDiffuseTexture()) {
+                defines.push("#define ALPHAFROMDIFFUSE");
+            }
+
             // Fog
             if (scene.fogMode !== BABYLON.Scene.FOGMODE_NONE) {
                 defines.push("#define FOG");

+ 7 - 3
Babylon/Shaders/default.fragment.fx

@@ -414,6 +414,9 @@ void main(void) {
 	vec4 baseColor = vec4(1., 1., 1., 1.);
 	vec3 diffuseColor = vDiffuseColor.rgb;
 
+	// Alpha
+	float alpha = vDiffuseColor.a;
+
 #ifdef VERTEXCOLOR
 	diffuseColor *= vColor;
 #endif
@@ -426,6 +429,10 @@ void main(void) {
 		discard;
 #endif
 
+#ifdef ALPHAFROMDIFFUSE
+	alpha *= baseColor.a;
+#endif
+
 	baseColor.rgb *= vDiffuseInfos.y;
 #endif
 
@@ -565,9 +572,6 @@ void main(void) {
 	}
 #endif
 
-	// Alpha
-	float alpha = vDiffuseColor.a;
-
 #ifdef OPACITY
 	vec4 opacityMap = texture2D(opacitySampler, vOpacityUV);
 	opacityMap.rgb = opacityMap.rgb * vec3(0.3, 0.59, 0.11) * opacityMap.a;