浏览代码

Improving shaders attributes handling

David Catuhe 10 年之前
父节点
当前提交
c9362c4739

+ 1 - 1
Babylon/Audio/babylon.audioengine.ts

@@ -19,7 +19,7 @@
                 }
             } catch (e) {
                 this.canUseWebAudio = false;
-                BABYLON.Tools.Error("Web Audio: " + e.message);
+                Tools.Error("Web Audio: " + e.message);
             }
 
             // create a global volume gain node 

+ 25 - 7
Babylon/Materials/babylon.standardMaterial.js

@@ -73,6 +73,8 @@ var BABYLON;
             var engine = scene.getEngine();
             var defines = [];
             var fallbacks = new BABYLON.EffectFallbacks();
+            var needNormals = false;
+            var needUVs = false;
             // Textures
             if (scene.texturesEnabled) {
                 if (this.diffuseTexture && StandardMaterial.DiffuseTextureEnabled) {
@@ -80,6 +82,7 @@ var BABYLON;
                         return false;
                     }
                     else {
+                        needUVs = true;
                         defines.push("#define DIFFUSE");
                     }
                 }
@@ -88,6 +91,7 @@ var BABYLON;
                         return false;
                     }
                     else {
+                        needUVs = true;
                         defines.push("#define AMBIENT");
                     }
                 }
@@ -96,6 +100,7 @@ var BABYLON;
                         return false;
                     }
                     else {
+                        needUVs = true;
                         defines.push("#define OPACITY");
                         if (this.opacityTexture.getAlphaFromRGB) {
                             defines.push("#define OPACITYRGB");
@@ -107,6 +112,8 @@ var BABYLON;
                         return false;
                     }
                     else {
+                        needNormals = true;
+                        needUVs = true;
                         defines.push("#define REFLECTION");
                         fallbacks.addFallback(0, "REFLECTION");
                     }
@@ -116,6 +123,7 @@ var BABYLON;
                         return false;
                     }
                     else {
+                        needUVs = true;
                         defines.push("#define EMISSIVE");
                     }
                 }
@@ -124,6 +132,7 @@ var BABYLON;
                         return false;
                     }
                     else {
+                        needUVs = true;
                         defines.push("#define SPECULAR");
                         fallbacks.addFallback(0, "SPECULAR");
                     }
@@ -134,6 +143,7 @@ var BABYLON;
                     return false;
                 }
                 else {
+                    needUVs = true;
                     defines.push("#define BUMP");
                     fallbacks.addFallback(0, "BUMP");
                 }
@@ -192,6 +202,7 @@ var BABYLON;
                     if (!light.canAffectMesh(mesh)) {
                         continue;
                     }
+                    needNormals = true;
                     defines.push("#define LIGHT" + lightIndex);
                     if (lightIndex > 0) {
                         fallbacks.addFallback(lightIndex, "LIGHT" + lightIndex);
@@ -263,20 +274,27 @@ var BABYLON;
                         fallbacks.addFallback(fresnelRank, "EMISSIVEFRESNEL");
                         fresnelRank++;
                     }
+                    needNormals = true;
                     defines.push("#define FRESNEL");
                     fallbacks.addFallback(fresnelRank - 1, "FRESNEL");
                 }
             }
             // Attribs
-            var attribs = [BABYLON.VertexBuffer.PositionKind, BABYLON.VertexBuffer.NormalKind];
+            var attribs = [BABYLON.VertexBuffer.PositionKind];
             if (mesh) {
-                if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UVKind)) {
-                    attribs.push(BABYLON.VertexBuffer.UVKind);
-                    defines.push("#define UV1");
+                if (needNormals && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.NormalKind)) {
+                    attribs.push(BABYLON.VertexBuffer.NormalKind);
+                    defines.push("#define NORMAL");
                 }
-                if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UV2Kind)) {
-                    attribs.push(BABYLON.VertexBuffer.UV2Kind);
-                    defines.push("#define UV2");
+                if (needUVs) {
+                    if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UVKind)) {
+                        attribs.push(BABYLON.VertexBuffer.UVKind);
+                        defines.push("#define UV1");
+                    }
+                    if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UV2Kind)) {
+                        attribs.push(BABYLON.VertexBuffer.UV2Kind);
+                        defines.push("#define UV2");
+                    }
                 }
                 if (mesh.useVertexColors && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.ColorKind)) {
                     attribs.push(BABYLON.VertexBuffer.ColorKind);

+ 25 - 8
Babylon/Materials/babylon.standardMaterial.ts

@@ -89,6 +89,8 @@
             var engine = scene.getEngine();
             var defines = [];
             var fallbacks = new EffectFallbacks();
+            var needNormals = false;
+            var needUVs = false;
 
             // Textures
             if (scene.texturesEnabled) {
@@ -96,6 +98,7 @@
                     if (!this.diffuseTexture.isReady()) {
                         return false;
                     } else {
+                        needUVs = true;
                         defines.push("#define DIFFUSE");
                     }
                 }
@@ -104,6 +107,7 @@
                     if (!this.ambientTexture.isReady()) {
                         return false;
                     } else {
+                        needUVs = true;
                         defines.push("#define AMBIENT");
                     }
                 }
@@ -112,6 +116,7 @@
                     if (!this.opacityTexture.isReady()) {
                         return false;
                     } else {
+                        needUVs = true;
                         defines.push("#define OPACITY");
 
                         if (this.opacityTexture.getAlphaFromRGB) {
@@ -124,6 +129,8 @@
                     if (!this.reflectionTexture.isReady()) {
                         return false;
                     } else {
+                        needNormals = true;
+                        needUVs = true;
                         defines.push("#define REFLECTION");
                         fallbacks.addFallback(0, "REFLECTION");
                     }
@@ -133,6 +140,7 @@
                     if (!this.emissiveTexture.isReady()) {
                         return false;
                     } else {
+                        needUVs = true;
                         defines.push("#define EMISSIVE");
                     }
                 }
@@ -141,6 +149,7 @@
                     if (!this.specularTexture.isReady()) {
                         return false;
                     } else {
+                        needUVs = true;
                         defines.push("#define SPECULAR");
                         fallbacks.addFallback(0, "SPECULAR");
                     }
@@ -151,6 +160,7 @@
                 if (!this.bumpTexture.isReady()) {
                     return false;
                 } else {
+                    needUVs = true;
                     defines.push("#define BUMP");
                     fallbacks.addFallback(0, "BUMP");
                 }
@@ -224,7 +234,7 @@
                     if (!light.canAffectMesh(mesh)) {
                         continue;
                     }
-
+                    needNormals = true;
                     defines.push("#define LIGHT" + lightIndex);
 
                     if (lightIndex > 0) {
@@ -312,6 +322,7 @@
                         fresnelRank++;
                     }
 
+                    needNormals = true;
                     defines.push("#define FRESNEL");
                     fallbacks.addFallback(fresnelRank - 1, "FRESNEL");
                 }
@@ -319,15 +330,21 @@
 
 
             // Attribs
-            var attribs = [VertexBuffer.PositionKind, VertexBuffer.NormalKind];
+            var attribs = [VertexBuffer.PositionKind];
             if (mesh) {
-                if (mesh.isVerticesDataPresent(VertexBuffer.UVKind)) {
-                    attribs.push(VertexBuffer.UVKind);
-                    defines.push("#define UV1");
+                if (needNormals && mesh.isVerticesDataPresent(VertexBuffer.NormalKind)) {
+                    attribs.push(VertexBuffer.NormalKind);
+                    defines.push("#define NORMAL");
                 }
-                if (mesh.isVerticesDataPresent(VertexBuffer.UV2Kind)) {
-                    attribs.push(VertexBuffer.UV2Kind);
-                    defines.push("#define UV2");
+                if (needUVs) {
+                    if (mesh.isVerticesDataPresent(VertexBuffer.UVKind)) {
+                        attribs.push(VertexBuffer.UVKind);
+                        defines.push("#define UV1");
+                    }
+                    if (mesh.isVerticesDataPresent(VertexBuffer.UV2Kind)) {
+                        attribs.push(VertexBuffer.UV2Kind);
+                        defines.push("#define UV2");
+                    }
                 }
                 if (mesh.useVertexColors && mesh.isVerticesDataPresent(VertexBuffer.ColorKind)) {
                     attribs.push(VertexBuffer.ColorKind);

+ 9 - 0
Babylon/Shaders/default.fragment.fx

@@ -18,7 +18,10 @@ uniform vec3 vEmissiveColor;
 
 // Input
 varying vec3 vPositionW;
+
+#ifdef NORMAL
 varying vec3 vNormalW;
+#endif
 
 #ifdef VERTEXCOLOR
 varying vec4 vColor;
@@ -492,12 +495,18 @@ void main(void) {
 #endif
 
 	// Bump
+#ifdef NORMAL
 	vec3 normalW = normalize(vNormalW);
+#else
+	vec3 normalW = vec3(1.0, 1.0, 1.0);
+#endif
+
 
 #ifdef BUMP
 	normalW = perturbNormal(viewDirectionW);
 #endif
 
+
 	// Ambient color
 	vec3 baseAmbientColor = vec3(1., 1., 1.);
 

+ 7 - 0
Babylon/Shaders/default.vertex.fx

@@ -4,7 +4,9 @@ precision highp float;
 
 // Attributes
 attribute vec3 position;
+#ifdef NORMAL
 attribute vec3 normal;
+#endif
 #ifdef UV1
 attribute vec2 uv;
 #endif
@@ -79,7 +81,9 @@ uniform float pointSize;
 
 // Output
 varying vec3 vPositionW;
+#ifdef NORMAL
 varying vec3 vNormalW;
+#endif
 
 #ifdef VERTEXCOLOR
 varying vec4 vColor;
@@ -147,7 +151,10 @@ void main(void) {
 
 	vec4 worldPos = finalWorld * vec4(position, 1.0);
 	vPositionW = vec3(worldPos);
+
+#ifdef NORMAL
 	vNormalW = normalize(vec3(finalWorld * vec4(normal, 0.0)));
+#endif
 
 	// Texture coordinates
 #ifndef UV1

+ 3 - 0
Babylon/babylon.scene.js

@@ -1150,6 +1150,9 @@ var BABYLON;
             this._lastFrameDuration = BABYLON.Tools.Now - startDate;
         };
         Scene.prototype._updateAudioParameters = function () {
+            if (this.mainSoundTrack.soundCollection.length === 0 || this.soundTracks.length === 0) {
+                return;
+            }
             var listeningCamera;
             var audioEngine = BABYLON.Engine.audioEngine;
             if (this.activeCameras.length > 0) {

+ 4 - 0
Babylon/babylon.scene.ts

@@ -1474,6 +1474,10 @@
         }
 
         private _updateAudioParameters() {
+            if (this.mainSoundTrack.soundCollection.length === 0 || this.soundTracks.length === 0) {
+                return;
+            }
+
             var listeningCamera: Camera;
             var audioEngine = Engine.audioEngine;
 

文件差异内容过多而无法显示
+ 30 - 9
babylon.2.1-alpha.debug.js


文件差异内容过多而无法显示
+ 6 - 6
babylon.2.1-alpha.js


+ 2 - 0
readme.md

@@ -76,6 +76,8 @@ Online [asset converter](http://www.babylonjs.com/converter.html) where you can
  - Shadow Maps and Variance Shadow Maps
  - Rendering layers
  - Post-processes (blur, refraction, black'n'white, fxaa, customs...)
+ - SSAO
+ - Volumetric Light Scattering 
  - Lens flares
  - Multi-views
 -  Textures: