@shapediver/viewer.data-engine.geometry-engine 3.3.3 → 3.3.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.
@@ -1,380 +0,0 @@
1
- import { Converter, HttpClient } from '@shapediver/viewer.shared.services';
2
- import {
3
- IGLTF_v2,
4
- IGLTF_v2_Material,
5
- IGLTF_v2_Material_KHR_materials_pbrSpecularGlossiness,
6
- ISHAPEDIVER_materials_preset,
7
- } from '@shapediver/viewer.data-engine.shared-types';
8
- import { vec2 } from 'gl-matrix';
9
- import {
10
- MATERIAL_ALPHA,
11
- MATERIAL_SIDE,
12
- MaterialSpecularGlossinessData,
13
- MaterialStandardData,
14
- MaterialUnlitData,
15
- IMaterialAbstractData,
16
- IMaterialAbstractDataProperties,
17
- IMaterialSpecularGlossinessDataProperties,
18
- IMaterialStandardDataProperties,
19
- IMaterialUnlitDataProperties,
20
- MapData,
21
- IMapData,
22
- TEXTURE_WRAPPING,
23
- TEXTURE_FILTERING,
24
- } from '@shapediver/viewer.shared.types';
25
- import { MaterialEngine } from '@shapediver/viewer.data-engine.material-engine';
26
-
27
- import { GLTF_EXTENSIONS } from '../GLTFLoader';
28
- import { TextureLoader } from './TextureLoader';
29
-
30
- export class MaterialLoader {
31
- // #region Properties (4)
32
-
33
- private readonly _converter: Converter = Converter.instance;
34
- private readonly _materialEngine: MaterialEngine = MaterialEngine.instance;
35
-
36
- private _loaded: { [key: string]: IMaterialAbstractData } = {};
37
-
38
- // #endregion Properties (4)
39
-
40
- // #region Constructors (1)
41
-
42
- constructor(private readonly _content: IGLTF_v2, private readonly _textureLoader: TextureLoader) { }
43
-
44
- // #endregion Constructors (1)
45
-
46
- // #region Public Methods (2)
47
-
48
- public getMaterial(materialId: number): IMaterialAbstractData {
49
- if (!this._content.materials) throw new Error('MaterialLoader.getMaterial: Materials not available.');
50
- if (!this._content.materials[materialId]) throw new Error('MaterialLoader.getMaterial: Material not available.');
51
- if (!this._loaded[materialId]) throw new Error('MaterialLoader.getMaterial: Material not loaded.');
52
- return this._loaded[materialId];
53
- }
54
-
55
- public async load(): Promise<void> {
56
- this._loaded = {};
57
- if (!this._content.materials) return;
58
-
59
- const promises: Promise<void>[] = [];
60
- for (let i = 0; i < this._content.materials.length; i++) {
61
- const materialId = i;
62
- const material: IGLTF_v2_Material = this._content.materials[materialId];
63
- const materialExtensions = material.extensions || {};
64
-
65
- const materialDataProperties: IMaterialAbstractDataProperties = {};
66
- if (material.name !== undefined) materialDataProperties.name = material.name;
67
-
68
- if (materialExtensions[GLTF_EXTENSIONS.SHAPEDIVER_MATERIALS_PRESET]) {
69
- const materialPreset: ISHAPEDIVER_materials_preset = materialExtensions[GLTF_EXTENSIONS.SHAPEDIVER_MATERIALS_PRESET];
70
- promises.push(
71
- new Promise<void>((resolve, reject) => {
72
- try {
73
- this._materialEngine.loadPresetMaterial(materialPreset.materialpreset)
74
- .then(materialData => {
75
- materialData.name = material.name;
76
- materialData.color = materialPreset.color;
77
- this._loaded[materialId] = materialData;
78
- resolve();
79
- })
80
- .catch(reject);
81
- } catch (e) {
82
- reject(e);
83
- }
84
- })
85
- );
86
- continue;
87
- }
88
-
89
- if (materialExtensions[GLTF_EXTENSIONS.KHR_MATERIALS_PBRSPECULARGLOSSINESS]) {
90
- const pbrSpecularGlossiness: IGLTF_v2_Material_KHR_materials_pbrSpecularGlossiness = materialExtensions[GLTF_EXTENSIONS.KHR_MATERIALS_PBRSPECULARGLOSSINESS];
91
- const specularGlossinessMaterialDataProperties: IMaterialSpecularGlossinessDataProperties = materialDataProperties;
92
-
93
- specularGlossinessMaterialDataProperties.color = '#ffffff';
94
- specularGlossinessMaterialDataProperties.opacity = 1.0;
95
-
96
- if (pbrSpecularGlossiness.diffuseFactor !== undefined) {
97
- specularGlossinessMaterialDataProperties.color = [pbrSpecularGlossiness.diffuseFactor[0] * 255, pbrSpecularGlossiness.diffuseFactor[1] * 255, pbrSpecularGlossiness.diffuseFactor[2] * 255];
98
- specularGlossinessMaterialDataProperties.opacity = pbrSpecularGlossiness.diffuseFactor[3];
99
- }
100
-
101
- if (pbrSpecularGlossiness.diffuseTexture !== undefined) {
102
- const diffuseTextureOptions = pbrSpecularGlossiness.diffuseTexture.extensions && pbrSpecularGlossiness.diffuseTexture.extensions[GLTF_EXTENSIONS.KHR_TEXTURE_TRANSFORM] ? pbrSpecularGlossiness.diffuseTexture.extensions[GLTF_EXTENSIONS.KHR_TEXTURE_TRANSFORM] : {};
103
- if(pbrSpecularGlossiness.diffuseTexture.texCoord !== undefined) diffuseTextureOptions.texCoord = pbrSpecularGlossiness.diffuseTexture.texCoord;
104
- specularGlossinessMaterialDataProperties.map = this.loadMap(pbrSpecularGlossiness.diffuseTexture.index, diffuseTextureOptions);
105
- }
106
- specularGlossinessMaterialDataProperties.emissiveness = '#000000';
107
- specularGlossinessMaterialDataProperties.glossiness = pbrSpecularGlossiness.glossinessFactor !== undefined ? pbrSpecularGlossiness.glossinessFactor : 1.0;
108
- specularGlossinessMaterialDataProperties.specular = '#ffffff';
109
-
110
- if (pbrSpecularGlossiness.specularFactor !== undefined) {
111
- specularGlossinessMaterialDataProperties.specular = [pbrSpecularGlossiness.specularFactor[0] * 255, pbrSpecularGlossiness.specularFactor[1] * 255, pbrSpecularGlossiness.specularFactor[2] * 255];
112
- }
113
-
114
- if (pbrSpecularGlossiness.specularGlossinessTexture !== undefined) {
115
- const specularGlossinessTextureOptions = pbrSpecularGlossiness.specularGlossinessTexture.extensions && pbrSpecularGlossiness.specularGlossinessTexture.extensions[GLTF_EXTENSIONS.KHR_TEXTURE_TRANSFORM] ? pbrSpecularGlossiness.specularGlossinessTexture.extensions[GLTF_EXTENSIONS.KHR_TEXTURE_TRANSFORM] : {};
116
- if(pbrSpecularGlossiness.specularGlossinessTexture.texCoord !== undefined) specularGlossinessTextureOptions.texCoord = pbrSpecularGlossiness.specularGlossinessTexture.texCoord;
117
- specularGlossinessMaterialDataProperties.specularGlossinessMap = this.loadMap(pbrSpecularGlossiness.specularGlossinessTexture.index, specularGlossinessTextureOptions);
118
- }
119
- } else if (materialExtensions[GLTF_EXTENSIONS.KHR_MATERIALS_UNLIT]) {
120
- const unlitMaterialDataProperties: IMaterialUnlitDataProperties = materialDataProperties;
121
- unlitMaterialDataProperties.color = '#ffffff';
122
- unlitMaterialDataProperties.opacity = 1.0;
123
-
124
- if (material.pbrMetallicRoughness !== undefined) {
125
- if (material.pbrMetallicRoughness.baseColorFactor !== undefined) {
126
- unlitMaterialDataProperties.color = [material.pbrMetallicRoughness.baseColorFactor[0] * 255, material.pbrMetallicRoughness.baseColorFactor[1] * 255, material.pbrMetallicRoughness.baseColorFactor[2] * 255];
127
- unlitMaterialDataProperties.opacity = material.pbrMetallicRoughness.baseColorFactor[3];
128
- }
129
- if (material.pbrMetallicRoughness.baseColorTexture !== undefined) {
130
- const baseColorTextureOptions = material.pbrMetallicRoughness.baseColorTexture.extensions && material.pbrMetallicRoughness.baseColorTexture.extensions[GLTF_EXTENSIONS.KHR_TEXTURE_TRANSFORM] ? material.pbrMetallicRoughness.baseColorTexture.extensions[GLTF_EXTENSIONS.KHR_TEXTURE_TRANSFORM] : {};
131
- if(material.pbrMetallicRoughness.baseColorTexture.texCoord !== undefined) baseColorTextureOptions.texCoord = material.pbrMetallicRoughness.baseColorTexture.texCoord;
132
- unlitMaterialDataProperties.map = this.loadMap(material.pbrMetallicRoughness.baseColorTexture.index, baseColorTextureOptions);
133
- }
134
- }
135
- } else {
136
- const standardMaterialDataProperties: IMaterialStandardDataProperties = materialDataProperties;
137
- if (material.pbrMetallicRoughness !== undefined) {
138
- standardMaterialDataProperties.color = '#ffffff';
139
- if (material.pbrMetallicRoughness.baseColorFactor !== undefined) {
140
- standardMaterialDataProperties.color = [material.pbrMetallicRoughness.baseColorFactor[0] * 255, material.pbrMetallicRoughness.baseColorFactor[1] * 255, material.pbrMetallicRoughness.baseColorFactor[2] * 255];
141
- standardMaterialDataProperties.opacity = material.pbrMetallicRoughness.baseColorFactor[3];
142
- }
143
- if (material.pbrMetallicRoughness.baseColorTexture !== undefined) {
144
- const baseColorTextureOptions = material.pbrMetallicRoughness.baseColorTexture.extensions && material.pbrMetallicRoughness.baseColorTexture.extensions[GLTF_EXTENSIONS.KHR_TEXTURE_TRANSFORM] ? material.pbrMetallicRoughness.baseColorTexture.extensions[GLTF_EXTENSIONS.KHR_TEXTURE_TRANSFORM] : {};
145
- if(material.pbrMetallicRoughness.baseColorTexture.texCoord !== undefined) baseColorTextureOptions.texCoord = material.pbrMetallicRoughness.baseColorTexture.texCoord;
146
- standardMaterialDataProperties.map = this.loadMap(material.pbrMetallicRoughness.baseColorTexture.index, baseColorTextureOptions);
147
- }
148
- if (material.pbrMetallicRoughness.metallicFactor !== undefined) {
149
- standardMaterialDataProperties.metalness = material.pbrMetallicRoughness.metallicFactor;
150
- }
151
- if (material.pbrMetallicRoughness.roughnessFactor !== undefined) {
152
- standardMaterialDataProperties.roughness = material.pbrMetallicRoughness.roughnessFactor;
153
- }
154
- if (material.pbrMetallicRoughness.metallicRoughnessTexture !== undefined) {
155
- const metallicRoughnessTextureOptions = material.pbrMetallicRoughness.metallicRoughnessTexture.extensions && material.pbrMetallicRoughness.metallicRoughnessTexture.extensions[GLTF_EXTENSIONS.KHR_TEXTURE_TRANSFORM] ? material.pbrMetallicRoughness.metallicRoughnessTexture.extensions[GLTF_EXTENSIONS.KHR_TEXTURE_TRANSFORM] : {};
156
- if(material.pbrMetallicRoughness.metallicRoughnessTexture.texCoord !== undefined) metallicRoughnessTextureOptions.texCoord = material.pbrMetallicRoughness.metallicRoughnessTexture.texCoord;
157
- standardMaterialDataProperties.metalnessRoughnessMap = this.loadMap(material.pbrMetallicRoughness.metallicRoughnessTexture.index, metallicRoughnessTextureOptions);
158
- }
159
- }
160
- }
161
-
162
- /**
163
- * Loading of the general properties
164
- */
165
-
166
- if (material.normalTexture !== undefined) {
167
- const normalTextureOptions = material.normalTexture.extensions && material.normalTexture.extensions[GLTF_EXTENSIONS.KHR_TEXTURE_TRANSFORM] ? material.normalTexture.extensions[GLTF_EXTENSIONS.KHR_TEXTURE_TRANSFORM] : {};
168
- if(material.normalTexture.texCoord !== undefined) normalTextureOptions.texCoord = material.normalTexture.texCoord;
169
-
170
- materialDataProperties.normalMap = this.loadMap(material.normalTexture.index, normalTextureOptions);
171
- materialDataProperties.normalScale = 1;
172
- if (material.normalTexture.scale !== undefined) {
173
- materialDataProperties.normalScale = material.normalTexture.scale;
174
- }
175
- }
176
- if (material.occlusionTexture !== undefined) {
177
- const occlusionTextureOptions = material.occlusionTexture.extensions && material.occlusionTexture.extensions[GLTF_EXTENSIONS.KHR_TEXTURE_TRANSFORM] ? material.occlusionTexture.extensions[GLTF_EXTENSIONS.KHR_TEXTURE_TRANSFORM] : {};
178
- if(material.occlusionTexture.texCoord !== undefined) occlusionTextureOptions.texCoord = material.occlusionTexture.texCoord;
179
-
180
- materialDataProperties.aoMap = this.loadMap(material.occlusionTexture.index, occlusionTextureOptions);
181
- if (material.occlusionTexture.strength !== undefined) {
182
- materialDataProperties.aoMapIntensity = material.occlusionTexture.strength;
183
- }
184
- }
185
- if (material.emissiveTexture !== undefined) {
186
- const emissiveTextureOptions = material.emissiveTexture.extensions && material.emissiveTexture.extensions[GLTF_EXTENSIONS.KHR_TEXTURE_TRANSFORM] ? material.emissiveTexture.extensions[GLTF_EXTENSIONS.KHR_TEXTURE_TRANSFORM] : {};
187
- if(material.emissiveTexture.texCoord !== undefined) emissiveTextureOptions.texCoord = material.emissiveTexture.texCoord;
188
- materialDataProperties.emissiveMap = this.loadMap(material.emissiveTexture.index, emissiveTextureOptions);
189
- }
190
-
191
- if (material.emissiveFactor !== undefined) {
192
- materialDataProperties.emissiveness = [material.emissiveFactor[0] * 255, material.emissiveFactor[1] * 255, material.emissiveFactor[2] * 255];
193
- }
194
- if (material.alphaMode !== undefined) {
195
- materialDataProperties.alphaMode = material.alphaMode.toLowerCase() === MATERIAL_ALPHA.MASK ? MATERIAL_ALPHA.MASK : material.alphaMode.toLowerCase() === MATERIAL_ALPHA.BLEND ? MATERIAL_ALPHA.BLEND : MATERIAL_ALPHA.OPAQUE;
196
- if (materialDataProperties.alphaMode === MATERIAL_ALPHA.MASK) {
197
- materialDataProperties.alphaCutoff = material.alphaCutoff !== undefined ? material.alphaCutoff : 0.5;
198
- }
199
- }
200
- if (material.alphaCutoff !== undefined) {
201
- materialDataProperties.alphaCutoff = material.alphaCutoff;
202
- }
203
- if (material.doubleSided !== undefined) {
204
- materialDataProperties.side = material.doubleSided ? MATERIAL_SIDE.DOUBLE : MATERIAL_SIDE.FRONT;
205
- }
206
-
207
- /**
208
- * Early exit for specular glossiness and unlit materials
209
- */
210
- if (materialExtensions[GLTF_EXTENSIONS.KHR_MATERIALS_PBRSPECULARGLOSSINESS]) {
211
- const specularGlossinessMaterialDataProperties: IMaterialSpecularGlossinessDataProperties = materialDataProperties;
212
- const materialData = new MaterialSpecularGlossinessData(specularGlossinessMaterialDataProperties);
213
- this._loaded[materialId] = materialData;
214
- continue;
215
- } else if (materialExtensions[GLTF_EXTENSIONS.KHR_MATERIALS_UNLIT]) {
216
- const unlitMaterialDataProperties: IMaterialUnlitDataProperties = materialDataProperties;
217
- const materialData = new MaterialUnlitData(unlitMaterialDataProperties);
218
- this._loaded[materialId] = materialData;
219
- continue;
220
- }
221
-
222
- const standardMaterialDataProperties: IMaterialStandardDataProperties = materialDataProperties;
223
-
224
- if (materialExtensions[GLTF_EXTENSIONS.KHR_MATERIALS_CLEARCOAT]) {
225
- const clearcoatExtension = materialExtensions[GLTF_EXTENSIONS.KHR_MATERIALS_CLEARCOAT];
226
- if (clearcoatExtension.clearcoatFactor !== undefined) {
227
- standardMaterialDataProperties.clearcoat = clearcoatExtension.clearcoatFactor;
228
- }
229
-
230
- if (clearcoatExtension.clearcoatTexture !== undefined) {
231
- const clearcoatTextureOptions = clearcoatExtension.clearcoatTexture.extensions && clearcoatExtension.clearcoatTexture.extensions[GLTF_EXTENSIONS.KHR_TEXTURE_TRANSFORM] ? clearcoatExtension.clearcoatTexture.extensions[GLTF_EXTENSIONS.KHR_TEXTURE_TRANSFORM] : {};
232
- if(clearcoatExtension.clearcoatTexture.texCoord !== undefined) clearcoatTextureOptions.texCoord = clearcoatExtension.clearcoatTexture.texCoord;
233
- standardMaterialDataProperties.clearcoatMap = this.loadMap(clearcoatExtension.clearcoatTexture.index, clearcoatTextureOptions);
234
- }
235
-
236
- if (clearcoatExtension.clearcoatRoughnessFactor !== undefined) {
237
- standardMaterialDataProperties.clearcoatRoughness = clearcoatExtension.clearcoatRoughnessFactor;
238
- }
239
-
240
- if (clearcoatExtension.clearcoatRoughnessTexture !== undefined) {
241
- const clearcoatRoughnessTextureOptions = clearcoatExtension.clearcoatRoughnessTexture.extensions && clearcoatExtension.clearcoatRoughnessTexture.extensions[GLTF_EXTENSIONS.KHR_TEXTURE_TRANSFORM] ? clearcoatExtension.clearcoatRoughnessTexture.extensions[GLTF_EXTENSIONS.KHR_TEXTURE_TRANSFORM] : {};
242
- if(clearcoatExtension.clearcoatRoughnessTexture.texCoord !== undefined) clearcoatRoughnessTextureOptions.texCoord = clearcoatExtension.clearcoatRoughnessTexture.texCoord;
243
- standardMaterialDataProperties.clearcoatRoughnessMap = this.loadMap(clearcoatExtension.clearcoatRoughnessTexture.index, clearcoatRoughnessTextureOptions);
244
- }
245
-
246
- if (clearcoatExtension.clearcoatNormalTexture !== undefined) {
247
- const clearcoatNormalTextureOptions = clearcoatExtension.clearcoatNormalTexture.extensions && clearcoatExtension.clearcoatNormalTexture.extensions[GLTF_EXTENSIONS.KHR_TEXTURE_TRANSFORM] ? clearcoatExtension.clearcoatNormalTexture.extensions[GLTF_EXTENSIONS.KHR_TEXTURE_TRANSFORM] : {};
248
- if(clearcoatExtension.clearcoatNormalTexture.texCoord !== undefined) clearcoatNormalTextureOptions.texCoord = clearcoatExtension.clearcoatNormalTexture.texCoord;
249
- standardMaterialDataProperties.clearcoatNormalMap = this.loadMap(clearcoatExtension.clearcoatNormalTexture.index, clearcoatNormalTextureOptions);
250
- }
251
- }
252
-
253
- if (materialExtensions[GLTF_EXTENSIONS.KHR_MATERIALS_IOR]) {
254
- const iorExtension = materialExtensions[GLTF_EXTENSIONS.KHR_MATERIALS_IOR];
255
- if (iorExtension.ior !== undefined) {
256
- standardMaterialDataProperties.ior = iorExtension.ior;
257
- }
258
- }
259
-
260
- if (materialExtensions[GLTF_EXTENSIONS.KHR_MATERIALS_TRANSMISSION]) {
261
- const transmissionExtension = materialExtensions[GLTF_EXTENSIONS.KHR_MATERIALS_TRANSMISSION];
262
- if (transmissionExtension.transmissionFactor !== undefined) {
263
- standardMaterialDataProperties.transmission = transmissionExtension.transmissionFactor;
264
- }
265
-
266
- if (transmissionExtension.transmissionTexture !== undefined) {
267
- const transmissionTextureOptions = transmissionExtension.transmissionTexture.extensions && transmissionExtension.transmissionTexture.extensions[GLTF_EXTENSIONS.KHR_TEXTURE_TRANSFORM] ? transmissionExtension.transmissionTexture.extensions[GLTF_EXTENSIONS.KHR_TEXTURE_TRANSFORM] : {};
268
- if(transmissionExtension.transmissionTexture.texCoord !== undefined) transmissionTextureOptions.texCoord = transmissionExtension.transmissionTexture.texCoord;
269
- standardMaterialDataProperties.transmissionMap = this.loadMap(transmissionExtension.transmissionTexture.index, transmissionTextureOptions);
270
- }
271
- }
272
-
273
- if (materialExtensions[GLTF_EXTENSIONS.KHR_MATERIALS_VOLUME]) {
274
- const volumeExtension = materialExtensions[GLTF_EXTENSIONS.KHR_MATERIALS_VOLUME];
275
- if (volumeExtension.thicknessFactor !== undefined) {
276
- standardMaterialDataProperties.thickness = volumeExtension.thicknessFactor;
277
- }
278
-
279
- if (volumeExtension.thicknessTexture !== undefined) {
280
- const thicknessTextureOptions = volumeExtension.thicknessTexture.extensions && volumeExtension.thicknessTexture.extensions[GLTF_EXTENSIONS.KHR_TEXTURE_TRANSFORM] ? volumeExtension.thicknessTexture.extensions[GLTF_EXTENSIONS.KHR_TEXTURE_TRANSFORM] : {};
281
- if(volumeExtension.thicknessTexture.texCoord !== undefined) thicknessTextureOptions.texCoord = volumeExtension.thicknessTexture.texCoord;
282
- standardMaterialDataProperties.thicknessMap = this.loadMap(volumeExtension.thicknessTexture.index, thicknessTextureOptions);
283
- }
284
-
285
- if (volumeExtension.attenuationDistance !== undefined) {
286
- standardMaterialDataProperties.attenuationDistance = volumeExtension.attenuationDistance;
287
- }
288
-
289
- if (volumeExtension.attenuationColor !== undefined) {
290
- standardMaterialDataProperties.attenuationColor = [volumeExtension.attenuationColor[0] * 255, volumeExtension.attenuationColor[1] * 255, volumeExtension.attenuationColor[2] * 255];
291
- }
292
- }
293
-
294
- if (materialExtensions[GLTF_EXTENSIONS.KHR_MATERIALS_SHEEN]) {
295
- const sheenExtension = materialExtensions[GLTF_EXTENSIONS.KHR_MATERIALS_SHEEN];
296
- standardMaterialDataProperties.sheen = 1.0;
297
- if (sheenExtension.sheenColorFactor !== undefined) {
298
- standardMaterialDataProperties.sheenColor = [sheenExtension.sheenColorFactor[0] * 255, sheenExtension.sheenColorFactor[1] * 255, sheenExtension.sheenColorFactor[2] * 255];
299
- }
300
-
301
- if (sheenExtension.sheenRoughnessFactor !== undefined) {
302
- standardMaterialDataProperties.sheenRoughness = sheenExtension.sheenRoughnessFactor;
303
- }
304
-
305
- if (sheenExtension.sheenColorTexture !== undefined) {
306
- const sheenColorTextureOptions = sheenExtension.sheenColorTexture.extensions && sheenExtension.sheenColorTexture.extensions[GLTF_EXTENSIONS.KHR_TEXTURE_TRANSFORM] ? sheenExtension.sheenColorTexture.extensions[GLTF_EXTENSIONS.KHR_TEXTURE_TRANSFORM] : {};
307
- if(sheenExtension.sheenColorTexture.texCoord !== undefined) sheenColorTextureOptions.texCoord = sheenExtension.sheenColorTexture.texCoord;
308
- standardMaterialDataProperties.sheenColorMap = this.loadMap(sheenExtension.sheenColorTexture.index, sheenColorTextureOptions);
309
- }
310
-
311
- if (sheenExtension.sheenRoughnessTexture !== undefined) {
312
- const sheenRoughnessTextureOptions = sheenExtension.sheenRoughnessTexture.extensions && sheenExtension.sheenRoughnessTexture.extensions[GLTF_EXTENSIONS.KHR_TEXTURE_TRANSFORM] ? sheenExtension.sheenRoughnessTexture.extensions[GLTF_EXTENSIONS.KHR_TEXTURE_TRANSFORM] : {};
313
- if(sheenExtension.sheenRoughnessTexture.texCoord !== undefined) sheenRoughnessTextureOptions.texCoord = sheenExtension.sheenRoughnessTexture.texCoord;
314
- standardMaterialDataProperties.sheenRoughnessMap = this.loadMap(sheenExtension.sheenRoughnessTexture.index, sheenRoughnessTextureOptions);
315
- }
316
- }
317
-
318
- if (materialExtensions[GLTF_EXTENSIONS.KHR_MATERIALS_SPECULAR]) {
319
- const specularExtension = materialExtensions[GLTF_EXTENSIONS.KHR_MATERIALS_SPECULAR];
320
-
321
- if (specularExtension.specularFactor !== undefined) {
322
- standardMaterialDataProperties.specularIntensity = specularExtension.specularFactor;
323
- }
324
-
325
- if (specularExtension.specularColorFactor !== undefined) {
326
- standardMaterialDataProperties.specularColor = [specularExtension.specularColorFactor[0] * 255, specularExtension.specularColorFactor[1] * 255, specularExtension.specularColorFactor[2] * 255];
327
- }
328
-
329
- if (specularExtension.specularColorTexture !== undefined) {
330
- const specularColorTextureOptions = specularExtension.specularColorTexture.extensions && specularExtension.specularColorTexture.extensions[GLTF_EXTENSIONS.KHR_TEXTURE_TRANSFORM] ? specularExtension.specularColorTexture.extensions[GLTF_EXTENSIONS.KHR_TEXTURE_TRANSFORM] : {};
331
- if(specularExtension.specularColorTexture.texCoord !== undefined) specularColorTextureOptions.texCoord = specularExtension.specularColorTexture.texCoord;
332
- standardMaterialDataProperties.specularColorMap = this.loadMap(specularExtension.specularColorTexture.index, specularColorTextureOptions);
333
- }
334
-
335
- if (specularExtension.specularTexture !== undefined) {
336
- const specularTextureOptions = specularExtension.specularTexture.extensions && specularExtension.specularTexture.extensions[GLTF_EXTENSIONS.KHR_TEXTURE_TRANSFORM] ? specularExtension.specularTexture.extensions[GLTF_EXTENSIONS.KHR_TEXTURE_TRANSFORM] : {};
337
- if(specularExtension.specularTexture.texCoord !== undefined) specularTextureOptions.texCoord = specularExtension.specularTexture.texCoord;
338
- standardMaterialDataProperties.specularIntensityMap = this.loadMap(specularExtension.specularTexture.index, specularTextureOptions);
339
- }
340
- }
341
-
342
- const materialData = new MaterialStandardData(standardMaterialDataProperties);
343
- this._loaded[materialId] = materialData;
344
- }
345
- await Promise.all(promises);
346
- }
347
-
348
- // #endregion Public Methods (2)
349
-
350
- // #region Private Methods (1)
351
-
352
- private loadMap(textureId: number, properties?: { offset?: number[], scale?: number[], rotation?: number, texCoord?: number }): IMapData | undefined {
353
- if (!this._content.textures) throw new Error('Textures not available.');
354
- const texture = this._content.textures[textureId];
355
- if (!this._content.images) throw new Error('Images not available.');
356
- const sampler = this._content.samplers && texture.sampler && this._content.samplers[texture.sampler] ? this._content.samplers[texture.sampler] : {};
357
- const loadedTexture = this._textureLoader.getTexture(textureId);
358
-
359
- if(!loadedTexture)
360
- return;
361
-
362
- return new MapData(
363
- loadedTexture.image,
364
- {
365
- blob: loadedTexture.blob,
366
- wrapS: sampler.wrapS as TEXTURE_WRAPPING,
367
- wrapT: sampler.wrapT as TEXTURE_WRAPPING,
368
- minFilter: sampler.minFilter as TEXTURE_FILTERING,
369
- magFilter: sampler.magFilter as TEXTURE_FILTERING,
370
- offset: properties && properties.offset ? vec2.fromValues(properties.offset[0], properties.offset[1]) : vec2.create(),
371
- repeat: properties && properties.scale ? vec2.fromValues(properties.scale[0], properties.scale[1]) : vec2.fromValues(1, 1),
372
- rotation: properties && properties.rotation !== undefined ? properties.rotation : 0,
373
- texCoord: properties?.texCoord,
374
- flipY: false
375
- }
376
- );
377
- }
378
-
379
- // #endregion Private Methods (1)
380
- }
@@ -1,116 +0,0 @@
1
- import { IGLTF_v2 } from '@shapediver/viewer.data-engine.shared-types';
2
- import { Converter, HttpClient } from '@shapediver/viewer.shared.services';
3
-
4
- import { BufferViewLoader } from './BufferViewLoader';
5
-
6
- export class TextureLoader {
7
- // #region Properties (3)
8
-
9
- private readonly _converter: Converter = Converter.instance;
10
- private readonly _httpClient: HttpClient = HttpClient.instance;
11
-
12
- private _loaded: {
13
- [key: string]: {
14
- image: HTMLImageElement | ArrayBuffer,
15
- blob: Blob
16
- }
17
- } = {};
18
-
19
- // #endregion Properties (3)
20
-
21
- // #region Constructors (1)
22
-
23
- constructor(private readonly _content: IGLTF_v2, private readonly _bufferViewLoader: BufferViewLoader, private _baseUri?: string) { }
24
-
25
- // #endregion Constructors (1)
26
-
27
- // #region Public Methods (2)
28
-
29
- public getTexture(textureId: number): {
30
- image: HTMLImageElement | ArrayBuffer,
31
- blob: Blob
32
- } {
33
- if (!this._content.textures) throw new Error('TextureLoader.getTexture: Textures not available.');
34
- if (!this._content.textures[textureId]) throw new Error('TextureLoader.getTexture: Texture not available.');
35
- return this._loaded[textureId];
36
- }
37
-
38
- public async load(): Promise<void> {
39
- if (!this._content.textures) return;
40
-
41
- const promises: Promise<void>[] = [];
42
- for (let i = 0; i < this._content.textures.length; i++) {
43
- const textureId = i;
44
- const texture = this._content.textures[textureId];
45
- if (!this._content.images) throw new Error('TextureLoader.load: Images not available.');
46
- const image = this._content.images[texture.source];
47
-
48
- const DATA_URI_REGEX = /^data:(.*?)(;base64)?,(.*)$/;
49
- const HTTPS_URI_REGEX = /^https:\/\//;
50
-
51
- if (image.bufferView !== undefined) {
52
- const bufferView = this._bufferViewLoader.getBufferView(image.bufferView);
53
- const dataView = new DataView(bufferView);
54
- const array: Array<number> = [];
55
- for (let i = 0; i < dataView.byteLength; i += 1)
56
- array[i] = dataView.getUint8(i);
57
-
58
- const blob = new Blob([new Uint8Array(array)], { type: image.mimeType });
59
- const dataUri = URL.createObjectURL(blob);
60
-
61
- const img = new Image();
62
-
63
- promises.push(
64
- new Promise<void>((resolve, reject) => {
65
- img.onload = () => {
66
- this._loaded[textureId] = {
67
- image: img,
68
- blob
69
- };
70
- URL.revokeObjectURL(dataUri);
71
- resolve();
72
- };
73
- img.onerror = reject;
74
- })
75
- );
76
-
77
- img.crossOrigin = 'anonymous';
78
- img.src = dataUri;
79
- } else {
80
- const url = DATA_URI_REGEX.test(image.uri!) || HTTPS_URI_REGEX.test(image.uri!) ? image.uri : `${this._baseUri}/${image.uri}`;
81
- promises.push(
82
- new Promise<void>((resolve, reject) => {
83
- this._httpClient.loadTexture(url!)
84
- .then(response => {
85
- if (!response) {
86
- resolve();
87
- } else {
88
- if (typeof window !== 'undefined') {
89
- Converter.instance.responseToImage(response).then(img => {
90
- this._loaded[textureId] = {
91
- image: img,
92
- blob: response.data.blob
93
- };
94
- resolve();
95
- })
96
- .catch(e => reject(e));
97
- } else {
98
- this._loaded[textureId] = {
99
- image: response.data.buffer,
100
- blob: response.data.blob
101
- };
102
- resolve();
103
- }
104
- }
105
- })
106
- .catch(e => reject(e));
107
- })
108
- );
109
- }
110
- }
111
-
112
- await Promise.all(promises);
113
- }
114
-
115
- // #endregion Public Methods (2)
116
- }
package/src/index.ts DELETED
@@ -1,5 +0,0 @@
1
- import { GeometryEngine } from './GeometryEngine'
2
-
3
- export {
4
- GeometryEngine
5
- }
package/tsconfig.json DELETED
@@ -1,19 +0,0 @@
1
- {
2
- "extends": "../../tsconfig.json",
3
- "include": [
4
- "./**/*.js",
5
- "./**/*.ts"
6
- ],
7
- "compilerOptions": {
8
- "rootDir": "src",
9
- "outDir": "dist",
10
- "allowJs": true
11
- },
12
- "exclude": [
13
- "__tests__",
14
- "node_modules",
15
- "dist",
16
- "dist-dev",
17
- "dist-prod"
18
- ]
19
- }