@loaders.gl/obj 3.1.0-alpha.2 → 3.1.0-beta.1

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