@combeenation/3d-viewer 7.0.0 → 7.1.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 (105) hide show
  1. package/README.md +111 -111
  2. package/dist/lib-cjs/api/classes/animationInterface.d.ts +8 -8
  3. package/dist/lib-cjs/api/classes/animationInterface.js +2 -2
  4. package/dist/lib-cjs/api/classes/dottedPath.d.ts +79 -79
  5. package/dist/lib-cjs/api/classes/dottedPath.js +166 -166
  6. package/dist/lib-cjs/api/classes/element.d.ts +153 -153
  7. package/dist/lib-cjs/api/classes/element.js +670 -670
  8. package/dist/lib-cjs/api/classes/event.d.ts +396 -396
  9. package/dist/lib-cjs/api/classes/event.js +419 -419
  10. package/dist/lib-cjs/api/classes/eventBroadcaster.d.ts +26 -26
  11. package/dist/lib-cjs/api/classes/eventBroadcaster.js +49 -49
  12. package/dist/lib-cjs/api/classes/fuzzyMap.d.ts +7 -7
  13. package/dist/lib-cjs/api/classes/fuzzyMap.js +21 -21
  14. package/dist/lib-cjs/api/classes/parameter.d.ts +351 -351
  15. package/dist/lib-cjs/api/classes/parameter.js +517 -517
  16. package/dist/lib-cjs/api/classes/parameterObservable.d.ts +36 -36
  17. package/dist/lib-cjs/api/classes/parameterObservable.js +72 -72
  18. package/dist/lib-cjs/api/classes/parameterizable.d.ts +15 -15
  19. package/dist/lib-cjs/api/classes/parameterizable.js +102 -102
  20. package/dist/lib-cjs/api/classes/placementAnimation.d.ts +45 -45
  21. package/dist/lib-cjs/api/classes/placementAnimation.js +176 -176
  22. package/dist/lib-cjs/api/classes/variant.d.ts +253 -253
  23. package/dist/lib-cjs/api/classes/variant.js +843 -843
  24. package/dist/lib-cjs/api/classes/variantInstance.d.ts +53 -53
  25. package/dist/lib-cjs/api/classes/variantInstance.js +125 -125
  26. package/dist/lib-cjs/api/classes/variantParameterizable.d.ts +17 -17
  27. package/dist/lib-cjs/api/classes/variantParameterizable.js +88 -88
  28. package/dist/lib-cjs/api/classes/viewer.d.ts +199 -200
  29. package/dist/lib-cjs/api/classes/viewer.js +670 -671
  30. package/dist/lib-cjs/api/classes/viewer.js.map +1 -1
  31. package/dist/lib-cjs/api/classes/viewerLight.d.ts +66 -66
  32. package/dist/lib-cjs/api/classes/viewerLight.js +348 -348
  33. package/dist/lib-cjs/api/internal/lensRendering.d.ts +8 -8
  34. package/dist/lib-cjs/api/internal/lensRendering.js +11 -11
  35. package/dist/lib-cjs/api/internal/sceneSetup.d.ts +13 -13
  36. package/dist/lib-cjs/api/internal/sceneSetup.js +226 -226
  37. package/dist/lib-cjs/api/manager/animationManager.d.ts +30 -30
  38. package/dist/lib-cjs/api/manager/animationManager.js +126 -126
  39. package/dist/lib-cjs/api/manager/gltfExportManager.d.ts +78 -78
  40. package/dist/lib-cjs/api/manager/gltfExportManager.js +241 -241
  41. package/dist/lib-cjs/api/manager/sceneManager.d.ts +33 -33
  42. package/dist/lib-cjs/api/manager/sceneManager.js +130 -130
  43. package/dist/lib-cjs/api/manager/tagManager.d.ts +108 -108
  44. package/dist/lib-cjs/api/manager/tagManager.js +419 -419
  45. package/dist/lib-cjs/api/manager/textureLoadManager.d.ts +22 -22
  46. package/dist/lib-cjs/api/manager/textureLoadManager.js +97 -97
  47. package/dist/lib-cjs/api/manager/variantInstanceManager.d.ts +102 -103
  48. package/dist/lib-cjs/api/manager/variantInstanceManager.js +284 -285
  49. package/dist/lib-cjs/api/manager/variantInstanceManager.js.map +1 -1
  50. package/dist/lib-cjs/api/store/specStorage.d.ts +32 -32
  51. package/dist/lib-cjs/api/store/specStorage.js +65 -65
  52. package/dist/lib-cjs/api/util/babylonHelper.d.ts +235 -235
  53. package/dist/lib-cjs/api/util/babylonHelper.js +753 -750
  54. package/dist/lib-cjs/api/util/babylonHelper.js.map +1 -1
  55. package/dist/lib-cjs/api/util/globalTypes.d.ts +436 -436
  56. package/dist/lib-cjs/api/util/globalTypes.js +1 -1
  57. package/dist/lib-cjs/api/util/resourceHelper.d.ts +58 -58
  58. package/dist/lib-cjs/api/util/resourceHelper.js +203 -203
  59. package/dist/lib-cjs/api/util/sceneLoaderHelper.d.ts +44 -44
  60. package/dist/lib-cjs/api/util/sceneLoaderHelper.js +175 -173
  61. package/dist/lib-cjs/api/util/sceneLoaderHelper.js.map +1 -1
  62. package/dist/lib-cjs/api/util/stringHelper.d.ts +13 -13
  63. package/dist/lib-cjs/api/util/stringHelper.js +32 -32
  64. package/dist/lib-cjs/api/util/structureHelper.d.ts +9 -9
  65. package/dist/lib-cjs/api/util/structureHelper.js +48 -48
  66. package/dist/lib-cjs/buildinfo.json +3 -3
  67. package/dist/lib-cjs/commonjs.tsconfig.tsbuildinfo +1 -1
  68. package/dist/lib-cjs/index.d.ts +53 -53
  69. package/dist/lib-cjs/index.js +114 -114
  70. package/package.json +81 -81
  71. package/src/api/classes/animationInterface.ts +10 -10
  72. package/src/api/classes/dottedPath.ts +181 -181
  73. package/src/api/classes/element.ts +731 -731
  74. package/src/api/classes/event.ts +452 -452
  75. package/src/api/classes/eventBroadcaster.ts +52 -52
  76. package/src/api/classes/fuzzyMap.ts +21 -21
  77. package/src/api/classes/parameter.ts +554 -554
  78. package/src/api/classes/parameterObservable.ts +73 -73
  79. package/src/api/classes/parameterizable.ts +87 -87
  80. package/src/api/classes/placementAnimation.ts +162 -162
  81. package/src/api/classes/variant.ts +933 -933
  82. package/src/api/classes/variantInstance.ts +123 -123
  83. package/src/api/classes/variantParameterizable.ts +85 -85
  84. package/src/api/classes/viewer.ts +743 -744
  85. package/src/api/classes/viewerLight.ts +339 -339
  86. package/src/api/internal/debugViewer.ts +90 -90
  87. package/src/api/internal/lensRendering.ts +9 -9
  88. package/src/api/internal/sceneSetup.ts +205 -205
  89. package/src/api/manager/animationManager.ts +143 -143
  90. package/src/api/manager/gltfExportManager.ts +236 -236
  91. package/src/api/manager/sceneManager.ts +136 -136
  92. package/src/api/manager/tagManager.ts +451 -451
  93. package/src/api/manager/textureLoadManager.ts +95 -95
  94. package/src/api/manager/variantInstanceManager.ts +297 -298
  95. package/src/api/store/specStorage.ts +68 -68
  96. package/src/api/util/babylonHelper.ts +823 -822
  97. package/src/api/util/globalTypes.ts +503 -503
  98. package/src/api/util/resourceHelper.ts +191 -191
  99. package/src/api/util/sceneLoaderHelper.ts +170 -170
  100. package/src/api/util/stringHelper.ts +30 -30
  101. package/src/api/util/structureHelper.ts +49 -49
  102. package/src/buildinfo.json +3 -3
  103. package/src/dev.ts +62 -62
  104. package/src/index.ts +100 -100
  105. package/src/types.d.ts +38 -35
@@ -1,236 +1,236 @@
1
- import { injectNodeMetadata } from '../util/babylonHelper';
2
- import { isMeshIncludedInExclusionList } from '../util/structureHelper';
3
- import { PBRMaterial } from '@babylonjs/core/Materials/PBR/pbrMaterial';
4
- import { Color3 } from '@babylonjs/core/Maths/math.color';
5
- import { AbstractMesh } from '@babylonjs/core/Meshes/abstractMesh';
6
- import { Mesh } from '@babylonjs/core/Meshes/mesh';
7
- import { GLTF2Export, IExportOptions } from '@babylonjs/serializers/glTF/2.0';
8
- import { has, merge } from 'lodash-es';
9
-
10
- export class GltfExportManager {
11
- public static readonly UNIQUE_MESH_METADATA_PROPERTY = 'originalName';
12
-
13
- /**
14
- * Constructor.
15
- */
16
- protected constructor(protected viewer: Viewer) {}
17
-
18
- /**
19
- * Creates an {@link GltfExportManager}.
20
- */
21
- public static async create(viewer: Viewer): Promise<GltfExportManager> {
22
- return new GltfExportManager(viewer);
23
- }
24
-
25
- /**
26
- * Exports selected nodes to a file.
27
- * @param filename optional name of the exported .GLB file.
28
- * @param exportOptions export options to be merged with default options.\
29
- * `exportOptions.createUniqueMeshNames` defaults to `true` if not given.
30
- * @param excluded optional list of geometry (meshes, elements, variants, variantInstances) to be excluded from export
31
- */
32
- public async exportGlb(
33
- filename = 'glb-export.glb',
34
- { createUniqueMeshNames = true, ...exportOptionsRest }: IExportOptionsExtended = {},
35
- excluded?: ExcludedGeometryList
36
- ): Promise<File | undefined> {
37
- const exportOptions = { createUniqueMeshNames, ...exportOptionsRest };
38
- this.exportPreProcess(exportOptions);
39
- const glbData = await GLTF2Export.GLBAsync(
40
- this.viewer.scene,
41
- 'dummy',
42
- this.gltfExportOptions(exportOptions, excluded)
43
- );
44
- this.exportPostProcess();
45
- const resBlob = glbData.glTFFiles['dummy.glb']; // should be only one file for glb
46
-
47
- // check if result is valid, according to the typings this could also be a string
48
- if (resBlob instanceof Blob) {
49
- if (!filename.endsWith('.glb')) {
50
- filename += '.glb';
51
- }
52
- return new File([resBlob], filename);
53
- } else {
54
- // result was not a valid blob
55
- return undefined;
56
- }
57
- }
58
-
59
- /**
60
- * Exports selected nodes to GLTF. This may result in more than one file, since textures are exported separately.
61
- * @param filename name of the main (text-based) .GLTF file referring to separate texture files.
62
- * @param exportOptions export options to be merged with default options.\
63
- * `exportOptions.createUniqueMeshNames` defaults to `true` if not given.
64
- * @param excluded optional list of geometry (meshes, elements, variants, variantInstances) to be excluded from export
65
- */
66
- public async exportGltfToFile(
67
- filename: string,
68
- { createUniqueMeshNames = true, ...exportOptionsRest }: IExportOptionsExtended = {},
69
- excluded?: ExcludedGeometryList
70
- ) {
71
- const exportOptions = { createUniqueMeshNames, ...exportOptionsRest };
72
- this.exportPreProcess(exportOptions);
73
- await GLTF2Export.GLTFAsync(this.viewer.scene, filename, this.gltfExportOptions(exportOptions, excluded)).then(
74
- glb => {
75
- glb.downloadFiles();
76
- }
77
- );
78
- this.exportPostProcess();
79
- }
80
-
81
- /**
82
- * Exports selected nodes to GLB. This results in one binary file.
83
- * @param filename name of the .GLB file.
84
- * @param exportOptions export options to be merged with default options.\
85
- * `exportOptions.createUniqueMeshNames` defaults to `true` if not given.
86
- * @param excluded optional list of geometry (meshes, elements, variants, variantInstances) to be excluded from export
87
- */
88
- public async exportGlbToFile(
89
- filename: string,
90
- { createUniqueMeshNames = true, ...exportOptionsRest }: IExportOptionsExtended = {},
91
- excluded?: ExcludedGeometryList
92
- ) {
93
- const exportOptions = { createUniqueMeshNames, ...exportOptionsRest };
94
- this.exportPreProcess(exportOptions);
95
- await GLTF2Export.GLBAsync(this.viewer.scene, filename, this.gltfExportOptions(exportOptions, excluded)).then(
96
- glb => {
97
- glb.downloadFiles();
98
- }
99
- );
100
- this.exportPostProcess();
101
- }
102
-
103
- /**
104
- * Gets predefined {@link IExportOptionsExtended } merged with given ones.
105
- */
106
- protected gltfExportOptions(
107
- mergeWithOptions: IExportOptionsExtended = {},
108
- excluded?: ExcludedGeometryList
109
- ): IExportOptions {
110
- const defaultOptions: IExportOptions = {
111
- // includeCoordinateSystemConversionNodes: true,
112
- shouldExportNode: function (node: any) {
113
- if (!node.isEnabled()) {
114
- return false;
115
- }
116
- if ((node.name as string).startsWith('__bounding_box__')) {
117
- return false;
118
- }
119
- if (excluded && node instanceof Mesh && isMeshIncludedInExclusionList(node, excluded)) {
120
- return false;
121
- }
122
- return true;
123
- },
124
- };
125
- return merge({}, defaultOptions, mergeWithOptions);
126
- }
127
-
128
- /**
129
- * Stuff to be done before exporting to GLTF
130
- */
131
- protected exportPreProcess({ createUniqueMeshNames = true }: IExportOptionsExtended) {
132
- this.viewer.pauseRendering();
133
- if (createUniqueMeshNames ?? true) {
134
- this.setUniqueMeshNames();
135
- }
136
- this.exchangeRefractionMaterials();
137
- }
138
-
139
- /**
140
- * Stuff to be done after the GLTF export
141
- */
142
- protected exportPostProcess() {
143
- this.restoreRefractionMaterials();
144
- this.restoreOriginalMeshNames();
145
- this.viewer.resumeRendering();
146
- }
147
-
148
- /**
149
- * Materials with refraction set are not exported properly.
150
- * Exchange all such (relevant) materials with a more export-friendly version
151
- */
152
- protected exchangeRefractionMaterials() {
153
- for (const n of this.viewer.scene.getNodes()) {
154
- if (!(n instanceof AbstractMesh)) continue;
155
- if (!(n.material instanceof PBRMaterial)) continue;
156
- if (!(n.material as PBRMaterial).subSurface.isRefractionEnabled) continue;
157
- if ((n.material as PBRMaterial).transparencyMode !== PBRMaterial.PBRMATERIAL_OPAQUE) continue;
158
- // if we're here, we have a node holding a material with set refraction whose transparencyMode is set to
159
- // PBRMATERIAL_OPAQUE
160
- n.material = this.createRefractionMaterialReplacement(n.material);
161
- }
162
- }
163
-
164
- /**
165
- * Restore original materials with set refraction.
166
- */
167
- protected restoreRefractionMaterials() {
168
- for (const n of this.viewer.scene.getNodes()) {
169
- if (!(n instanceof AbstractMesh)) continue;
170
- if (!(n.material instanceof PBRMaterial)) continue;
171
- if (!this.isMaterialClonedForExport(n.material)) continue;
172
- // at this point we have a pbrmaterial tagged as cloned in its metadata that's set on a mesh
173
-
174
- // restore and dispose
175
- const currMaterial = n.material;
176
- const prevMaterial = this.viewer.scene.getMaterialByUniqueID(n.material.metadata.clonedFrom);
177
- if (prevMaterial) {
178
- n.material = prevMaterial; // restore previous material
179
- currMaterial.dispose(false, true); // dispose of clone
180
- }
181
- }
182
- }
183
-
184
- /**
185
- * Create an export-friendly replacement material for a material using refraction.
186
- * @param mat Material to be replaced
187
- */
188
- protected createRefractionMaterialReplacement(mat: PBRMaterial): PBRMaterial {
189
- // if we're dealing with a clone already, return it instead of cloning
190
- if (this.isMaterialClonedForExport(mat)) return mat;
191
-
192
- // change material according to https://www.notion.so/combeenation/Glas-materials-don-t-look-glasy-after-export-d5fda2c6515e4420a8772744d3e6b460
193
- const clonedMaterial = mat.clone(mat.name); // clone material. clone uses same name
194
- clonedMaterial.metadata = { ...mat.metadata, clonedFrom: mat.uniqueId }; // create shallow copy of metadata on clone. see https://forum.babylonjs.com/t/the-metadata-of-the-mesh-cloned-by-the-instantiatemodelstoscene-method-is-a-shallow-copy/21563
195
- clonedMaterial.refractionTexture = null;
196
- clonedMaterial.metallicReflectanceTexture = null; // is this the correct one for metallic roughness?
197
- clonedMaterial.alpha = 0.7;
198
- clonedMaterial.albedoColor = new Color3(0.3, 0.3, 0.3);
199
- clonedMaterial.transparencyMode = PBRMaterial.PBRMATERIAL_ALPHABLEND;
200
- clonedMaterial.metallic = 0.65;
201
- clonedMaterial.roughness = 0.15;
202
-
203
- return clonedMaterial;
204
- }
205
-
206
- /**
207
- * Inspect if a material was temporarily cloned for GLB export
208
- * @param mat Material to be inspected
209
- */
210
- protected isMaterialClonedForExport(mat: PBRMaterial): boolean {
211
- return !!mat.metadata?.clonedFrom;
212
- }
213
-
214
- /**
215
- * Set unique mesh names for GLB export
216
- */
217
- protected setUniqueMeshNames(): void {
218
- this.viewer.scene.meshes.forEach(currMesh => {
219
- injectNodeMetadata(currMesh, { [GltfExportManager.UNIQUE_MESH_METADATA_PROPERTY]: currMesh.name }, false);
220
- currMesh.name = `${currMesh.name}_${currMesh.uniqueId}`;
221
- });
222
- }
223
-
224
- /**
225
- * Restore original mesh names for meshes that were
226
- * renamed with {@link setUniqueMeshNames()}
227
- */
228
- protected restoreOriginalMeshNames(): void {
229
- this.viewer.scene.meshes.forEach(currMesh => {
230
- if (has(currMesh.metadata, GltfExportManager.UNIQUE_MESH_METADATA_PROPERTY)) {
231
- currMesh.name = currMesh.metadata[GltfExportManager.UNIQUE_MESH_METADATA_PROPERTY];
232
- delete currMesh.metadata[GltfExportManager.UNIQUE_MESH_METADATA_PROPERTY];
233
- }
234
- });
235
- }
236
- }
1
+ import { injectNodeMetadata } from '../util/babylonHelper';
2
+ import { isMeshIncludedInExclusionList } from '../util/structureHelper';
3
+ import { PBRMaterial } from '@babylonjs/core/Materials/PBR/pbrMaterial';
4
+ import { Color3 } from '@babylonjs/core/Maths/math.color';
5
+ import { AbstractMesh } from '@babylonjs/core/Meshes/abstractMesh';
6
+ import { Mesh } from '@babylonjs/core/Meshes/mesh';
7
+ import { GLTF2Export, IExportOptions } from '@babylonjs/serializers/glTF/2.0';
8
+ import { has, merge } from 'lodash-es';
9
+
10
+ export class GltfExportManager {
11
+ public static readonly UNIQUE_MESH_METADATA_PROPERTY = 'originalName';
12
+
13
+ /**
14
+ * Constructor.
15
+ */
16
+ protected constructor(protected viewer: Viewer) {}
17
+
18
+ /**
19
+ * Creates an {@link GltfExportManager}.
20
+ */
21
+ public static async create(viewer: Viewer): Promise<GltfExportManager> {
22
+ return new GltfExportManager(viewer);
23
+ }
24
+
25
+ /**
26
+ * Exports selected nodes to a file.
27
+ * @param filename optional name of the exported .GLB file.
28
+ * @param exportOptions export options to be merged with default options.\
29
+ * `exportOptions.createUniqueMeshNames` defaults to `true` if not given.
30
+ * @param excluded optional list of geometry (meshes, elements, variants, variantInstances) to be excluded from export
31
+ */
32
+ public async exportGlb(
33
+ filename = 'glb-export.glb',
34
+ { createUniqueMeshNames = true, ...exportOptionsRest }: IExportOptionsExtended = {},
35
+ excluded?: ExcludedGeometryList
36
+ ): Promise<File | undefined> {
37
+ const exportOptions = { createUniqueMeshNames, ...exportOptionsRest };
38
+ this.exportPreProcess(exportOptions);
39
+ const glbData = await GLTF2Export.GLBAsync(
40
+ this.viewer.scene,
41
+ 'dummy',
42
+ this.gltfExportOptions(exportOptions, excluded)
43
+ );
44
+ this.exportPostProcess();
45
+ const resBlob = glbData.glTFFiles['dummy.glb']; // should be only one file for glb
46
+
47
+ // check if result is valid, according to the typings this could also be a string
48
+ if (resBlob instanceof Blob) {
49
+ if (!filename.endsWith('.glb')) {
50
+ filename += '.glb';
51
+ }
52
+ return new File([resBlob], filename);
53
+ } else {
54
+ // result was not a valid blob
55
+ return undefined;
56
+ }
57
+ }
58
+
59
+ /**
60
+ * Exports selected nodes to GLTF. This may result in more than one file, since textures are exported separately.
61
+ * @param filename name of the main (text-based) .GLTF file referring to separate texture files.
62
+ * @param exportOptions export options to be merged with default options.\
63
+ * `exportOptions.createUniqueMeshNames` defaults to `true` if not given.
64
+ * @param excluded optional list of geometry (meshes, elements, variants, variantInstances) to be excluded from export
65
+ */
66
+ public async exportGltfToFile(
67
+ filename: string,
68
+ { createUniqueMeshNames = true, ...exportOptionsRest }: IExportOptionsExtended = {},
69
+ excluded?: ExcludedGeometryList
70
+ ) {
71
+ const exportOptions = { createUniqueMeshNames, ...exportOptionsRest };
72
+ this.exportPreProcess(exportOptions);
73
+ await GLTF2Export.GLTFAsync(this.viewer.scene, filename, this.gltfExportOptions(exportOptions, excluded)).then(
74
+ glb => {
75
+ glb.downloadFiles();
76
+ }
77
+ );
78
+ this.exportPostProcess();
79
+ }
80
+
81
+ /**
82
+ * Exports selected nodes to GLB. This results in one binary file.
83
+ * @param filename name of the .GLB file.
84
+ * @param exportOptions export options to be merged with default options.\
85
+ * `exportOptions.createUniqueMeshNames` defaults to `true` if not given.
86
+ * @param excluded optional list of geometry (meshes, elements, variants, variantInstances) to be excluded from export
87
+ */
88
+ public async exportGlbToFile(
89
+ filename: string,
90
+ { createUniqueMeshNames = true, ...exportOptionsRest }: IExportOptionsExtended = {},
91
+ excluded?: ExcludedGeometryList
92
+ ) {
93
+ const exportOptions = { createUniqueMeshNames, ...exportOptionsRest };
94
+ this.exportPreProcess(exportOptions);
95
+ await GLTF2Export.GLBAsync(this.viewer.scene, filename, this.gltfExportOptions(exportOptions, excluded)).then(
96
+ glb => {
97
+ glb.downloadFiles();
98
+ }
99
+ );
100
+ this.exportPostProcess();
101
+ }
102
+
103
+ /**
104
+ * Gets predefined {@link IExportOptionsExtended } merged with given ones.
105
+ */
106
+ protected gltfExportOptions(
107
+ mergeWithOptions: IExportOptionsExtended = {},
108
+ excluded?: ExcludedGeometryList
109
+ ): IExportOptions {
110
+ const defaultOptions: IExportOptions = {
111
+ // includeCoordinateSystemConversionNodes: true,
112
+ shouldExportNode: function (node: any) {
113
+ if (!node.isEnabled()) {
114
+ return false;
115
+ }
116
+ if ((node.name as string).startsWith('__bounding_box__')) {
117
+ return false;
118
+ }
119
+ if (excluded && node instanceof Mesh && isMeshIncludedInExclusionList(node, excluded)) {
120
+ return false;
121
+ }
122
+ return true;
123
+ },
124
+ };
125
+ return merge({}, defaultOptions, mergeWithOptions);
126
+ }
127
+
128
+ /**
129
+ * Stuff to be done before exporting to GLTF
130
+ */
131
+ protected exportPreProcess({ createUniqueMeshNames = true }: IExportOptionsExtended) {
132
+ this.viewer.pauseRendering();
133
+ if (createUniqueMeshNames ?? true) {
134
+ this.setUniqueMeshNames();
135
+ }
136
+ this.exchangeRefractionMaterials();
137
+ }
138
+
139
+ /**
140
+ * Stuff to be done after the GLTF export
141
+ */
142
+ protected exportPostProcess() {
143
+ this.restoreRefractionMaterials();
144
+ this.restoreOriginalMeshNames();
145
+ this.viewer.resumeRendering();
146
+ }
147
+
148
+ /**
149
+ * Materials with refraction set are not exported properly.
150
+ * Exchange all such (relevant) materials with a more export-friendly version
151
+ */
152
+ protected exchangeRefractionMaterials() {
153
+ for (const n of this.viewer.scene.getNodes()) {
154
+ if (!(n instanceof AbstractMesh)) continue;
155
+ if (!(n.material instanceof PBRMaterial)) continue;
156
+ if (!(n.material as PBRMaterial).subSurface.isRefractionEnabled) continue;
157
+ if ((n.material as PBRMaterial).transparencyMode !== PBRMaterial.PBRMATERIAL_OPAQUE) continue;
158
+ // if we're here, we have a node holding a material with set refraction whose transparencyMode is set to
159
+ // PBRMATERIAL_OPAQUE
160
+ n.material = this.createRefractionMaterialReplacement(n.material);
161
+ }
162
+ }
163
+
164
+ /**
165
+ * Restore original materials with set refraction.
166
+ */
167
+ protected restoreRefractionMaterials() {
168
+ for (const n of this.viewer.scene.getNodes()) {
169
+ if (!(n instanceof AbstractMesh)) continue;
170
+ if (!(n.material instanceof PBRMaterial)) continue;
171
+ if (!this.isMaterialClonedForExport(n.material)) continue;
172
+ // at this point we have a pbrmaterial tagged as cloned in its metadata that's set on a mesh
173
+
174
+ // restore and dispose
175
+ const currMaterial = n.material;
176
+ const prevMaterial = this.viewer.scene.getMaterialByUniqueID(n.material.metadata.clonedFrom);
177
+ if (prevMaterial) {
178
+ n.material = prevMaterial; // restore previous material
179
+ currMaterial.dispose(false, true); // dispose of clone
180
+ }
181
+ }
182
+ }
183
+
184
+ /**
185
+ * Create an export-friendly replacement material for a material using refraction.
186
+ * @param mat Material to be replaced
187
+ */
188
+ protected createRefractionMaterialReplacement(mat: PBRMaterial): PBRMaterial {
189
+ // if we're dealing with a clone already, return it instead of cloning
190
+ if (this.isMaterialClonedForExport(mat)) return mat;
191
+
192
+ // change material according to https://www.notion.so/combeenation/Glas-materials-don-t-look-glasy-after-export-d5fda2c6515e4420a8772744d3e6b460
193
+ const clonedMaterial = mat.clone(mat.name); // clone material. clone uses same name
194
+ clonedMaterial.metadata = { ...mat.metadata, clonedFrom: mat.uniqueId }; // create shallow copy of metadata on clone. see https://forum.babylonjs.com/t/the-metadata-of-the-mesh-cloned-by-the-instantiatemodelstoscene-method-is-a-shallow-copy/21563
195
+ clonedMaterial.refractionTexture = null;
196
+ clonedMaterial.metallicReflectanceTexture = null; // is this the correct one for metallic roughness?
197
+ clonedMaterial.alpha = 0.7;
198
+ clonedMaterial.albedoColor = new Color3(0.3, 0.3, 0.3);
199
+ clonedMaterial.transparencyMode = PBRMaterial.PBRMATERIAL_ALPHABLEND;
200
+ clonedMaterial.metallic = 0.65;
201
+ clonedMaterial.roughness = 0.15;
202
+
203
+ return clonedMaterial;
204
+ }
205
+
206
+ /**
207
+ * Inspect if a material was temporarily cloned for GLB export
208
+ * @param mat Material to be inspected
209
+ */
210
+ protected isMaterialClonedForExport(mat: PBRMaterial): boolean {
211
+ return !!mat.metadata?.clonedFrom;
212
+ }
213
+
214
+ /**
215
+ * Set unique mesh names for GLB export
216
+ */
217
+ protected setUniqueMeshNames(): void {
218
+ this.viewer.scene.meshes.forEach(currMesh => {
219
+ injectNodeMetadata(currMesh, { [GltfExportManager.UNIQUE_MESH_METADATA_PROPERTY]: currMesh.name }, false);
220
+ currMesh.name = `${currMesh.name}_${currMesh.uniqueId}`;
221
+ });
222
+ }
223
+
224
+ /**
225
+ * Restore original mesh names for meshes that were
226
+ * renamed with {@link setUniqueMeshNames()}
227
+ */
228
+ protected restoreOriginalMeshNames(): void {
229
+ this.viewer.scene.meshes.forEach(currMesh => {
230
+ if (has(currMesh.metadata, GltfExportManager.UNIQUE_MESH_METADATA_PROPERTY)) {
231
+ currMesh.name = currMesh.metadata[GltfExportManager.UNIQUE_MESH_METADATA_PROPERTY];
232
+ delete currMesh.metadata[GltfExportManager.UNIQUE_MESH_METADATA_PROPERTY];
233
+ }
234
+ });
235
+ }
236
+ }