Skip to content

Commit 565c6fd

Browse files
committed
OBJLoader: Removed more regexp usage and clean up.
1 parent 82b60ac commit 565c6fd

File tree

1 file changed

+54
-108
lines changed

1 file changed

+54
-108
lines changed

examples/js/loaders/OBJLoader.js

+54-108
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,6 @@
44

55
THREE.OBJLoader = ( function () {
66

7-
// v float float float
8-
var vertex_pattern = /^v\s+([\d\.\+\-eE]+)\s+([\d\.\+\-eE]+)\s+([\d\.\+\-eE]+)/;
9-
// vn float float float
10-
var normal_pattern = /^vn\s+([\d\.\+\-eE]+)\s+([\d\.\+\-eE]+)\s+([\d\.\+\-eE]+)/;
11-
// vt float float
12-
var uv_pattern = /^vt\s+([\d\.\+\-eE]+)\s+([\d\.\+\-eE]+)/;
137
// o object_name | g group_name
148
var object_pattern = /^[og]\s*(.+)?/;
159
// mtllib file_reference
@@ -61,7 +55,7 @@ THREE.OBJLoader = ( function () {
6155
materials : [],
6256
smooth : true,
6357

64-
startMaterial : function( name, libraries ) {
58+
startMaterial: function ( name, libraries ) {
6559

6660
var previous = this._finalize( false );
6761

@@ -83,7 +77,7 @@ THREE.OBJLoader = ( function () {
8377
groupCount : -1,
8478
inherited : false,
8579

86-
clone : function( index ) {
80+
clone: function ( index ) {
8781
var cloned = {
8882
index : ( typeof index === 'number' ? index : this.index ),
8983
name : this.name,
@@ -105,7 +99,7 @@ THREE.OBJLoader = ( function () {
10599

106100
},
107101

108-
currentMaterial : function() {
102+
currentMaterial: function () {
109103

110104
if ( this.materials.length > 0 ) {
111105
return this.materials[ this.materials.length - 1 ];
@@ -115,7 +109,7 @@ THREE.OBJLoader = ( function () {
115109

116110
},
117111

118-
_finalize : function( end ) {
112+
_finalize: function ( end ) {
119113

120114
var lastMultiMaterial = this.currentMaterial();
121115
if ( lastMultiMaterial && lastMultiMaterial.groupEnd === -1 ) {
@@ -158,7 +152,7 @@ THREE.OBJLoader = ( function () {
158152
// overwrite the inherited material. Exception being that there was already face declarations
159153
// to the inherited material, then it will be preserved for proper MultiMaterial continuation.
160154

161-
if ( previousMaterial && previousMaterial.name && typeof previousMaterial.clone === "function" ) {
155+
if ( previousMaterial && previousMaterial.name && typeof previousMaterial.clone === 'function' ) {
162156

163157
var declared = previousMaterial.clone( 0 );
164158
declared.inherited = true;
@@ -170,7 +164,7 @@ THREE.OBJLoader = ( function () {
170164

171165
},
172166

173-
finalize : function() {
167+
finalize: function () {
174168

175169
if ( this.object && typeof this.object._finalize === 'function' ) {
176170

@@ -221,7 +215,7 @@ THREE.OBJLoader = ( function () {
221215

222216
},
223217

224-
addNormal : function ( a, b, c ) {
218+
addNormal: function ( a, b, c ) {
225219

226220
var src = this.normals;
227221
var dst = this.object.geometry.normals;
@@ -252,27 +246,15 @@ THREE.OBJLoader = ( function () {
252246

253247
},
254248

255-
addFace: function ( a, b, c, d, ua, ub, uc, ud, na, nb, nc, nd ) {
249+
addFace: function ( a, b, c, ua, ub, uc, na, nb, nc ) {
256250

257251
var vLen = this.vertices.length;
258252

259253
var ia = this.parseVertexIndex( a, vLen );
260254
var ib = this.parseVertexIndex( b, vLen );
261255
var ic = this.parseVertexIndex( c, vLen );
262-
var id;
263256

264-
if ( d === undefined ) {
265-
266-
this.addVertex( ia, ib, ic );
267-
268-
} else {
269-
270-
id = this.parseVertexIndex( d, vLen );
271-
272-
this.addVertex( ia, ib, id );
273-
this.addVertex( ib, ic, id );
274-
275-
}
257+
this.addVertex( ia, ib, ic );
276258

277259
if ( ua !== undefined ) {
278260

@@ -282,18 +264,7 @@ THREE.OBJLoader = ( function () {
282264
ib = this.parseUVIndex( ub, uvLen );
283265
ic = this.parseUVIndex( uc, uvLen );
284266

285-
if ( d === undefined ) {
286-
287-
this.addUV( ia, ib, ic );
288-
289-
} else {
290-
291-
id = this.parseUVIndex( ud, uvLen );
292-
293-
this.addUV( ia, ib, id );
294-
this.addUV( ib, ic, id );
295-
296-
}
267+
this.addUV( ia, ib, ic );
297268

298269
}
299270

@@ -306,18 +277,7 @@ THREE.OBJLoader = ( function () {
306277
ib = na === nb ? ia : this.parseNormalIndex( nb, nLen );
307278
ic = na === nc ? ia : this.parseNormalIndex( nc, nLen );
308279

309-
if ( d === undefined ) {
310-
311-
this.addNormal( ia, ib, ic );
312-
313-
} else {
314-
315-
id = this.parseNormalIndex( nd, nLen );
316-
317-
this.addNormal( ia, ib, id );
318-
this.addNormal( ib, ic, id );
319-
320-
}
280+
this.addNormal( ia, ib, ic );
321281

322282
}
323283

@@ -415,7 +375,7 @@ THREE.OBJLoader = ( function () {
415375
}
416376

417377
var lines = text.split( '\n' );
418-
var line = '', lineFirstChar = '', lineSecondChar = '';
378+
var line = '', lineFirstChar = '';
419379
var lineLength = 0;
420380
var result = [];
421381

@@ -439,84 +399,70 @@ THREE.OBJLoader = ( function () {
439399

440400
if ( lineFirstChar === 'v' ) {
441401

442-
lineSecondChar = line.charAt( 1 );
443-
444-
if ( lineSecondChar === ' ' && ( result = vertex_pattern.exec( line ) ) !== null ) {
445-
446-
// 0 1 2 3
447-
// ["v 1.0 2.0 3.0", "1.0", "2.0", "3.0"]
448-
449-
state.vertices.push(
450-
parseFloat( result[ 1 ] ),
451-
parseFloat( result[ 2 ] ),
452-
parseFloat( result[ 3 ] )
453-
);
454-
455-
} else if ( lineSecondChar === 'n' && ( result = normal_pattern.exec( line ) ) !== null ) {
456-
457-
// 0 1 2 3
458-
// ["vn 1.0 2.0 3.0", "1.0", "2.0", "3.0"]
459-
460-
state.normals.push(
461-
parseFloat( result[ 1 ] ),
462-
parseFloat( result[ 2 ] ),
463-
parseFloat( result[ 3 ] )
464-
);
465-
466-
} else if ( lineSecondChar === 't' && ( result = uv_pattern.exec( line ) ) !== null ) {
467-
468-
// 0 1 2
469-
// ["vt 0.1 0.2", "0.1", "0.2"]
470-
471-
state.uvs.push(
472-
parseFloat( result[ 1 ] ),
473-
parseFloat( result[ 2 ] )
474-
);
475-
476-
} else {
477-
478-
throw new Error( "Unexpected vertex/normal/uv line: '" + line + "'" );
479-
402+
var data = line.split( /\s+/ );
403+
404+
switch ( data[ 0 ] ) {
405+
406+
case 'v':
407+
state.vertices.push(
408+
parseFloat( data[ 1 ] ),
409+
parseFloat( data[ 2 ] ),
410+
parseFloat( data[ 3 ] )
411+
);
412+
break;
413+
case 'vn':
414+
state.normals.push(
415+
parseFloat( data[ 1 ] ),
416+
parseFloat( data[ 2 ] ),
417+
parseFloat( data[ 3 ] )
418+
);
419+
case 'vt':
420+
state.uvs.push(
421+
parseFloat( data[ 1 ] ),
422+
parseFloat( data[ 2 ] )
423+
);
424+
break;
480425
}
481426

482-
} else if ( lineFirstChar === "f" ) {
427+
} else if ( lineFirstChar === 'f' ) {
483428

484-
var lineData = line.substr(1).trim(),
485-
vertexData = lineData.split(' '),
486-
faceVertices = [];
429+
var lineData = line.substr( 1 ).trim();
430+
var vertexData = lineData.split( /\s+/ );
431+
var faceVertices = [];
487432

488433
// Parse the face vertex data into an easy to work with format
489434

490-
for (var idx = 0; idx < vertexData.length; idx++) {
435+
for ( var j = 0, jl = vertexData.length; j < jl; j ++ ) {
436+
437+
var vertex = vertexData[ j ];
491438

492-
if (vertexData[idx].length > 0) {
439+
if ( vertex.length > 0 ) {
493440

494-
var vertexParts = vertexData[idx].split('/');
495-
faceVertices.push(vertexParts);
441+
var vertexParts = vertex.split( '/' );
442+
faceVertices.push( vertexParts );
496443

497444
}
498445

499446
}
500447

501448
// Draw an edge between the first vertex and all subsequent vertices to form an n-gon
502449

503-
var v1 = faceVertices[0],
504-
numFaces = faceVertices.length - 1;
450+
var v1 = faceVertices[ 0 ];
505451

506-
for (var idx = 1; idx < numFaces; idx++) {
452+
for ( var j = 1, jl = faceVertices.length - 1; j < jl; j ++ ) {
507453

508-
var v2 = faceVertices[idx],
509-
v3 = faceVertices[idx+1];
454+
var v2 = faceVertices[ j ];
455+
var v3 = faceVertices[ j + 1 ];
510456

511457
state.addFace(
512-
v1[ 0 ], v2[ 0 ], v3[ 0 ], undefined,
513-
v1[ 1 ], v2[ 1 ], v3[ 1 ], undefined,
514-
v1[ 2 ], v2[ 2 ], v3[ 2 ], undefined
458+
v1[ 0 ], v2[ 0 ], v3[ 0 ],
459+
v1[ 1 ], v2[ 1 ], v3[ 1 ],
460+
v1[ 2 ], v2[ 2 ], v3[ 2 ]
515461
);
516462

517463
}
518464

519-
} else if ( lineFirstChar === "l" ) {
465+
} else if ( lineFirstChar === 'l' ) {
520466

521467
var lineParts = line.substring( 1 ).trim().split( " " );
522468
var lineVertices = [], lineUVs = [];
@@ -563,7 +509,7 @@ THREE.OBJLoader = ( function () {
563509

564510
state.materialLibraries.push( line.substring( 7 ).trim() );
565511

566-
} else if ( lineFirstChar === "s" ) {
512+
} else if ( lineFirstChar === 's' ) {
567513

568514
result = line.split( ' ' );
569515

0 commit comments

Comments
 (0)