@loaders.gl/obj 4.0.0-alpha.5 → 4.0.0-alpha.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (80) hide show
  1. package/dist/bundle.js +2 -2
  2. package/dist/dist.min.js +97 -549
  3. package/dist/es5/bundle.js +6 -0
  4. package/dist/es5/bundle.js.map +1 -0
  5. package/dist/es5/index.js +74 -0
  6. package/dist/es5/index.js.map +1 -0
  7. package/dist/es5/lib/get-obj-schema.js +54 -0
  8. package/dist/es5/lib/get-obj-schema.js.map +1 -0
  9. package/dist/es5/lib/obj-types.js +2 -0
  10. package/dist/es5/lib/obj-types.js.map +1 -0
  11. package/dist/es5/lib/parse-mtl.js +86 -0
  12. package/dist/es5/lib/parse-mtl.js.map +1 -0
  13. package/dist/es5/lib/parse-obj-meshes.js +486 -0
  14. package/dist/es5/lib/parse-obj-meshes.js.map +1 -0
  15. package/dist/es5/lib/parse-obj.js +100 -0
  16. package/dist/es5/lib/parse-obj.js.map +1 -0
  17. package/dist/es5/mtl-loader.js +26 -0
  18. package/dist/es5/mtl-loader.js.map +1 -0
  19. package/dist/es5/obj-loader.js +27 -0
  20. package/dist/es5/obj-loader.js.map +1 -0
  21. package/dist/es5/workers/obj-worker.js +6 -0
  22. package/dist/es5/workers/obj-worker.js.map +1 -0
  23. package/dist/esm/bundle.js +4 -0
  24. package/dist/esm/bundle.js.map +1 -0
  25. package/dist/esm/index.js +18 -0
  26. package/dist/esm/index.js.map +1 -0
  27. package/dist/esm/lib/get-obj-schema.js +49 -0
  28. package/dist/esm/lib/get-obj-schema.js.map +1 -0
  29. package/dist/esm/lib/obj-types.js +2 -0
  30. package/dist/esm/lib/obj-types.js.map +1 -0
  31. package/dist/esm/lib/parse-mtl.js +68 -0
  32. package/dist/esm/lib/parse-mtl.js.map +1 -0
  33. package/dist/esm/lib/parse-obj-meshes.js +385 -0
  34. package/dist/esm/lib/parse-obj-meshes.js.map +1 -0
  35. package/dist/esm/lib/parse-obj.js +82 -0
  36. package/dist/esm/lib/parse-obj.js.map +1 -0
  37. package/dist/esm/mtl-loader.js +16 -0
  38. package/dist/esm/mtl-loader.js.map +1 -0
  39. package/dist/esm/obj-loader.js +19 -0
  40. package/dist/esm/obj-loader.js.map +1 -0
  41. package/dist/esm/workers/obj-worker.js +4 -0
  42. package/dist/esm/workers/obj-worker.js.map +1 -0
  43. package/dist/index.d.ts +4 -58
  44. package/dist/index.d.ts.map +1 -1
  45. package/dist/index.js +25 -14
  46. package/dist/lib/get-obj-schema.d.ts +3 -2
  47. package/dist/lib/get-obj-schema.d.ts.map +1 -1
  48. package/dist/lib/get-obj-schema.js +33 -33
  49. package/dist/lib/obj-types.js +1 -2
  50. package/dist/lib/parse-mtl.d.ts +2 -2
  51. package/dist/lib/parse-mtl.d.ts.map +1 -1
  52. package/dist/lib/parse-mtl.js +185 -70
  53. package/dist/lib/parse-obj-meshes.js +408 -426
  54. package/dist/lib/parse-obj.d.ts +2 -13
  55. package/dist/lib/parse-obj.d.ts.map +1 -1
  56. package/dist/lib/parse-obj.js +66 -86
  57. package/dist/mtl-loader.d.ts +3 -15
  58. package/dist/mtl-loader.d.ts.map +1 -1
  59. package/dist/mtl-loader.js +23 -15
  60. package/dist/obj-loader.d.ts +3 -16
  61. package/dist/obj-loader.d.ts.map +1 -1
  62. package/dist/obj-loader.js +24 -18
  63. package/dist/obj-worker.js +133 -517
  64. package/dist/workers/obj-worker.js +5 -4
  65. package/package.json +6 -6
  66. package/src/index.ts +6 -3
  67. package/src/lib/get-obj-schema.ts +24 -20
  68. package/src/lib/parse-obj.ts +4 -3
  69. package/src/mtl-loader.ts +2 -2
  70. package/src/obj-loader.ts +2 -1
  71. package/dist/bundle.js.map +0 -1
  72. package/dist/index.js.map +0 -1
  73. package/dist/lib/get-obj-schema.js.map +0 -1
  74. package/dist/lib/obj-types.js.map +0 -1
  75. package/dist/lib/parse-mtl.js.map +0 -1
  76. package/dist/lib/parse-obj-meshes.js.map +0 -1
  77. package/dist/lib/parse-obj.js.map +0 -1
  78. package/dist/mtl-loader.js.map +0 -1
  79. package/dist/obj-loader.js.map +0 -1
  80. package/dist/workers/obj-worker.js.map +0 -1
@@ -1,458 +1,440 @@
1
+ "use strict";
2
+ // OBJ Loader, adapted from THREE.js (MIT license)
3
+ //
4
+ // Attributions per original THREE.js source file:
5
+ //
6
+ // @author mrdoob / http://mrdoob.com/
7
+ Object.defineProperty(exports, "__esModule", { value: true });
8
+ exports.parseOBJMeshes = void 0;
9
+ // @ts-nocheck
10
+ // o object_name | g group_name
1
11
  const OBJECT_RE = /^[og]\s*(.+)?/;
12
+ // mtllib file_reference
2
13
  const MATERIAL_RE = /^mtllib /;
14
+ // usemtl material_name
3
15
  const MATERIAL_USE_RE = /^usemtl /;
4
-
5
16
  class MeshMaterial {
6
- constructor({
7
- index,
8
- name = '',
9
- mtllib,
10
- smooth,
11
- groupStart
12
- }) {
13
- this.index = index;
14
- this.name = name;
15
- this.mtllib = mtllib;
16
- this.smooth = smooth;
17
- this.groupStart = groupStart;
18
- this.groupEnd = -1;
19
- this.groupCount = -1;
20
- this.inherited = false;
21
- }
22
-
23
- clone(index = this.index) {
24
- return new MeshMaterial({
25
- index,
26
- name: this.name,
27
- mtllib: this.mtllib,
28
- smooth: this.smooth,
29
- groupStart: 0
30
- });
31
- }
32
-
17
+ constructor({ index, name = '', mtllib, smooth, groupStart }) {
18
+ this.index = index;
19
+ this.name = name;
20
+ this.mtllib = mtllib;
21
+ this.smooth = smooth;
22
+ this.groupStart = groupStart;
23
+ this.groupEnd = -1;
24
+ this.groupCount = -1;
25
+ this.inherited = false;
26
+ }
27
+ clone(index = this.index) {
28
+ return new MeshMaterial({
29
+ index,
30
+ name: this.name,
31
+ mtllib: this.mtllib,
32
+ smooth: this.smooth,
33
+ groupStart: 0
34
+ });
35
+ }
33
36
  }
34
-
35
37
  class MeshObject {
36
- constructor(name = '') {
37
- this.name = name;
38
- this.geometry = {
39
- vertices: [],
40
- normals: [],
41
- colors: [],
42
- uvs: []
43
- };
44
- this.materials = [];
45
- this.smooth = true;
46
- this.fromDeclaration = null;
47
- }
48
-
49
- startMaterial(name, libraries) {
50
- const previous = this._finalize(false);
51
-
52
- if (previous && (previous.inherited || previous.groupCount <= 0)) {
53
- this.materials.splice(previous.index, 1);
54
- }
55
-
56
- const material = new MeshMaterial({
57
- index: this.materials.length,
58
- name,
59
- mtllib: Array.isArray(libraries) && libraries.length > 0 ? libraries[libraries.length - 1] : '',
60
- smooth: previous !== undefined ? previous.smooth : this.smooth,
61
- groupStart: previous !== undefined ? previous.groupEnd : 0
62
- });
63
- this.materials.push(material);
64
- return material;
65
- }
66
-
67
- currentMaterial() {
68
- if (this.materials.length > 0) {
69
- return this.materials[this.materials.length - 1];
38
+ constructor(name = '') {
39
+ this.name = name;
40
+ this.geometry = {
41
+ vertices: [],
42
+ normals: [],
43
+ colors: [],
44
+ uvs: []
45
+ };
46
+ this.materials = [];
47
+ this.smooth = true;
48
+ this.fromDeclaration = null;
70
49
  }
71
-
72
- return undefined;
73
- }
74
-
75
- _finalize(end) {
76
- const lastMultiMaterial = this.currentMaterial();
77
-
78
- if (lastMultiMaterial && lastMultiMaterial.groupEnd === -1) {
79
- lastMultiMaterial.groupEnd = this.geometry.vertices.length / 3;
80
- lastMultiMaterial.groupCount = lastMultiMaterial.groupEnd - lastMultiMaterial.groupStart;
81
- lastMultiMaterial.inherited = false;
50
+ startMaterial(name, libraries) {
51
+ const previous = this._finalize(false);
52
+ // New usemtl declaration overwrites an inherited material, except if faces were declared
53
+ // after the material, then it must be preserved for proper MultiMaterial continuation.
54
+ if (previous && (previous.inherited || previous.groupCount <= 0)) {
55
+ this.materials.splice(previous.index, 1);
56
+ }
57
+ const material = new MeshMaterial({
58
+ index: this.materials.length,
59
+ name,
60
+ mtllib: Array.isArray(libraries) && libraries.length > 0 ? libraries[libraries.length - 1] : '',
61
+ smooth: previous !== undefined ? previous.smooth : this.smooth,
62
+ groupStart: previous !== undefined ? previous.groupEnd : 0
63
+ });
64
+ this.materials.push(material);
65
+ return material;
82
66
  }
83
-
84
- if (end && this.materials.length > 1) {
85
- for (let mi = this.materials.length - 1; mi >= 0; mi--) {
86
- if (this.materials[mi].groupCount <= 0) {
87
- this.materials.splice(mi, 1);
67
+ currentMaterial() {
68
+ if (this.materials.length > 0) {
69
+ return this.materials[this.materials.length - 1];
88
70
  }
89
- }
71
+ return undefined;
90
72
  }
91
-
92
- if (end && this.materials.length === 0) {
93
- this.materials.push({
94
- name: '',
95
- smooth: this.smooth
96
- });
73
+ _finalize(end) {
74
+ const lastMultiMaterial = this.currentMaterial();
75
+ if (lastMultiMaterial && lastMultiMaterial.groupEnd === -1) {
76
+ lastMultiMaterial.groupEnd = this.geometry.vertices.length / 3;
77
+ lastMultiMaterial.groupCount = lastMultiMaterial.groupEnd - lastMultiMaterial.groupStart;
78
+ lastMultiMaterial.inherited = false;
79
+ }
80
+ // Ignore objects tail materials if no face declarations followed them before a new o/g started.
81
+ if (end && this.materials.length > 1) {
82
+ for (let mi = this.materials.length - 1; mi >= 0; mi--) {
83
+ if (this.materials[mi].groupCount <= 0) {
84
+ this.materials.splice(mi, 1);
85
+ }
86
+ }
87
+ }
88
+ // Guarantee at least one empty material, this makes the creation later more straight forward.
89
+ if (end && this.materials.length === 0) {
90
+ this.materials.push({
91
+ name: '',
92
+ smooth: this.smooth
93
+ });
94
+ }
95
+ return lastMultiMaterial;
97
96
  }
98
-
99
- return lastMultiMaterial;
100
- }
101
-
102
97
  }
103
-
104
98
  class ParserState {
105
- constructor() {
106
- this.objects = [];
107
- this.object = null;
108
- this.vertices = [];
109
- this.normals = [];
110
- this.colors = [];
111
- this.uvs = [];
112
- this.materialLibraries = [];
113
- this.startObject('', false);
114
- }
115
-
116
- startObject(name, fromDeclaration = true) {
117
- if (this.object && !this.object.fromDeclaration) {
118
- this.object.name = name;
119
- this.object.fromDeclaration = fromDeclaration;
120
- return;
99
+ constructor() {
100
+ this.objects = [];
101
+ this.object = null;
102
+ this.vertices = [];
103
+ this.normals = [];
104
+ this.colors = [];
105
+ this.uvs = [];
106
+ this.materialLibraries = [];
107
+ this.startObject('', false);
108
+ }
109
+ startObject(name, fromDeclaration = true) {
110
+ // If the current object (initial from reset) is not from a g/o declaration in the parsed
111
+ // file. We need to use it for the first parsed g/o to keep things in sync.
112
+ if (this.object && !this.object.fromDeclaration) {
113
+ this.object.name = name;
114
+ this.object.fromDeclaration = fromDeclaration;
115
+ return;
116
+ }
117
+ const previousMaterial = this.object && typeof this.object.currentMaterial === 'function'
118
+ ? this.object.currentMaterial()
119
+ : undefined;
120
+ if (this.object && typeof this.object._finalize === 'function') {
121
+ this.object._finalize(true);
122
+ }
123
+ this.object = new MeshObject(name);
124
+ this.object.fromDeclaration = fromDeclaration;
125
+ // Inherit previous objects material.
126
+ // Spec tells us that a declared material must be set to all objects until a new material is declared.
127
+ // If a usemtl declaration is encountered while this new object is being parsed, it will
128
+ // overwrite the inherited material. Exception being that there was already face declarations
129
+ // to the inherited material, then it will be preserved for proper MultiMaterial continuation.
130
+ if (previousMaterial && previousMaterial.name && typeof previousMaterial.clone === 'function') {
131
+ const declared = previousMaterial.clone(0);
132
+ declared.inherited = true;
133
+ this.object.materials.push(declared);
134
+ }
135
+ this.objects.push(this.object);
121
136
  }
122
-
123
- const previousMaterial = this.object && typeof this.object.currentMaterial === 'function' ? this.object.currentMaterial() : undefined;
124
-
125
- if (this.object && typeof this.object._finalize === 'function') {
126
- this.object._finalize(true);
137
+ finalize() {
138
+ if (this.object && typeof this.object._finalize === 'function') {
139
+ this.object._finalize(true);
140
+ }
127
141
  }
128
-
129
- this.object = new MeshObject(name);
130
- this.object.fromDeclaration = fromDeclaration;
131
-
132
- if (previousMaterial && previousMaterial.name && typeof previousMaterial.clone === 'function') {
133
- const declared = previousMaterial.clone(0);
134
- declared.inherited = true;
135
- this.object.materials.push(declared);
142
+ parseVertexIndex(value, len) {
143
+ const index = parseInt(value);
144
+ return (index >= 0 ? index - 1 : index + len / 3) * 3;
136
145
  }
137
-
138
- this.objects.push(this.object);
139
- }
140
-
141
- finalize() {
142
- if (this.object && typeof this.object._finalize === 'function') {
143
- this.object._finalize(true);
146
+ parseNormalIndex(value, len) {
147
+ const index = parseInt(value);
148
+ return (index >= 0 ? index - 1 : index + len / 3) * 3;
144
149
  }
145
- }
146
-
147
- parseVertexIndex(value, len) {
148
- const index = parseInt(value);
149
- return (index >= 0 ? index - 1 : index + len / 3) * 3;
150
- }
151
-
152
- parseNormalIndex(value, len) {
153
- const index = parseInt(value);
154
- return (index >= 0 ? index - 1 : index + len / 3) * 3;
155
- }
156
-
157
- parseUVIndex(value, len) {
158
- const index = parseInt(value);
159
- return (index >= 0 ? index - 1 : index + len / 2) * 2;
160
- }
161
-
162
- addVertex(a, b, c) {
163
- const src = this.vertices;
164
- const dst = this.object.geometry.vertices;
165
- dst.push(src[a + 0], src[a + 1], src[a + 2]);
166
- dst.push(src[b + 0], src[b + 1], src[b + 2]);
167
- dst.push(src[c + 0], src[c + 1], src[c + 2]);
168
- }
169
-
170
- addVertexPoint(a) {
171
- const src = this.vertices;
172
- const dst = this.object.geometry.vertices;
173
- dst.push(src[a + 0], src[a + 1], src[a + 2]);
174
- }
175
-
176
- addVertexLine(a) {
177
- const src = this.vertices;
178
- const dst = this.object.geometry.vertices;
179
- dst.push(src[a + 0], src[a + 1], src[a + 2]);
180
- }
181
-
182
- addNormal(a, b, c) {
183
- const src = this.normals;
184
- const dst = this.object.geometry.normals;
185
- dst.push(src[a + 0], src[a + 1], src[a + 2]);
186
- dst.push(src[b + 0], src[b + 1], src[b + 2]);
187
- dst.push(src[c + 0], src[c + 1], src[c + 2]);
188
- }
189
-
190
- addColor(a, b, c) {
191
- const src = this.colors;
192
- const dst = this.object.geometry.colors;
193
- dst.push(src[a + 0], src[a + 1], src[a + 2]);
194
- dst.push(src[b + 0], src[b + 1], src[b + 2]);
195
- dst.push(src[c + 0], src[c + 1], src[c + 2]);
196
- }
197
-
198
- addUV(a, b, c) {
199
- const src = this.uvs;
200
- const dst = this.object.geometry.uvs;
201
- dst.push(src[a + 0], src[a + 1]);
202
- dst.push(src[b + 0], src[b + 1]);
203
- dst.push(src[c + 0], src[c + 1]);
204
- }
205
-
206
- addUVLine(a) {
207
- const src = this.uvs;
208
- const dst = this.object.geometry.uvs;
209
- dst.push(src[a + 0], src[a + 1]);
210
- }
211
-
212
- addFace(a, b, c, ua, ub, uc, na, nb, nc) {
213
- const vLen = this.vertices.length;
214
- let ia = this.parseVertexIndex(a, vLen);
215
- let ib = this.parseVertexIndex(b, vLen);
216
- let ic = this.parseVertexIndex(c, vLen);
217
- this.addVertex(ia, ib, ic);
218
-
219
- if (ua !== undefined && ua !== '') {
220
- const uvLen = this.uvs.length;
221
- ia = this.parseUVIndex(ua, uvLen);
222
- ib = this.parseUVIndex(ub, uvLen);
223
- ic = this.parseUVIndex(uc, uvLen);
224
- this.addUV(ia, ib, ic);
150
+ parseUVIndex(value, len) {
151
+ const index = parseInt(value);
152
+ return (index >= 0 ? index - 1 : index + len / 2) * 2;
225
153
  }
226
-
227
- if (na !== undefined && na !== '') {
228
- const nLen = this.normals.length;
229
- ia = this.parseNormalIndex(na, nLen);
230
- ib = na === nb ? ia : this.parseNormalIndex(nb, nLen);
231
- ic = na === nc ? ia : this.parseNormalIndex(nc, nLen);
232
- this.addNormal(ia, ib, ic);
154
+ addVertex(a, b, c) {
155
+ const src = this.vertices;
156
+ const dst = this.object.geometry.vertices;
157
+ dst.push(src[a + 0], src[a + 1], src[a + 2]);
158
+ dst.push(src[b + 0], src[b + 1], src[b + 2]);
159
+ dst.push(src[c + 0], src[c + 1], src[c + 2]);
233
160
  }
234
-
235
- if (this.colors.length > 0) {
236
- this.addColor(ia, ib, ic);
161
+ addVertexPoint(a) {
162
+ const src = this.vertices;
163
+ const dst = this.object.geometry.vertices;
164
+ dst.push(src[a + 0], src[a + 1], src[a + 2]);
237
165
  }
238
- }
239
-
240
- addPointGeometry(vertices) {
241
- this.object.geometry.type = 'Points';
242
- const vLen = this.vertices.length;
243
-
244
- for (const vertex of vertices) {
245
- this.addVertexPoint(this.parseVertexIndex(vertex, vLen));
166
+ addVertexLine(a) {
167
+ const src = this.vertices;
168
+ const dst = this.object.geometry.vertices;
169
+ dst.push(src[a + 0], src[a + 1], src[a + 2]);
246
170
  }
247
- }
248
-
249
- addLineGeometry(vertices, uvs) {
250
- this.object.geometry.type = 'Line';
251
- const vLen = this.vertices.length;
252
- const uvLen = this.uvs.length;
253
-
254
- for (const vertex of vertices) {
255
- this.addVertexLine(this.parseVertexIndex(vertex, vLen));
171
+ addNormal(a, b, c) {
172
+ const src = this.normals;
173
+ const dst = this.object.geometry.normals;
174
+ dst.push(src[a + 0], src[a + 1], src[a + 2]);
175
+ dst.push(src[b + 0], src[b + 1], src[b + 2]);
176
+ dst.push(src[c + 0], src[c + 1], src[c + 2]);
256
177
  }
257
-
258
- for (const uv of uvs) {
259
- this.addUVLine(this.parseUVIndex(uv, uvLen));
178
+ addColor(a, b, c) {
179
+ const src = this.colors;
180
+ const dst = this.object.geometry.colors;
181
+ dst.push(src[a + 0], src[a + 1], src[a + 2]);
182
+ dst.push(src[b + 0], src[b + 1], src[b + 2]);
183
+ dst.push(src[c + 0], src[c + 1], src[c + 2]);
260
184
  }
261
- }
262
-
263
- }
264
-
265
- export function parseOBJMeshes(text) {
266
- const state = new ParserState();
267
-
268
- if (text.indexOf('\r\n') !== -1) {
269
- text = text.replace(/\r\n/g, '\n');
270
- }
271
-
272
- if (text.indexOf('\\\n') !== -1) {
273
- text = text.replace(/\\\n/g, '');
274
- }
275
-
276
- const lines = text.split('\n');
277
- let line = '';
278
- let lineFirstChar = '';
279
- let lineLength = 0;
280
- let result = [];
281
- const trimLeft = typeof ''.trimLeft === 'function';
282
-
283
- for (let i = 0, l = lines.length; i < l; i++) {
284
- line = lines[i];
285
- line = trimLeft ? line.trimLeft() : line.trim();
286
- lineLength = line.length;
287
- if (lineLength === 0) continue;
288
- lineFirstChar = line.charAt(0);
289
- if (lineFirstChar === '#') continue;
290
-
291
- if (lineFirstChar === 'v') {
292
- const data = line.split(/\s+/);
293
-
294
- switch (data[0]) {
295
- case 'v':
296
- state.vertices.push(parseFloat(data[1]), parseFloat(data[2]), parseFloat(data[3]));
297
-
298
- if (data.length === 8) {
299
- state.colors.push(parseFloat(data[4]), parseFloat(data[5]), parseFloat(data[6]));
300
- }
301
-
302
- break;
303
-
304
- case 'vn':
305
- state.normals.push(parseFloat(data[1]), parseFloat(data[2]), parseFloat(data[3]));
306
- break;
307
-
308
- case 'vt':
309
- state.uvs.push(parseFloat(data[1]), parseFloat(data[2]));
310
- break;
311
-
312
- default:
313
- }
314
- } else if (lineFirstChar === 'f') {
315
- const lineData = line.substr(1).trim();
316
- const vertexData = lineData.split(/\s+/);
317
- const faceVertices = [];
318
-
319
- for (let j = 0, jl = vertexData.length; j < jl; j++) {
320
- const vertex = vertexData[j];
321
-
322
- if (vertex.length > 0) {
323
- const vertexParts = vertex.split('/');
324
- faceVertices.push(vertexParts);
185
+ addUV(a, b, c) {
186
+ const src = this.uvs;
187
+ const dst = this.object.geometry.uvs;
188
+ dst.push(src[a + 0], src[a + 1]);
189
+ dst.push(src[b + 0], src[b + 1]);
190
+ dst.push(src[c + 0], src[c + 1]);
191
+ }
192
+ addUVLine(a) {
193
+ const src = this.uvs;
194
+ const dst = this.object.geometry.uvs;
195
+ dst.push(src[a + 0], src[a + 1]);
196
+ }
197
+ // eslint-disable-next-line max-params
198
+ addFace(a, b, c, ua, ub, uc, na, nb, nc) {
199
+ const vLen = this.vertices.length;
200
+ let ia = this.parseVertexIndex(a, vLen);
201
+ let ib = this.parseVertexIndex(b, vLen);
202
+ let ic = this.parseVertexIndex(c, vLen);
203
+ this.addVertex(ia, ib, ic);
204
+ if (ua !== undefined && ua !== '') {
205
+ const uvLen = this.uvs.length;
206
+ ia = this.parseUVIndex(ua, uvLen);
207
+ ib = this.parseUVIndex(ub, uvLen);
208
+ ic = this.parseUVIndex(uc, uvLen);
209
+ this.addUV(ia, ib, ic);
210
+ }
211
+ if (na !== undefined && na !== '') {
212
+ // Normals are many times the same. If so, skip function call and parseInt.
213
+ const nLen = this.normals.length;
214
+ ia = this.parseNormalIndex(na, nLen);
215
+ ib = na === nb ? ia : this.parseNormalIndex(nb, nLen);
216
+ ic = na === nc ? ia : this.parseNormalIndex(nc, nLen);
217
+ this.addNormal(ia, ib, ic);
325
218
  }
326
- }
327
-
328
- const v1 = faceVertices[0];
329
-
330
- for (let j = 1, jl = faceVertices.length - 1; j < jl; j++) {
331
- const v2 = faceVertices[j];
332
- const v3 = faceVertices[j + 1];
333
- state.addFace(v1[0], v2[0], v3[0], v1[1], v2[1], v3[1], v1[2], v2[2], v3[2]);
334
- }
335
- } else if (lineFirstChar === 'l') {
336
- const lineParts = line.substring(1).trim().split(' ');
337
- let lineVertices;
338
- const lineUVs = [];
339
-
340
- if (line.indexOf('/') === -1) {
341
- lineVertices = lineParts;
342
- } else {
343
- lineVertices = [];
344
-
345
- for (let li = 0, llen = lineParts.length; li < llen; li++) {
346
- const parts = lineParts[li].split('/');
347
- if (parts[0] !== '') lineVertices.push(parts[0]);
348
- if (parts[1] !== '') lineUVs.push(parts[1]);
219
+ if (this.colors.length > 0) {
220
+ this.addColor(ia, ib, ic);
349
221
  }
350
- }
351
-
352
- state.addLineGeometry(lineVertices, lineUVs);
353
- } else if (lineFirstChar === 'p') {
354
- const lineData = line.substr(1).trim();
355
- const pointData = lineData.split(' ');
356
- state.addPointGeometry(pointData);
357
- } else if ((result = OBJECT_RE.exec(line)) !== null) {
358
- const name = (' ' + result[0].substr(1).trim()).substr(1);
359
- state.startObject(name);
360
- } else if (MATERIAL_USE_RE.test(line)) {
361
- state.object.startMaterial(line.substring(7).trim(), state.materialLibraries);
362
- } else if (MATERIAL_RE.test(line)) {
363
- state.materialLibraries.push(line.substring(7).trim());
364
- } else if (lineFirstChar === 's') {
365
- result = line.split(' ');
366
-
367
- if (result.length > 1) {
368
- const value = result[1].trim().toLowerCase();
369
- state.object.smooth = value !== '0' && value !== 'off';
370
- } else {
371
- state.object.smooth = true;
372
- }
373
-
374
- const material = state.object.currentMaterial();
375
- if (material) material.smooth = state.object.smooth;
376
- } else {
377
- if (line === '\0') continue;
378
- throw new Error("Unexpected line: \"".concat(line, "\""));
379
222
  }
380
- }
381
-
382
- state.finalize();
383
- const meshes = [];
384
- const materials = [];
385
-
386
- for (const object of state.objects) {
387
- const {
388
- geometry
389
- } = object;
390
- if (geometry.vertices.length === 0) continue;
391
- const mesh = {
392
- header: {
393
- vertexCount: geometry.vertices.length / 3
394
- },
395
- attributes: {}
396
- };
397
-
398
- switch (geometry.type) {
399
- case 'Points':
400
- mesh.mode = 0;
401
- break;
402
-
403
- case 'Line':
404
- mesh.mode = 1;
405
- break;
406
-
407
- default:
408
- mesh.mode = 4;
409
- break;
223
+ addPointGeometry(vertices) {
224
+ this.object.geometry.type = 'Points';
225
+ const vLen = this.vertices.length;
226
+ for (const vertex of vertices) {
227
+ this.addVertexPoint(this.parseVertexIndex(vertex, vLen));
228
+ }
410
229
  }
411
-
412
- mesh.attributes.POSITION = {
413
- value: new Float32Array(geometry.vertices),
414
- size: 3
415
- };
416
-
417
- if (geometry.normals.length > 0) {
418
- mesh.attributes.NORMAL = {
419
- value: new Float32Array(geometry.normals),
420
- size: 3
421
- };
230
+ addLineGeometry(vertices, uvs) {
231
+ this.object.geometry.type = 'Line';
232
+ const vLen = this.vertices.length;
233
+ const uvLen = this.uvs.length;
234
+ for (const vertex of vertices) {
235
+ this.addVertexLine(this.parseVertexIndex(vertex, vLen));
236
+ }
237
+ for (const uv of uvs) {
238
+ this.addUVLine(this.parseUVIndex(uv, uvLen));
239
+ }
422
240
  }
423
-
424
- if (geometry.colors.length > 0) {
425
- mesh.attributes.COLOR_0 = {
426
- value: new Float32Array(geometry.colors),
427
- size: 3
428
- };
241
+ }
242
+ // eslint-disable-next-line max-statements, complexity
243
+ function parseOBJMeshes(text) {
244
+ const state = new ParserState();
245
+ if (text.indexOf('\r\n') !== -1) {
246
+ // This is faster than String.split with regex that splits on both
247
+ text = text.replace(/\r\n/g, '\n');
429
248
  }
430
-
431
- if (geometry.uvs.length > 0) {
432
- mesh.attributes.TEXCOORD_0 = {
433
- value: new Float32Array(geometry.uvs),
434
- size: 2
435
- };
249
+ if (text.indexOf('\\\n') !== -1) {
250
+ // join lines separated by a line continuation character (\)
251
+ text = text.replace(/\\\n/g, '');
436
252
  }
437
-
438
- mesh.materials = [];
439
-
440
- for (const sourceMaterial of object.materials) {
441
- const _material = {
442
- name: sourceMaterial.name,
443
- flatShading: !sourceMaterial.smooth
444
- };
445
- mesh.materials.push(_material);
446
- materials.push(_material);
253
+ const lines = text.split('\n');
254
+ let line = '';
255
+ let lineFirstChar = '';
256
+ let lineLength = 0;
257
+ let result = [];
258
+ // Faster to just trim left side of the line. Use if available.
259
+ const trimLeft = typeof ''.trimLeft === 'function';
260
+ /* eslint-disable no-continue, max-depth */
261
+ for (let i = 0, l = lines.length; i < l; i++) {
262
+ line = lines[i];
263
+ line = trimLeft ? line.trimLeft() : line.trim();
264
+ lineLength = line.length;
265
+ if (lineLength === 0)
266
+ continue;
267
+ lineFirstChar = line.charAt(0);
268
+ // @todo invoke passed in handler if any
269
+ if (lineFirstChar === '#')
270
+ continue;
271
+ if (lineFirstChar === 'v') {
272
+ const data = line.split(/\s+/);
273
+ switch (data[0]) {
274
+ case 'v':
275
+ state.vertices.push(parseFloat(data[1]), parseFloat(data[2]), parseFloat(data[3]));
276
+ if (data.length === 8) {
277
+ state.colors.push(parseFloat(data[4]), parseFloat(data[5]), parseFloat(data[6]));
278
+ }
279
+ break;
280
+ case 'vn':
281
+ state.normals.push(parseFloat(data[1]), parseFloat(data[2]), parseFloat(data[3]));
282
+ break;
283
+ case 'vt':
284
+ state.uvs.push(parseFloat(data[1]), parseFloat(data[2]));
285
+ break;
286
+ default:
287
+ }
288
+ }
289
+ else if (lineFirstChar === 'f') {
290
+ const lineData = line.substr(1).trim();
291
+ const vertexData = lineData.split(/\s+/);
292
+ const faceVertices = [];
293
+ // Parse the face vertex data into an easy to work with format
294
+ for (let j = 0, jl = vertexData.length; j < jl; j++) {
295
+ const vertex = vertexData[j];
296
+ if (vertex.length > 0) {
297
+ const vertexParts = vertex.split('/');
298
+ faceVertices.push(vertexParts);
299
+ }
300
+ }
301
+ // Draw an edge between the first vertex and all subsequent vertices to form an n-gon
302
+ const v1 = faceVertices[0];
303
+ for (let j = 1, jl = faceVertices.length - 1; j < jl; j++) {
304
+ const v2 = faceVertices[j];
305
+ const v3 = faceVertices[j + 1];
306
+ state.addFace(v1[0], v2[0], v3[0], v1[1], v2[1], v3[1], v1[2], v2[2], v3[2]);
307
+ }
308
+ }
309
+ else if (lineFirstChar === 'l') {
310
+ const lineParts = line.substring(1).trim().split(' ');
311
+ let lineVertices;
312
+ const lineUVs = [];
313
+ if (line.indexOf('/') === -1) {
314
+ lineVertices = lineParts;
315
+ }
316
+ else {
317
+ lineVertices = [];
318
+ for (let li = 0, llen = lineParts.length; li < llen; li++) {
319
+ const parts = lineParts[li].split('/');
320
+ if (parts[0] !== '')
321
+ lineVertices.push(parts[0]);
322
+ if (parts[1] !== '')
323
+ lineUVs.push(parts[1]);
324
+ }
325
+ }
326
+ state.addLineGeometry(lineVertices, lineUVs);
327
+ }
328
+ else if (lineFirstChar === 'p') {
329
+ const lineData = line.substr(1).trim();
330
+ const pointData = lineData.split(' ');
331
+ state.addPointGeometry(pointData);
332
+ }
333
+ else if ((result = OBJECT_RE.exec(line)) !== null) {
334
+ // o object_name
335
+ // or
336
+ // g group_name
337
+ // WORKAROUND: https://bugs.chromium.org/p/v8/issues/detail?id=2869
338
+ // var name = result[ 0 ].substr( 1 ).trim();
339
+ const name = (' ' + result[0].substr(1).trim()).substr(1); // eslint-disable-line
340
+ state.startObject(name);
341
+ }
342
+ else if (MATERIAL_USE_RE.test(line)) {
343
+ // material
344
+ state.object.startMaterial(line.substring(7).trim(), state.materialLibraries);
345
+ }
346
+ else if (MATERIAL_RE.test(line)) {
347
+ // mtl file
348
+ state.materialLibraries.push(line.substring(7).trim());
349
+ }
350
+ else if (lineFirstChar === 's') {
351
+ result = line.split(' ');
352
+ // smooth shading
353
+ // @todo Handle files that have varying smooth values for a set of faces inside one geometry,
354
+ // but does not define a usemtl for each face set.
355
+ // This should be detected and a dummy material created (later MultiMaterial and geometry groups).
356
+ // This requires some care to not create extra material on each smooth value for "normal" obj files.
357
+ // where explicit usemtl defines geometry groups.
358
+ // Example asset: examples/models/obj/cerberus/Cerberus.obj
359
+ /*
360
+ * http://paulbourke.net/dataformats/obj/
361
+ * or
362
+ * http://www.cs.utah.edu/~boulos/cs3505/obj_spec.pdf
363
+ *
364
+ * From chapter "Grouping" Syntax explanation "s group_number":
365
+ * "group_number is the smoothing group number. To turn off smoothing groups, use a value of 0 or off.
366
+ * Polygonal elements use group numbers to put elements in different smoothing groups. For free-form
367
+ * surfaces, smoothing groups are either turned on or off; there is no difference between values greater
368
+ * than 0."
369
+ */
370
+ if (result.length > 1) {
371
+ const value = result[1].trim().toLowerCase();
372
+ state.object.smooth = value !== '0' && value !== 'off';
373
+ }
374
+ else {
375
+ // ZBrush can produce "s" lines #11707
376
+ state.object.smooth = true;
377
+ }
378
+ const material = state.object.currentMaterial();
379
+ if (material)
380
+ material.smooth = state.object.smooth;
381
+ }
382
+ else {
383
+ // Handle null terminated files without exception
384
+ if (line === '\0')
385
+ continue;
386
+ throw new Error(`Unexpected line: "${line}"`);
387
+ }
388
+ }
389
+ state.finalize();
390
+ const meshes = [];
391
+ const materials = [];
392
+ for (const object of state.objects) {
393
+ const { geometry } = object;
394
+ // Skip o/g line declarations that did not follow with any faces
395
+ if (geometry.vertices.length === 0)
396
+ continue;
397
+ const mesh = {
398
+ header: {
399
+ vertexCount: geometry.vertices.length / 3
400
+ },
401
+ attributes: {}
402
+ };
403
+ switch (geometry.type) {
404
+ case 'Points':
405
+ mesh.mode = 0; // GL.POINTS
406
+ break;
407
+ case 'Line':
408
+ mesh.mode = 1; // GL.LINES
409
+ break;
410
+ default:
411
+ mesh.mode = 4; // GL.TRIANGLES
412
+ break;
413
+ }
414
+ mesh.attributes.POSITION = { value: new Float32Array(geometry.vertices), size: 3 };
415
+ if (geometry.normals.length > 0) {
416
+ mesh.attributes.NORMAL = { value: new Float32Array(geometry.normals), size: 3 };
417
+ }
418
+ if (geometry.colors.length > 0) {
419
+ mesh.attributes.COLOR_0 = { value: new Float32Array(geometry.colors), size: 3 };
420
+ }
421
+ if (geometry.uvs.length > 0) {
422
+ mesh.attributes.TEXCOORD_0 = { value: new Float32Array(geometry.uvs), size: 2 };
423
+ }
424
+ // Create materials
425
+ mesh.materials = [];
426
+ for (const sourceMaterial of object.materials) {
427
+ // TODO - support full spec
428
+ const _material = {
429
+ name: sourceMaterial.name,
430
+ flatShading: !sourceMaterial.smooth
431
+ };
432
+ mesh.materials.push(_material);
433
+ materials.push(_material);
434
+ }
435
+ mesh.name = object.name;
436
+ meshes.push(mesh);
447
437
  }
448
-
449
- mesh.name = object.name;
450
- meshes.push(mesh);
451
- }
452
-
453
- return {
454
- meshes,
455
- materials
456
- };
438
+ return { meshes, materials };
457
439
  }
458
- //# sourceMappingURL=parse-obj-meshes.js.map
440
+ exports.parseOBJMeshes = parseOBJMeshes;