Prechádzať zdrojové kódy

Fixed fire Material (clone and shaders)

luaacro 9 rokov pred
rodič
commit
b94b7a8a5b

Rozdielové dáta súboru neboli zobrazené, pretože súbor je príliš veľký
+ 40 - 35
materialsLibrary/dist/babylon.fireMaterial.js


Rozdielové dáta súboru neboli zobrazené, pretože súbor je príliš veľký
+ 1 - 1
materialsLibrary/dist/babylon.fireMaterial.min.js


+ 16 - 9
materialsLibrary/dist/babylon.simpleMaterial.js

@@ -1,5 +1,13 @@
 /// <reference path="../../../dist/preview release/babylon.d.ts"/>
 
+var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
+    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") return Reflect.decorate(decorators, target, key, desc);
+    switch (arguments.length) {
+        case 2: return decorators.reduceRight(function(o, d) { return (d && d(o)) || o; }, target);
+        case 3: return decorators.reduceRight(function(o, d) { return (d && d(target, key)), void 0; }, void 0);
+        case 4: return decorators.reduceRight(function(o, d) { return (d && d(target, key, o)) || o; }, desc);
+    }
+};
 var BABYLON;
 (function (BABYLON) {
     var maxSimultaneousLights = 4;
@@ -278,15 +286,8 @@ var BABYLON;
             _super.prototype.dispose.call(this, forceDisposeEffect);
         };
         SimpleMaterial.prototype.clone = function (name) {
-            var newMaterial = new SimpleMaterial(name, this.getScene());
-            // Base material
-            this.copyTo(newMaterial);
-            // Simple material
-            if (this.diffuseTexture && this.diffuseTexture.clone) {
-                newMaterial.diffuseTexture = this.diffuseTexture.clone();
-            }
-            newMaterial.diffuseColor = this.diffuseColor.clone();
-            return newMaterial;
+            var _this = this;
+            return BABYLON.SerializationHelper.Clone(function () { return new SimpleMaterial(name, _this.getScene()); }, this);
         };
         SimpleMaterial.prototype.serialize = function () {
             var serializationObject = _super.prototype.serialize.call(this);
@@ -315,6 +316,12 @@ var BABYLON;
             }
             return material;
         };
+        __decorate([
+            BABYLON.serializeAsTexture()
+        ], SimpleMaterial.prototype, "diffuseTexture");
+        __decorate([
+            BABYLON.serializeAsColor3("diffuseColor")
+        ], SimpleMaterial.prototype, "diffuseColor");
         return SimpleMaterial;
     })(BABYLON.Material);
     BABYLON.SimpleMaterial = SimpleMaterial;

Rozdielové dáta súboru neboli zobrazené, pretože súbor je príliš veľký
+ 1 - 1
materialsLibrary/dist/babylon.simpleMaterial.min.js


+ 0 - 1
materialsLibrary/dist/dts/babylon.fireMaterial.d.ts

@@ -5,7 +5,6 @@ declare module BABYLON {
         distortionTexture: BaseTexture;
         opacityTexture: BaseTexture;
         diffuseColor: Color3;
-        disableLighting: boolean;
         speed: number;
         private _scaledDiffuse;
         private _renderId;

+ 18 - 55
materialsLibrary/materials/fire/babylon.fireMaterial.ts

@@ -10,12 +10,10 @@ module BABYLON {
         public POINTSIZE = false;
         public FOG = false;
         public UV1 = false;
-        public NORMAL = false;
         public VERTEXCOLOR = false;
         public VERTEXALPHA = false;
-        public BONES = false;
-        public BONES4 = false;
         public BonesPerMesh = 0;
+        public NUM_BONE_INFLUENCERS = 0;
         public INSTANCES = false;
 
         constructor() {
@@ -25,13 +23,19 @@ module BABYLON {
     }
 
     export class FireMaterial extends Material {
+        @serializeAsTexture()
         public diffuseTexture: BaseTexture;
+        
+        @serializeAsTexture()
         public distortionTexture: BaseTexture;
+        
+        @serializeAsTexture()
         public opacityTexture: BaseTexture;
-
+        
+        @serialize("diffuseColor")
         public diffuseColor = new Color3(1, 1, 1);
-        public disableLighting = false;
         
+        @serialize()
         public speed = 1.0;
         
         private _scaledDiffuse = new Color3();
@@ -131,9 +135,6 @@ module BABYLON {
             
             // Attribs
             if (mesh) {
-                if (needNormals && mesh.isVerticesDataPresent(VertexBuffer.NormalKind)) {
-                    this._defines.NORMAL = true;
-                }
                 if (needUVs) {
                     if (mesh.isVerticesDataPresent(VertexBuffer.UVKind)) {
                         this._defines.UV1 = true;
@@ -147,9 +148,8 @@ module BABYLON {
                     }
                 }
                 if (mesh.useBones && mesh.computeBonesUsingShaders) {
-                    this._defines.BONES = true;
+                    this._defines.NUM_BONE_INFLUENCERS = mesh.numBoneInfluencers;
                     this._defines.BonesPerMesh = (mesh.skeleton.bones.length + 1);
-                    this._defines.BONES4 = true;
                 }
 
                 // Instances
@@ -169,18 +169,14 @@ module BABYLON {
                 if (this._defines.FOG) {
                     fallbacks.addFallback(1, "FOG");
                 }
-             
-                if (this._defines.BONES4) {
-                    fallbacks.addFallback(0, "BONES4");
+                
+                if (this._defines.NUM_BONE_INFLUENCERS > 0) {
+                    fallbacks.addCPUSkinningFallback(0, mesh);
                 }
 
                 //Attributes
                 var attribs = [VertexBuffer.PositionKind];
 
-                if (this._defines.NORMAL) {
-                    attribs.push(VertexBuffer.NormalKind);
-                }
-
                 if (this._defines.UV1) {
                     attribs.push(VertexBuffer.UVKind);
                 }
@@ -189,17 +185,8 @@ module BABYLON {
                     attribs.push(VertexBuffer.ColorKind);
                 }
 
-                if (this._defines.BONES) {
-                    attribs.push(VertexBuffer.MatricesIndicesKind);
-                    attribs.push(VertexBuffer.MatricesWeightsKind);
-                }
-
-                if (this._defines.INSTANCES) {
-                    attribs.push("world0");
-                    attribs.push("world1");
-                    attribs.push("world2");
-                    attribs.push("world3");
-                }
+                MaterialHelper.PrepareAttributesForBones(attribs, mesh, this._defines, fallbacks);
+                MaterialHelper.PrepareAttributesForInstances(attribs, this._defines);
 
                 // Legacy browser patch
                 var shaderName = "fire";
@@ -252,9 +239,7 @@ module BABYLON {
             this._effect.setMatrix("viewProjection", scene.getTransformMatrix());
 
             // Bones
-            if (mesh && mesh.useBones && mesh.computeBonesUsingShaders) {
-                this._effect.setMatrices("mBones", mesh.skeleton.getTransformMatrices(mesh));
-            }
+            MaterialHelper.BindBonesParameters(mesh, this._effect);
 
             if (scene.getCachedMaterial() !== this) {
                 // Textures        
@@ -290,10 +275,7 @@ module BABYLON {
             }
             
             // Fog
-            if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== Scene.FOGMODE_NONE) {
-                this._effect.setFloat4("vFogInfos", scene.fogMode, scene.fogStart, scene.fogEnd, scene.fogDensity);
-                this._effect.setColor3("vFogColor", scene.fogColor);
-            }
+            MaterialHelper.BindFogParameters(scene, mesh, this._effect);
             
             // Time
             this._lastTime += scene.getEngine().getDeltaTime();
@@ -333,24 +315,7 @@ module BABYLON {
         }
 
         public clone(name: string): FireMaterial {
-            var newMaterial = new FireMaterial(name, this.getScene());
-
-            // Base material
-            this.copyTo(newMaterial);
-
-            // Fire material
-            if (this.diffuseTexture && this.diffuseTexture.clone) {
-                newMaterial.diffuseTexture = this.diffuseTexture.clone();
-            }
-            if (this.distortionTexture && this.distortionTexture.clone) {
-                newMaterial.distortionTexture = this.distortionTexture.clone();
-            }
-            if (this.opacityTexture && this.opacityTexture.clone) {
-                newMaterial.opacityTexture = this.opacityTexture.clone();
-            }
-
-            newMaterial.diffuseColor = this.diffuseColor.clone();
-            return newMaterial;
+            return SerializationHelper.Clone<FireMaterial>(() => new FireMaterial(name, this.getScene()), this);
         }
 		
 		public serialize(): any {
@@ -359,7 +324,6 @@ module BABYLON {
             serializationObject.customType      = "BABYLON.FireMaterial";
             serializationObject.diffuseColor    = this.diffuseColor.asArray();
             serializationObject.speed           = this.speed;
-            serializationObject.disableLighting = this.disableLighting;
 
             if (this.diffuseTexture) {
                 serializationObject.diffuseTexture = this.diffuseTexture.serialize();
@@ -381,7 +345,6 @@ module BABYLON {
 
             material.diffuseColor   = Color3.FromArray(source.diffuseColor);
             material.speed          = source.speed;
-            material.disableLighting    = source.disableLighting;
 
             material.alpha          = source.alpha;
 

+ 4 - 57
materialsLibrary/materials/fire/fire.fragment.fx

@@ -6,10 +6,6 @@ uniform vec3 vEyePosition;
 // Input
 varying vec3 vPositionW;
 
-#ifdef NORMAL
-varying vec3 vNormalW;
-#endif
-
 #ifdef VERTEXCOLOR
 varying vec4 vColor;
 #endif
@@ -29,46 +25,10 @@ varying vec2 vDistortionCoords1;
 varying vec2 vDistortionCoords2;
 varying vec2 vDistortionCoords3;
 
-#ifdef CLIPPLANE
-varying float fClipDistance;
-#endif
+#include<clipPlaneFragmentDeclaration>
 
 // Fog
-#ifdef FOG
-
-#define FOGMODE_NONE    0.
-#define FOGMODE_EXP     1.
-#define FOGMODE_EXP2    2.
-#define FOGMODE_LINEAR  3.
-#define E 2.71828
-
-uniform vec4 vFogInfos;
-uniform vec3 vFogColor;
-varying float fFogDistance;
-
-float CalcFogFactor()
-{
-	float fogCoeff = 1.0;
-	float fogStart = vFogInfos.y;
-	float fogEnd = vFogInfos.z;
-	float fogDensity = vFogInfos.w;
-
-	if (FOGMODE_LINEAR == vFogInfos.x)
-	{
-		fogCoeff = (fogEnd - fFogDistance) / (fogEnd - fogStart);
-	}
-	else if (FOGMODE_EXP == vFogInfos.x)
-	{
-		fogCoeff = 1.0 / pow(E, fFogDistance * fogDensity);
-	}
-	else if (FOGMODE_EXP2 == vFogInfos.x)
-	{
-		fogCoeff = 1.0 / pow(E, fFogDistance * fFogDistance * fogDensity * fogDensity);
-	}
-
-	return clamp(fogCoeff, 0.0, 1.0);
-}
-#endif
+#include<fogFragmentDeclaration>
 
 vec4 bx2(vec4 x)
 {
@@ -77,10 +37,7 @@ vec4 bx2(vec4 x)
 
 void main(void) {
 	// Clip plane
-#ifdef CLIPPLANE
-	if (fClipDistance > 0.0)
-		discard;
-#endif
+#include<clipPlaneFragment>
 
 	vec3 viewDirectionW = normalize(vEyePosition - vPositionW);
 
@@ -123,13 +80,6 @@ void main(void) {
 	baseColor.rgb *= vColor.rgb;
 #endif
 
-	// Bump
-#ifdef NORMAL
-	vec3 normalW = normalize(vNormalW);
-#else
-	vec3 normalW = vec3(1.0, 1.0, 1.0);
-#endif
-
 	// Lighting
 	vec3 diffuseBase = vec3(1.0, 1.0, 1.0);
 
@@ -140,10 +90,7 @@ void main(void) {
 	// Composition
 	vec4 color = vec4(baseColor.rgb, alpha);
 
-#ifdef FOG
-	float fog = CalcFogFactor();
-	color.rgb = fog * color.rgb + (1.0 - fog) * vFogColor;
-#endif
+#include<fogFragment>
 
 	gl_FragColor = color;
 }

+ 10 - 59
materialsLibrary/materials/fire/fire.vertex.fx

@@ -2,9 +2,6 @@ precision highp float;
 
 // Attributes
 attribute vec3 position;
-#ifdef NORMAL
-attribute vec3 normal;
-#endif
 #ifdef UV1
 attribute vec2 uv;
 #endif
@@ -14,21 +11,11 @@ attribute vec2 uv2;
 #ifdef VERTEXCOLOR
 attribute vec4 color;
 #endif
-#ifdef BONES
-attribute vec4 matricesIndices;
-attribute vec4 matricesWeights;
-#endif
 
-// Uniforms
+#include<bonesDeclaration>
 
-#ifdef INSTANCES
-attribute vec4 world0;
-attribute vec4 world1;
-attribute vec4 world2;
-attribute vec4 world3;
-#else
-uniform mat4 world;
-#endif
+// Uniforms
+#include<instancesDeclaration>
 
 uniform mat4 view;
 uniform mat4 viewProjection;
@@ -37,32 +24,21 @@ uniform mat4 viewProjection;
 varying vec2 vDiffuseUV;
 #endif
 
-#ifdef BONES
-uniform mat4 mBones[BonesPerMesh];
-#endif
-
 #ifdef POINTSIZE
 uniform float pointSize;
 #endif
 
 // Output
 varying vec3 vPositionW;
-#ifdef NORMAL
-varying vec3 vNormalW;
-#endif
 
 #ifdef VERTEXCOLOR
 varying vec4 vColor;
 #endif
 
-#ifdef CLIPPLANE
-uniform vec4 vClipPlane;
-varying float fClipDistance;
-#endif
+#include<clipPlaneVertexDeclaration>
 
-#ifdef FOG
-varying float fFogDistance;
-#endif
+#include<fogVertexDeclaration>
+#include<shadowsVertexDeclaration>
 
 // Fire
 uniform float time;
@@ -73,36 +49,15 @@ varying vec2 vDistortionCoords2;
 varying vec2 vDistortionCoords3;
 
 void main(void) {
-	mat4 finalWorld;
-
-#ifdef INSTANCES
-	finalWorld = mat4(world0, world1, world2, world3);
-#else
-	finalWorld = world;
-#endif
 
-#ifdef BONES
-	mat4 m0 = mBones[int(matricesIndices.x)] * matricesWeights.x;
-	mat4 m1 = mBones[int(matricesIndices.y)] * matricesWeights.y;
-	mat4 m2 = mBones[int(matricesIndices.z)] * matricesWeights.z;
+#include<instancesVertex>
+#include<bonesVertex>
 
-#ifdef BONES4
-	mat4 m3 = mBones[int(matricesIndices.w)] * matricesWeights.w;
-	finalWorld = finalWorld * (m0 + m1 + m2 + m3);
-#else
-	finalWorld = finalWorld * (m0 + m1 + m2);
-#endif 
-
-#endif
 	gl_Position = viewProjection * finalWorld * vec4(position, 1.0);
 
 	vec4 worldPos = finalWorld * vec4(position, 1.0);
 	vPositionW = vec3(worldPos);
 
-#ifdef NORMAL
-	vNormalW = normalize(vec3(finalWorld * vec4(normal, 0.0)));
-#endif
-
 	// Texture coordinates
 #ifdef DIFFUSE
 	vDiffuseUV = uv;
@@ -110,14 +65,10 @@ void main(void) {
 #endif
 
 	// Clip plane
-#ifdef CLIPPLANE
-	fClipDistance = dot(worldPos, vClipPlane);
-#endif
+#include<clipPlaneVertex>
 
 	// Fog
-#ifdef FOG
-	fFogDistance = (view * worldPos).z;
-#endif
+#include<fogVertex>
 
 	// Vertex color
 #ifdef VERTEXCOLOR

+ 1 - 1
materialsLibrary/materials/simple/babylon.simpleMaterial.ts

@@ -61,7 +61,7 @@ module BABYLON {
         @serializeAsTexture()
         public diffuseTexture: BaseTexture;
 
-        @serialize("diffuseColor")
+        @serializeAsColor3("diffuseColor")
         public diffuseColor = new Color3(1, 1, 1);
         public disableLighting = false;