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