@loaders.gl/obj 4.2.0-alpha.4 → 4.2.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.
- 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 +5 -3
- 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,49 +1,35 @@
|
|
|
1
|
+
// loaders.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// Copyright (c) vis.gl contributors
|
|
1
4
|
import { getDataTypeFromArray } from '@loaders.gl/schema';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
/** Get Mesh Schema */
|
|
6
|
+
export function getOBJSchema(attributes, metadata = {}) {
|
|
7
|
+
const stringMetadata = {};
|
|
8
|
+
for (const key in metadata) {
|
|
9
|
+
if (key !== 'value') {
|
|
10
|
+
stringMetadata[key] = JSON.stringify(metadata[key]);
|
|
11
|
+
}
|
|
8
12
|
}
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
return {
|
|
17
|
-
fields,
|
|
18
|
-
metadata: stringMetadata
|
|
19
|
-
};
|
|
13
|
+
const fields = [];
|
|
14
|
+
for (const attributeName in attributes) {
|
|
15
|
+
const attribute = attributes[attributeName];
|
|
16
|
+
const field = getFieldFromAttribute(attributeName, attribute);
|
|
17
|
+
fields.push(field);
|
|
18
|
+
}
|
|
19
|
+
return { fields, metadata: stringMetadata };
|
|
20
20
|
}
|
|
21
|
+
/** Get a Field describing the column from an OBJ attribute */
|
|
21
22
|
function getFieldFromAttribute(name, attribute) {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
23
|
+
const metadata = {};
|
|
24
|
+
for (const key in attribute) {
|
|
25
|
+
if (key !== 'value') {
|
|
26
|
+
metadata[key] = JSON.stringify(attribute[key]);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
let { type } = getDataTypeFromArray(attribute.value);
|
|
30
|
+
const isSingleValue = attribute.size === 1 || attribute.size === undefined;
|
|
31
|
+
if (!isSingleValue) {
|
|
32
|
+
type = { type: 'fixed-size-list', listSize: attribute.size, children: [{ name: 'values', type }] };
|
|
26
33
|
}
|
|
27
|
-
|
|
28
|
-
let {
|
|
29
|
-
type
|
|
30
|
-
} = getDataTypeFromArray(attribute.value);
|
|
31
|
-
const isSingleValue = attribute.size === 1 || attribute.size === undefined;
|
|
32
|
-
if (!isSingleValue) {
|
|
33
|
-
type = {
|
|
34
|
-
type: 'fixed-size-list',
|
|
35
|
-
listSize: attribute.size,
|
|
36
|
-
children: [{
|
|
37
|
-
name: 'values',
|
|
38
|
-
type
|
|
39
|
-
}]
|
|
40
|
-
};
|
|
41
|
-
}
|
|
42
|
-
return {
|
|
43
|
-
name,
|
|
44
|
-
type,
|
|
45
|
-
nullable: false,
|
|
46
|
-
metadata
|
|
47
|
-
};
|
|
34
|
+
return { name, type, nullable: false, metadata };
|
|
48
35
|
}
|
|
49
|
-
//# sourceMappingURL=get-obj-schema.js.map
|
package/dist/lib/obj-types.js
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
//# sourceMappingURL=obj-types.js.map
|
|
1
|
+
"use strict";
|
package/dist/lib/parse-mtl.js
CHANGED
|
@@ -1,68 +1,199 @@
|
|
|
1
|
+
// loaders.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// Copyright (c) vis.gl contributors
|
|
4
|
+
// Forked from THREE.js under MIT license
|
|
5
|
+
// https://github.com/mrdoob/three.js/blob/master/examples/jsm/loaders/MTLLoader.js
|
|
1
6
|
const DELIMITER_PATTERN = /\s+/;
|
|
7
|
+
/**
|
|
8
|
+
* Parses a MTL file.
|
|
9
|
+
* Parses a Wavefront .mtl file specifying materials
|
|
10
|
+
* http://paulbourke.net/dataformats/mtl/
|
|
11
|
+
* https://www.loc.gov/preservation/digital/formats/fdd/fdd000508.shtml
|
|
12
|
+
*
|
|
13
|
+
* @param text - Content of MTL file
|
|
14
|
+
*/
|
|
15
|
+
// eslint-disable-next-line complexity
|
|
2
16
|
export function parseMTL(text, options) {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
name: 'placeholder'
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
17
|
+
// const materialsInfo: Record<string, MTLMaterial> = {};
|
|
18
|
+
const materials = [];
|
|
19
|
+
let currentMaterial = { name: 'placeholder' };
|
|
20
|
+
const lines = text.split('\n');
|
|
21
|
+
for (let line of lines) {
|
|
22
|
+
line = line.trim();
|
|
23
|
+
if (line.length === 0 || line.charAt(0) === '#') {
|
|
24
|
+
// Blank line or comment ignore
|
|
25
|
+
continue; // eslint-disable-line no-continue
|
|
26
|
+
}
|
|
27
|
+
const pos = line.indexOf(' ');
|
|
28
|
+
let key = pos >= 0 ? line.substring(0, pos) : line;
|
|
29
|
+
key = key.toLowerCase();
|
|
30
|
+
let value = pos >= 0 ? line.substring(pos + 1) : '';
|
|
31
|
+
value = value.trim();
|
|
32
|
+
switch (key) {
|
|
33
|
+
case 'newmtl':
|
|
34
|
+
// New material
|
|
35
|
+
currentMaterial = { name: value };
|
|
36
|
+
// insert into map
|
|
37
|
+
materials.push(currentMaterial);
|
|
38
|
+
break;
|
|
39
|
+
case 'ka': // Ka
|
|
40
|
+
currentMaterial.ambientColor = parseColor(value);
|
|
41
|
+
break;
|
|
42
|
+
case 'kd':
|
|
43
|
+
// Kd: Diffuse color (color under white light) using RGB values
|
|
44
|
+
currentMaterial.diffuseColor = parseColor(value);
|
|
45
|
+
break;
|
|
46
|
+
case 'map_kd':
|
|
47
|
+
// Diffuse texture map
|
|
48
|
+
currentMaterial.diffuseTextureUrl = value;
|
|
49
|
+
// setMapForType('map', value);
|
|
50
|
+
break;
|
|
51
|
+
case 'ks':
|
|
52
|
+
// Specular color (color when light is reflected from shiny surface) using RGB values
|
|
53
|
+
currentMaterial.specularColor = parseColor(value);
|
|
54
|
+
break;
|
|
55
|
+
case 'map_ks':
|
|
56
|
+
// Specular map
|
|
57
|
+
currentMaterial.specularTextureUrl = value;
|
|
58
|
+
// setMapForType('specularMap', value);
|
|
59
|
+
break;
|
|
60
|
+
case 'ke':
|
|
61
|
+
// Emissive using RGB values
|
|
62
|
+
currentMaterial.emissiveColor = parseColor(value);
|
|
63
|
+
break;
|
|
64
|
+
case 'map_ke':
|
|
65
|
+
// Emissive map
|
|
66
|
+
currentMaterial.emissiveTextureUrl = value;
|
|
67
|
+
// setMapForType('emissiveMap', value);
|
|
68
|
+
break;
|
|
69
|
+
case 'ns':
|
|
70
|
+
// Ns is material specular exponent (defines the focus of the specular highlight)
|
|
71
|
+
// A high exponent results in a tight, concentrated highlight. Ns values normally range from 0 to 1000.
|
|
72
|
+
currentMaterial.shininess = parseFloat(value);
|
|
73
|
+
break;
|
|
74
|
+
case 'map_ns':
|
|
75
|
+
// Ns is material specular exponent
|
|
76
|
+
// TODO?
|
|
77
|
+
// currentMaterial.shininessMap = parseFloat(value);
|
|
78
|
+
break;
|
|
79
|
+
case 'ni':
|
|
80
|
+
currentMaterial.refraction = parseFloat(value);
|
|
81
|
+
break;
|
|
82
|
+
case 'illum':
|
|
83
|
+
currentMaterial.illumination = parseFloat(value);
|
|
84
|
+
break;
|
|
85
|
+
default:
|
|
86
|
+
// log unknown message?
|
|
87
|
+
break;
|
|
88
|
+
/*
|
|
89
|
+
case 'norm':
|
|
90
|
+
setMapForType('normalMap', value);
|
|
91
|
+
break;
|
|
92
|
+
|
|
93
|
+
case 'map_bump':
|
|
94
|
+
case 'bump':
|
|
95
|
+
// Bump texture map
|
|
96
|
+
setMapForType('bumpMap', value);
|
|
97
|
+
break;
|
|
98
|
+
|
|
99
|
+
case 'd':
|
|
100
|
+
n = parseFloat(value);
|
|
101
|
+
if (n < 1) {
|
|
102
|
+
params.opacity = n;
|
|
103
|
+
params.transparent = true;
|
|
104
|
+
}
|
|
105
|
+
break;
|
|
106
|
+
|
|
107
|
+
case 'map_d':
|
|
108
|
+
// Alpha map
|
|
109
|
+
setMapForType('alphaMap', value);
|
|
110
|
+
params.transparent = true;
|
|
111
|
+
break;
|
|
112
|
+
|
|
113
|
+
case 'tr':
|
|
114
|
+
n = parseFloat(value);
|
|
115
|
+
if (this.options && this.options.invertTrProperty) n = 1 - n;
|
|
116
|
+
if (n > 0) {
|
|
117
|
+
params.opacity = 1 - n;
|
|
118
|
+
params.transparent = true;
|
|
119
|
+
}
|
|
120
|
+
*/
|
|
121
|
+
}
|
|
59
122
|
}
|
|
60
|
-
|
|
61
|
-
return materials;
|
|
123
|
+
return materials;
|
|
62
124
|
}
|
|
63
125
|
function parseColor(value, options) {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
126
|
+
const rgb = value.split(DELIMITER_PATTERN, 3);
|
|
127
|
+
const color = [
|
|
128
|
+
parseFloat(rgb[0]),
|
|
129
|
+
parseFloat(rgb[1]),
|
|
130
|
+
parseFloat(rgb[2])
|
|
131
|
+
];
|
|
132
|
+
// TODO auto detect big values?
|
|
133
|
+
// if (this.options && this.options.normalizeRGB) {
|
|
134
|
+
// value = [ value[ 0 ] / 255, value[ 1 ] / 255, value[ 2 ] / 255 ];
|
|
135
|
+
// }
|
|
136
|
+
// if (this.options && this.options.ignoreZeroRGBs) {
|
|
137
|
+
// if (value[ 0 ] === 0 && value[ 1 ] === 0 && value[ 2 ] === 0) {
|
|
138
|
+
// // ignore
|
|
139
|
+
// save = false;
|
|
140
|
+
// }
|
|
141
|
+
// }
|
|
142
|
+
return color;
|
|
143
|
+
}
|
|
144
|
+
/* TODO parse url options
|
|
145
|
+
function parseTexture(value, matParams) {
|
|
146
|
+
const texParams = {
|
|
147
|
+
scale: new Vector2(1, 1),
|
|
148
|
+
offset: new Vector2(0, 0)
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
const items = value.split(/\s+/);
|
|
152
|
+
let pos;
|
|
153
|
+
|
|
154
|
+
pos = items.indexOf('-bm');
|
|
155
|
+
if (pos >= 0) {
|
|
156
|
+
matParams.bumpScale = parseFloat(items[ pos + 1 ]);
|
|
157
|
+
items.splice(pos, 2);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
pos = items.indexOf('-s');
|
|
161
|
+
if (pos >= 0) {
|
|
162
|
+
texParams.scale.set(parseFloat(items[ pos + 1 ]), parseFloat(items[ pos + 2 ]));
|
|
163
|
+
items.splice(pos, 4); // we expect 3 parameters here!
|
|
164
|
+
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
pos = items.indexOf('-o');
|
|
168
|
+
|
|
169
|
+
if (pos >= 0) {
|
|
170
|
+
texParams.offset.set(parseFloat(items[ pos + 1 ]), parseFloat(items[ pos + 2 ]));
|
|
171
|
+
items.splice(pos, 4); // we expect 3 parameters here!
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
texParams.url = items.join(' ').trim();
|
|
175
|
+
return texParams;
|
|
67
176
|
}
|
|
68
|
-
|
|
177
|
+
|
|
178
|
+
*function resolveURL(baseUrl, url) {
|
|
179
|
+
* baseUrl?: string;
|
|
180
|
+
// Absolute URL
|
|
181
|
+
if (/^https?:\/\//i.test(url)) return url;
|
|
182
|
+
return baseUrl + url;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function setMapForType(mapType, value) {
|
|
186
|
+
if (params[ mapType ]) return; // Keep the first encountered texture
|
|
187
|
+
|
|
188
|
+
const texParams = scope.getTextureParams(value, params);
|
|
189
|
+
const map = scope.loadTexture(resolveURL(scope.baseUrl, texParams.url));
|
|
190
|
+
|
|
191
|
+
map.repeat.copy(texParams.scale);
|
|
192
|
+
map.offset.copy(texParams.offset);
|
|
193
|
+
|
|
194
|
+
map.wrapS = scope.wrap;
|
|
195
|
+
map.wrapT = scope.wrap;
|
|
196
|
+
|
|
197
|
+
params[ mapType ] = map;
|
|
198
|
+
}
|
|
199
|
+
*/
|