@combeenation/3d-viewer 9.2.1-alpha1 → 10.0.0-alpha3

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 (113) hide show
  1. package/README.md +9 -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 +666 -667
  8. package/dist/lib-cjs/api/classes/element.js.map +1 -1
  9. package/dist/lib-cjs/api/classes/event.d.ts +401 -401
  10. package/dist/lib-cjs/api/classes/event.js +424 -424
  11. package/dist/lib-cjs/api/classes/eventBroadcaster.d.ts +26 -26
  12. package/dist/lib-cjs/api/classes/eventBroadcaster.js +49 -49
  13. package/dist/lib-cjs/api/classes/fuzzyMap.d.ts +7 -7
  14. package/dist/lib-cjs/api/classes/fuzzyMap.js +21 -21
  15. package/dist/lib-cjs/api/classes/parameter.d.ts +410 -410
  16. package/dist/lib-cjs/api/classes/parameter.js +642 -642
  17. package/dist/lib-cjs/api/classes/parameterObservable.d.ts +36 -36
  18. package/dist/lib-cjs/api/classes/parameterObservable.js +72 -72
  19. package/dist/lib-cjs/api/classes/parameterizable.d.ts +15 -15
  20. package/dist/lib-cjs/api/classes/parameterizable.js +102 -102
  21. package/dist/lib-cjs/api/classes/placementAnimation.d.ts +45 -45
  22. package/dist/lib-cjs/api/classes/placementAnimation.js +176 -176
  23. package/dist/lib-cjs/api/classes/variant.d.ts +261 -261
  24. package/dist/lib-cjs/api/classes/variant.js +871 -870
  25. package/dist/lib-cjs/api/classes/variant.js.map +1 -1
  26. package/dist/lib-cjs/api/classes/variantInstance.d.ts +53 -53
  27. package/dist/lib-cjs/api/classes/variantInstance.js +125 -125
  28. package/dist/lib-cjs/api/classes/variantParameterizable.d.ts +17 -17
  29. package/dist/lib-cjs/api/classes/variantParameterizable.js +86 -86
  30. package/dist/lib-cjs/api/classes/viewer.d.ts +211 -213
  31. package/dist/lib-cjs/api/classes/viewer.js +736 -717
  32. package/dist/lib-cjs/api/classes/viewer.js.map +1 -1
  33. package/dist/lib-cjs/api/classes/viewerError.d.ts +43 -43
  34. package/dist/lib-cjs/api/classes/viewerError.js +55 -55
  35. package/dist/lib-cjs/api/classes/viewerLight.d.ts +66 -66
  36. package/dist/lib-cjs/api/classes/viewerLight.js +344 -348
  37. package/dist/lib-cjs/api/classes/viewerLight.js.map +1 -1
  38. package/dist/lib-cjs/api/internal/lensRendering.d.ts +8 -8
  39. package/dist/lib-cjs/api/internal/lensRendering.js +11 -11
  40. package/dist/lib-cjs/api/internal/sceneSetup.d.ts +13 -13
  41. package/dist/lib-cjs/api/internal/sceneSetup.js +224 -226
  42. package/dist/lib-cjs/api/internal/sceneSetup.js.map +1 -1
  43. package/dist/lib-cjs/api/manager/animationManager.d.ts +30 -30
  44. package/dist/lib-cjs/api/manager/animationManager.js +126 -126
  45. package/dist/lib-cjs/api/manager/gltfExportManager.d.ts +102 -102
  46. package/dist/lib-cjs/api/manager/gltfExportManager.js +322 -316
  47. package/dist/lib-cjs/api/manager/gltfExportManager.js.map +1 -1
  48. package/dist/lib-cjs/api/manager/sceneManager.d.ts +33 -33
  49. package/dist/lib-cjs/api/manager/sceneManager.js +128 -128
  50. package/dist/lib-cjs/api/manager/tagManager.d.ts +118 -118
  51. package/dist/lib-cjs/api/manager/tagManager.js +535 -535
  52. package/dist/lib-cjs/api/manager/textureLoadManager.d.ts +22 -22
  53. package/dist/lib-cjs/api/manager/textureLoadManager.js +98 -98
  54. package/dist/lib-cjs/api/manager/variantInstanceManager.d.ts +106 -106
  55. package/dist/lib-cjs/api/manager/variantInstanceManager.js +290 -290
  56. package/dist/lib-cjs/api/store/specStorage.d.ts +32 -32
  57. package/dist/lib-cjs/api/store/specStorage.js +65 -65
  58. package/dist/lib-cjs/api/util/babylonHelper.d.ts +238 -238
  59. package/dist/lib-cjs/api/util/babylonHelper.js +826 -826
  60. package/dist/lib-cjs/api/util/geometryHelper.d.ts +7 -7
  61. package/dist/lib-cjs/api/util/geometryHelper.js +115 -115
  62. package/dist/lib-cjs/api/util/globalTypes.d.ts +484 -484
  63. package/dist/lib-cjs/api/util/globalTypes.js +1 -1
  64. package/dist/lib-cjs/api/util/resourceHelper.d.ts +58 -58
  65. package/dist/lib-cjs/api/util/resourceHelper.js +214 -214
  66. package/dist/lib-cjs/api/util/sceneLoaderHelper.d.ts +44 -44
  67. package/dist/lib-cjs/api/util/sceneLoaderHelper.js +175 -175
  68. package/dist/lib-cjs/api/util/stringHelper.d.ts +13 -13
  69. package/dist/lib-cjs/api/util/stringHelper.js +32 -32
  70. package/dist/lib-cjs/api/util/structureHelper.d.ts +9 -9
  71. package/dist/lib-cjs/api/util/structureHelper.js +64 -64
  72. package/dist/lib-cjs/buildinfo.json +3 -3
  73. package/dist/lib-cjs/commonjs.tsconfig.tsbuildinfo +1 -1
  74. package/dist/lib-cjs/index.d.ts +54 -54
  75. package/dist/lib-cjs/index.js +117 -117
  76. package/package.json +84 -82
  77. package/src/api/classes/animationInterface.ts +10 -10
  78. package/src/api/classes/dottedPath.ts +181 -181
  79. package/src/api/classes/element.ts +723 -723
  80. package/src/api/classes/event.ts +457 -457
  81. package/src/api/classes/eventBroadcaster.ts +52 -52
  82. package/src/api/classes/fuzzyMap.ts +21 -21
  83. package/src/api/classes/parameter.ts +686 -686
  84. package/src/api/classes/parameterObservable.ts +73 -73
  85. package/src/api/classes/parameterizable.ts +87 -87
  86. package/src/api/classes/placementAnimation.ts +162 -162
  87. package/src/api/classes/variant.ts +964 -963
  88. package/src/api/classes/variantInstance.ts +123 -123
  89. package/src/api/classes/variantParameterizable.ts +83 -83
  90. package/src/api/classes/viewer.ts +786 -770
  91. package/src/api/classes/viewerError.ts +63 -63
  92. package/src/api/classes/viewerLight.ts +335 -339
  93. package/src/api/internal/debugViewer.ts +90 -90
  94. package/src/api/internal/lensRendering.ts +9 -9
  95. package/src/api/internal/sceneSetup.ts +203 -205
  96. package/src/api/manager/animationManager.ts +143 -143
  97. package/src/api/manager/gltfExportManager.ts +312 -306
  98. package/src/api/manager/sceneManager.ts +134 -134
  99. package/src/api/manager/tagManager.ts +576 -576
  100. package/src/api/manager/textureLoadManager.ts +96 -96
  101. package/src/api/manager/variantInstanceManager.ts +306 -306
  102. package/src/api/store/specStorage.ts +68 -68
  103. package/src/api/util/babylonHelper.ts +915 -915
  104. package/src/api/util/geometryHelper.ts +140 -140
  105. package/src/api/util/globalTypes.ts +560 -560
  106. package/src/api/util/resourceHelper.ts +201 -201
  107. package/src/api/util/sceneLoaderHelper.ts +170 -170
  108. package/src/api/util/stringHelper.ts +30 -30
  109. package/src/api/util/structureHelper.ts +63 -63
  110. package/src/buildinfo.json +3 -3
  111. package/src/dev.ts +70 -62
  112. package/src/index.ts +103 -103
  113. package/src/types.d.ts +49 -38
@@ -1,317 +1,323 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
25
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
- return new (P || (P = Promise))(function (resolve, reject) {
28
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
- step((generator = generator.apply(thisArg, _arguments || [])).next());
32
- });
33
- };
34
- var __rest = (this && this.__rest) || function (s, e) {
35
- var t = {};
36
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
37
- t[p] = s[p];
38
- if (s != null && typeof Object.getOwnPropertySymbols === "function")
39
- for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
40
- if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
41
- t[p[i]] = s[p[i]];
42
- }
43
- return t;
44
- };
45
- Object.defineProperty(exports, "__esModule", { value: true });
46
- exports.GltfExportManager = void 0;
47
- const geometryHelper_1 = require("..//util/geometryHelper");
48
- const viewer_1 = require("../classes/viewer");
49
- const babylonHelper_1 = require("../util/babylonHelper");
50
- const structureHelper_1 = require("../util/structureHelper");
51
- const engine_1 = require("@babylonjs/core/Engines/engine");
52
- const engineStore_1 = require("@babylonjs/core/Engines/engineStore");
53
- const sceneLoader_1 = require("@babylonjs/core/Loading/sceneLoader");
54
- const pbrMaterial_1 = require("@babylonjs/core/Materials/PBR/pbrMaterial");
55
- const math_color_1 = require("@babylonjs/core/Maths/math.color");
56
- const abstractMesh_1 = require("@babylonjs/core/Meshes/abstractMesh");
57
- const transformNode_1 = require("@babylonjs/core/Meshes/transformNode");
58
- const sceneSerializer_1 = require("@babylonjs/core/Misc/sceneSerializer");
59
- const scene_1 = require("@babylonjs/core/scene");
60
- const _2_0_1 = require("@babylonjs/serializers/glTF/2.0");
61
- const lodash_es_1 = require("lodash-es");
62
- class GltfExportManager {
63
- /**
64
- * Constructor.
65
- */
66
- constructor(viewer) {
67
- this.viewer = viewer;
68
- }
69
- /**
70
- * Creates an {@link GltfExportManager}.
71
- */
72
- static create(viewer) {
73
- return __awaiter(this, void 0, void 0, function* () {
74
- return new GltfExportManager(viewer);
75
- });
76
- }
77
- /**
78
- * Exports selected nodes to a file.
79
- * @param filename optional name of the exported .GLB file.
80
- * @param exportOptions export options to be merged with default options.\
81
- * `exportOptions.exchangeRefractionMaterials` and `exportOptions.limitTextureSize`
82
- * default to `true` if not given.
83
- * @param optimizeForAR adjusts the exported GLB so that known AR issues get fixed automatically
84
- * @param excluded optional list of geometry (meshes, elements, variants, variantInstances) to be excluded from export
85
- */
86
- exportGlb(filename = 'glb-export.glb', _a = {}, optimizeForAR, excluded) {
87
- var { exchangeRefractionMaterials = true, limitTextureSize = true } = _a, exportOptions = __rest(_a, ["exchangeRefractionMaterials", "limitTextureSize"]);
88
- if (optimizeForAR === void 0) { optimizeForAR = false; }
89
- return __awaiter(this, void 0, void 0, function* () {
90
- const { scene, sceneCopied } = yield this.exportPreProcess(exchangeRefractionMaterials, limitTextureSize, optimizeForAR);
91
- const glbData = yield _2_0_1.GLTF2Export.GLBAsync(scene, 'dummy', this.gltfExportOptions(exportOptions, excluded));
92
- this.exportPostProcess(scene, sceneCopied);
93
- const resBlob = glbData.glTFFiles['dummy.glb'];
94
- // check if result is valid, according to the typings this could also be a string
95
- if (resBlob instanceof Blob) {
96
- if (!filename.endsWith('.glb')) {
97
- filename += '.glb';
98
- }
99
- return new File([resBlob], filename);
100
- }
101
- else {
102
- // result was not a valid blob
103
- return undefined;
104
- }
105
- });
106
- }
107
- /**
108
- * Exports selected nodes to GLTF. This may result in more than one file, since textures are exported separately.
109
- * @param filename name of the main (text-based) .GLTF file referring to separate texture files.
110
- * @param exportOptions export options to be merged with default options.\
111
- * `exportOptions.exchangeRefractionMaterials` and `exportOptions.limitTextureSize`
112
- * default to `true` if not given.
113
- * @param optimizeForAR adjusts the exported GLB so that known AR issues get fixed automatically
114
- * @param excluded optional list of geometry (meshes, elements, variants, variantInstances) to be excluded from export
115
- */
116
- exportGltfToFile(filename, _a = {}, optimizeForAR, excluded) {
117
- var { exchangeRefractionMaterials = true, limitTextureSize = true } = _a, exportOptions = __rest(_a, ["exchangeRefractionMaterials", "limitTextureSize"]);
118
- if (optimizeForAR === void 0) { optimizeForAR = false; }
119
- return __awaiter(this, void 0, void 0, function* () {
120
- const { scene, sceneCopied } = yield this.exportPreProcess(exchangeRefractionMaterials, limitTextureSize, optimizeForAR);
121
- const gltf = yield _2_0_1.GLTF2Export.GLTFAsync(scene, filename, this.gltfExportOptions(exportOptions, excluded));
122
- gltf.downloadFiles();
123
- this.exportPostProcess(scene, sceneCopied);
124
- });
125
- }
126
- /**
127
- * Exports selected nodes to GLB. This results in one binary file.
128
- * @param filename name of the .GLB file.
129
- * @param exportOptions export options to be merged with default options.\
130
- * `exportOptions.exchangeRefractionMaterials` and `exportOptions.limitTextureSize`
131
- * default to `true` if not given.
132
- * @param optimizeForAR adjusts the exported GLB so that known AR issues get fixed automatically
133
- * @param excluded optional list of geometry (meshes, elements, variants, variantInstances) to be excluded from export
134
- */
135
- exportGlbToFile(filename, _a = {}, optimizeForAR, excluded) {
136
- var { exchangeRefractionMaterials = true, limitTextureSize = true } = _a, exportOptions = __rest(_a, ["exchangeRefractionMaterials", "limitTextureSize"]);
137
- if (optimizeForAR === void 0) { optimizeForAR = false; }
138
- return __awaiter(this, void 0, void 0, function* () {
139
- const { scene, sceneCopied } = yield this.exportPreProcess(exchangeRefractionMaterials, limitTextureSize, optimizeForAR);
140
- const glb = yield _2_0_1.GLTF2Export.GLBAsync(scene, filename, this.gltfExportOptions(exportOptions, excluded));
141
- glb.downloadFiles();
142
- this.exportPostProcess(scene, sceneCopied);
143
- });
144
- }
145
- /**
146
- * Gets predefined {@link IExportOptions } merged with given ones.
147
- */
148
- gltfExportOptions(mergeWithOptions = {}, excluded) {
149
- const defaultOptions = {
150
- shouldExportNode: function (node) {
151
- if (!node.isEnabled()) {
152
- return false;
153
- }
154
- if (node.name.startsWith(viewer_1.Viewer.BOUNDING_BOX_NAME)) {
155
- return false;
156
- }
157
- if (excluded && node instanceof transformNode_1.TransformNode && (0, structureHelper_1.isNodeIncludedInExclusionList)(node, excluded)) {
158
- return false;
159
- }
160
- return true;
161
- },
162
- };
163
- return (0, lodash_es_1.merge)({}, defaultOptions, mergeWithOptions);
164
- }
165
- /**
166
- * Stuff to be done before exporting to GLTF
167
- *
168
- * @returns Either the original scene if no adjustments have been made or a copied scene (which can be disposed after
169
- * the export) if adjustments have been made
170
- */
171
- exportPreProcess(exchangeRefractionMaterials, limitTextureSize, optimizeForAR) {
172
- return __awaiter(this, void 0, void 0, function* () {
173
- if (!exchangeRefractionMaterials && !optimizeForAR && !limitTextureSize) {
174
- // no need to copy the scene if no adjustments have to be made
175
- return { scene: this.viewer.scene, sceneCopied: false };
176
- }
177
- // copy the scene for the GLB export improvements, so that the original viewer scene remains untouched
178
- const copiedScene = yield this.copyViewerScene(limitTextureSize);
179
- if (exchangeRefractionMaterials) {
180
- this.exchangeRefractionMaterials(copiedScene);
181
- }
182
- if (optimizeForAR) {
183
- this.setUniqueMeshNames(copiedScene);
184
- // get rid of negative scalings and morph targets, since iOS can't handle it in the QuickLook app
185
- (0, geometryHelper_1.bakeGeometryOfAllMeshes)(copiedScene);
186
- }
187
- return { scene: copiedScene, sceneCopied: true };
188
- });
189
- }
190
- /**
191
- * Create a copy of the viewer scene, which can be further processed and optimized for the GLB export.
192
- * There is no "scene.clone" function, therefore the scene is serialized and loaded again to achieve this result.
193
- */
194
- copyViewerScene(limitTextureSize) {
195
- return __awaiter(this, void 0, void 0, function* () {
196
- // required by the scene serializer
197
- yield Promise.resolve().then(() => __importStar(require(/* webpackChunkName: "physicsenginecomponent" */ '@babylonjs/core/Physics/physicsEngineComponent')));
198
- // copy scene function uses scene serializer, which can't handle "complex" data like classes, function or recursive
199
- // data structures in metadata
200
- // it basically does a `JSON.stringify` on the metadata
201
- // therefore the metadata has to be removed from the nodes before the copy process
202
- const metadataArr = this.extractMetadataOfAllNodes();
203
- // serialize the scene
204
- const serializedScene = yield sceneSerializer_1.SceneSerializer.SerializeAsync(this.viewer.scene);
205
- // set the cropped metadata back to the viewer scene nodes
206
- this.restoreMetadataOfAllNodes(metadataArr);
207
- // load the scene into an invisible "dummy" canvas
208
- const dummyCanvas = document.createElement('canvas');
209
- const engine = new engine_1.Engine(dummyCanvas);
210
- if (limitTextureSize) {
211
- // according to some tests iOS devices break when exporting a scene with textures above 1024
212
- // this only affects the scene clone for the export
213
- engine.getCaps().maxTextureSize = 1024;
214
- }
215
- // overwritting _LastCreatedScene to fix issue: https://combeenation.youtrack.cloud/issue/CB-8972
216
- // In short: dummy scene erroneously interacts with original viewer scene
217
- // setting viewer scene as fallback again (`_LastCreatedScene`) resolves that issue
218
- const curLastScene = engineStore_1.EngineStore._LastCreatedScene;
219
- const newScene = new scene_1.Scene(engine);
220
- engineStore_1.EngineStore._LastCreatedScene = curLastScene;
221
- sceneLoader_1.SceneLoader.ShowLoadingScreen = false;
222
- yield sceneLoader_1.SceneLoader.AppendAsync('', 'data:' + JSON.stringify(serializedScene), newScene, undefined, '.babylon');
223
- return newScene;
224
- });
225
- }
226
- /**
227
- * Stuff to be done after the GLTF export
228
- */
229
- exportPostProcess(scene, disposeScene) {
230
- if (disposeScene) {
231
- // copied scene for GLB export is not needed anymore, therefore it can be removed
232
- scene.getEngine().dispose();
233
- }
234
- }
235
- /**
236
- * Remove all node metadata and return them in a map
237
- */
238
- extractMetadataOfAllNodes() {
239
- return this.viewer.scene.getNodes().reduce((accMetadataObj, curNode) => {
240
- const metadata = curNode.metadata;
241
- delete curNode.metadata;
242
- return Object.assign(Object.assign({}, accMetadataObj), { [curNode.uniqueId.toString()]: metadata });
243
- }, {});
244
- }
245
- /**
246
- * Restore node metadata from previously cropped metadata map
247
- */
248
- restoreMetadataOfAllNodes(metadataMap) {
249
- this.viewer.scene.getNodes().forEach(node => {
250
- const metadata = metadataMap[node.uniqueId.toString()];
251
- if (metadata) {
252
- node.metadata = metadata;
253
- }
254
- });
255
- }
256
- /**
257
- * Materials with refraction set are not exported properly.
258
- * Exchange all such (relevant) materials with a more export-friendly version
259
- */
260
- exchangeRefractionMaterials(scene) {
261
- for (const n of scene.getNodes()) {
262
- if (!(n instanceof abstractMesh_1.AbstractMesh))
263
- continue;
264
- if (!(n.material instanceof pbrMaterial_1.PBRMaterial))
265
- continue;
266
- if (!n.material.subSurface.isRefractionEnabled)
267
- continue;
268
- if (n.material.transparencyMode !== pbrMaterial_1.PBRMaterial.PBRMATERIAL_OPAQUE)
269
- continue;
270
- // if we're here, we have a node holding a material with set refraction whose transparencyMode is set to
271
- // PBRMATERIAL_OPAQUE
272
- n.material = this.createRefractionMaterialReplacement(n.material);
273
- }
274
- }
275
- /**
276
- * Create an export-friendly replacement material for a material using refraction.
277
- * @param mat Material to be replaced
278
- */
279
- createRefractionMaterialReplacement(mat) {
280
- // if we're dealing with a clone already, return it instead of cloning
281
- if (this.isMaterialClonedForExport(mat))
282
- return mat;
283
- // change material according to https://www.notion.so/combeenation/Glas-materials-don-t-look-glasy-after-export-d5fda2c6515e4420a8772744d3e6b460
284
- const clonedMaterial = mat.clone(mat.name); // clone material. clone uses same name
285
- clonedMaterial.metadata = Object.assign(Object.assign({}, mat.metadata), { [GltfExportManager._CLONED_FROM_MAT_METADATA_PROPERTY]: 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
286
- clonedMaterial.refractionTexture = null;
287
- clonedMaterial.metallicReflectanceTexture = null; // is this the correct one for metallic roughness?
288
- clonedMaterial.alpha = 0.7;
289
- clonedMaterial.albedoColor = new math_color_1.Color3(0.3, 0.3, 0.3);
290
- clonedMaterial.transparencyMode = pbrMaterial_1.PBRMaterial.PBRMATERIAL_ALPHABLEND;
291
- clonedMaterial.metallic = 0.65;
292
- clonedMaterial.roughness = 0.15;
293
- return clonedMaterial;
294
- }
295
- /**
296
- * Inspect if a material was temporarily cloned for GLB export
297
- * @param mat Material to be inspected
298
- */
299
- isMaterialClonedForExport(mat) {
300
- return (0, lodash_es_1.has)(mat.metadata, GltfExportManager._CLONED_FROM_MAT_METADATA_PROPERTY);
301
- }
302
- /**
303
- * Set unique mesh names for GLB export.
304
- * Duplicate names lead to problems in AR on iOS devices.
305
- */
306
- setUniqueMeshNames(scene) {
307
- const allNodes = scene.getNodes().filter(node => node instanceof transformNode_1.TransformNode);
308
- allNodes.forEach(currMesh => {
309
- (0, babylonHelper_1.injectMetadata)(currMesh, { [GltfExportManager.NAME_BEFORE_EXPORT_METADATA_PROPERTY]: currMesh.name }, false);
310
- currMesh.name = `${currMesh.name}_${currMesh.uniqueId}`;
311
- });
312
- }
313
- }
314
- exports.GltfExportManager = GltfExportManager;
315
- GltfExportManager._CLONED_FROM_MAT_METADATA_PROPERTY = 'clonedFrom';
316
- GltfExportManager.NAME_BEFORE_EXPORT_METADATA_PROPERTY = 'nameBeforeExport';
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
+ return new (P || (P = Promise))(function (resolve, reject) {
28
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
32
+ });
33
+ };
34
+ var __rest = (this && this.__rest) || function (s, e) {
35
+ var t = {};
36
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
37
+ t[p] = s[p];
38
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
39
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
40
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
41
+ t[p[i]] = s[p[i]];
42
+ }
43
+ return t;
44
+ };
45
+ Object.defineProperty(exports, "__esModule", { value: true });
46
+ exports.GltfExportManager = void 0;
47
+ const geometryHelper_1 = require("..//util/geometryHelper");
48
+ const viewer_1 = require("../classes/viewer");
49
+ const babylonHelper_1 = require("../util/babylonHelper");
50
+ const structureHelper_1 = require("../util/structureHelper");
51
+ const engine_1 = require("@babylonjs/core/Engines/engine");
52
+ const engineStore_1 = require("@babylonjs/core/Engines/engineStore");
53
+ const sceneLoader_1 = require("@babylonjs/core/Loading/sceneLoader");
54
+ const pbrMaterial_1 = require("@babylonjs/core/Materials/PBR/pbrMaterial");
55
+ const math_color_1 = require("@babylonjs/core/Maths/math.color");
56
+ const abstractMesh_1 = require("@babylonjs/core/Meshes/abstractMesh");
57
+ const transformNode_1 = require("@babylonjs/core/Meshes/transformNode");
58
+ const sceneSerializer_1 = require("@babylonjs/core/Misc/sceneSerializer");
59
+ const scene_1 = require("@babylonjs/core/scene");
60
+ const _2_0_1 = require("@babylonjs/serializers/glTF/2.0");
61
+ const lodash_es_1 = require("lodash-es");
62
+ class GltfExportManager {
63
+ /**
64
+ * Constructor.
65
+ */
66
+ constructor(viewer) {
67
+ this.viewer = viewer;
68
+ }
69
+ /**
70
+ * Creates an {@link GltfExportManager}.
71
+ */
72
+ static create(viewer) {
73
+ return __awaiter(this, void 0, void 0, function* () {
74
+ return new GltfExportManager(viewer);
75
+ });
76
+ }
77
+ /**
78
+ * Exports selected nodes to a file.
79
+ * @param filename optional name of the exported .GLB file.
80
+ * @param exportOptions export options to be merged with default options.\
81
+ * `exportOptions.exchangeRefractionMaterials` and `exportOptions.limitTextureSize`
82
+ * default to `true` if not given.
83
+ * @param optimizeForAR adjusts the exported GLB so that known AR issues get fixed automatically
84
+ * @param excluded optional list of geometry (meshes, elements, variants, variantInstances) to be excluded from export
85
+ */
86
+ exportGlb(filename = 'glb-export.glb', _a = {}, optimizeForAR, excluded) {
87
+ var { exchangeRefractionMaterials = true, limitTextureSize = true } = _a, exportOptions = __rest(_a, ["exchangeRefractionMaterials", "limitTextureSize"]);
88
+ if (optimizeForAR === void 0) { optimizeForAR = false; }
89
+ return __awaiter(this, void 0, void 0, function* () {
90
+ const { scene, sceneCopied } = yield this.exportPreProcess(exchangeRefractionMaterials, limitTextureSize, optimizeForAR);
91
+ const glbData = yield _2_0_1.GLTF2Export.GLBAsync(scene, 'dummy', this.gltfExportOptions(exportOptions, excluded));
92
+ this.exportPostProcess(scene, sceneCopied);
93
+ const resBlob = glbData.glTFFiles['dummy.glb'];
94
+ // check if result is valid, according to the typings this could also be a string
95
+ if (resBlob instanceof Blob) {
96
+ if (!filename.endsWith('.glb')) {
97
+ filename += '.glb';
98
+ }
99
+ return new File([resBlob], filename);
100
+ }
101
+ else {
102
+ // result was not a valid blob
103
+ return undefined;
104
+ }
105
+ });
106
+ }
107
+ /**
108
+ * Exports selected nodes to GLTF. This may result in more than one file, since textures are exported separately.
109
+ * @param filename name of the main (text-based) .GLTF file referring to separate texture files.
110
+ * @param exportOptions export options to be merged with default options.\
111
+ * `exportOptions.exchangeRefractionMaterials` and `exportOptions.limitTextureSize`
112
+ * default to `true` if not given.
113
+ * @param optimizeForAR adjusts the exported GLB so that known AR issues get fixed automatically
114
+ * @param excluded optional list of geometry (meshes, elements, variants, variantInstances) to be excluded from export
115
+ */
116
+ exportGltfToFile(filename, _a = {}, optimizeForAR, excluded) {
117
+ var { exchangeRefractionMaterials = true, limitTextureSize = true } = _a, exportOptions = __rest(_a, ["exchangeRefractionMaterials", "limitTextureSize"]);
118
+ if (optimizeForAR === void 0) { optimizeForAR = false; }
119
+ return __awaiter(this, void 0, void 0, function* () {
120
+ const { scene, sceneCopied } = yield this.exportPreProcess(exchangeRefractionMaterials, limitTextureSize, optimizeForAR);
121
+ const gltf = yield _2_0_1.GLTF2Export.GLTFAsync(scene, filename, this.gltfExportOptions(exportOptions, excluded));
122
+ gltf.downloadFiles();
123
+ this.exportPostProcess(scene, sceneCopied);
124
+ });
125
+ }
126
+ /**
127
+ * Exports selected nodes to GLB. This results in one binary file.
128
+ * @param filename name of the .GLB file.
129
+ * @param exportOptions export options to be merged with default options.\
130
+ * `exportOptions.exchangeRefractionMaterials` and `exportOptions.limitTextureSize`
131
+ * default to `true` if not given.
132
+ * @param optimizeForAR adjusts the exported GLB so that known AR issues get fixed automatically
133
+ * @param excluded optional list of geometry (meshes, elements, variants, variantInstances) to be excluded from export
134
+ */
135
+ exportGlbToFile(filename, _a = {}, optimizeForAR, excluded) {
136
+ var { exchangeRefractionMaterials = true, limitTextureSize = true } = _a, exportOptions = __rest(_a, ["exchangeRefractionMaterials", "limitTextureSize"]);
137
+ if (optimizeForAR === void 0) { optimizeForAR = false; }
138
+ return __awaiter(this, void 0, void 0, function* () {
139
+ const { scene, sceneCopied } = yield this.exportPreProcess(exchangeRefractionMaterials, limitTextureSize, optimizeForAR);
140
+ const glb = yield _2_0_1.GLTF2Export.GLBAsync(scene, filename, this.gltfExportOptions(exportOptions, excluded));
141
+ glb.downloadFiles();
142
+ this.exportPostProcess(scene, sceneCopied);
143
+ });
144
+ }
145
+ /**
146
+ * Gets predefined {@link IExportOptions } merged with given ones.
147
+ */
148
+ gltfExportOptions(mergeWithOptions = {}, excluded) {
149
+ const defaultOptions = {
150
+ shouldExportNode: function (node) {
151
+ if (!node.isEnabled()) {
152
+ return false;
153
+ }
154
+ if (node.name.startsWith(viewer_1.Viewer.BOUNDING_BOX_NAME)) {
155
+ return false;
156
+ }
157
+ if (excluded && node instanceof transformNode_1.TransformNode && (0, structureHelper_1.isNodeIncludedInExclusionList)(node, excluded)) {
158
+ return false;
159
+ }
160
+ return true;
161
+ },
162
+ };
163
+ return (0, lodash_es_1.merge)({}, defaultOptions, mergeWithOptions);
164
+ }
165
+ /**
166
+ * Stuff to be done before exporting to GLTF
167
+ *
168
+ * @returns Either the original scene if no adjustments have been made or a copied scene (which can be disposed after
169
+ * the export) if adjustments have been made
170
+ */
171
+ exportPreProcess(exchangeRefractionMaterials, limitTextureSize, optimizeForAR) {
172
+ return __awaiter(this, void 0, void 0, function* () {
173
+ if (!exchangeRefractionMaterials && !optimizeForAR && !limitTextureSize) {
174
+ // no need to copy the scene if no adjustments have to be made
175
+ return { scene: this.viewer.scene, sceneCopied: false };
176
+ }
177
+ // copy the scene for the GLB export improvements, so that the original viewer scene remains untouched
178
+ const copiedScene = yield this.copyViewerScene(limitTextureSize);
179
+ if (exchangeRefractionMaterials) {
180
+ this.exchangeRefractionMaterials(copiedScene);
181
+ }
182
+ if (optimizeForAR) {
183
+ this.setUniqueMeshNames(copiedScene);
184
+ // get rid of negative scalings and morph targets, since iOS can't handle it in the QuickLook app
185
+ (0, geometryHelper_1.bakeGeometryOfAllMeshes)(copiedScene);
186
+ // certain devices can't load models with skeletons, just remove them since animations are not supported in the AR
187
+ // scene anyway
188
+ while (copiedScene.skeletons.length) {
189
+ const skeleton = copiedScene.skeletons.pop();
190
+ skeleton.dispose();
191
+ }
192
+ }
193
+ return { scene: copiedScene, sceneCopied: true };
194
+ });
195
+ }
196
+ /**
197
+ * Create a copy of the viewer scene, which can be further processed and optimized for the GLB export.
198
+ * There is no "scene.clone" function, therefore the scene is serialized and loaded again to achieve this result.
199
+ */
200
+ copyViewerScene(limitTextureSize) {
201
+ return __awaiter(this, void 0, void 0, function* () {
202
+ // required by the scene serializer
203
+ yield Promise.resolve().then(() => __importStar(require(/* webpackChunkName: "physicsenginecomponent" */ '@babylonjs/core/Physics/physicsEngineComponent')));
204
+ // copy scene function uses scene serializer, which can't handle "complex" data like classes, function or recursive
205
+ // data structures in metadata
206
+ // it basically does a `JSON.stringify` on the metadata
207
+ // therefore the metadata has to be removed from the nodes before the copy process
208
+ const metadataArr = this.extractMetadataOfAllNodes();
209
+ // serialize the scene
210
+ const serializedScene = yield sceneSerializer_1.SceneSerializer.SerializeAsync(this.viewer.scene);
211
+ // set the cropped metadata back to the viewer scene nodes
212
+ this.restoreMetadataOfAllNodes(metadataArr);
213
+ // load the scene into an invisible "dummy" canvas
214
+ const dummyCanvas = document.createElement('canvas');
215
+ const engine = new engine_1.Engine(dummyCanvas);
216
+ if (limitTextureSize) {
217
+ // according to some tests iOS devices break when exporting a scene with textures above 1024
218
+ // this only affects the scene clone for the export
219
+ engine.getCaps().maxTextureSize = 1024;
220
+ }
221
+ // overwritting _LastCreatedScene to fix issue: https://combeenation.youtrack.cloud/issue/CB-8972
222
+ // In short: dummy scene erroneously interacts with original viewer scene
223
+ // setting viewer scene as fallback again (`_LastCreatedScene`) resolves that issue
224
+ const curLastScene = engineStore_1.EngineStore._LastCreatedScene;
225
+ const newScene = new scene_1.Scene(engine);
226
+ engineStore_1.EngineStore._LastCreatedScene = curLastScene;
227
+ sceneLoader_1.SceneLoader.ShowLoadingScreen = false;
228
+ yield sceneLoader_1.SceneLoader.AppendAsync('', 'data:' + JSON.stringify(serializedScene), newScene, undefined, '.babylon');
229
+ return newScene;
230
+ });
231
+ }
232
+ /**
233
+ * Stuff to be done after the GLTF export
234
+ */
235
+ exportPostProcess(scene, disposeScene) {
236
+ if (disposeScene) {
237
+ // copied scene for GLB export is not needed anymore, therefore it can be removed
238
+ scene.getEngine().dispose();
239
+ }
240
+ }
241
+ /**
242
+ * Remove all node metadata and return them in a map
243
+ */
244
+ extractMetadataOfAllNodes() {
245
+ return this.viewer.scene.getNodes().reduce((accMetadataObj, curNode) => {
246
+ const metadata = curNode.metadata;
247
+ delete curNode.metadata;
248
+ return Object.assign(Object.assign({}, accMetadataObj), { [curNode.uniqueId.toString()]: metadata });
249
+ }, {});
250
+ }
251
+ /**
252
+ * Restore node metadata from previously cropped metadata map
253
+ */
254
+ restoreMetadataOfAllNodes(metadataMap) {
255
+ this.viewer.scene.getNodes().forEach(node => {
256
+ const metadata = metadataMap[node.uniqueId.toString()];
257
+ if (metadata) {
258
+ node.metadata = metadata;
259
+ }
260
+ });
261
+ }
262
+ /**
263
+ * Materials with refraction set are not exported properly.
264
+ * Exchange all such (relevant) materials with a more export-friendly version
265
+ */
266
+ exchangeRefractionMaterials(scene) {
267
+ for (const n of scene.getNodes()) {
268
+ if (!(n instanceof abstractMesh_1.AbstractMesh))
269
+ continue;
270
+ if (!(n.material instanceof pbrMaterial_1.PBRMaterial))
271
+ continue;
272
+ if (!n.material.subSurface.isRefractionEnabled)
273
+ continue;
274
+ if (n.material.transparencyMode !== pbrMaterial_1.PBRMaterial.PBRMATERIAL_OPAQUE)
275
+ continue;
276
+ // if we're here, we have a node holding a material with set refraction whose transparencyMode is set to
277
+ // PBRMATERIAL_OPAQUE
278
+ n.material = this.createRefractionMaterialReplacement(n.material);
279
+ }
280
+ }
281
+ /**
282
+ * Create an export-friendly replacement material for a material using refraction.
283
+ * @param mat Material to be replaced
284
+ */
285
+ createRefractionMaterialReplacement(mat) {
286
+ // if we're dealing with a clone already, return it instead of cloning
287
+ if (this.isMaterialClonedForExport(mat))
288
+ return mat;
289
+ // change material according to https://www.notion.so/combeenation/Glas-materials-don-t-look-glasy-after-export-d5fda2c6515e4420a8772744d3e6b460
290
+ const clonedMaterial = mat.clone(mat.name); // clone material. clone uses same name
291
+ clonedMaterial.metadata = Object.assign(Object.assign({}, mat.metadata), { [GltfExportManager._CLONED_FROM_MAT_METADATA_PROPERTY]: 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
292
+ clonedMaterial.refractionTexture = null;
293
+ clonedMaterial.metallicReflectanceTexture = null; // is this the correct one for metallic roughness?
294
+ clonedMaterial.alpha = 0.7;
295
+ clonedMaterial.albedoColor = new math_color_1.Color3(0.3, 0.3, 0.3);
296
+ clonedMaterial.transparencyMode = pbrMaterial_1.PBRMaterial.PBRMATERIAL_ALPHABLEND;
297
+ clonedMaterial.metallic = 0.65;
298
+ clonedMaterial.roughness = 0.15;
299
+ return clonedMaterial;
300
+ }
301
+ /**
302
+ * Inspect if a material was temporarily cloned for GLB export
303
+ * @param mat Material to be inspected
304
+ */
305
+ isMaterialClonedForExport(mat) {
306
+ return (0, lodash_es_1.has)(mat.metadata, GltfExportManager._CLONED_FROM_MAT_METADATA_PROPERTY);
307
+ }
308
+ /**
309
+ * Set unique mesh names for GLB export.
310
+ * Duplicate names lead to problems in AR on iOS devices.
311
+ */
312
+ setUniqueMeshNames(scene) {
313
+ const allNodes = scene.getNodes().filter(node => node instanceof transformNode_1.TransformNode);
314
+ allNodes.forEach(currMesh => {
315
+ (0, babylonHelper_1.injectMetadata)(currMesh, { [GltfExportManager.NAME_BEFORE_EXPORT_METADATA_PROPERTY]: currMesh.name }, false);
316
+ currMesh.name = `${currMesh.name}_${currMesh.uniqueId}`;
317
+ });
318
+ }
319
+ }
320
+ exports.GltfExportManager = GltfExportManager;
321
+ GltfExportManager._CLONED_FROM_MAT_METADATA_PROPERTY = 'clonedFrom';
322
+ GltfExportManager.NAME_BEFORE_EXPORT_METADATA_PROPERTY = 'nameBeforeExport';
317
323
  //# sourceMappingURL=gltfExportManager.js.map