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