@loaders.gl/obj 4.0.0-alpha.4 → 4.0.0-alpha.6

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 (89) hide show
  1. package/dist/bundle.d.ts +2 -0
  2. package/dist/bundle.d.ts.map +1 -0
  3. package/dist/bundle.js +2 -2
  4. package/dist/dist.min.js +744 -0
  5. package/dist/es5/bundle.js +6 -0
  6. package/dist/es5/bundle.js.map +1 -0
  7. package/dist/es5/index.js +74 -0
  8. package/dist/es5/index.js.map +1 -0
  9. package/dist/es5/lib/get-obj-schema.js +54 -0
  10. package/dist/es5/lib/get-obj-schema.js.map +1 -0
  11. package/dist/es5/lib/obj-types.js +2 -0
  12. package/dist/es5/lib/obj-types.js.map +1 -0
  13. package/dist/es5/lib/parse-mtl.js +86 -0
  14. package/dist/es5/lib/parse-mtl.js.map +1 -0
  15. package/dist/es5/lib/parse-obj-meshes.js +486 -0
  16. package/dist/es5/lib/parse-obj-meshes.js.map +1 -0
  17. package/dist/es5/lib/parse-obj.js +100 -0
  18. package/dist/es5/lib/parse-obj.js.map +1 -0
  19. package/dist/es5/mtl-loader.js +26 -0
  20. package/dist/es5/mtl-loader.js.map +1 -0
  21. package/dist/es5/obj-loader.js +27 -0
  22. package/dist/es5/obj-loader.js.map +1 -0
  23. package/dist/es5/workers/obj-worker.js +6 -0
  24. package/dist/es5/workers/obj-worker.js.map +1 -0
  25. package/dist/esm/bundle.js +4 -0
  26. package/dist/esm/bundle.js.map +1 -0
  27. package/dist/esm/index.js +18 -0
  28. package/dist/esm/index.js.map +1 -0
  29. package/dist/esm/lib/get-obj-schema.js +49 -0
  30. package/dist/esm/lib/get-obj-schema.js.map +1 -0
  31. package/dist/esm/lib/obj-types.js +2 -0
  32. package/dist/esm/lib/obj-types.js.map +1 -0
  33. package/dist/esm/lib/parse-mtl.js +68 -0
  34. package/dist/esm/lib/parse-mtl.js.map +1 -0
  35. package/dist/esm/lib/parse-obj-meshes.js +385 -0
  36. package/dist/esm/lib/parse-obj-meshes.js.map +1 -0
  37. package/dist/{lib/load-obj.js → esm/lib/parse-obj.js} +5 -15
  38. package/dist/esm/lib/parse-obj.js.map +1 -0
  39. package/dist/esm/mtl-loader.js +16 -0
  40. package/dist/esm/mtl-loader.js.map +1 -0
  41. package/dist/esm/obj-loader.js +19 -0
  42. package/dist/esm/obj-loader.js.map +1 -0
  43. package/dist/esm/workers/obj-worker.js +4 -0
  44. package/dist/esm/workers/obj-worker.js.map +1 -0
  45. package/dist/index.d.ts +18 -0
  46. package/dist/index.d.ts.map +1 -0
  47. package/dist/index.js +26 -8
  48. package/dist/lib/get-obj-schema.d.ts +4 -0
  49. package/dist/lib/get-obj-schema.d.ts.map +1 -0
  50. package/dist/lib/get-obj-schema.js +33 -33
  51. package/dist/lib/obj-types.d.ts +1 -0
  52. package/dist/lib/obj-types.d.ts.map +1 -0
  53. package/dist/lib/obj-types.js +1 -2
  54. package/dist/lib/parse-mtl.d.ts +34 -0
  55. package/dist/lib/parse-mtl.d.ts.map +1 -0
  56. package/dist/lib/parse-mtl.js +201 -0
  57. package/dist/lib/parse-obj-meshes.d.ts +5 -0
  58. package/dist/lib/parse-obj-meshes.d.ts.map +1 -0
  59. package/dist/lib/parse-obj-meshes.js +440 -0
  60. package/dist/lib/parse-obj.d.ts +3 -0
  61. package/dist/lib/parse-obj.d.ts.map +1 -0
  62. package/dist/lib/parse-obj.js +63 -449
  63. package/dist/mtl-loader.d.ts +12 -0
  64. package/dist/mtl-loader.d.ts.map +1 -0
  65. package/dist/mtl-loader.js +24 -0
  66. package/dist/obj-loader.d.ts +11 -0
  67. package/dist/obj-loader.d.ts.map +1 -0
  68. package/dist/obj-loader.js +24 -18
  69. package/dist/obj-worker.js +213 -507
  70. package/dist/workers/obj-worker.d.ts +2 -0
  71. package/dist/workers/obj-worker.d.ts.map +1 -0
  72. package/dist/workers/obj-worker.js +5 -4
  73. package/package.json +9 -9
  74. package/src/index.ts +25 -4
  75. package/src/lib/get-obj-schema.ts +24 -20
  76. package/src/lib/parse-mtl.ts +246 -0
  77. package/src/lib/parse-obj-meshes.ts +525 -0
  78. package/src/lib/parse-obj.ts +67 -508
  79. package/src/mtl-loader.ts +31 -0
  80. package/src/obj-loader.ts +8 -3
  81. package/dist/bundle.js.map +0 -1
  82. package/dist/index.js.map +0 -1
  83. package/dist/lib/get-obj-schema.js.map +0 -1
  84. package/dist/lib/load-obj.js.map +0 -1
  85. package/dist/lib/obj-types.js.map +0 -1
  86. package/dist/lib/parse-obj.js.map +0 -1
  87. package/dist/obj-loader.js.map +0 -1
  88. package/src/lib/load-obj.ts +0 -83
  89. /package/src/workers/{obj-worker.js → obj-worker.ts} +0 -0
@@ -1,525 +1,84 @@
1
- // OBJ Loader, adapted from THREE.js (MIT license)
2
- //
3
- // Attributions per original THREE.js source file:
4
- //
5
- // @author mrdoob / http://mrdoob.com/
6
-
7
- // @ts-nocheck
8
-
9
- // o object_name | g group_name
10
- const OBJECT_RE = /^[og]\s*(.+)?/;
11
- // mtllib file_reference
12
- const MATERIAL_RE = /^mtllib /;
13
- // usemtl material_name
14
- const MATERIAL_USE_RE = /^usemtl /;
15
-
16
- class MeshMaterial {
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
-
28
- clone(index = this.index) {
29
- return new MeshMaterial({
30
- index,
31
- name: this.name,
32
- mtllib: this.mtllib,
33
- smooth: this.smooth,
34
- groupStart: 0
35
- });
36
- }
1
+ import type {Mesh, MeshAttributes} from '@loaders.gl/schema';
2
+ import {getMeshBoundingBox} from '@loaders.gl/schema';
3
+ import {parseOBJMeshes} from './parse-obj-meshes';
4
+ import {getOBJSchema} from './get-obj-schema';
5
+
6
+ export function parseOBJ(text, options): Mesh {
7
+ const {meshes} = parseOBJMeshes(text);
8
+
9
+ // @ts-expect-error
10
+ const vertexCount = meshes.reduce((s, mesh) => s + mesh.header.vertexCount, 0);
11
+ // TODO - render objects separately
12
+ const attributes = mergeAttributes(meshes, vertexCount);
13
+
14
+ const header = {
15
+ vertexCount,
16
+ // @ts-ignore Need to export Attributes type
17
+ boundingBox: getMeshBoundingBox(attributes)
18
+ };
19
+
20
+ const schema = getOBJSchema(attributes, {
21
+ mode: 4,
22
+ boundingBox: header.boundingBox
23
+ });
24
+
25
+ return {
26
+ // Data return by this loader implementation
27
+ loaderData: {
28
+ header: {}
29
+ },
30
+
31
+ // Normalised data
32
+ schema,
33
+ header,
34
+ mode: 4, // TRIANGLES
35
+ topology: 'point-list',
36
+
37
+ attributes
38
+ };
37
39
  }
38
40
 
39
- class MeshObject {
40
- constructor(name = '') {
41
- this.name = name;
42
-
43
- this.geometry = {
44
- vertices: [],
45
- normals: [],
46
- colors: [],
47
- uvs: []
48
- };
41
+ // eslint-disable-next-line max-statements
42
+ function mergeAttributes(meshes, vertexCount): MeshAttributes {
43
+ const positions = new Float32Array(vertexCount * 3);
44
+ let normals;
45
+ let colors;
46
+ let uvs;
47
+ let i = 0;
49
48
 
50
- this.materials = [];
51
- this.smooth = true;
52
-
53
- this.fromDeclaration = null;
54
- }
49
+ for (const mesh of meshes) {
50
+ const {POSITION, NORMAL, COLOR_0, TEXCOORD_0} = mesh.attributes;
55
51
 
56
- startMaterial(name, libraries) {
57
- const previous = this._finalize(false);
52
+ positions.set(POSITION.value, i * 3);
58
53
 
59
- // New usemtl declaration overwrites an inherited material, except if faces were declared
60
- // after the material, then it must be preserved for proper MultiMaterial continuation.
61
- if (previous && (previous.inherited || previous.groupCount <= 0)) {
62
- this.materials.splice(previous.index, 1);
54
+ if (NORMAL) {
55
+ normals = normals || new Float32Array(vertexCount * 3);
56
+ normals.set(NORMAL.value, i * 3);
63
57
  }
64
-
65
- const material = new MeshMaterial({
66
- index: this.materials.length,
67
- name,
68
- mtllib:
69
- Array.isArray(libraries) && libraries.length > 0 ? libraries[libraries.length - 1] : '',
70
- smooth: previous !== undefined ? previous.smooth : this.smooth,
71
- groupStart: previous !== undefined ? previous.groupEnd : 0
72
- });
73
-
74
- this.materials.push(material);
75
-
76
- return material;
77
- }
78
-
79
- currentMaterial() {
80
- if (this.materials.length > 0) {
81
- return this.materials[this.materials.length - 1];
58
+ if (COLOR_0) {
59
+ colors = colors || new Float32Array(vertexCount * 3);
60
+ colors.set(COLOR_0.value, i * 3);
82
61
  }
83
-
84
- return undefined;
85
- }
86
-
87
- _finalize(end) {
88
- const lastMultiMaterial = this.currentMaterial();
89
- if (lastMultiMaterial && lastMultiMaterial.groupEnd === -1) {
90
- lastMultiMaterial.groupEnd = this.geometry.vertices.length / 3;
91
- lastMultiMaterial.groupCount = lastMultiMaterial.groupEnd - lastMultiMaterial.groupStart;
92
- lastMultiMaterial.inherited = false;
62
+ if (TEXCOORD_0) {
63
+ uvs = uvs || new Float32Array(vertexCount * 2);
64
+ uvs.set(TEXCOORD_0.value, i * 2);
93
65
  }
94
66
 
95
- // Ignore objects tail materials if no face declarations followed them before a new o/g started.
96
- if (end && this.materials.length > 1) {
97
- for (let mi = this.materials.length - 1; mi >= 0; mi--) {
98
- if (this.materials[mi].groupCount <= 0) {
99
- this.materials.splice(mi, 1);
100
- }
101
- }
102
- }
103
-
104
- // Guarantee at least one empty material, this makes the creation later more straight forward.
105
- if (end && this.materials.length === 0) {
106
- this.materials.push({
107
- name: '',
108
- smooth: this.smooth
109
- });
110
- }
111
-
112
- return lastMultiMaterial;
67
+ i += POSITION.value.length / 3;
113
68
  }
114
- }
115
-
116
- class ParserState {
117
- constructor() {
118
- this.objects = [];
119
- this.object = null;
120
69
 
121
- this.vertices = [];
122
- this.normals = [];
123
- this.colors = [];
124
- this.uvs = [];
70
+ const attributes: MeshAttributes = {};
71
+ attributes.POSITION = {value: positions, size: 3};
125
72
 
126
- this.materialLibraries = [];
127
-
128
- this.startObject('', false);
73
+ if (normals) {
74
+ attributes.NORMAL = {value: normals, size: 3};
129
75
  }
130
-
131
- startObject(name, fromDeclaration = true) {
132
- // If the current object (initial from reset) is not from a g/o declaration in the parsed
133
- // file. We need to use it for the first parsed g/o to keep things in sync.
134
- if (this.object && !this.object.fromDeclaration) {
135
- this.object.name = name;
136
- this.object.fromDeclaration = fromDeclaration;
137
- return;
138
- }
139
-
140
- const previousMaterial =
141
- this.object && typeof this.object.currentMaterial === 'function'
142
- ? this.object.currentMaterial()
143
- : undefined;
144
-
145
- if (this.object && typeof this.object._finalize === 'function') {
146
- this.object._finalize(true);
147
- }
148
-
149
- this.object = new MeshObject(name);
150
- this.object.fromDeclaration = fromDeclaration;
151
-
152
- // Inherit previous objects material.
153
- // Spec tells us that a declared material must be set to all objects until a new material is declared.
154
- // If a usemtl declaration is encountered while this new object is being parsed, it will
155
- // overwrite the inherited material. Exception being that there was already face declarations
156
- // to the inherited material, then it will be preserved for proper MultiMaterial continuation.
157
- if (previousMaterial && previousMaterial.name && typeof previousMaterial.clone === 'function') {
158
- const declared = previousMaterial.clone(0);
159
- declared.inherited = true;
160
- this.object.materials.push(declared);
161
- }
162
-
163
- this.objects.push(this.object);
76
+ if (colors) {
77
+ attributes.COLOR_0 = {value: colors, size: 3};
164
78
  }
165
-
166
- finalize() {
167
- if (this.object && typeof this.object._finalize === 'function') {
168
- this.object._finalize(true);
169
- }
79
+ if (uvs) {
80
+ attributes.TEXCOORD_0 = {value: uvs, size: 2};
170
81
  }
171
82
 
172
- parseVertexIndex(value, len) {
173
- const index = parseInt(value);
174
- return (index >= 0 ? index - 1 : index + len / 3) * 3;
175
- }
176
-
177
- parseNormalIndex(value, len) {
178
- const index = parseInt(value);
179
- return (index >= 0 ? index - 1 : index + len / 3) * 3;
180
- }
181
-
182
- parseUVIndex(value, len) {
183
- const index = parseInt(value);
184
- return (index >= 0 ? index - 1 : index + len / 2) * 2;
185
- }
186
-
187
- addVertex(a, b, c) {
188
- const src = this.vertices;
189
- const dst = this.object.geometry.vertices;
190
-
191
- dst.push(src[a + 0], src[a + 1], src[a + 2]);
192
- dst.push(src[b + 0], src[b + 1], src[b + 2]);
193
- dst.push(src[c + 0], src[c + 1], src[c + 2]);
194
- }
195
-
196
- addVertexPoint(a) {
197
- const src = this.vertices;
198
- const dst = this.object.geometry.vertices;
199
-
200
- dst.push(src[a + 0], src[a + 1], src[a + 2]);
201
- }
202
-
203
- addVertexLine(a) {
204
- const src = this.vertices;
205
- const dst = this.object.geometry.vertices;
206
-
207
- dst.push(src[a + 0], src[a + 1], src[a + 2]);
208
- }
209
-
210
- addNormal(a, b, c) {
211
- const src = this.normals;
212
- const dst = this.object.geometry.normals;
213
-
214
- dst.push(src[a + 0], src[a + 1], src[a + 2]);
215
- dst.push(src[b + 0], src[b + 1], src[b + 2]);
216
- dst.push(src[c + 0], src[c + 1], src[c + 2]);
217
- }
218
-
219
- addColor(a, b, c) {
220
- const src = this.colors;
221
- const dst = this.object.geometry.colors;
222
-
223
- dst.push(src[a + 0], src[a + 1], src[a + 2]);
224
- dst.push(src[b + 0], src[b + 1], src[b + 2]);
225
- dst.push(src[c + 0], src[c + 1], src[c + 2]);
226
- }
227
-
228
- addUV(a, b, c) {
229
- const src = this.uvs;
230
- const dst = this.object.geometry.uvs;
231
-
232
- dst.push(src[a + 0], src[a + 1]);
233
- dst.push(src[b + 0], src[b + 1]);
234
- dst.push(src[c + 0], src[c + 1]);
235
- }
236
-
237
- addUVLine(a) {
238
- const src = this.uvs;
239
- const dst = this.object.geometry.uvs;
240
-
241
- dst.push(src[a + 0], src[a + 1]);
242
- }
243
-
244
- // eslint-disable-next-line max-params
245
- addFace(a, b, c, ua, ub, uc, na, nb, nc) {
246
- const vLen = this.vertices.length;
247
-
248
- let ia = this.parseVertexIndex(a, vLen);
249
- let ib = this.parseVertexIndex(b, vLen);
250
- let ic = this.parseVertexIndex(c, vLen);
251
-
252
- this.addVertex(ia, ib, ic);
253
-
254
- if (ua !== undefined && ua !== '') {
255
- const uvLen = this.uvs.length;
256
- ia = this.parseUVIndex(ua, uvLen);
257
- ib = this.parseUVIndex(ub, uvLen);
258
- ic = this.parseUVIndex(uc, uvLen);
259
- this.addUV(ia, ib, ic);
260
- }
261
-
262
- if (na !== undefined && na !== '') {
263
- // Normals are many times the same. If so, skip function call and parseInt.
264
- const nLen = this.normals.length;
265
- ia = this.parseNormalIndex(na, nLen);
266
-
267
- ib = na === nb ? ia : this.parseNormalIndex(nb, nLen);
268
- ic = na === nc ? ia : this.parseNormalIndex(nc, nLen);
269
-
270
- this.addNormal(ia, ib, ic);
271
- }
272
-
273
- if (this.colors.length > 0) {
274
- this.addColor(ia, ib, ic);
275
- }
276
- }
277
-
278
- addPointGeometry(vertices) {
279
- this.object.geometry.type = 'Points';
280
-
281
- const vLen = this.vertices.length;
282
-
283
- for (const vertex of vertices) {
284
- this.addVertexPoint(this.parseVertexIndex(vertex, vLen));
285
- }
286
- }
287
-
288
- addLineGeometry(vertices, uvs) {
289
- this.object.geometry.type = 'Line';
290
-
291
- const vLen = this.vertices.length;
292
- const uvLen = this.uvs.length;
293
-
294
- for (const vertex of vertices) {
295
- this.addVertexLine(this.parseVertexIndex(vertex, vLen));
296
- }
297
-
298
- for (const uv of uvs) {
299
- this.addUVLine(this.parseUVIndex(uv, uvLen));
300
- }
301
- }
83
+ return attributes;
302
84
  }
303
-
304
- // eslint-disable-next-line max-statements, complexity
305
- export default (text) => {
306
- const state = new ParserState();
307
-
308
- if (text.indexOf('\r\n') !== -1) {
309
- // This is faster than String.split with regex that splits on both
310
- text = text.replace(/\r\n/g, '\n');
311
- }
312
-
313
- if (text.indexOf('\\\n') !== -1) {
314
- // join lines separated by a line continuation character (\)
315
- text = text.replace(/\\\n/g, '');
316
- }
317
-
318
- const lines = text.split('\n');
319
- let line = '';
320
- let lineFirstChar = '';
321
- let lineLength = 0;
322
- let result = [];
323
-
324
- // Faster to just trim left side of the line. Use if available.
325
- const trimLeft = typeof ''.trimLeft === 'function';
326
-
327
- /* eslint-disable no-continue, max-depth */
328
- for (let i = 0, l = lines.length; i < l; i++) {
329
- line = lines[i];
330
- line = trimLeft ? line.trimLeft() : line.trim();
331
- lineLength = line.length;
332
-
333
- if (lineLength === 0) continue;
334
-
335
- lineFirstChar = line.charAt(0);
336
-
337
- // @todo invoke passed in handler if any
338
- if (lineFirstChar === '#') continue;
339
-
340
- if (lineFirstChar === 'v') {
341
- const data = line.split(/\s+/);
342
-
343
- switch (data[0]) {
344
- case 'v':
345
- state.vertices.push(parseFloat(data[1]), parseFloat(data[2]), parseFloat(data[3]));
346
- if (data.length === 8) {
347
- state.colors.push(parseFloat(data[4]), parseFloat(data[5]), parseFloat(data[6]));
348
- }
349
- break;
350
- case 'vn':
351
- state.normals.push(parseFloat(data[1]), parseFloat(data[2]), parseFloat(data[3]));
352
- break;
353
- case 'vt':
354
- state.uvs.push(parseFloat(data[1]), parseFloat(data[2]));
355
- break;
356
- default:
357
- }
358
- } else if (lineFirstChar === 'f') {
359
- const lineData = line.substr(1).trim();
360
- const vertexData = lineData.split(/\s+/);
361
- const faceVertices = [];
362
-
363
- // Parse the face vertex data into an easy to work with format
364
-
365
- for (let j = 0, jl = vertexData.length; j < jl; j++) {
366
- const vertex = vertexData[j];
367
-
368
- if (vertex.length > 0) {
369
- const vertexParts = vertex.split('/');
370
- faceVertices.push(vertexParts);
371
- }
372
- }
373
-
374
- // Draw an edge between the first vertex and all subsequent vertices to form an n-gon
375
-
376
- const v1 = faceVertices[0];
377
-
378
- for (let j = 1, jl = faceVertices.length - 1; j < jl; j++) {
379
- const v2 = faceVertices[j];
380
- const v3 = faceVertices[j + 1];
381
-
382
- state.addFace(v1[0], v2[0], v3[0], v1[1], v2[1], v3[1], v1[2], v2[2], v3[2]);
383
- }
384
- } else if (lineFirstChar === 'l') {
385
- const lineParts = line.substring(1).trim().split(' ');
386
- let lineVertices;
387
- const lineUVs = [];
388
-
389
- if (line.indexOf('/') === -1) {
390
- lineVertices = lineParts;
391
- } else {
392
- lineVertices = [];
393
- for (let li = 0, llen = lineParts.length; li < llen; li++) {
394
- const parts = lineParts[li].split('/');
395
-
396
- if (parts[0] !== '') lineVertices.push(parts[0]);
397
- if (parts[1] !== '') lineUVs.push(parts[1]);
398
- }
399
- }
400
- state.addLineGeometry(lineVertices, lineUVs);
401
- } else if (lineFirstChar === 'p') {
402
- const lineData = line.substr(1).trim();
403
- const pointData = lineData.split(' ');
404
-
405
- state.addPointGeometry(pointData);
406
- } else if ((result = OBJECT_RE.exec(line)) !== null) {
407
- // o object_name
408
- // or
409
- // g group_name
410
-
411
- // WORKAROUND: https://bugs.chromium.org/p/v8/issues/detail?id=2869
412
- // var name = result[ 0 ].substr( 1 ).trim();
413
- const name = (' ' + result[0].substr(1).trim()).substr(1); // eslint-disable-line
414
-
415
- state.startObject(name);
416
- } else if (MATERIAL_USE_RE.test(line)) {
417
- // material
418
-
419
- state.object.startMaterial(line.substring(7).trim(), state.materialLibraries);
420
- } else if (MATERIAL_RE.test(line)) {
421
- // mtl file
422
-
423
- state.materialLibraries.push(line.substring(7).trim());
424
- } else if (lineFirstChar === 's') {
425
- result = line.split(' ');
426
-
427
- // smooth shading
428
-
429
- // @todo Handle files that have varying smooth values for a set of faces inside one geometry,
430
- // but does not define a usemtl for each face set.
431
- // This should be detected and a dummy material created (later MultiMaterial and geometry groups).
432
- // This requires some care to not create extra material on each smooth value for "normal" obj files.
433
- // where explicit usemtl defines geometry groups.
434
- // Example asset: examples/models/obj/cerberus/Cerberus.obj
435
-
436
- /*
437
- * http://paulbourke.net/dataformats/obj/
438
- * or
439
- * http://www.cs.utah.edu/~boulos/cs3505/obj_spec.pdf
440
- *
441
- * From chapter "Grouping" Syntax explanation "s group_number":
442
- * "group_number is the smoothing group number. To turn off smoothing groups, use a value of 0 or off.
443
- * Polygonal elements use group numbers to put elements in different smoothing groups. For free-form
444
- * surfaces, smoothing groups are either turned on or off; there is no difference between values greater
445
- * than 0."
446
- */
447
- if (result.length > 1) {
448
- const value = result[1].trim().toLowerCase();
449
- state.object.smooth = value !== '0' && value !== 'off';
450
- } else {
451
- // ZBrush can produce "s" lines #11707
452
- state.object.smooth = true;
453
- }
454
- const material = state.object.currentMaterial();
455
- if (material) material.smooth = state.object.smooth;
456
- } else {
457
- // Handle null terminated files without exception
458
- if (line === '\0') continue;
459
-
460
- throw new Error(`Unexpected line: "${line}"`);
461
- }
462
- }
463
-
464
- state.finalize();
465
-
466
- const meshes = [];
467
- const materials = [];
468
-
469
- for (const object of state.objects) {
470
- const {geometry} = object;
471
-
472
- // Skip o/g line declarations that did not follow with any faces
473
- if (geometry.vertices.length === 0) continue;
474
-
475
- const mesh = {
476
- header: {
477
- vertexCount: geometry.vertices.length / 3
478
- },
479
- attributes: {}
480
- };
481
-
482
- switch (geometry.type) {
483
- case 'Points':
484
- mesh.mode = 0; // GL.POINTS
485
- break;
486
- case 'Line':
487
- mesh.mode = 1; // GL.LINES
488
- break;
489
- default:
490
- mesh.mode = 4; // GL.TRIANGLES
491
- break;
492
- }
493
-
494
- mesh.attributes.POSITION = {value: new Float32Array(geometry.vertices), size: 3};
495
-
496
- if (geometry.normals.length > 0) {
497
- mesh.attributes.NORMAL = {value: new Float32Array(geometry.normals), size: 3};
498
- }
499
-
500
- if (geometry.colors.length > 0) {
501
- mesh.attributes.COLOR_0 = {value: new Float32Array(geometry.colors), size: 3};
502
- }
503
-
504
- if (geometry.uvs.length > 0) {
505
- mesh.attributes.TEXCOORD_0 = {value: new Float32Array(geometry.uvs), size: 2};
506
- }
507
-
508
- // Create materials
509
- mesh.materials = [];
510
- for (const sourceMaterial of object.materials) {
511
- // TODO - support full spec
512
- const _material = {
513
- name: sourceMaterial.name,
514
- flatShading: !sourceMaterial.smooth
515
- };
516
- mesh.materials.push(_material);
517
- materials.push(_material);
518
- }
519
-
520
- mesh.name = object.name;
521
- meshes.push(mesh);
522
- }
523
-
524
- return {meshes, materials};
525
- };
@@ -0,0 +1,31 @@
1
+ // loaders.gl, MIT license
2
+ import type {Loader, LoaderOptions} from '@loaders.gl/loader-utils';
3
+ import type {MTLMaterial, ParseMTLOptions} from './lib/parse-mtl';
4
+
5
+ // __VERSION__ is injected by babel-plugin-version-inline
6
+ // @ts-ignore TS2304: Cannot find name '__VERSION__'.
7
+ const VERSION = typeof __VERSION__ !== 'undefined' ? __VERSION__ : 'latest';
8
+
9
+ export type MTLLoaderOptions = LoaderOptions & {
10
+ mtl?: ParseMTLOptions;
11
+ };
12
+
13
+ /**
14
+ * Loader for the MTL material format
15
+ * Parses a Wavefront .mtl file specifying materials
16
+ */
17
+ export const MTLLoader: Loader<MTLMaterial[], never, LoaderOptions> = {
18
+ name: 'MTL',
19
+ id: 'mtl',
20
+ module: 'mtl',
21
+ version: VERSION,
22
+ worker: true,
23
+ extensions: ['mtl'],
24
+ mimeTypes: ['text/plain'],
25
+ testText: (text: string): boolean => text.includes('newmtl'),
26
+ options: {
27
+ mtl: {}
28
+ }
29
+ };
30
+
31
+ export const _typecheckMTLLoader: Loader = MTLLoader;
package/src/obj-loader.ts CHANGED
@@ -1,13 +1,18 @@
1
- import type {Loader} from '@loaders.gl/loader-utils';
1
+ import type {Loader, LoaderOptions} from '@loaders.gl/loader-utils';
2
+ import {Mesh} from '@loaders.gl/schema';
2
3
 
3
4
  // __VERSION__ is injected by babel-plugin-version-inline
4
5
  // @ts-ignore TS2304: Cannot find name '__VERSION__'.
5
6
  const VERSION = typeof __VERSION__ !== 'undefined' ? __VERSION__ : 'latest';
6
7
 
8
+ export type OBJLoaderOptions = LoaderOptions & {
9
+ obj?: {};
10
+ };
11
+
7
12
  /**
8
13
  * Worker loader for the OBJ geometry format
9
14
  */
10
- export const OBJLoader = {
15
+ export const OBJLoader: Loader<Mesh, never, OBJLoaderOptions> = {
11
16
  name: 'OBJ',
12
17
  id: 'obj',
13
18
  module: 'obj',
@@ -21,7 +26,7 @@ export const OBJLoader = {
21
26
  }
22
27
  };
23
28
 
24
- function testOBJFile(text) {
29
+ function testOBJFile(text: string): boolean {
25
30
  // TODO - There could be comment line first
26
31
  return text[0] === 'v';
27
32
  }
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/bundle.ts"],"names":["moduleExports","require","globalThis","loaders","module","exports","Object","assign"],"mappings":"AACA,MAAMA,aAAa,GAAGC,OAAO,CAAC,SAAD,CAA7B;;AACAC,UAAU,CAACC,OAAX,GAAqBD,UAAU,CAACC,OAAX,IAAsB,EAA3C;AACAC,MAAM,CAACC,OAAP,GAAiBC,MAAM,CAACC,MAAP,CAAcL,UAAU,CAACC,OAAzB,EAAkCH,aAAlC,CAAjB","sourcesContent":["// @ts-nocheck\nconst moduleExports = require('./index');\nglobalThis.loaders = globalThis.loaders || {};\nmodule.exports = Object.assign(globalThis.loaders, moduleExports);\n"],"file":"bundle.js"}
package/dist/index.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/index.ts"],"names":["loadOBJ","OBJLoader","OBJWorkerLoader","parse","arrayBuffer","options","TextDecoder","decode","parseTextSync","_typecheckOBJLoader"],"mappings":"AACA,OAAOA,OAAP,MAAoB,gBAApB;AACA,SAAQC,SAAS,IAAIC,eAArB,QAA2C,cAA3C;AAIA,SAAQA,eAAR;AAKA,OAAO,MAAMD,SAAS,GAAG,EACvB,GAAGC,eADoB;AAEvBC,EAAAA,KAAK,EAAE,OAAOC,WAAP,EAAoBC,OAApB,KAAgCL,OAAO,CAAC,IAAIM,WAAJ,GAAkBC,MAAlB,CAAyBH,WAAzB,CAAD,EAAwCC,OAAxC,CAFvB;AAGvBG,EAAAA,aAAa,EAAER;AAHQ,CAAlB;AAMP,OAAO,MAAMS,mBAAqC,GAAGR,SAA9C","sourcesContent":["import type {LoaderWithParser} from '@loaders.gl/loader-utils';\nimport loadOBJ from './lib/load-obj';\nimport {OBJLoader as OBJWorkerLoader} from './obj-loader';\n\n// OBJLoader\n\nexport {OBJWorkerLoader};\n\n/**\n * Loader for the OBJ geometry format\n */\nexport const OBJLoader = {\n ...OBJWorkerLoader,\n parse: async (arrayBuffer, options) => loadOBJ(new TextDecoder().decode(arrayBuffer), options),\n parseTextSync: loadOBJ\n};\n\nexport const _typecheckOBJLoader: LoaderWithParser = OBJLoader;\n"],"file":"index.js"}
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../src/lib/get-obj-schema.ts"],"names":["Schema","Field","FixedSizeList","getArrowTypeFromTypedArray","getOBJSchema","attributes","metadata","metadataMap","key","Map","set","JSON","stringify","fields","attributeName","attribute","field","getArrowFieldFromAttribute","push","type","value","isSingleValue","size"],"mappings":"AAAA,SAAQA,MAAR,EAAgBC,KAAhB,EAAuBC,aAAvB,EAAsCC,0BAAtC,QAAuE,oBAAvE;AAEA,OAAO,SAASC,YAAT,CAAsBC,UAAtB,EAAkCC,QAAQ,GAAG,EAA7C,EAAiD;AACtD,MAAIC,WAAJ;;AACA,OAAK,MAAMC,GAAX,IAAkBF,QAAlB,EAA4B;AAC1BC,IAAAA,WAAW,GAAGA,WAAW,IAAI,IAAIE,GAAJ,EAA7B;;AACA,QAAID,GAAG,KAAK,OAAZ,EAAqB;AACnBD,MAAAA,WAAW,CAACG,GAAZ,CAAgBF,GAAhB,EAAqBG,IAAI,CAACC,SAAL,CAAeN,QAAQ,CAACE,GAAD,CAAvB,CAArB;AACD;AACF;;AAED,QAAMK,MAAe,GAAG,EAAxB;;AACA,OAAK,MAAMC,aAAX,IAA4BT,UAA5B,EAAwC;AACtC,UAAMU,SAAS,GAAGV,UAAU,CAACS,aAAD,CAA5B;AACA,UAAME,KAAK,GAAGC,0BAA0B,CAACH,aAAD,EAAgBC,SAAhB,CAAxC;AACAF,IAAAA,MAAM,CAACK,IAAP,CAAYF,KAAZ;AACD;;AACD,SAAO,IAAIhB,MAAJ,CAAWa,MAAX,EAAmBN,WAAnB,CAAP;AACD;;AAED,SAASU,0BAAT,CAAoCH,aAApC,EAAmDC,SAAnD,EAA8D;AAC5D,QAAMR,WAAW,GAAG,IAAIE,GAAJ,EAApB;;AACA,OAAK,MAAMD,GAAX,IAAkBO,SAAlB,EAA6B;AAC3B,QAAIP,GAAG,KAAK,OAAZ,EAAqB;AACnBD,MAAAA,WAAW,CAACG,GAAZ,CAAgBF,GAAhB,EAAqBG,IAAI,CAACC,SAAL,CAAeG,SAAS,CAACP,GAAD,CAAxB,CAArB;AACD;AACF;;AAED,QAAMW,IAAI,GAAGhB,0BAA0B,CAACY,SAAS,CAACK,KAAX,CAAvC;AACA,QAAMC,aAAa,GAAG,EAAE,UAAUN,SAAZ,KAA0BA,SAAS,CAACO,IAAV,KAAmB,CAAnE;AACA,SAAOD,aAAa,GAChB,IAAIpB,KAAJ,CAAUa,aAAV,EAAyBK,IAAzB,EAA+B,KAA/B,EAAsCZ,WAAtC,CADgB,GAEhB,IAAIN,KAAJ,CACAa,aADA,EAEA,IAAIZ,aAAJ,CAAkBa,SAAS,CAACO,IAA5B,EAAkC,IAAIrB,KAAJ,CAAU,OAAV,EAAmBkB,IAAnB,CAAlC,CAFA,EAGA,KAHA,EAIAZ,WAJA,CAFJ;AAQD","sourcesContent":["import {Schema, Field, FixedSizeList, getArrowTypeFromTypedArray} from '@loaders.gl/schema';\n\nexport function getOBJSchema(attributes, metadata = {}) {\n let metadataMap;\n for (const key in metadata) {\n metadataMap = metadataMap || new Map();\n if (key !== 'value') {\n metadataMap.set(key, JSON.stringify(metadata[key]));\n }\n }\n\n const fields: Field[] = [];\n for (const attributeName in attributes) {\n const attribute = attributes[attributeName];\n const field = getArrowFieldFromAttribute(attributeName, attribute);\n fields.push(field);\n }\n return new Schema(fields, metadataMap);\n}\n\nfunction getArrowFieldFromAttribute(attributeName, attribute) {\n const metadataMap = new Map();\n for (const key in attribute) {\n if (key !== 'value') {\n metadataMap.set(key, JSON.stringify(attribute[key]));\n }\n }\n\n const type = getArrowTypeFromTypedArray(attribute.value);\n const isSingleValue = !('size' in attribute) || attribute.size === 1;\n return isSingleValue\n ? new Field(attributeName, type, false, metadataMap)\n : new Field(\n attributeName,\n new FixedSizeList(attribute.size, new Field('value', type)),\n false,\n metadataMap\n );\n}\n"],"file":"get-obj-schema.js"}
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../src/lib/load-obj.ts"],"names":["getMeshBoundingBox","parseOBJ","getOBJSchema","loadOBJ","text","options","meshes","vertexCount","reduce","s","mesh","header","attributes","mergeAttributes","boundingBox","schema","mode","loaderData","positions","Float32Array","normals","colors","uvs","i","POSITION","NORMAL","COLOR_0","TEXCOORD_0","set","value","length","size"],"mappings":"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","sourcesContent":["import type {MeshAttributes} from '@loaders.gl/schema';\nimport {getMeshBoundingBox} from '@loaders.gl/schema';\nimport parseOBJ from './parse-obj';\nimport {getOBJSchema} from './get-obj-schema';\n\nexport default function loadOBJ(text, options) {\n const {meshes} = parseOBJ(text);\n\n // @ts-expect-error\n const vertexCount = meshes.reduce((s, mesh) => s + mesh.header.vertexCount, 0);\n // TODO - render objects separately\n const attributes = mergeAttributes(meshes, vertexCount);\n\n const header = {\n vertexCount,\n // @ts-ignore Need to export Attributes type\n boundingBox: getMeshBoundingBox(attributes)\n };\n\n const schema = getOBJSchema(attributes, {\n mode: 4,\n boundingBox: header.boundingBox\n });\n\n return {\n // Data return by this loader implementation\n loaderData: {\n header: {}\n },\n\n // Normalised data\n schema,\n header,\n mode: 4, // TRIANGLES\n\n attributes\n };\n}\n\n// eslint-disable-next-line max-statements\nfunction mergeAttributes(meshes, vertexCount) {\n const positions = new Float32Array(vertexCount * 3);\n let normals;\n let colors;\n let uvs;\n let i = 0;\n\n for (const mesh of meshes) {\n const {POSITION, NORMAL, COLOR_0, TEXCOORD_0} = mesh.attributes;\n\n positions.set(POSITION.value, i * 3);\n\n if (NORMAL) {\n normals = normals || new Float32Array(vertexCount * 3);\n normals.set(NORMAL.value, i * 3);\n }\n if (COLOR_0) {\n colors = colors || new Float32Array(vertexCount * 3);\n colors.set(COLOR_0.value, i * 3);\n }\n if (TEXCOORD_0) {\n uvs = uvs || new Float32Array(vertexCount * 2);\n uvs.set(TEXCOORD_0.value, i * 2);\n }\n\n i += POSITION.value.length / 3;\n }\n\n const attributes: MeshAttributes = {};\n attributes.POSITION = {value: positions, size: 3};\n\n if (normals) {\n attributes.NORMAL = {value: normals, size: 3};\n }\n if (colors) {\n attributes.COLOR_0 = {value: colors, size: 3};\n }\n if (uvs) {\n attributes.TEXCOORD_0 = {value: uvs, size: 2};\n }\n\n return attributes;\n}\n"],"file":"load-obj.js"}