@shapediver/viewer.data-engine.geometry-engine 1.15.5 → 2.0.0

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