Jelajahi Sumber

Merge pull request #1330 from julien-moreau/master

Fixed glTF File Loader (FloatX32Array.from() no longer used)
David Catuhe 9 tahun lalu
induk
melakukan
3f34a2ca1a

+ 34 - 25
loaders/glTF/babylon.glTFFileLoader.js

@@ -1,5 +1,3 @@
-
-
 var BABYLON;
 (function (BABYLON) {
     /**
@@ -89,6 +87,10 @@ var BABYLON;
         EBlendingFunction[EBlendingFunction["SRC_ALPHA_SATURATE"] = 776] = "SRC_ALPHA_SATURATE";
     })(BABYLON.EBlendingFunction || (BABYLON.EBlendingFunction = {}));
     var EBlendingFunction = BABYLON.EBlendingFunction;
+})(BABYLON || (BABYLON = {}));
+
+var BABYLON;
+(function (BABYLON) {
     /**
     * Tokenizer. Used for shaders compatibility
     * Automatically map world, view, projection, worldViewProjection, attributes and so on
@@ -673,7 +675,8 @@ var BABYLON;
                     accessor = gltfRuntime.accessors[attributes[semantic]];
                     buffer = BABYLON.GLTFUtils.GetBufferFromAccessor(gltfRuntime, accessor);
                     if (semantic === "NORMAL") {
-                        tempVertexData.normals = Float32Array.from(buffer);
+                        tempVertexData.normals = new Float32Array(buffer.length);
+                        tempVertexData.normals.set(buffer);
                     }
                     else if (semantic === "POSITION") {
                         if (GLTFFileLoader.HomogeneousCoordinates) {
@@ -685,31 +688,37 @@ var BABYLON;
                             }
                         }
                         else {
-                            tempVertexData.positions = Float32Array.from(buffer);
+                            tempVertexData.positions = new Float32Array(buffer.length);
+                            tempVertexData.positions.set(buffer);
                         }
                         verticesCounts.push(tempVertexData.positions.length);
                     }
                     else if (semantic.indexOf("TEXCOORD_") !== -1) {
                         var channel = Number(semantic.split("_")[1]);
                         var uvKind = BABYLON.VertexBuffer.UVKind + (channel === 0 ? "" : (channel + 1));
-                        var uvs = Float32Array.from(buffer);
+                        var uvs = new Float32Array(buffer.length);
+                        uvs.set(buffer);
                         normalizeUVs(uvs);
                         tempVertexData.set(uvs, uvKind);
                     }
                     else if (semantic === "JOINT") {
-                        tempVertexData.matricesIndices = Float32Array.from(buffer);
+                        tempVertexData.matricesIndices = new Float32Array(buffer.length);
+                        tempVertexData.matricesIndices.set(buffer);
                     }
                     else if (semantic === "WEIGHT") {
-                        tempVertexData.matricesWeights = Float32Array.from(buffer);
+                        tempVertexData.matricesWeights = new Float32Array(buffer.length);
+                        tempVertexData.matricesWeights.set(buffer);
                     }
                     else if (semantic === "COLOR") {
-                        tempVertexData.colors = Float32Array.from(buffer);
+                        tempVertexData.colors = new Float32Array(buffer.length);
+                        tempVertexData.colors.set(buffer);
                     }
                 }
                 // Indices
                 accessor = gltfRuntime.accessors[primitive.indices];
                 buffer = BABYLON.GLTFUtils.GetBufferFromAccessor(gltfRuntime, accessor);
-                tempVertexData.indices = Int32Array.from(buffer);
+                tempVertexData.indices = new Int32Array(buffer.length);
+                tempVertexData.indices.set(buffer);
                 indexCounts.push(tempVertexData.indices.length);
                 vertexData.merge(tempVertexData);
                 tempVertexData = undefined;
@@ -1032,7 +1041,7 @@ var BABYLON;
         for (var unif in unTreatedUniforms) {
             var uniform = unTreatedUniforms[unif];
             var type = uniform.type;
-            if (type === EParameterType.FLOAT_MAT2 || type === EParameterType.FLOAT_MAT3 || type === EParameterType.FLOAT_MAT4) {
+            if (type === BABYLON.EParameterType.FLOAT_MAT2 || type === BABYLON.EParameterType.FLOAT_MAT3 || type === BABYLON.EParameterType.FLOAT_MAT4) {
                 if (uniform.semantic && !uniform.source && !uniform.node) {
                     BABYLON.GLTFUtils.SetMatrix(gltfRuntime.scene, mesh, uniform, unif, shaderMaterial.getEffect());
                 }
@@ -1052,7 +1061,7 @@ var BABYLON;
                 if (!value) {
                     continue;
                 }
-                if (type === EParameterType.SAMPLER_2D) {
+                if (type === BABYLON.EParameterType.SAMPLER_2D) {
                     var texture = gltfRuntime.textures[value].babylonTexture;
                     if (texture === null) {
                         continue;
@@ -1083,7 +1092,7 @@ var BABYLON;
                 continue;
             }
             // Texture (sampler2D)
-            if (type === EParameterType.SAMPLER_2D) {
+            if (type === BABYLON.EParameterType.SAMPLER_2D) {
                 var texture = gltfRuntime.textures[value];
                 var sampler = gltfRuntime.samplers[texture.sampler];
                 if (!texture || !texture.source) {
@@ -1091,10 +1100,10 @@ var BABYLON;
                 }
                 var source = gltfRuntime.images[texture.source];
                 var newTexture = null;
-                var createMipMaps = (sampler.minFilter === ETextureFilterType.NEAREST_MIPMAP_NEAREST) ||
-                    (sampler.minFilter === ETextureFilterType.NEAREST_MIPMAP_LINEAR) ||
-                    (sampler.minFilter === ETextureFilterType.LINEAR_MIPMAP_NEAREST) ||
-                    (sampler.minFilter === ETextureFilterType.LINEAR_MIPMAP_LINEAR);
+                var createMipMaps = (sampler.minFilter === BABYLON.ETextureFilterType.NEAREST_MIPMAP_NEAREST) ||
+                    (sampler.minFilter === BABYLON.ETextureFilterType.NEAREST_MIPMAP_LINEAR) ||
+                    (sampler.minFilter === BABYLON.ETextureFilterType.LINEAR_MIPMAP_NEAREST) ||
+                    (sampler.minFilter === BABYLON.ETextureFilterType.LINEAR_MIPMAP_LINEAR);
                 var samplingMode = BABYLON.Texture.BILINEAR_SAMPLINGMODE;
                 if (BABYLON.GLTFUtils.IsBase64(source.uri)) {
                     newTexture = new BABYLON.Texture(source.uri, gltfRuntime.scene, !createMipMaps, true, samplingMode, null, null, source.uri, true);
@@ -1203,7 +1212,7 @@ var BABYLON;
                         uniforms.push(unif);
                     }
                 }
-                else if (uniformParameter.type === EParameterType.SAMPLER_2D) {
+                else if (uniformParameter.type === BABYLON.EParameterType.SAMPLER_2D) {
                     samplers.push(unif);
                 }
                 else {
@@ -1267,27 +1276,27 @@ var BABYLON;
             shaderMaterial.onCompiled = onShaderCompileSuccess(gltfRuntime, shaderMaterial, technique, material, unTreatedUniforms);
             if (states.functions) {
                 var functions = states.functions;
-                if (functions.cullFace && functions.cullFace[0] !== ECullingType.BACK) {
+                if (functions.cullFace && functions.cullFace[0] !== BABYLON.ECullingType.BACK) {
                     shaderMaterial.backFaceCulling = false;
                 }
                 var blendFunc = functions.blendFuncSeparate;
                 if (blendFunc) {
-                    if (blendFunc[0] === EBlendingFunction.SRC_ALPHA && blendFunc[1] === EBlendingFunction.ONE_MINUS_SRC_ALPHA && blendFunc[2] === EBlendingFunction.ONE && blendFunc[3] === EBlendingFunction.ONE) {
+                    if (blendFunc[0] === BABYLON.EBlendingFunction.SRC_ALPHA && blendFunc[1] === BABYLON.EBlendingFunction.ONE_MINUS_SRC_ALPHA && blendFunc[2] === BABYLON.EBlendingFunction.ONE && blendFunc[3] === BABYLON.EBlendingFunction.ONE) {
                         shaderMaterial.alphaMode = BABYLON.Engine.ALPHA_COMBINE;
                     }
-                    else if (blendFunc[0] === EBlendingFunction.ONE && blendFunc[1] === EBlendingFunction.ONE && blendFunc[2] === EBlendingFunction.ZERO && blendFunc[3] === EBlendingFunction.ONE) {
+                    else if (blendFunc[0] === BABYLON.EBlendingFunction.ONE && blendFunc[1] === BABYLON.EBlendingFunction.ONE && blendFunc[2] === BABYLON.EBlendingFunction.ZERO && blendFunc[3] === BABYLON.EBlendingFunction.ONE) {
                         shaderMaterial.alphaMode = BABYLON.Engine.ALPHA_ONEONE;
                     }
-                    else if (blendFunc[0] === EBlendingFunction.SRC_ALPHA && blendFunc[1] === EBlendingFunction.ONE && blendFunc[2] === EBlendingFunction.ZERO && blendFunc[3] === EBlendingFunction.ONE) {
+                    else if (blendFunc[0] === BABYLON.EBlendingFunction.SRC_ALPHA && blendFunc[1] === BABYLON.EBlendingFunction.ONE && blendFunc[2] === BABYLON.EBlendingFunction.ZERO && blendFunc[3] === BABYLON.EBlendingFunction.ONE) {
                         shaderMaterial.alphaMode = BABYLON.Engine.ALPHA_ADD;
                     }
-                    else if (blendFunc[0] === EBlendingFunction.ZERO && blendFunc[1] === EBlendingFunction.ONE_MINUS_SRC_COLOR && blendFunc[2] === EBlendingFunction.ONE && blendFunc[3] === EBlendingFunction.ONE) {
+                    else if (blendFunc[0] === BABYLON.EBlendingFunction.ZERO && blendFunc[1] === BABYLON.EBlendingFunction.ONE_MINUS_SRC_COLOR && blendFunc[2] === BABYLON.EBlendingFunction.ONE && blendFunc[3] === BABYLON.EBlendingFunction.ONE) {
                         shaderMaterial.alphaMode = BABYLON.Engine.ALPHA_SUBTRACT;
                     }
-                    else if (blendFunc[0] === EBlendingFunction.DST_COLOR && blendFunc[1] === EBlendingFunction.ZERO && blendFunc[2] === EBlendingFunction.ONE && blendFunc[3] === EBlendingFunction.ONE) {
+                    else if (blendFunc[0] === BABYLON.EBlendingFunction.DST_COLOR && blendFunc[1] === BABYLON.EBlendingFunction.ZERO && blendFunc[2] === BABYLON.EBlendingFunction.ONE && blendFunc[3] === BABYLON.EBlendingFunction.ONE) {
                         shaderMaterial.alphaMode = BABYLON.Engine.ALPHA_MULTIPLY;
                     }
-                    else if (blendFunc[0] === EBlendingFunction.SRC_ALPHA && blendFunc[1] === EBlendingFunction.ONE_MINUS_SRC_COLOR && blendFunc[2] === EBlendingFunction.ONE && blendFunc[3] === EBlendingFunction.ONE) {
+                    else if (blendFunc[0] === BABYLON.EBlendingFunction.SRC_ALPHA && blendFunc[1] === BABYLON.EBlendingFunction.ONE_MINUS_SRC_COLOR && blendFunc[2] === BABYLON.EBlendingFunction.ONE && blendFunc[3] === BABYLON.EBlendingFunction.ONE) {
                         shaderMaterial.alphaMode = BABYLON.Engine.ALPHA_MAXIMIZED;
                     }
                 }
@@ -1302,7 +1311,7 @@ var BABYLON;
     var onLoadShader = function (gltfRuntime, sha) {
         return function (data) {
             gltfRuntime.loadedShaders++;
-            BABYLON.Effect.ShadersStore[sha + (gltfRuntime.shaders[sha].type === EShaderType.VERTEX ? "VertexShader" : "PixelShader")] = data;
+            BABYLON.Effect.ShadersStore[sha + (gltfRuntime.shaders[sha].type === BABYLON.EShaderType.VERTEX ? "VertexShader" : "PixelShader")] = data;
             if (gltfRuntime.loadedShaders === gltfRuntime.shaderscount) {
                 onShadersLoaded(gltfRuntime);
             }

+ 15 - 95
loaders/glTF/babylon.glTFFileLoader.ts

@@ -1,92 +1,5 @@
 module BABYLON {
     /**
-    * Enums
-    */
-    export enum EComponentType {
-        BYTE = 5120,
-        UNSIGNED_BYTE = 5121,
-        SHORT = 5122,
-        UNSIGNED_SHORT = 5123,
-        FLOAT = 5126
-    }
-
-    export enum EShaderType {
-        FRAGMENT = 35632,
-        VERTEX = 35633
-    }
-
-    export enum EParameterType {
-        BYTE = 5120,
-        UNSIGNED_BYTE = 5121,
-        SHORT = 5122,
-        UNSIGNED_SHORT = 5123,
-        INT = 5124,
-        UNSIGNED_INT = 5125,
-        FLOAT = 5126,
-        FLOAT_VEC2 = 35664,
-        FLOAT_VEC3 = 35665,
-        FLOAT_VEC4 = 35666,
-        INT_VEC2 = 35667,
-        INT_VEC3 = 35668,
-        INT_VEC4 = 35669,
-        BOOL = 35670,
-        BOOL_VEC2 = 35671,
-        BOOL_VEC3 = 35672,
-        BOOL_VEC4 = 35673,
-        FLOAT_MAT2 = 35674,
-        FLOAT_MAT3 = 35675,
-        FLOAT_MAT4 = 35676,
-        SAMPLER_2D = 35678
-    }
-
-    export enum ETextureWrapMode {
-        CLAMP_TO_EDGE = 33071,
-        MIRRORED_REPEAT = 33648,
-        REPEAT = 10497
-    }
-
-    export enum ETextureFilterType {
-        NEAREST = 9728,
-        LINEAR = 9728,
-        NEAREST_MIPMAP_NEAREST = 9984,
-        LINEAR_MIPMAP_NEAREST = 9985,
-        NEAREST_MIPMAP_LINEAR = 9986,
-        LINEAR_MIPMAP_LINEAR = 9987
-    }
-
-    export enum ETextureFormat {
-        ALPHA = 6406,
-        RGB = 6407,
-        RGBA = 6408,
-        LUMINANCE = 6409,
-        LUMINANCE_ALPHA = 6410 
-    }
-
-    export enum ECullingType {
-        FRONT = 1028,
-        BACK = 1029,
-        FRONT_AND_BACK = 1032
-    }
-
-    export enum EBlendingFunction {
-        ZERO = 0,
-        ONE = 1,
-        SRC_COLOR = 768,
-        ONE_MINUS_SRC_COLOR = 769,
-        DST_COLOR = 774,
-        ONE_MINUS_DST_COLOR = 775,
-        SRC_ALPHA = 770,
-        ONE_MINUS_SRC_ALPHA = 771,
-        DST_ALPHA = 772,
-        ONE_MINUS_DST_ALPHA = 773,
-        CONSTANT_COLOR = 32769,
-        ONE_MINUS_CONSTANT_COLOR = 32770,
-        CONSTANT_ALPHA = 32771,
-        ONE_MINUS_CONSTANT_ALPHA = 32772,
-        SRC_ALPHA_SATURATE = 776
-    }
-
-    /**
     * Tokenizer. Used for shaders compatibility
     * Automatically map world, view, projection, worldViewProjection, attributes and so on
     */
@@ -809,7 +722,8 @@
                     buffer = GLTFUtils.GetBufferFromAccessor(gltfRuntime, accessor);
 
                     if (semantic === "NORMAL") {
-                        tempVertexData.normals = Float32Array.from(buffer);
+                        tempVertexData.normals = new Float32Array(buffer.length);
+                        (<Float32Array>tempVertexData.normals).set(buffer);
                     }
                     else if (semantic === "POSITION") {
                         if (GLTFFileLoader.HomogeneousCoordinates) {
@@ -822,7 +736,8 @@
                             }
                         }
                         else {
-                            tempVertexData.positions = Float32Array.from(buffer);
+                            tempVertexData.positions = new Float32Array(buffer.length);
+                            (<Float32Array>tempVertexData.positions).set(buffer);
                         }
 
                         verticesCounts.push(tempVertexData.positions.length);
@@ -830,26 +745,31 @@
                     else if (semantic.indexOf("TEXCOORD_") !== -1) {
                         var channel = Number(semantic.split("_")[1]);
                         var uvKind = VertexBuffer.UVKind + (channel === 0 ? "" : (channel + 1));
-                        var uvs = Float32Array.from(buffer);
+                        var uvs = new Float32Array(buffer.length);
+                        (<Float32Array>uvs).set(buffer);
                         normalizeUVs(uvs);
                         tempVertexData.set(uvs, uvKind);
                     }
                     else if (semantic === "JOINT") {
-                        tempVertexData.matricesIndices = Float32Array.from(buffer);
+                        tempVertexData.matricesIndices = new Float32Array(buffer.length);
+                        (<Float32Array>tempVertexData.matricesIndices).set(buffer);
                     }
                     else if (semantic === "WEIGHT") {
-                        tempVertexData.matricesWeights = Float32Array.from(buffer);
+                        tempVertexData.matricesWeights = new Float32Array(buffer.length);
+                        (<Float32Array>tempVertexData.matricesWeights).set(buffer);
                     }
                     else if (semantic === "COLOR") {
-                        tempVertexData.colors = Float32Array.from(buffer);
+                        tempVertexData.colors = new Float32Array(buffer.length);
+                        (<Float32Array>tempVertexData.colors).set(buffer);
                     }
                 }
 
                 // Indices
                 accessor = gltfRuntime.accessors[primitive.indices];
                 buffer = GLTFUtils.GetBufferFromAccessor(gltfRuntime, accessor);
-                
-                tempVertexData.indices = Int32Array.from(buffer);
+
+                tempVertexData.indices = new Int32Array(buffer.length);
+                (<Float32Array>tempVertexData.indices).set(buffer);
                 indexCounts.push(tempVertexData.indices.length);
 
                 vertexData.merge(tempVertexData);

+ 87 - 0
loaders/glTF/babylon.glTFFileLoaderInterfaces.ts

@@ -1,5 +1,92 @@
 module BABYLON {
     /**
+    * Enums
+    */
+    export enum EComponentType {
+        BYTE = 5120,
+        UNSIGNED_BYTE = 5121,
+        SHORT = 5122,
+        UNSIGNED_SHORT = 5123,
+        FLOAT = 5126
+    }
+
+    export enum EShaderType {
+        FRAGMENT = 35632,
+        VERTEX = 35633
+    }
+
+    export enum EParameterType {
+        BYTE = 5120,
+        UNSIGNED_BYTE = 5121,
+        SHORT = 5122,
+        UNSIGNED_SHORT = 5123,
+        INT = 5124,
+        UNSIGNED_INT = 5125,
+        FLOAT = 5126,
+        FLOAT_VEC2 = 35664,
+        FLOAT_VEC3 = 35665,
+        FLOAT_VEC4 = 35666,
+        INT_VEC2 = 35667,
+        INT_VEC3 = 35668,
+        INT_VEC4 = 35669,
+        BOOL = 35670,
+        BOOL_VEC2 = 35671,
+        BOOL_VEC3 = 35672,
+        BOOL_VEC4 = 35673,
+        FLOAT_MAT2 = 35674,
+        FLOAT_MAT3 = 35675,
+        FLOAT_MAT4 = 35676,
+        SAMPLER_2D = 35678
+    }
+
+    export enum ETextureWrapMode {
+        CLAMP_TO_EDGE = 33071,
+        MIRRORED_REPEAT = 33648,
+        REPEAT = 10497
+    }
+
+    export enum ETextureFilterType {
+        NEAREST = 9728,
+        LINEAR = 9728,
+        NEAREST_MIPMAP_NEAREST = 9984,
+        LINEAR_MIPMAP_NEAREST = 9985,
+        NEAREST_MIPMAP_LINEAR = 9986,
+        LINEAR_MIPMAP_LINEAR = 9987
+    }
+
+    export enum ETextureFormat {
+        ALPHA = 6406,
+        RGB = 6407,
+        RGBA = 6408,
+        LUMINANCE = 6409,
+        LUMINANCE_ALPHA = 6410
+    }
+
+    export enum ECullingType {
+        FRONT = 1028,
+        BACK = 1029,
+        FRONT_AND_BACK = 1032
+    }
+
+    export enum EBlendingFunction {
+        ZERO = 0,
+        ONE = 1,
+        SRC_COLOR = 768,
+        ONE_MINUS_SRC_COLOR = 769,
+        DST_COLOR = 774,
+        ONE_MINUS_DST_COLOR = 775,
+        SRC_ALPHA = 770,
+        ONE_MINUS_SRC_ALPHA = 771,
+        DST_ALPHA = 772,
+        ONE_MINUS_DST_ALPHA = 773,
+        CONSTANT_COLOR = 32769,
+        ONE_MINUS_CONSTANT_COLOR = 32770,
+        CONSTANT_ALPHA = 32771,
+        ONE_MINUS_CONSTANT_ALPHA = 32772,
+        SRC_ALPHA_SATURATE = 776
+    }
+
+    /**
     * Interfaces
     */
     export interface IGLTFProperty {