Browse Source

STL data: urls
Closing #4473

Raanan Weber 7 years ago
parent
commit
aa9fa6396c
2 changed files with 35 additions and 26 deletions
  1. 4 0
      dist/preview release/what's new.md
  2. 31 26
      loaders/src/STL/babylon.stlFileLoader.ts

+ 4 - 0
dist/preview release/what's new.md

@@ -85,6 +85,10 @@
 - Animation blending was always set to true, ignoring configuration [#4412](https://github.com/BabylonJS/Babylon.js/issues/4412) ([RaananW](https://github.com/RaananW))
 - Animation navbar now updates correctly when a new model is loaded [#4441](https://github.com/BabylonJS/Babylon.js/issues/4441) ([RaananW](https://github.com/RaananW))
 
+### Loaders
+
+- STL Loader only supported binary downloads and no data: urls [#4473](https://github.com/BabylonJS/Babylon.js/issues/4473) ([RaananW](https://github.com/RaananW))
+
 ## Breaking changes
 
 - Fixing support for R and RG texture formats made us remove TextureFormat_R32F and TextureFormat_RG32F as they were mixing formats and types. Please, use the respective TextureFormat_R and TextureFormat_RG with the Float types ([sebavan](http://www.github.com/sebavan))

+ 31 - 26
loaders/src/STL/babylon.stlFileLoader.ts

@@ -15,31 +15,36 @@ module BABYLON {
         // force data to come in as an ArrayBuffer
         // we'll convert to string if it looks like it's an ASCII .stl
         public extensions: ISceneLoaderPluginExtensions = {
-            ".stl": {isBinary: true},
+            ".stl": { isBinary: true },
         };
 
         public importMesh(meshesNames: any, scene: Scene, data: any, rootUrl: string, meshes: Nullable<AbstractMesh[]>, particleSystems: Nullable<ParticleSystem[]>, skeletons: Nullable<Skeleton[]>): boolean {
             var matches;
 
-            if (this.isBinary(data)) {
-                // binary .stl
-                var babylonMesh = new Mesh("stlmesh", scene);
-                this.parseBinary(babylonMesh, data);
-                if (meshes) {
-                    meshes.push(babylonMesh);
+            if (typeof data !== "string") {
+
+                if (this.isBinary(data)) {
+                    // binary .stl
+                    var babylonMesh = new Mesh("stlmesh", scene);
+                    this.parseBinary(babylonMesh, data);
+                    if (meshes) {
+                        meshes.push(babylonMesh);
+                    }
+                    return true;
                 }
-                return true;
-            }
 
-            // ASCII .stl
+                // ASCII .stl
 
-            // convert to string
-            var array_buffer = new Uint8Array(data);
-            var str = '';
-            for (var i = 0; i < data.byteLength; i++) {
-                str += String.fromCharCode( array_buffer[ i ] ); // implicitly assumes little-endian
+                // convert to string
+                var array_buffer = new Uint8Array(data);
+                var str = '';
+                for (var i = 0; i < data.byteLength; i++) {
+                    str += String.fromCharCode(array_buffer[i]); // implicitly assumes little-endian
+                }
+                data = str;
             }
-            data = str;
+
+            //if arrived here, data is a string, containing the STLA data.
 
             while (matches = this.solidPattern.exec(data)) {
                 var meshName = matches[1];
@@ -93,13 +98,13 @@ module BABYLON {
             return container;
         }
 
-        private isBinary (data: any) {
+        private isBinary(data: any) {
 
             // check if file size is correct for binary stl
             var faceSize, nFaces, reader;
             reader = new DataView(data);
             faceSize = (32 / 8 * 3) + ((32 / 8 * 3) * 3) + (16 / 8);
-            nFaces = reader.getUint32( 80, true );
+            nFaces = reader.getUint32(80, true);
 
             if (80 + (32 / 8) + (nFaces * faceSize) === reader.byteLength) {
                 return true;
@@ -107,8 +112,8 @@ module BABYLON {
 
             // check characters higher than ASCII to confirm binary
             var fileLength = reader.byteLength;
-            for (var index=0; index < fileLength; index++) {
-                if (reader.getUint8( index ) > 127) {
+            for (var index = 0; index < fileLength; index++) {
+                if (reader.getUint8(index) > 127) {
                     return true;
                 }
             }
@@ -134,9 +139,9 @@ module BABYLON {
             for (var face = 0; face < faces; face++) {
 
                 var start = dataOffset + face * faceLength;
-                var normalX = reader.getFloat32( start, true );
-                var normalY = reader.getFloat32( start + 4, true );
-                var normalZ = reader.getFloat32( start + 8, true );
+                var normalX = reader.getFloat32(start, true);
+                var normalY = reader.getFloat32(start + 4, true);
+                var normalZ = reader.getFloat32(start + 8, true);
 
 
                 for (var i = 1; i <= 3; i++) {
@@ -144,9 +149,9 @@ module BABYLON {
                     var vertexstart = start + i * 12;
 
                     // ordering is intentional to match ascii import
-                    positions[offset] = reader.getFloat32( vertexstart, true );
-                    positions[offset + 2] = reader.getFloat32( vertexstart + 4, true );
-                    positions[offset + 1] = reader.getFloat32( vertexstart + 8, true );
+                    positions[offset] = reader.getFloat32(vertexstart, true);
+                    positions[offset + 2] = reader.getFloat32(vertexstart + 4, true);
+                    positions[offset + 1] = reader.getFloat32(vertexstart + 8, true);
 
                     normals[offset] = normalX;
                     normals[offset + 2] = normalY;