瀏覽代碼

Add regression test for lenient parsing of vertex position/color (#5956). Update regex as we only need to check for spaces.

Brian Zinn 6 年之前
父節點
當前提交
a8c0be2086
共有 2 個文件被更改,包括 32 次插入4 次删除
  1. 4 4
      loaders/src/OBJ/objFileLoader.ts
  2. 28 0
      tests/unit/babylon/src/Loading/babylon.sceneLoader.tests.ts

+ 4 - 4
loaders/src/OBJ/objFileLoader.ts

@@ -849,11 +849,11 @@ export class OBJFileLoader implements ISceneLoaderPluginAsync, ISceneLoaderPlugi
 
             //Get information about one position possible for the vertices
             } else if (this.vertexPattern.test(line)) {
-                result = line.match(/\S+/g)!;  // match will return non-null due to passing regex pattern
+                result = line.match(/[^ ]+/g)!;  // match will return non-null due to passing regex pattern
 
-                //Value of result with line: "v 1.0 2.0 3.0"
+                // Value of result with line: "v 1.0 2.0 3.0"
                 // ["v", "1.0", "2.0", "3.0"]
-                //Create a Vector3 with the position x, y, z
+                // Create a Vector3 with the position x, y, z
                 positions.push(new Vector3(
                     parseFloat(result[1]),
                     parseFloat(result[2]),
@@ -1012,7 +1012,7 @@ export class OBJFileLoader implements ISceneLoaderPluginAsync, ISceneLoaderPlugi
                 //Apply smoothing
             } else if (this.smooth.test(line)) {
                 // smooth shading => apply smoothing
-                //Toda  y I don't know it work with babylon and with obj.
+                //Today I don't know it work with babylon and with obj.
                 //With the obj file  an integer is set
             } else {
                 //If there is another possibility

+ 28 - 0
tests/unit/babylon/src/Loading/babylon.sceneLoader.tests.ts

@@ -550,6 +550,34 @@ describe('Babylon Scene Loader', function() {
                 assert.isNull(colors, 'expecting colors vertex buffer to be null')
             })
         })
+
+        it('should parse leniently allowing extra spaces with vertex definitions', () => {
+            var fileContents = `               
+                g tetrahedron
+                
+                v  1.00 1.00 1.00 0.666 0 0
+                v  2.00 1.00 1.00 0.666 0 0
+                v  1.00 2.00 1.00 0.666 0 0
+                v  1.00 1.00 2.00 0.666 0 0
+                # ^
+                # └── allow extra spaces before position/color
+
+                f 1 3 2
+                f 1 4 3
+                f 1 2 4
+                f 2 3 4
+            `;
+
+            var scene = new BABYLON.Scene(subject);
+            return BABYLON.SceneLoader.LoadAssetContainerAsync('', 'data:' + fileContents, scene, ()=> {}, ".obj").then(container => {
+                expect(container.meshes.length).to.eq(1);
+                let tetrahedron = container.meshes[0];
+
+                var positions : BABYLON.FloatArray = tetrahedron.getVerticesData(BABYLON.VertexBuffer.PositionKind);
+
+                expect(positions).to.deep.equal([1, 1, 1, 1, 2, 1, 2, 1, 1, 1, 1, 2]);
+            })
+        })
     })
 
     describe('#AssetContainer', () => {