4
4
5
5
THREE . OBJLoader = ( function ( ) {
6
6
7
- // v float float float
8
- var vertex_pattern = / ^ v \s + ( [ \d \. \+ \- e E ] + ) \s + ( [ \d \. \+ \- e E ] + ) \s + ( [ \d \. \+ \- e E ] + ) / ;
9
- // vn float float float
10
- var normal_pattern = / ^ v n \s + ( [ \d \. \+ \- e E ] + ) \s + ( [ \d \. \+ \- e E ] + ) \s + ( [ \d \. \+ \- e E ] + ) / ;
11
- // vt float float
12
- var uv_pattern = / ^ v t \s + ( [ \d \. \+ \- e E ] + ) \s + ( [ \d \. \+ \- e E ] + ) / ;
13
7
// o object_name | g group_name
14
8
var object_pattern = / ^ [ o g ] \s * ( .+ ) ? / ;
15
9
// mtllib file_reference
@@ -61,7 +55,7 @@ THREE.OBJLoader = ( function () {
61
55
materials : [ ] ,
62
56
smooth : true ,
63
57
64
- startMaterial : function ( name , libraries ) {
58
+ startMaterial : function ( name , libraries ) {
65
59
66
60
var previous = this . _finalize ( false ) ;
67
61
@@ -83,7 +77,7 @@ THREE.OBJLoader = ( function () {
83
77
groupCount : - 1 ,
84
78
inherited : false ,
85
79
86
- clone : function ( index ) {
80
+ clone : function ( index ) {
87
81
var cloned = {
88
82
index : ( typeof index === 'number' ? index : this . index ) ,
89
83
name : this . name ,
@@ -105,7 +99,7 @@ THREE.OBJLoader = ( function () {
105
99
106
100
} ,
107
101
108
- currentMaterial : function ( ) {
102
+ currentMaterial : function ( ) {
109
103
110
104
if ( this . materials . length > 0 ) {
111
105
return this . materials [ this . materials . length - 1 ] ;
@@ -115,7 +109,7 @@ THREE.OBJLoader = ( function () {
115
109
116
110
} ,
117
111
118
- _finalize : function ( end ) {
112
+ _finalize : function ( end ) {
119
113
120
114
var lastMultiMaterial = this . currentMaterial ( ) ;
121
115
if ( lastMultiMaterial && lastMultiMaterial . groupEnd === - 1 ) {
@@ -158,7 +152,7 @@ THREE.OBJLoader = ( function () {
158
152
// overwrite the inherited material. Exception being that there was already face declarations
159
153
// to the inherited material, then it will be preserved for proper MultiMaterial continuation.
160
154
161
- if ( previousMaterial && previousMaterial . name && typeof previousMaterial . clone === " function" ) {
155
+ if ( previousMaterial && previousMaterial . name && typeof previousMaterial . clone === ' function' ) {
162
156
163
157
var declared = previousMaterial . clone ( 0 ) ;
164
158
declared . inherited = true ;
@@ -170,7 +164,7 @@ THREE.OBJLoader = ( function () {
170
164
171
165
} ,
172
166
173
- finalize : function ( ) {
167
+ finalize : function ( ) {
174
168
175
169
if ( this . object && typeof this . object . _finalize === 'function' ) {
176
170
@@ -221,7 +215,7 @@ THREE.OBJLoader = ( function () {
221
215
222
216
} ,
223
217
224
- addNormal : function ( a , b , c ) {
218
+ addNormal : function ( a , b , c ) {
225
219
226
220
var src = this . normals ;
227
221
var dst = this . object . geometry . normals ;
@@ -252,27 +246,15 @@ THREE.OBJLoader = ( function () {
252
246
253
247
} ,
254
248
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 ) {
256
250
257
251
var vLen = this . vertices . length ;
258
252
259
253
var ia = this . parseVertexIndex ( a , vLen ) ;
260
254
var ib = this . parseVertexIndex ( b , vLen ) ;
261
255
var ic = this . parseVertexIndex ( c , vLen ) ;
262
- var id ;
263
256
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 ) ;
276
258
277
259
if ( ua !== undefined ) {
278
260
@@ -282,18 +264,7 @@ THREE.OBJLoader = ( function () {
282
264
ib = this . parseUVIndex ( ub , uvLen ) ;
283
265
ic = this . parseUVIndex ( uc , uvLen ) ;
284
266
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 ) ;
297
268
298
269
}
299
270
@@ -306,18 +277,7 @@ THREE.OBJLoader = ( function () {
306
277
ib = na === nb ? ia : this . parseNormalIndex ( nb , nLen ) ;
307
278
ic = na === nc ? ia : this . parseNormalIndex ( nc , nLen ) ;
308
279
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 ) ;
321
281
322
282
}
323
283
@@ -415,7 +375,7 @@ THREE.OBJLoader = ( function () {
415
375
}
416
376
417
377
var lines = text . split ( '\n' ) ;
418
- var line = '' , lineFirstChar = '' , lineSecondChar = '' ;
378
+ var line = '' , lineFirstChar = '' ;
419
379
var lineLength = 0 ;
420
380
var result = [ ] ;
421
381
@@ -439,84 +399,70 @@ THREE.OBJLoader = ( function () {
439
399
440
400
if ( lineFirstChar === 'v' ) {
441
401
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 ;
480
425
}
481
426
482
- } else if ( lineFirstChar === "f" ) {
427
+ } else if ( lineFirstChar === 'f' ) {
483
428
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 = [ ] ;
487
432
488
433
// Parse the face vertex data into an easy to work with format
489
434
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 ] ;
491
438
492
- if ( vertexData [ idx ] . length > 0 ) {
439
+ if ( vertex . length > 0 ) {
493
440
494
- var vertexParts = vertexData [ idx ] . split ( '/' ) ;
495
- faceVertices . push ( vertexParts ) ;
441
+ var vertexParts = vertex . split ( '/' ) ;
442
+ faceVertices . push ( vertexParts ) ;
496
443
497
444
}
498
445
499
446
}
500
447
501
448
// Draw an edge between the first vertex and all subsequent vertices to form an n-gon
502
449
503
- var v1 = faceVertices [ 0 ] ,
504
- numFaces = faceVertices . length - 1 ;
450
+ var v1 = faceVertices [ 0 ] ;
505
451
506
- for ( var idx = 1 ; idx < numFaces ; idx ++ ) {
452
+ for ( var j = 1 , jl = faceVertices . length - 1 ; j < jl ; j ++ ) {
507
453
508
- var v2 = faceVertices [ idx ] ,
509
- v3 = faceVertices [ idx + 1 ] ;
454
+ var v2 = faceVertices [ j ] ;
455
+ var v3 = faceVertices [ j + 1 ] ;
510
456
511
457
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 ]
515
461
) ;
516
462
517
463
}
518
464
519
- } else if ( lineFirstChar === "l" ) {
465
+ } else if ( lineFirstChar === 'l' ) {
520
466
521
467
var lineParts = line . substring ( 1 ) . trim ( ) . split ( " " ) ;
522
468
var lineVertices = [ ] , lineUVs = [ ] ;
@@ -563,7 +509,7 @@ THREE.OBJLoader = ( function () {
563
509
564
510
state . materialLibraries . push ( line . substring ( 7 ) . trim ( ) ) ;
565
511
566
- } else if ( lineFirstChar === "s" ) {
512
+ } else if ( lineFirstChar === 's' ) {
567
513
568
514
result = line . split ( ' ' ) ;
569
515
0 commit comments