| 1 | import { |
| 2 | BufferGeometry, |
| 3 | FileLoader, |
| 4 | Float32BufferAttribute, |
| 5 | Group, |
| 6 | LineBasicMaterial, |
| 7 | LineSegments, |
| 8 | Loader, |
| 9 | Material, |
| 10 | Mesh, |
| 11 | MeshPhongMaterial, |
| 12 | Points, |
| 13 | PointsMaterial, |
| 14 | Vector3, |
| 15 | Color |
| 16 | } from 'three'; |
| 17 | |
| 18 | // o object_name | g group_name |
| 19 | const _object_pattern = /^[og]\s*(.+)?/; |
| 20 | // mtllib file_reference |
| 21 | const _material_library_pattern = /^mtllib /; |
| 22 | // usemtl material_name |
| 23 | const _material_use_pattern = /^usemtl /; |
| 24 | // usemap map_name |
| 25 | const _map_use_pattern = /^usemap /; |
| 26 | const _face_vertex_data_separator_pattern = /\s+/; |
| 27 | |
| 28 | const _vA = new Vector3(); |
| 29 | const _vB = new Vector3(); |
| 30 | const _vC = new Vector3(); |
| 31 | |
| 32 | const _ab = new Vector3(); |
| 33 | const _cb = new Vector3(); |
| 34 | |
| 35 | const _color = new Color(); |
| 36 | |
| 37 | function ParserState() { |
| 38 | |
| 39 | const state = { |
| 40 | objects: [], |
| 41 | object: {}, |
| 42 | |
| 43 | vertices: [], |
| 44 | normals: [], |
| 45 | colors: [], |
| 46 | uvs: [], |
| 47 | |
| 48 | materials: {}, |
| 49 | materialLibraries: [], |
| 50 | |
| 51 | startObject: function ( name, fromDeclaration ) { |
| 52 | |
| 53 | // If the current object (initial from reset) is not from a g/o declaration in the parsed |
| 54 | // file. We need to use it for the first parsed g/o to keep things in sync. |
| 55 | if ( this.object && this.object.fromDeclaration === false ) { |
| 56 | |
| 57 | this.object.name = name; |
| 58 | this.object.fromDeclaration = ( fromDeclaration !== false ); |
| 59 | return; |
| 60 | |
| 61 | } |
| 62 | |
| 63 | const previousMaterial = ( this.object && typeof this.object.currentMaterial === 'function' ? this.object.currentMaterial() : undefined ); |
| 64 | |
| 65 | if ( this.object && typeof this.object._finalize === 'function' ) { |
| 66 | |
| 67 | this.object._finalize( true ); |
| 68 | |
| 69 | } |
| 70 | |
| 71 | this.object = { |
| 72 | name: name || '', |
| 73 | fromDeclaration: ( fromDeclaration !== false ), |
| 74 | |
| 75 | geometry: { |
| 76 | vertices: [], |
| 77 | normals: [], |
| 78 | colors: [], |
| 79 | uvs: [], |
| 80 | hasUVIndices: false |
| 81 | }, |
| 82 | materials: [], |
| 83 | smooth: true, |
| 84 | |
| 85 | startMaterial: function ( name, libraries ) { |
| 86 | |
| 87 | const previous = this._finalize( false ); |
| 88 | |
| 89 | // New usemtl declaration overwrites an inherited material, except if faces were declared |
| 90 | // after the material, then it must be preserved for proper MultiMaterial continuation. |
| 91 | if ( previous && ( previous.inherited || previous.groupCount <= 0 ) ) { |
| 92 | |
| 93 | this.materials.splice( previous.index, 1 ); |
| 94 | |
| 95 | } |
| 96 | |
| 97 | const material = { |
| 98 | index: this.materials.length, |
| 99 | name: name || '', |
| 100 | mtllib: ( Array.isArray( libraries ) && libraries.length > 0 ? libraries[ libraries.length - 1 ] : '' ), |
| 101 | smooth: ( previous !== undefined ? previous.smooth : this.smooth ), |
| 102 | groupStart: ( previous !== undefined ? previous.groupEnd : 0 ), |
| 103 | groupEnd: - 1, |
| 104 | groupCount: - 1, |
| 105 | inherited: false, |
| 106 | |
| 107 | clone: function ( index ) { |
| 108 | |
| 109 | const cloned = { |
| 110 | index: ( typeof index === 'number' ? index : this.index ), |
| 111 | name: this.name, |
| 112 | mtllib: this.mtllib, |
| 113 | smooth: this.smooth, |
| 114 | groupStart: 0, |
| 115 | groupEnd: - 1, |
| 116 | groupCount: - 1, |
| 117 | inherited: false |
| 118 | }; |
| 119 | cloned.clone = this.clone.bind( cloned ); |
| 120 | return cloned; |
| 121 | |
| 122 | } |
| 123 | }; |
| 124 | |
| 125 | this.materials.push( material ); |
| 126 | |
| 127 | return material; |
| 128 | |
| 129 | }, |
| 130 | |
| 131 | currentMaterial: function () { |
| 132 | |
| 133 | if ( this.materials.length > 0 ) { |
| 134 | |
| 135 | return this.materials[ this.materials.length - 1 ]; |
| 136 | |
| 137 | } |
| 138 | |
| 139 | return undefined; |
| 140 | |
| 141 | }, |
| 142 | |
| 143 | _finalize: function ( end ) { |
| 144 | |
| 145 | const lastMultiMaterial = this.currentMaterial(); |
| 146 | if ( lastMultiMaterial && lastMultiMaterial.groupEnd === - 1 ) { |
| 147 | |
| 148 | lastMultiMaterial.groupEnd = this.geometry.vertices.length / 3; |
| 149 | lastMultiMaterial.groupCount = lastMultiMaterial.groupEnd - lastMultiMaterial.groupStart; |
| 150 | lastMultiMaterial.inherited = false; |
| 151 | |
| 152 | } |
| 153 | |
| 154 | // Ignore objects tail materials if no face declarations followed them before a new o/g started. |
| 155 | if ( end && this.materials.length > 1 ) { |
| 156 | |
| 157 | for ( let mi = this.materials.length - 1; mi >= 0; mi -- ) { |
| 158 | |
| 159 | if ( this.materials[ mi ].groupCount <= 0 ) { |
| 160 | |
| 161 | this.materials.splice( mi, 1 ); |
| 162 | |
| 163 | } |
| 164 | |
| 165 | } |
| 166 | |
| 167 | } |
| 168 | |
| 169 | // Guarantee at least one empty material, this makes the creation later more straight forward. |
| 170 | if ( end && this.materials.length === 0 ) { |
| 171 | |
| 172 | this.materials.push( { |
| 173 | name: '', |
| 174 | smooth: this.smooth |
| 175 | } ); |
| 176 | |
| 177 | } |
| 178 | |
| 179 | return lastMultiMaterial; |
| 180 | |
| 181 | } |
| 182 | }; |
| 183 | |
| 184 | // Inherit previous objects material. |
| 185 | // Spec tells us that a declared material must be set to all objects until a new material is declared. |
| 186 | // If a usemtl declaration is encountered while this new object is being parsed, it will |
| 187 | // overwrite the inherited material. Exception being that there was already face declarations |
| 188 | // to the inherited material, then it will be preserved for proper MultiMaterial continuation. |
| 189 | |
| 190 | if ( previousMaterial && previousMaterial.name && typeof previousMaterial.clone === 'function' ) { |
| 191 | |
| 192 | const declared = previousMaterial.clone( 0 ); |
| 193 | declared.inherited = true; |
| 194 | this.object.materials.push( declared ); |
| 195 | |
| 196 | } |
| 197 | |
| 198 | this.objects.push( this.object ); |
| 199 | |
| 200 | }, |
| 201 | |
| 202 | finalize: function () { |
| 203 | |
| 204 | if ( this.object && typeof this.object._finalize === 'function' ) { |
| 205 | |
| 206 | this.object._finalize( true ); |
| 207 | |
| 208 | } |
| 209 | |
| 210 | }, |
| 211 | |
| 212 | parseVertexIndex: function ( value, len ) { |
| 213 | |
| 214 | const index = parseInt( value, 10 ); |
| 215 | return ( index >= 0 ? index - 1 : index + len / 3 ) * 3; |
| 216 | |
| 217 | }, |
| 218 | |
| 219 | parseNormalIndex: function ( value, len ) { |
| 220 | |
| 221 | const index = parseInt( value, 10 ); |
| 222 | return ( index >= 0 ? index - 1 : index + len / 3 ) * 3; |
| 223 | |
| 224 | }, |
| 225 | |
| 226 | parseUVIndex: function ( value, len ) { |
| 227 | |
| 228 | const index = parseInt( value, 10 ); |
| 229 | return ( index >= 0 ? index - 1 : index + len / 2 ) * 2; |
| 230 | |
| 231 | }, |
| 232 | |
| 233 | addVertex: function ( a, b, c ) { |
| 234 | |
| 235 | const src = this.vertices; |
| 236 | const dst = this.object.geometry.vertices; |
| 237 | |
| 238 | dst.push( src[ a + 0 ], src[ a + 1 ], src[ a + 2 ] ); |
| 239 | dst.push( src[ b + 0 ], src[ b + 1 ], src[ b + 2 ] ); |
| 240 | dst.push( src[ c + 0 ], src[ c + 1 ], src[ c + 2 ] ); |
| 241 | |
| 242 | }, |
| 243 | |
| 244 | addVertexPoint: function ( a ) { |
| 245 | |
| 246 | const src = this.vertices; |
| 247 | const dst = this.object.geometry.vertices; |
| 248 | |
| 249 | dst.push( src[ a + 0 ], src[ a + 1 ], src[ a + 2 ] ); |
| 250 | |
| 251 | }, |
| 252 | |
| 253 | addVertexLine: function ( a ) { |
| 254 | |
| 255 | const src = this.vertices; |
| 256 | const dst = this.object.geometry.vertices; |
| 257 | |
| 258 | dst.push( src[ a + 0 ], src[ a + 1 ], src[ a + 2 ] ); |
| 259 | |
| 260 | }, |
| 261 | |
| 262 | addNormal: function ( a, b, c ) { |
| 263 | |
| 264 | const src = this.normals; |
| 265 | const dst = this.object.geometry.normals; |
| 266 | |
| 267 | dst.push( src[ a + 0 ], src[ a + 1 ], src[ a + 2 ] ); |
| 268 | dst.push( src[ b + 0 ], src[ b + 1 ], src[ b + 2 ] ); |
| 269 | dst.push( src[ c + 0 ], src[ c + 1 ], src[ c + 2 ] ); |
| 270 | |
| 271 | }, |
| 272 | |
| 273 | addFaceNormal: function ( a, b, c ) { |
| 274 | |
| 275 | const src = this.vertices; |
| 276 | const dst = this.object.geometry.normals; |
| 277 | |
| 278 | _vA.fromArray( src, a ); |
| 279 | _vB.fromArray( src, b ); |
| 280 | _vC.fromArray( src, c ); |
| 281 | |
| 282 | _cb.subVectors( _vC, _vB ); |
| 283 | _ab.subVectors( _vA, _vB ); |
| 284 | _cb.cross( _ab ); |
| 285 | |
| 286 | _cb.normalize(); |
| 287 | |
| 288 | dst.push( _cb.x, _cb.y, _cb.z ); |
| 289 | dst.push( _cb.x, _cb.y, _cb.z ); |
| 290 | dst.push( _cb.x, _cb.y, _cb.z ); |
| 291 | |
| 292 | }, |
| 293 | |
| 294 | addColor: function ( a, b, c ) { |
| 295 | |
| 296 | const src = this.colors; |
| 297 | const dst = this.object.geometry.colors; |
| 298 | |
| 299 | if ( src[ a ] !== undefined ) dst.push( src[ a + 0 ], src[ a + 1 ], src[ a + 2 ] ); |
| 300 | if ( src[ b ] !== undefined ) dst.push( src[ b + 0 ], src[ b + 1 ], src[ b + 2 ] ); |
| 301 | if ( src[ c ] !== undefined ) dst.push( src[ c + 0 ], src[ c + 1 ], src[ c + 2 ] ); |
| 302 | |
| 303 | }, |
| 304 | |
| 305 | addUV: function ( a, b, c ) { |
| 306 | |
| 307 | const src = this.uvs; |
| 308 | const dst = this.object.geometry.uvs; |
| 309 | |
| 310 | dst.push( src[ a + 0 ], src[ a + 1 ] ); |
| 311 | dst.push( src[ b + 0 ], src[ b + 1 ] ); |
| 312 | dst.push( src[ c + 0 ], src[ c + 1 ] ); |
| 313 | |
| 314 | }, |
| 315 | |
| 316 | addDefaultUV: function () { |
| 317 | |
| 318 | const dst = this.object.geometry.uvs; |
| 319 | |
| 320 | dst.push( 0, 0 ); |
| 321 | dst.push( 0, 0 ); |
| 322 | dst.push( 0, 0 ); |
| 323 | |
| 324 | }, |
| 325 | |
| 326 | addUVLine: function ( a ) { |
| 327 | |
| 328 | const src = this.uvs; |
| 329 | const dst = this.object.geometry.uvs; |
| 330 | |
| 331 | dst.push( src[ a + 0 ], src[ a + 1 ] ); |
| 332 | |
| 333 | }, |
| 334 | |
| 335 | addFace: function ( a, b, c, ua, ub, uc, na, nb, nc ) { |
| 336 | |
| 337 | const vLen = this.vertices.length; |
| 338 | |
| 339 | let ia = this.parseVertexIndex( a, vLen ); |
| 340 | let ib = this.parseVertexIndex( b, vLen ); |
| 341 | let ic = this.parseVertexIndex( c, vLen ); |
| 342 | |
| 343 | this.addVertex( ia, ib, ic ); |
| 344 | this.addColor( ia, ib, ic ); |
| 345 | |
| 346 | // normals |
| 347 | |
| 348 | if ( na !== undefined && na !== '' ) { |
| 349 | |
| 350 | const nLen = this.normals.length; |
| 351 | |
| 352 | ia = this.parseNormalIndex( na, nLen ); |
| 353 | ib = this.parseNormalIndex( nb, nLen ); |
| 354 | ic = this.parseNormalIndex( nc, nLen ); |
| 355 | |
| 356 | this.addNormal( ia, ib, ic ); |
| 357 | |
| 358 | } else { |
| 359 | |
| 360 | this.addFaceNormal( ia, ib, ic ); |
| 361 | |
| 362 | } |
| 363 | |
| 364 | // uvs |
| 365 | |
| 366 | if ( ua !== undefined && ua !== '' ) { |
| 367 | |
| 368 | const uvLen = this.uvs.length; |
| 369 | |
| 370 | ia = this.parseUVIndex( ua, uvLen ); |
| 371 | ib = this.parseUVIndex( ub, uvLen ); |
| 372 | ic = this.parseUVIndex( uc, uvLen ); |
| 373 | |
| 374 | this.addUV( ia, ib, ic ); |
| 375 | |
| 376 | this.object.geometry.hasUVIndices = true; |
| 377 | |
| 378 | } else { |
| 379 | |
| 380 | // add placeholder values (for inconsistent face definitions) |
| 381 | |
| 382 | this.addDefaultUV(); |
| 383 | |
| 384 | } |
| 385 | |
| 386 | }, |
| 387 | |
| 388 | addPointGeometry: function ( vertices ) { |
| 389 | |
| 390 | this.object.geometry.type = 'Points'; |
| 391 | |
| 392 | const vLen = this.vertices.length; |
| 393 | |
| 394 | for ( let vi = 0, l = vertices.length; vi < l; vi ++ ) { |
| 395 | |
| 396 | const index = this.parseVertexIndex( vertices[ vi ], vLen ); |
| 397 | |
| 398 | this.addVertexPoint( index ); |
| 399 | this.addColor( index ); |
| 400 | |
| 401 | } |
| 402 | |
| 403 | }, |
| 404 | |
| 405 | addLineGeometry: function ( vertices, uvs ) { |
| 406 | |
| 407 | this.object.geometry.type = 'Line'; |
| 408 | |
| 409 | const vLen = this.vertices.length; |
| 410 | const uvLen = this.uvs.length; |
| 411 | |
| 412 | for ( let vi = 0, l = vertices.length; vi < l; vi ++ ) { |
| 413 | |
| 414 | this.addVertexLine( this.parseVertexIndex( vertices[ vi ], vLen ) ); |
| 415 | |
| 416 | } |
| 417 | |
| 418 | for ( let uvi = 0, l = uvs.length; uvi < l; uvi ++ ) { |
| 419 | |
| 420 | this.addUVLine( this.parseUVIndex( uvs[ uvi ], uvLen ) ); |
| 421 | |
| 422 | } |
| 423 | |
| 424 | } |
| 425 | |
| 426 | }; |
| 427 | |
| 428 | state.startObject( '', false ); |
| 429 | |
| 430 | return state; |
| 431 | |
| 432 | } |
| 433 | |
| 434 | // |
| 435 | |
| 436 | class OBJLoader extends Loader { |
| 437 | |
| 438 | constructor( manager ) { |
| 439 | |
| 440 | super( manager ); |
| 441 | |
| 442 | this.materials = null; |
| 443 | |
| 444 | } |
| 445 | |
| 446 | load( url, onLoad, onProgress, onError ) { |
| 447 | |
| 448 | const scope = this; |
| 449 | |
| 450 | const loader = new FileLoader( this.manager ); |
| 451 | loader.setPath( this.path ); |
| 452 | loader.setRequestHeader( this.requestHeader ); |
| 453 | loader.setWithCredentials( this.withCredentials ); |
| 454 | loader.load( url, function ( text ) { |
| 455 | |
| 456 | try { |
| 457 | |
| 458 | onLoad( scope.parse( text ) ); |
| 459 | |
| 460 | } catch ( e ) { |
| 461 | |
| 462 | if ( onError ) { |
| 463 | |
| 464 | onError( e ); |
| 465 | |
| 466 | } else { |
| 467 | |
| 468 | console.error( e ); |
| 469 | |
| 470 | } |
| 471 | |
| 472 | scope.manager.itemError( url ); |
| 473 | |
| 474 | } |
| 475 | |
| 476 | }, onProgress, onError ); |
| 477 | |
| 478 | } |
| 479 | |
| 480 | setMaterials( materials ) { |
| 481 | |
| 482 | this.materials = materials; |
| 483 | |
| 484 | return this; |
| 485 | |
| 486 | } |
| 487 | |
| 488 | parse( text ) { |
| 489 | |
| 490 | const state = new ParserState(); |
| 491 | |
| 492 | if ( text.indexOf( '\r\n' ) !== - 1 ) { |
| 493 | |
| 494 | // This is faster than String.split with regex that splits on both |
| 495 | text = text.replace( /\r\n/g, '\n' ); |
| 496 | |
| 497 | } |
| 498 | |
| 499 | if ( text.indexOf( '\\\n' ) !== - 1 ) { |
| 500 | |
| 501 | // join lines separated by a line continuation character (\) |
| 502 | text = text.replace( /\\\n/g, '' ); |
| 503 | |
| 504 | } |
| 505 | |
| 506 | const lines = text.split( '\n' ); |
| 507 | let result = []; |
| 508 | |
| 509 | for ( let i = 0, l = lines.length; i < l; i ++ ) { |
| 510 | |
| 511 | const line = lines[ i ].trimStart(); |
| 512 | |
| 513 | if ( line.length === 0 ) continue; |
| 514 | |
| 515 | const lineFirstChar = line.charAt( 0 ); |
| 516 | |
| 517 | // @todo invoke passed in handler if any |
| 518 | if ( lineFirstChar === '#' ) continue; // skip comments |
| 519 | |
| 520 | if ( lineFirstChar === 'v' ) { |
| 521 | |
| 522 | const data = line.split( _face_vertex_data_separator_pattern ); |
| 523 | |
| 524 | switch ( data[ 0 ] ) { |
| 525 | |
| 526 | case 'v': |
| 527 | state.vertices.push( |
| 528 | parseFloat( data[ 1 ] ), |
| 529 | parseFloat( data[ 2 ] ), |
| 530 | parseFloat( data[ 3 ] ) |
| 531 | ); |
| 532 | if ( data.length >= 7 ) { |
| 533 | |
| 534 | _color.setRGB( |
| 535 | parseFloat( data[ 4 ] ), |
| 536 | parseFloat( data[ 5 ] ), |
| 537 | parseFloat( data[ 6 ] ) |
| 538 | ).convertSRGBToLinear(); |
| 539 | |
| 540 | state.colors.push( _color.r, _color.g, _color.b ); |
| 541 | |
| 542 | } else { |
| 543 | |
| 544 | // if no colors are defined, add placeholders so color and vertex indices match |
| 545 | |
| 546 | state.colors.push( undefined, undefined, undefined ); |
| 547 | |
| 548 | } |
| 549 | |
| 550 | break; |
| 551 | case 'vn': |
| 552 | state.normals.push( |
| 553 | parseFloat( data[ 1 ] ), |
| 554 | parseFloat( data[ 2 ] ), |
| 555 | parseFloat( data[ 3 ] ) |
| 556 | ); |
| 557 | break; |
| 558 | case 'vt': |
| 559 | state.uvs.push( |
| 560 | parseFloat( data[ 1 ] ), |
| 561 | parseFloat( data[ 2 ] ) |
| 562 | ); |
| 563 | break; |
| 564 | |
| 565 | } |
| 566 | |
| 567 | } else if ( lineFirstChar === 'f' ) { |
| 568 | |
| 569 | const lineData = line.slice( 1 ).trim(); |
| 570 | const vertexData = lineData.split( _face_vertex_data_separator_pattern ); |
| 571 | const faceVertices = []; |
| 572 | |
| 573 | // Parse the face vertex data into an easy to work with format |
| 574 | |
| 575 | for ( let j = 0, jl = vertexData.length; j < jl; j ++ ) { |
| 576 | |
| 577 | const vertex = vertexData[ j ]; |
| 578 | |
| 579 | if ( vertex.length > 0 ) { |
| 580 | |
| 581 | const vertexParts = vertex.split( '/' ); |
| 582 | faceVertices.push( vertexParts ); |
| 583 | |
| 584 | } |
| 585 | |
| 586 | } |
| 587 | |
| 588 | // Draw an edge between the first vertex and all subsequent vertices to form an n-gon |
| 589 | |
| 590 | const v1 = faceVertices[ 0 ]; |
| 591 | |
| 592 | for ( let j = 1, jl = faceVertices.length - 1; j < jl; j ++ ) { |
| 593 | |
| 594 | const v2 = faceVertices[ j ]; |
| 595 | const v3 = faceVertices[ j + 1 ]; |
| 596 | |
| 597 | state.addFace( |
| 598 | v1[ 0 ], v2[ 0 ], v3[ 0 ], |
| 599 | v1[ 1 ], v2[ 1 ], v3[ 1 ], |
| 600 | v1[ 2 ], v2[ 2 ], v3[ 2 ] |
| 601 | ); |
| 602 | |
| 603 | } |
| 604 | |
| 605 | } else if ( lineFirstChar === 'l' ) { |
| 606 | |
| 607 | const lineParts = line.substring( 1 ).trim().split( ' ' ); |
| 608 | let lineVertices = []; |
| 609 | const lineUVs = []; |
| 610 | |
| 611 | if ( line.indexOf( '/' ) === - 1 ) { |
| 612 | |
| 613 | lineVertices = lineParts; |
| 614 | |
| 615 | } else { |
| 616 | |
| 617 | for ( let li = 0, llen = lineParts.length; li < llen; li ++ ) { |
| 618 | |
| 619 | const parts = lineParts[ li ].split( '/' ); |
| 620 | |
| 621 | if ( parts[ 0 ] !== '' ) lineVertices.push( parts[ 0 ] ); |
| 622 | if ( parts[ 1 ] !== '' ) lineUVs.push( parts[ 1 ] ); |
| 623 | |
| 624 | } |
| 625 | |
| 626 | } |
| 627 | |
| 628 | state.addLineGeometry( lineVertices, lineUVs ); |
| 629 | |
| 630 | } else if ( lineFirstChar === 'p' ) { |
| 631 | |
| 632 | const lineData = line.slice( 1 ).trim(); |
| 633 | const pointData = lineData.split( ' ' ); |
| 634 | |
| 635 | state.addPointGeometry( pointData ); |
| 636 | |
| 637 | } else if ( ( result = _object_pattern.exec( line ) ) !== null ) { |
| 638 | |
| 639 | // o object_name |
| 640 | // or |
| 641 | // g group_name |
| 642 | |
| 643 | // WORKAROUND: https://bugs.chromium.org/p/v8/issues/detail?id=2869 |
| 644 | // let name = result[ 0 ].slice( 1 ).trim(); |
| 645 | const name = ( ' ' + result[ 0 ].slice( 1 ).trim() ).slice( 1 ); |
| 646 | |
| 647 | state.startObject( name ); |
| 648 | |
| 649 | } else if ( _material_use_pattern.test( line ) ) { |
| 650 | |
| 651 | // material |
| 652 | |
| 653 | state.object.startMaterial( line.substring( 7 ).trim(), state.materialLibraries ); |
| 654 | |
| 655 | } else if ( _material_library_pattern.test( line ) ) { |
| 656 | |
| 657 | // mtl file |
| 658 | |
| 659 | state.materialLibraries.push( line.substring( 7 ).trim() ); |
| 660 | |
| 661 | } else if ( _map_use_pattern.test( line ) ) { |
| 662 | |
| 663 | // the line is parsed but ignored since the loader assumes textures are defined MTL files |
| 664 | // (according to https://www.okino.com/conv/imp_wave.htm, 'usemap' is the old-style Wavefront texture reference method) |
| 665 | |
| 666 | console.warn( 'THREE.OBJLoader: Rendering identifier "usemap" not supported. Textures must be defined in MTL files.' ); |
| 667 | |
| 668 | } else if ( lineFirstChar === 's' ) { |
| 669 | |
| 670 | result = line.split( ' ' ); |
| 671 | |
| 672 | // smooth shading |
| 673 | |
| 674 | // @todo Handle files that have varying smooth values for a set of faces inside one geometry, |
| 675 | // but does not define a usemtl for each face set. |
| 676 | // This should be detected and a dummy material created (later MultiMaterial and geometry groups). |
| 677 | // This requires some care to not create extra material on each smooth value for "normal" obj files. |
| 678 | // where explicit usemtl defines geometry groups. |
| 679 | // Example asset: examples/models/obj/cerberus/Cerberus.obj |
| 680 | |
| 681 | /* |
| 682 | * http://paulbourke.net/dataformats/obj/ |
| 683 | * |
| 684 | * From chapter "Grouping" Syntax explanation "s group_number": |
| 685 | * "group_number is the smoothing group number. To turn off smoothing groups, use a value of 0 or off. |
| 686 | * Polygonal elements use group numbers to put elements in different smoothing groups. For free-form |
| 687 | * surfaces, smoothing groups are either turned on or off; there is no difference between values greater |
| 688 | * than 0." |
| 689 | */ |
| 690 | if ( result.length > 1 ) { |
| 691 | |
| 692 | const value = result[ 1 ].trim().toLowerCase(); |
| 693 | state.object.smooth = ( value !== '0' && value !== 'off' ); |
| 694 | |
| 695 | } else { |
| 696 | |
| 697 | // ZBrush can produce "s" lines #11707 |
| 698 | state.object.smooth = true; |
| 699 | |
| 700 | } |
| 701 | |
| 702 | const material = state.object.currentMaterial(); |
| 703 | if ( material ) material.smooth = state.object.smooth; |
| 704 | |
| 705 | } else { |
| 706 | |
| 707 | // Handle null terminated files without exception |
| 708 | if ( line === '\0' ) continue; |
| 709 | |
| 710 | console.warn( 'THREE.OBJLoader: Unexpected line: "' + line + '"' ); |
| 711 | |
| 712 | } |
| 713 | |
| 714 | } |
| 715 | |
| 716 | state.finalize(); |
| 717 | |
| 718 | const container = new Group(); |
| 719 | container.materialLibraries = [].concat( state.materialLibraries ); |
| 720 | |
| 721 | const hasPrimitives = ! ( state.objects.length === 1 && state.objects[ 0 ].geometry.vertices.length === 0 ); |
| 722 | |
| 723 | if ( hasPrimitives === true ) { |
| 724 | |
| 725 | for ( let i = 0, l = state.objects.length; i < l; i ++ ) { |
| 726 | |
| 727 | const object = state.objects[ i ]; |
| 728 | const geometry = object.geometry; |
| 729 | const materials = object.materials; |
| 730 | const isLine = ( geometry.type === 'Line' ); |
| 731 | const isPoints = ( geometry.type === 'Points' ); |
| 732 | let hasVertexColors = false; |
| 733 | |
| 734 | // Skip o/g line declarations that did not follow with any faces |
| 735 | if ( geometry.vertices.length === 0 ) continue; |
| 736 | |
| 737 | const buffergeometry = new BufferGeometry(); |
| 738 | |
| 739 | buffergeometry.setAttribute( 'position', new Float32BufferAttribute( geometry.vertices, 3 ) ); |
| 740 | |
| 741 | if ( geometry.normals.length > 0 ) { |
| 742 | |
| 743 | buffergeometry.setAttribute( 'normal', new Float32BufferAttribute( geometry.normals, 3 ) ); |
| 744 | |
| 745 | } |
| 746 | |
| 747 | if ( geometry.colors.length > 0 ) { |
| 748 | |
| 749 | hasVertexColors = true; |
| 750 | buffergeometry.setAttribute( 'color', new Float32BufferAttribute( geometry.colors, 3 ) ); |
| 751 | |
| 752 | } |
| 753 | |
| 754 | if ( geometry.hasUVIndices === true ) { |
| 755 | |
| 756 | buffergeometry.setAttribute( 'uv', new Float32BufferAttribute( geometry.uvs, 2 ) ); |
| 757 | |
| 758 | } |
| 759 | |
| 760 | // Create materials |
| 761 | |
| 762 | const createdMaterials = []; |
| 763 | |
| 764 | for ( let mi = 0, miLen = materials.length; mi < miLen; mi ++ ) { |
| 765 | |
| 766 | const sourceMaterial = materials[ mi ]; |
| 767 | const materialHash = sourceMaterial.name + '_' + sourceMaterial.smooth + '_' + hasVertexColors; |
| 768 | let material = state.materials[ materialHash ]; |
| 769 | |
| 770 | if ( this.materials !== null ) { |
| 771 | |
| 772 | material = this.materials.create( sourceMaterial.name ); |
| 773 | |
| 774 | // mtl etc. loaders probably can't create line materials correctly, copy properties to a line material. |
| 775 | if ( isLine && material && ! ( material instanceof LineBasicMaterial ) ) { |
| 776 | |
| 777 | const materialLine = new LineBasicMaterial(); |
| 778 | Material.prototype.copy.call( materialLine, material ); |
| 779 | materialLine.color.copy( material.color ); |
| 780 | material = materialLine; |
| 781 | |
| 782 | } else if ( isPoints && material && ! ( material instanceof PointsMaterial ) ) { |
| 783 | |
| 784 | const materialPoints = new PointsMaterial( { size: 10, sizeAttenuation: false } ); |
| 785 | Material.prototype.copy.call( materialPoints, material ); |
| 786 | materialPoints.color.copy( material.color ); |
| 787 | materialPoints.map = material.map; |
| 788 | material = materialPoints; |
| 789 | |
| 790 | } |
| 791 | |
| 792 | } |
| 793 | |
| 794 | if ( material === undefined ) { |
| 795 | |
| 796 | if ( isLine ) { |
| 797 | |
| 798 | material = new LineBasicMaterial(); |
| 799 | |
| 800 | } else if ( isPoints ) { |
| 801 | |
| 802 | material = new PointsMaterial( { size: 1, sizeAttenuation: false } ); |
| 803 | |
| 804 | } else { |
| 805 | |
| 806 | material = new MeshPhongMaterial(); |
| 807 | |
| 808 | } |
| 809 | |
| 810 | material.name = sourceMaterial.name; |
| 811 | material.flatShading = sourceMaterial.smooth ? false : true; |
| 812 | material.vertexColors = hasVertexColors; |
| 813 | |
| 814 | state.materials[ materialHash ] = material; |
| 815 | |
| 816 | } |
| 817 | |
| 818 | createdMaterials.push( material ); |
| 819 | |
| 820 | } |
| 821 | |
| 822 | // Create mesh |
| 823 | |
| 824 | let mesh; |
| 825 | |
| 826 | if ( createdMaterials.length > 1 ) { |
| 827 | |
| 828 | for ( let mi = 0, miLen = materials.length; mi < miLen; mi ++ ) { |
| 829 | |
| 830 | const sourceMaterial = materials[ mi ]; |
| 831 | buffergeometry.addGroup( sourceMaterial.groupStart, sourceMaterial.groupCount, mi ); |
| 832 | |
| 833 | } |
| 834 | |
| 835 | if ( isLine ) { |
| 836 | |
| 837 | mesh = new LineSegments( buffergeometry, createdMaterials ); |
| 838 | |
| 839 | } else if ( isPoints ) { |
| 840 | |
| 841 | mesh = new Points( buffergeometry, createdMaterials ); |
| 842 | |
| 843 | } else { |
| 844 | |
| 845 | mesh = new Mesh( buffergeometry, createdMaterials ); |
| 846 | |
| 847 | } |
| 848 | |
| 849 | } else { |
| 850 | |
| 851 | if ( isLine ) { |
| 852 | |
| 853 | mesh = new LineSegments( buffergeometry, createdMaterials[ 0 ] ); |
| 854 | |
| 855 | } else if ( isPoints ) { |
| 856 | |
| 857 | mesh = new Points( buffergeometry, createdMaterials[ 0 ] ); |
| 858 | |
| 859 | } else { |
| 860 | |
| 861 | mesh = new Mesh( buffergeometry, createdMaterials[ 0 ] ); |
| 862 | |
| 863 | } |
| 864 | |
| 865 | } |
| 866 | |
| 867 | mesh.name = object.name; |
| 868 | |
| 869 | container.add( mesh ); |
| 870 | |
| 871 | } |
| 872 | |
| 873 | } else { |
| 874 | |
| 875 | // if there is only the default parser state object with no geometry data, interpret data as point cloud |
| 876 | |
| 877 | if ( state.vertices.length > 0 ) { |
| 878 | |
| 879 | const material = new PointsMaterial( { size: 1, sizeAttenuation: false } ); |
| 880 | |
| 881 | const buffergeometry = new BufferGeometry(); |
| 882 | |
| 883 | buffergeometry.setAttribute( 'position', new Float32BufferAttribute( state.vertices, 3 ) ); |
| 884 | |
| 885 | if ( state.colors.length > 0 && state.colors[ 0 ] !== undefined ) { |
| 886 | |
| 887 | buffergeometry.setAttribute( 'color', new Float32BufferAttribute( state.colors, 3 ) ); |
| 888 | material.vertexColors = true; |
| 889 | |
| 890 | } |
| 891 | |
| 892 | const points = new Points( buffergeometry, material ); |
| 893 | container.add( points ); |
| 894 | |
| 895 | } |
| 896 | |
| 897 | } |
| 898 | |
| 899 | return container; |
| 900 | |
| 901 | } |
| 902 | |
| 903 | } |
| 904 | |
| 905 | export { OBJLoader }; |