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

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 (75) hide show
  1. package/dist/bundle.js +2 -2
  2. package/dist/bundle.js.map +1 -0
  3. package/dist/dist.min.js +269 -164
  4. package/dist/index.d.ts +41 -4
  5. package/dist/index.d.ts.map +1 -1
  6. package/dist/index.js +14 -16
  7. package/dist/index.js.map +1 -0
  8. package/dist/lib/get-obj-schema.js +32 -29
  9. package/dist/lib/get-obj-schema.js.map +1 -0
  10. package/dist/lib/obj-types.js +2 -1
  11. package/dist/{es5/lib → lib}/obj-types.js.map +0 -0
  12. package/dist/lib/parse-mtl.d.ts +34 -0
  13. package/dist/lib/parse-mtl.d.ts.map +1 -0
  14. package/dist/lib/parse-mtl.js +86 -0
  15. package/dist/lib/parse-mtl.js.map +1 -0
  16. package/dist/lib/parse-obj-meshes.d.ts +5 -0
  17. package/dist/lib/parse-obj-meshes.d.ts.map +1 -0
  18. package/dist/{esm/lib/parse-obj.js → lib/parse-obj-meshes.js} +3 -3
  19. package/dist/lib/parse-obj-meshes.js.map +1 -0
  20. package/dist/lib/parse-obj.d.ts +12 -4
  21. package/dist/lib/parse-obj.d.ts.map +1 -1
  22. package/dist/lib/parse-obj.js +90 -436
  23. package/dist/lib/parse-obj.js.map +1 -0
  24. package/dist/mtl-loader.d.ts +24 -0
  25. package/dist/mtl-loader.d.ts.map +1 -0
  26. package/dist/mtl-loader.js +16 -0
  27. package/dist/mtl-loader.js.map +1 -0
  28. package/dist/obj-loader.d.ts +5 -2
  29. package/dist/obj-loader.d.ts.map +1 -1
  30. package/dist/obj-loader.js +18 -24
  31. package/dist/obj-loader.js.map +1 -0
  32. package/dist/obj-worker.js +99 -9
  33. package/dist/workers/obj-worker.js +4 -5
  34. package/dist/workers/obj-worker.js.map +1 -0
  35. package/package.json +7 -7
  36. package/src/index.ts +21 -3
  37. package/src/lib/parse-mtl.ts +246 -0
  38. package/src/lib/parse-obj-meshes.ts +525 -0
  39. package/src/lib/parse-obj.ts +66 -508
  40. package/src/mtl-loader.ts +31 -0
  41. package/src/obj-loader.ts +6 -2
  42. package/dist/es5/bundle.js +0 -7
  43. package/dist/es5/bundle.js.map +0 -1
  44. package/dist/es5/index.js +0 -59
  45. package/dist/es5/index.js.map +0 -1
  46. package/dist/es5/lib/get-obj-schema.js +0 -46
  47. package/dist/es5/lib/get-obj-schema.js.map +0 -1
  48. package/dist/es5/lib/load-obj.js +0 -121
  49. package/dist/es5/lib/load-obj.js.map +0 -1
  50. package/dist/es5/lib/obj-types.js +0 -2
  51. package/dist/es5/lib/parse-obj.js +0 -561
  52. package/dist/es5/lib/parse-obj.js.map +0 -1
  53. package/dist/es5/obj-loader.js +0 -29
  54. package/dist/es5/obj-loader.js.map +0 -1
  55. package/dist/es5/workers/obj-worker.js +0 -8
  56. package/dist/es5/workers/obj-worker.js.map +0 -1
  57. package/dist/esm/bundle.js +0 -5
  58. package/dist/esm/bundle.js.map +0 -1
  59. package/dist/esm/index.js +0 -9
  60. package/dist/esm/index.js.map +0 -1
  61. package/dist/esm/lib/get-obj-schema.js +0 -37
  62. package/dist/esm/lib/get-obj-schema.js.map +0 -1
  63. package/dist/esm/lib/load-obj.js +0 -92
  64. package/dist/esm/lib/load-obj.js.map +0 -1
  65. package/dist/esm/lib/obj-types.js +0 -2
  66. package/dist/esm/lib/obj-types.js.map +0 -1
  67. package/dist/esm/lib/parse-obj.js.map +0 -1
  68. package/dist/esm/obj-loader.js +0 -21
  69. package/dist/esm/obj-loader.js.map +0 -1
  70. package/dist/esm/workers/obj-worker.js +0 -4
  71. package/dist/esm/workers/obj-worker.js.map +0 -1
  72. package/dist/lib/load-obj.d.ts +0 -14
  73. package/dist/lib/load-obj.d.ts.map +0 -1
  74. package/dist/lib/load-obj.js +0 -73
  75. package/src/lib/load-obj.ts +0 -83
@@ -1,438 +1,92 @@
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
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
- class MeshMaterial {
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
- }
1
+ import { getMeshBoundingBox } from '@loaders.gl/schema';
2
+ import { parseOBJMeshes } from './parse-obj-meshes';
3
+ import { getOBJSchema } from './get-obj-schema';
4
+ export function parseOBJ(text, options) {
5
+ const {
6
+ meshes
7
+ } = parseOBJMeshes(text);
8
+ const vertexCount = meshes.reduce((s, mesh) => s + mesh.header.vertexCount, 0);
9
+ const attributes = mergeAttributes(meshes, vertexCount);
10
+ const header = {
11
+ vertexCount,
12
+ boundingBox: getMeshBoundingBox(attributes)
13
+ };
14
+ const schema = getOBJSchema(attributes, {
15
+ mode: 4,
16
+ boundingBox: header.boundingBox
17
+ });
18
+ return {
19
+ loaderData: {
20
+ header: {}
21
+ },
22
+ schema,
23
+ header,
24
+ mode: 4,
25
+ attributes
26
+ };
35
27
  }
36
- class MeshObject {
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;
48
- }
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;
65
- }
66
- currentMaterial() {
67
- if (this.materials.length > 0) {
68
- return this.materials[this.materials.length - 1];
69
- }
70
- return undefined;
71
- }
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;
95
- }
28
+
29
+ function mergeAttributes(meshes, vertexCount) {
30
+ const positions = new Float32Array(vertexCount * 3);
31
+ let normals;
32
+ let colors;
33
+ let uvs;
34
+ let i = 0;
35
+
36
+ for (const mesh of meshes) {
37
+ const {
38
+ POSITION,
39
+ NORMAL,
40
+ COLOR_0,
41
+ TEXCOORD_0
42
+ } = mesh.attributes;
43
+ positions.set(POSITION.value, i * 3);
44
+
45
+ if (NORMAL) {
46
+ normals = normals || new Float32Array(vertexCount * 3);
47
+ normals.set(NORMAL.value, i * 3);
48
+ }
49
+
50
+ if (COLOR_0) {
51
+ colors = colors || new Float32Array(vertexCount * 3);
52
+ colors.set(COLOR_0.value, i * 3);
53
+ }
54
+
55
+ if (TEXCOORD_0) {
56
+ uvs = uvs || new Float32Array(vertexCount * 2);
57
+ uvs.set(TEXCOORD_0.value, i * 2);
58
+ }
59
+
60
+ i += POSITION.value.length / 3;
61
+ }
62
+
63
+ const attributes = {};
64
+ attributes.POSITION = {
65
+ value: positions,
66
+ size: 3
67
+ };
68
+
69
+ if (normals) {
70
+ attributes.NORMAL = {
71
+ value: normals,
72
+ size: 3
73
+ };
74
+ }
75
+
76
+ if (colors) {
77
+ attributes.COLOR_0 = {
78
+ value: colors,
79
+ size: 3
80
+ };
81
+ }
82
+
83
+ if (uvs) {
84
+ attributes.TEXCOORD_0 = {
85
+ value: uvs,
86
+ size: 2
87
+ };
88
+ }
89
+
90
+ return attributes;
96
91
  }
97
- class ParserState {
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);
135
- }
136
- finalize() {
137
- if (this.object && typeof this.object._finalize === 'function') {
138
- this.object._finalize(true);
139
- }
140
- }
141
- parseVertexIndex(value, len) {
142
- const index = parseInt(value);
143
- return (index >= 0 ? index - 1 : index + len / 3) * 3;
144
- }
145
- parseNormalIndex(value, len) {
146
- const index = parseInt(value);
147
- return (index >= 0 ? index - 1 : index + len / 3) * 3;
148
- }
149
- parseUVIndex(value, len) {
150
- const index = parseInt(value);
151
- return (index >= 0 ? index - 1 : index + len / 2) * 2;
152
- }
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]);
159
- }
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]);
164
- }
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]);
169
- }
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]);
176
- }
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]);
183
- }
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);
217
- }
218
- if (this.colors.length > 0) {
219
- this.addColor(ia, ib, ic);
220
- }
221
- }
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
- }
228
- }
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
- }
239
- }
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');
247
- }
248
- if (text.indexOf('\\\n') !== -1) {
249
- // join lines separated by a line continuation character (\)
250
- text = text.replace(/\\\n/g, '');
251
- }
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);
436
- }
437
- return { meshes, materials };
438
- };
92
+ //# sourceMappingURL=parse-obj.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/lib/parse-obj.ts"],"names":["getMeshBoundingBox","parseOBJMeshes","getOBJSchema","parseOBJ","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 {parseOBJMeshes} from './parse-obj-meshes';\nimport {getOBJSchema} from './get-obj-schema';\n\nexport function parseOBJ(text, options) {\n const {meshes} = parseOBJMeshes(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":"parse-obj.js"}
@@ -0,0 +1,24 @@
1
+ import type { Loader, LoaderOptions } from '@loaders.gl/loader-utils';
2
+ import type { ParseMTLOptions } from './lib/parse-mtl';
3
+ export declare type MTLLoaderOptions = LoaderOptions & {
4
+ mtl?: ParseMTLOptions;
5
+ };
6
+ /**
7
+ * Loader for the MTL material format
8
+ * Parses a Wavefront .mtl file specifying materials
9
+ */
10
+ export declare const MTLLoader: {
11
+ name: string;
12
+ id: string;
13
+ module: string;
14
+ version: any;
15
+ worker: boolean;
16
+ extensions: string[];
17
+ mimeTypes: string[];
18
+ testText: (text: string) => boolean;
19
+ options: {
20
+ mtl: {};
21
+ };
22
+ };
23
+ export declare const _typecheckMTLLoader: Loader;
24
+ //# sourceMappingURL=mtl-loader.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mtl-loader.d.ts","sourceRoot":"","sources":["../src/mtl-loader.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAC,MAAM,EAAE,aAAa,EAAC,MAAM,0BAA0B,CAAC;AACpE,OAAO,KAAK,EAAC,eAAe,EAAC,MAAM,iBAAiB,CAAC;AAMrD,oBAAY,gBAAgB,GAAG,aAAa,GAAG;IAC7C,GAAG,CAAC,EAAE,eAAe,CAAC;CACvB,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,SAAS;;;;;;;;qBAQH,MAAM,KAAG,OAAO;;;;CAIlC,CAAC;AAEF,eAAO,MAAM,mBAAmB,EAAE,MAAkB,CAAC"}
@@ -0,0 +1,16 @@
1
+ const VERSION = typeof "4.0.0-alpha.5" !== 'undefined' ? "4.0.0-alpha.5" : 'latest';
2
+ export const MTLLoader = {
3
+ name: 'MTL',
4
+ id: 'mtl',
5
+ module: 'mtl',
6
+ version: VERSION,
7
+ worker: true,
8
+ extensions: ['mtl'],
9
+ mimeTypes: ['text/plain'],
10
+ testText: text => text.includes('newmtl'),
11
+ options: {
12
+ mtl: {}
13
+ }
14
+ };
15
+ export const _typecheckMTLLoader = MTLLoader;
16
+ //# sourceMappingURL=mtl-loader.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/mtl-loader.ts"],"names":["VERSION","MTLLoader","name","id","module","version","worker","extensions","mimeTypes","testText","text","includes","options","mtl","_typecheckMTLLoader"],"mappings":"AAMA,MAAMA,OAAO,GAAG,2BAAuB,WAAvB,qBAAmD,QAAnE;AAUA,OAAO,MAAMC,SAAS,GAAG;AACvBC,EAAAA,IAAI,EAAE,KADiB;AAEvBC,EAAAA,EAAE,EAAE,KAFmB;AAGvBC,EAAAA,MAAM,EAAE,KAHe;AAIvBC,EAAAA,OAAO,EAAEL,OAJc;AAKvBM,EAAAA,MAAM,EAAE,IALe;AAMvBC,EAAAA,UAAU,EAAE,CAAC,KAAD,CANW;AAOvBC,EAAAA,SAAS,EAAE,CAAC,YAAD,CAPY;AAQvBC,EAAAA,QAAQ,EAAGC,IAAD,IAA2BA,IAAI,CAACC,QAAL,CAAc,QAAd,CARd;AASvBC,EAAAA,OAAO,EAAE;AACPC,IAAAA,GAAG,EAAE;AADE;AATc,CAAlB;AAcP,OAAO,MAAMC,mBAA2B,GAAGb,SAApC","sourcesContent":["// loaders.gl, MIT license\nimport type {Loader, LoaderOptions} from '@loaders.gl/loader-utils';\nimport type {ParseMTLOptions} from './lib/parse-mtl';\n\n// __VERSION__ is injected by babel-plugin-version-inline\n// @ts-ignore TS2304: Cannot find name '__VERSION__'.\nconst VERSION = typeof __VERSION__ !== 'undefined' ? __VERSION__ : 'latest';\n\nexport type MTLLoaderOptions = LoaderOptions & {\n mtl?: ParseMTLOptions;\n};\n\n/**\n * Loader for the MTL material format\n * Parses a Wavefront .mtl file specifying materials\n */\nexport const MTLLoader = {\n name: 'MTL',\n id: 'mtl',\n module: 'mtl',\n version: VERSION,\n worker: true,\n extensions: ['mtl'],\n mimeTypes: ['text/plain'],\n testText: (text: string): boolean => text.includes('newmtl'),\n options: {\n mtl: {}\n }\n};\n\nexport const _typecheckMTLLoader: Loader = MTLLoader;\n"],"file":"mtl-loader.js"}
@@ -1,4 +1,7 @@
1
- import type { Loader } from '@loaders.gl/loader-utils';
1
+ import type { Loader, LoaderOptions } from '@loaders.gl/loader-utils';
2
+ export declare type OBJLoaderOptions = LoaderOptions & {
3
+ obj?: {};
4
+ };
2
5
  /**
3
6
  * Worker loader for the OBJ geometry format
4
7
  */
@@ -15,7 +18,7 @@ export declare const OBJLoader: {
15
18
  obj: {};
16
19
  };
17
20
  };
18
- declare function testOBJFile(text: any): boolean;
21
+ declare function testOBJFile(text: string): boolean;
19
22
  export declare const _typecheckOBJLoader: Loader;
20
23
  export {};
21
24
  //# sourceMappingURL=obj-loader.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"obj-loader.d.ts","sourceRoot":"","sources":["../src/obj-loader.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,MAAM,EAAC,MAAM,0BAA0B,CAAC;AAMrD;;GAEG;AACH,eAAO,MAAM,SAAS;;;;;;;;;;;;CAYrB,CAAC;AAEF,iBAAS,WAAW,CAAC,IAAI,KAAA,WAGxB;AAED,eAAO,MAAM,mBAAmB,EAAE,MAAkB,CAAC"}
1
+ {"version":3,"file":"obj-loader.d.ts","sourceRoot":"","sources":["../src/obj-loader.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,MAAM,EAAE,aAAa,EAAC,MAAM,0BAA0B,CAAC;AAMpE,oBAAY,gBAAgB,GAAG,aAAa,GAAG;IAC7C,GAAG,CAAC,EAAE,EAAE,CAAC;CACV,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,SAAS;;;;;;;;;;;;CAYrB,CAAC;AAEF,iBAAS,WAAW,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAG1C;AAED,eAAO,MAAM,mBAAmB,EAAE,MAAkB,CAAC"}
@@ -1,27 +1,21 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports._typecheckOBJLoader = exports.OBJLoader = void 0;
4
- // __VERSION__ is injected by babel-plugin-version-inline
5
- // @ts-ignore TS2304: Cannot find name '__VERSION__'.
6
- const VERSION = typeof __VERSION__ !== 'undefined' ? __VERSION__ : 'latest';
7
- /**
8
- * Worker loader for the OBJ geometry format
9
- */
10
- exports.OBJLoader = {
11
- name: 'OBJ',
12
- id: 'obj',
13
- module: 'obj',
14
- version: VERSION,
15
- worker: true,
16
- extensions: ['obj'],
17
- mimeTypes: ['text/plain'],
18
- testText: testOBJFile,
19
- options: {
20
- obj: {}
21
- }
1
+ const VERSION = typeof "4.0.0-alpha.5" !== 'undefined' ? "4.0.0-alpha.5" : 'latest';
2
+ export const OBJLoader = {
3
+ name: 'OBJ',
4
+ id: 'obj',
5
+ module: 'obj',
6
+ version: VERSION,
7
+ worker: true,
8
+ extensions: ['obj'],
9
+ mimeTypes: ['text/plain'],
10
+ testText: testOBJFile,
11
+ options: {
12
+ obj: {}
13
+ }
22
14
  };
15
+
23
16
  function testOBJFile(text) {
24
- // TODO - There could be comment line first
25
- return text[0] === 'v';
17
+ return text[0] === 'v';
26
18
  }
27
- exports._typecheckOBJLoader = exports.OBJLoader;
19
+
20
+ export const _typecheckOBJLoader = OBJLoader;
21
+ //# sourceMappingURL=obj-loader.js.map