@combeenation/3d-viewer 7.1.1 → 7.1.2

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 (115) 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 +672 -670
  8. package/dist/lib-cjs/api/classes/element.js.map +1 -1
  9. package/dist/lib-cjs/api/classes/event.d.ts +401 -396
  10. package/dist/lib-cjs/api/classes/event.js +424 -419
  11. package/dist/lib-cjs/api/classes/event.js.map +1 -1
  12. package/dist/lib-cjs/api/classes/eventBroadcaster.d.ts +26 -26
  13. package/dist/lib-cjs/api/classes/eventBroadcaster.js +49 -49
  14. package/dist/lib-cjs/api/classes/fuzzyMap.d.ts +7 -7
  15. package/dist/lib-cjs/api/classes/fuzzyMap.js +21 -21
  16. package/dist/lib-cjs/api/classes/parameter.d.ts +351 -351
  17. package/dist/lib-cjs/api/classes/parameter.js +524 -517
  18. package/dist/lib-cjs/api/classes/parameter.js.map +1 -1
  19. package/dist/lib-cjs/api/classes/parameterObservable.d.ts +36 -36
  20. package/dist/lib-cjs/api/classes/parameterObservable.js +72 -72
  21. package/dist/lib-cjs/api/classes/parameterizable.d.ts +15 -15
  22. package/dist/lib-cjs/api/classes/parameterizable.js +102 -102
  23. package/dist/lib-cjs/api/classes/placementAnimation.d.ts +45 -45
  24. package/dist/lib-cjs/api/classes/placementAnimation.js +176 -176
  25. package/dist/lib-cjs/api/classes/variant.d.ts +253 -253
  26. package/dist/lib-cjs/api/classes/variant.js +858 -843
  27. package/dist/lib-cjs/api/classes/variant.js.map +1 -1
  28. package/dist/lib-cjs/api/classes/variantInstance.d.ts +53 -53
  29. package/dist/lib-cjs/api/classes/variantInstance.js +125 -125
  30. package/dist/lib-cjs/api/classes/variantParameterizable.d.ts +17 -17
  31. package/dist/lib-cjs/api/classes/variantParameterizable.js +86 -88
  32. package/dist/lib-cjs/api/classes/variantParameterizable.js.map +1 -1
  33. package/dist/lib-cjs/api/classes/viewer.d.ts +204 -200
  34. package/dist/lib-cjs/api/classes/viewer.js +682 -670
  35. package/dist/lib-cjs/api/classes/viewer.js.map +1 -1
  36. package/dist/lib-cjs/api/classes/viewerError.d.ts +43 -0
  37. package/dist/lib-cjs/api/classes/viewerError.js +56 -0
  38. package/dist/lib-cjs/api/classes/viewerError.js.map +1 -0
  39. package/dist/lib-cjs/api/classes/viewerLight.d.ts +66 -66
  40. package/dist/lib-cjs/api/classes/viewerLight.js +348 -348
  41. package/dist/lib-cjs/api/internal/lensRendering.d.ts +8 -8
  42. package/dist/lib-cjs/api/internal/lensRendering.js +11 -11
  43. package/dist/lib-cjs/api/internal/sceneSetup.d.ts +13 -13
  44. package/dist/lib-cjs/api/internal/sceneSetup.js +226 -226
  45. package/dist/lib-cjs/api/manager/animationManager.d.ts +30 -30
  46. package/dist/lib-cjs/api/manager/animationManager.js +126 -126
  47. package/dist/lib-cjs/api/manager/gltfExportManager.d.ts +79 -79
  48. package/dist/lib-cjs/api/manager/gltfExportManager.js +242 -242
  49. package/dist/lib-cjs/api/manager/sceneManager.d.ts +33 -33
  50. package/dist/lib-cjs/api/manager/sceneManager.js +128 -130
  51. package/dist/lib-cjs/api/manager/sceneManager.js.map +1 -1
  52. package/dist/lib-cjs/api/manager/tagManager.d.ts +116 -109
  53. package/dist/lib-cjs/api/manager/tagManager.js +444 -425
  54. package/dist/lib-cjs/api/manager/tagManager.js.map +1 -1
  55. package/dist/lib-cjs/api/manager/textureLoadManager.d.ts +22 -22
  56. package/dist/lib-cjs/api/manager/textureLoadManager.js +97 -97
  57. package/dist/lib-cjs/api/manager/variantInstanceManager.d.ts +106 -106
  58. package/dist/lib-cjs/api/manager/variantInstanceManager.js +292 -292
  59. package/dist/lib-cjs/api/store/specStorage.d.ts +32 -32
  60. package/dist/lib-cjs/api/store/specStorage.js +65 -65
  61. package/dist/lib-cjs/api/util/babylonHelper.d.ts +235 -235
  62. package/dist/lib-cjs/api/util/babylonHelper.js +753 -753
  63. package/dist/lib-cjs/api/util/globalTypes.d.ts +441 -437
  64. package/dist/lib-cjs/api/util/globalTypes.js +1 -1
  65. package/dist/lib-cjs/api/util/resourceHelper.d.ts +58 -58
  66. package/dist/lib-cjs/api/util/resourceHelper.js +203 -203
  67. package/dist/lib-cjs/api/util/sceneLoaderHelper.d.ts +44 -44
  68. package/dist/lib-cjs/api/util/sceneLoaderHelper.js +175 -175
  69. package/dist/lib-cjs/api/util/stringHelper.d.ts +13 -13
  70. package/dist/lib-cjs/api/util/stringHelper.js +32 -32
  71. package/dist/lib-cjs/api/util/structureHelper.d.ts +9 -9
  72. package/dist/lib-cjs/api/util/structureHelper.js +57 -48
  73. package/dist/lib-cjs/api/util/structureHelper.js.map +1 -1
  74. package/dist/lib-cjs/buildinfo.json +3 -3
  75. package/dist/lib-cjs/commonjs.tsconfig.tsbuildinfo +1 -1
  76. package/dist/lib-cjs/index.d.ts +54 -53
  77. package/dist/lib-cjs/index.js +117 -114
  78. package/dist/lib-cjs/index.js.map +1 -1
  79. package/package.json +81 -81
  80. package/src/api/classes/animationInterface.ts +10 -10
  81. package/src/api/classes/dottedPath.ts +181 -181
  82. package/src/api/classes/element.ts +733 -731
  83. package/src/api/classes/event.ts +457 -452
  84. package/src/api/classes/eventBroadcaster.ts +52 -52
  85. package/src/api/classes/fuzzyMap.ts +21 -21
  86. package/src/api/classes/parameter.ts +561 -554
  87. package/src/api/classes/parameterObservable.ts +73 -73
  88. package/src/api/classes/parameterizable.ts +87 -87
  89. package/src/api/classes/placementAnimation.ts +162 -162
  90. package/src/api/classes/variant.ts +949 -933
  91. package/src/api/classes/variantInstance.ts +123 -123
  92. package/src/api/classes/variantParameterizable.ts +83 -85
  93. package/src/api/classes/viewer.ts +760 -744
  94. package/src/api/classes/viewerError.ts +63 -0
  95. package/src/api/classes/viewerLight.ts +339 -339
  96. package/src/api/internal/debugViewer.ts +90 -90
  97. package/src/api/internal/lensRendering.ts +9 -9
  98. package/src/api/internal/sceneSetup.ts +205 -205
  99. package/src/api/manager/animationManager.ts +143 -143
  100. package/src/api/manager/gltfExportManager.ts +237 -237
  101. package/src/api/manager/sceneManager.ts +134 -136
  102. package/src/api/manager/tagManager.ts +477 -457
  103. package/src/api/manager/textureLoadManager.ts +95 -95
  104. package/src/api/manager/variantInstanceManager.ts +309 -309
  105. package/src/api/store/specStorage.ts +68 -68
  106. package/src/api/util/babylonHelper.ts +823 -823
  107. package/src/api/util/globalTypes.ts +508 -504
  108. package/src/api/util/resourceHelper.ts +191 -191
  109. package/src/api/util/sceneLoaderHelper.ts +170 -170
  110. package/src/api/util/stringHelper.ts +30 -30
  111. package/src/api/util/structureHelper.ts +58 -49
  112. package/src/buildinfo.json +3 -3
  113. package/src/dev.ts +62 -62
  114. package/src/index.ts +103 -100
  115. package/src/types.d.ts +38 -38
@@ -1,744 +1,760 @@
1
- import buildInfo from '../../buildinfo.json';
2
- import { sceneSetup } from '../internal/sceneSetup';
3
- import { AnimationManager } from '../manager/animationManager';
4
- import { GltfExportManager } from '../manager/gltfExportManager';
5
- import { SceneManager } from '../manager/sceneManager';
6
- import { TagManager } from '../manager/tagManager';
7
- import { VariantInstanceManager } from '../manager/variantInstanceManager';
8
- import { SpecStorage } from '../store/specStorage';
9
- import { backgroundDomeName, envHelperMetadataName } from '../util/babylonHelper';
10
- import { debounce, loadJavascript, loadJson } from '../util/resourceHelper';
11
- import { getCustomCbnBabylonLoaderPlugin } from '../util/sceneLoaderHelper';
12
- import { replaceDots } from '../util/stringHelper';
13
- import { isMeshIncludedInExclusionList } from '../util/structureHelper';
14
- import { AnimationInterface } from './animationInterface';
15
- import { Event } from './event';
16
- import { EventBroadcaster } from './eventBroadcaster';
17
- import { Parameter } from './parameter';
18
- import { VariantInstance } from './variantInstance';
19
- import { ArcRotateCamera } from '@babylonjs/core/Cameras/arcRotateCamera';
20
- import { PickingInfo } from '@babylonjs/core/Collisions/pickingInfo';
21
- import { BoundingInfo } from '@babylonjs/core/Culling/boundingInfo';
22
- import { DebugLayer } from '@babylonjs/core/Debug/debugLayer';
23
- import { Engine } from '@babylonjs/core/Engines/engine';
24
- import { IPointerEvent } from '@babylonjs/core/Events/deviceInputEvents';
25
- import { EnvironmentHelper } from '@babylonjs/core/Helpers/environmentHelper';
26
- import { PhotoDome } from '@babylonjs/core/Helpers/photoDome';
27
- import { HighlightLayer } from '@babylonjs/core/Layers/highlightLayer';
28
- import { ISceneLoaderPlugin, SceneLoader } from '@babylonjs/core/Loading/sceneLoader';
29
- import { DynamicTexture } from '@babylonjs/core/Materials/Textures/dynamicTexture';
30
- import { StandardMaterial } from '@babylonjs/core/Materials/standardMaterial';
31
- import { Color3 } from '@babylonjs/core/Maths/math.color';
32
- import { Vector3 } from '@babylonjs/core/Maths/math.vector';
33
- import { Mesh } from '@babylonjs/core/Meshes/mesh';
34
- import { ScreenshotTools } from '@babylonjs/core/Misc/screenshotTools';
35
- import { WebXRSessionManager } from '@babylonjs/core/XR/webXRSessionManager';
36
- import { Scene } from '@babylonjs/core/scene';
37
- import { isArray, isString } from 'lodash-es';
38
-
39
- /**
40
- * The main exposed object. This is the entry point into the application
41
- *
42
- * ```js
43
- * const canvas = document.getElementById( 'babylon-canvas' );
44
- * const viewer = Viewer( canvas, '/path/to/index.json' );
45
- * ```
46
- * The class does nothing on its own and needs to {@link bootstrap}
47
- */
48
- export class Viewer extends EventBroadcaster {
49
- public static readonly BOUNDING_BOX_NAME = '__bounding_box__';
50
-
51
- protected _scene: Scene | null = null;
52
-
53
- protected _animationManager: AnimationManager | null = null;
54
-
55
- protected _sceneManager: SceneManager | null = null;
56
-
57
- protected _tagManager: TagManager | null = null;
58
-
59
- protected _gltfExportManager: GltfExportManager | null = null;
60
-
61
- protected _variantInstances: VariantInstanceManager | null = null;
62
-
63
- // defaults to `false` as material cloning should be the edge case
64
- protected _cloneMaterialsOnMutation: boolean = false;
65
-
66
- protected _isRenderLoopPaused: boolean = false;
67
-
68
- protected _inspectorLoaded: boolean = false;
69
-
70
- protected _nodeNamingStrategyHandler: NodeNamingStrategyHandler | null = null;
71
-
72
- static version = buildInfo.version;
73
-
74
- // these are constants for calculating the camera settings, depending on the bounding box size of the meshes to zoom
75
- // the algorithms and constant values are directly taken from the Babylon.js repository
76
- private static readonly _autofocusConstants = {
77
- minZ: 100,
78
- wheelPrecision: 100,
79
- panningSensibility: 5000,
80
- pinchPrecision: 200,
81
- };
82
-
83
- /**
84
- * Help function for automatically creating a valid Spec from a list of variant names and dedicated GLB/babylon URLs.
85
- * This data is most likely coming from babylon assets from the Combeenation system but other sources are also valid.
86
- */
87
- public static generateSpec(genData: SpecGenerationData[]): AutoSpecStructureJson {
88
- // dots in the variant name indicate inheritance, but this should not be the case for an auto-generated spec
89
- // therefore dots are exchanged with slashes
90
- const safeGenData: SpecGenerationData[] = genData.map(data => ({ ...data, name: replaceDots(data.name) }));
91
-
92
- const spec: AutoSpecStructureJson = {
93
- // common scene settings as suggested in the viewer docs
94
- scene: {
95
- engine: {
96
- antialiasing: true,
97
- options: {
98
- preserveDrawingBuffer: true,
99
- stencil: true,
100
- xrCompatible: false,
101
- },
102
- },
103
- scene: {
104
- globals: {},
105
- },
106
- },
107
- setup: {
108
- // create one instance for each input entry
109
- // name and variant are named accordingly from the input, instance will be hidden by default and lazy loading
110
- // is activated
111
- instances: safeGenData.map(instanceData => ({
112
- name: instanceData.name,
113
- variant: instanceData.name,
114
- lazy: true,
115
- parameters: {
116
- [Parameter.VISIBLE]: false,
117
- },
118
- })),
119
- },
120
- // variants definition is also mapped to the input array, using the name as key and url as glTF source
121
- // no elements are created here, since this should be done automatically from the system
122
- // => create one element which contains all root nodes of the imported 3d file (GLB or babylon)
123
- variants: safeGenData.reduce((accVariants, curVariant) => {
124
- accVariants[curVariant.name] = {
125
- glTF: curVariant.url,
126
- };
127
-
128
- return accVariants;
129
- }, {} as { [id: string]: StructureJson }),
130
- };
131
-
132
- return spec;
133
- }
134
-
135
- /**
136
- * Constructor
137
- */
138
- public constructor(public readonly canvas: HTMLCanvasElement, protected structureJson: string | StructureJson) {
139
- super();
140
- this._tagManager = new TagManager(this);
141
- this._nodeNamingStrategyHandler = (node, payload) => {
142
- const suffix: string[] = [];
143
- if (payload.variant.elements.length > 0) {
144
- const nodeElements = payload.variant.elements.filter(
145
- e => e.nodesFlat.filter(n => n.metadata?.cloneSource?.id === node.id).length > 0
146
- );
147
- if (nodeElements.length > 0) {
148
- suffix.push(payload.variantParameterizable.name);
149
- }
150
- }
151
- const variantInstances = this.variantInstances.allWithVariantName(payload.variant.name);
152
- if (variantInstances.length > 0) {
153
- suffix.push(payload.variantInstance.name);
154
- }
155
- const originalName = node.metadata?.originalName || node.name;
156
- return [originalName, ...suffix.filter(s => !!s)].join('.');
157
- };
158
- }
159
-
160
- /**
161
- * Gets the Babylon.js Scene that is attached to the instance.
162
- *
163
- * @throws Error if the `scene` has not been initialized.
164
- */
165
- get scene(): Scene {
166
- if (!this._scene) {
167
- throw new Error(`Scene has not been initialized.`);
168
- }
169
- return this._scene;
170
- }
171
-
172
- /**
173
- * Gets the {@link SceneManager} attached to the viewer.
174
- *
175
- * @throws Error if the {@link SceneManager} has not been initialized.
176
- */
177
- get sceneManager(): SceneManager {
178
- if (!this._sceneManager) {
179
- throw new Error(`SceneManager has not been initialized.`);
180
- }
181
- return this._sceneManager;
182
- }
183
-
184
- /**
185
- * Gets the {@link GltfExportManager} attached to the viewer.
186
- *
187
- * @throws Error if the {@link GltfExportManager} has not been initialized.
188
- */
189
- get gltfExportManager(): GltfExportManager {
190
- if (!this._gltfExportManager) {
191
- throw new Error(`GltfExportManager has not been initialized.`);
192
- }
193
- return this._gltfExportManager;
194
- }
195
-
196
- /**
197
- * Gets the Babylon.js Engine that is attached to the viewer.
198
- */
199
- get engine(): Engine {
200
- return this.scene.getEngine();
201
- }
202
-
203
- /**
204
- * Gets the {@link VariantInstanceManager} attached to the viewer.
205
- *
206
- * @throws Error if the {@link VariantInstanceManager} has not been initialized.
207
- */
208
- get variantInstances(): VariantInstanceManager {
209
- if (!this._variantInstances) {
210
- throw Error(`There is no variantInstanceManager.`);
211
- }
212
- return this._variantInstances;
213
- }
214
-
215
- /**
216
- * Gets the {@link AnimationManager} attached to the viewer.
217
- *
218
- * @throws Error if the {@link AnimationManager} has not been initialized.
219
- */
220
- get animationManager(): AnimationManager {
221
- if (!this._animationManager) {
222
- throw new Error(`There is no animationManager instance.`);
223
- }
224
- return this._animationManager;
225
- }
226
-
227
- get tagManager(): TagManager {
228
- if (!this._tagManager) {
229
- throw new Error(`There is no tagManager instance.`);
230
- }
231
- return this._tagManager;
232
- }
233
-
234
- /**
235
- * Gets the `cloneMaterialsOnMutation` flag, as defined in the spec
236
- */
237
- get cloneMaterialsOnMutation(): boolean {
238
- return this._cloneMaterialsOnMutation;
239
- }
240
-
241
- /**
242
- * Gets the strategy handler for naming cloned nodes.
243
- */
244
- get nodeNamingStrategyHandler(): NodeNamingStrategyHandler {
245
- if (!this._nodeNamingStrategyHandler) {
246
- throw new Error(`The NodeNamingStrategyHandler has not been registered yet.`);
247
- }
248
- return this._nodeNamingStrategyHandler;
249
- }
250
-
251
- /**
252
- * Sets the strategy handler for naming cloned nodes.\
253
- * Check the docs of the tag managers [renaming](https://3dviewer.docs.combeenation.com/pages/documentation/Tag-Manager.html#uniqueness-of-node-and-tag-names)
254
- * chapter for further details.
255
- */
256
- set nodeNamingStrategyHandler(value: NodeNamingStrategyHandler) {
257
- if (!value || typeof value !== 'function') {
258
- throw new Error(`The NodeNamingStrategyHandler is not a callable function.`);
259
- }
260
- this._nodeNamingStrategyHandler = value;
261
- }
262
-
263
- /**
264
- * Starts the application. This will
265
- * * load the given "index" JSON file
266
- * * setup the scene with the "scene" JSON file
267
- * * create an (optional) default setup with different variant settings
268
- * * sets up resizing by attaching a debounced version of {@link resize}
269
- *
270
- * @throws Error if any of the files is not found/valid
271
- *
272
- * @emits {@link Event.BOOTSTRAP_START}
273
- * @emits {@link Event.BOOTSTRAP_END}
274
- */
275
- public async bootstrap(tagManagerParameterValues?: TagManagerParameterValue[]): Promise<Viewer> {
276
- this.broadcastEvent(Event.BOOTSTRAP_START, this);
277
- let indexJson;
278
- if (isString(this.structureJson)) {
279
- indexJson = await loadJson<StructureJson>(this.structureJson);
280
- } else {
281
- indexJson = this.structureJson;
282
- }
283
- if (!indexJson.scene) {
284
- throw new Error(`No "scene" property found for bootstrapping.`);
285
- }
286
- // fill spec store
287
- SpecStorage.createFromSpec(indexJson);
288
- // init custom loader plugin
289
- this.initCbnBabylonLoaderPlugin();
290
- // load scene
291
- if (isString(indexJson.scene)) {
292
- const sceneJson = await loadJson<SceneJson>(indexJson.scene);
293
- indexJson.scene = sceneJson;
294
- }
295
- this._scene = await this.initScene();
296
- // create instance manager
297
- this._variantInstances = await VariantInstanceManager.create(this);
298
- // create optional default instances
299
- if (indexJson.setup) {
300
- if (isString(indexJson.setup)) {
301
- const setupJson = await loadJson<SetupJson>(indexJson.setup);
302
- indexJson.setup = setupJson;
303
- }
304
- await this.createVariantInstances(tagManagerParameterValues);
305
- }
306
- this.broadcastEvent(Event.VARIANT_INSTANCES_READY, this);
307
- // create gltf export manager
308
- this._gltfExportManager = await GltfExportManager.create(this);
309
- // resize handler
310
- window.addEventListener('resize', debounce(this.resize.bind(this), 100));
311
- // wait until scene is completely ready
312
- await this.scene.whenReadyAsync();
313
- // event broadcasting
314
- this.broadcastEvent(Event.BOOTSTRAP_END, this);
315
- // render loop
316
- this.engine.runRenderLoop(() => {
317
- if (!this._isRenderLoopPaused) this.scene.render();
318
- });
319
- return this;
320
- }
321
-
322
- /**
323
- * Enables the Babylon.js [Inspector](https://doc.babylonjs.com/toolsAndResources/tools/inspector).\
324
- * Due to the additional size of the inspector, the CDN version is used instead of shipping the required code with
325
- * the viewer.\
326
- * This means that the code will be downloaded only when needed and calling `enableDebugLayer` can take a little while
327
- * depending on your internet connection etc.
328
- */
329
- public async enableDebugLayer(options?: IInspectorOptions) {
330
- if (!this._inspectorLoaded) {
331
- // CDN version of inspector requires the Babylon.js core to be available as CDN module as well
332
- await loadJavascript('https://cdn.jsdelivr.net/npm/babylonjs@5.6.0/babylon.min.js');
333
-
334
- DebugLayer.InspectorURL =
335
- 'https://cdn.jsdelivr.net/npm/babylonjs-inspector@5.6.0/babylon.inspector.bundle.max.min.js';
336
-
337
- this._inspectorLoaded = true;
338
- }
339
-
340
- await this.scene.debugLayer.show(options);
341
- }
342
-
343
- /**
344
- * Destroys all registered {@link VariantInstance}s that are registered
345
- */
346
- public destroyVariantInstances(): Viewer {
347
- this.variantInstances.all.forEach(variantInstance => {
348
- this.variantInstances.destroy(variantInstance.name);
349
- });
350
- return this;
351
- }
352
-
353
- /**
354
- * Trigger a resize event for the `Engine`
355
- */
356
- public resize(): Viewer {
357
- this.engine.resize();
358
- return this;
359
- }
360
-
361
- /**
362
- * A convenience method for directly getting a Node from a {@link VariantInstance} and an {@link Element} by its
363
- * {@link DottedPath}s.
364
- */
365
- public async getNode(
366
- variantInstanceName: string,
367
- elementDottedPath: DottedPathArgument,
368
- nodeDottedPath: DottedPathArgument
369
- ): Promise<TransformNode> {
370
- const variantInstance = await this.variantInstances.get(variantInstanceName);
371
- return variantInstance.getNode(elementDottedPath, nodeDottedPath);
372
- }
373
-
374
- /**
375
- * A convenience method for directly getting a Node from a {@link VariantInstance} and an {@link Element} by its
376
- * {@link DottedPath}s.
377
- */
378
- public async getMesh(
379
- variantInstanceName: string,
380
- elementDottedPath: DottedPathArgument,
381
- meshDottedPath: DottedPathArgument
382
- ): Promise<Mesh | null> {
383
- const variantInstance = await this.variantInstances.get(variantInstanceName);
384
- return variantInstance.getMesh(elementDottedPath, meshDottedPath);
385
- }
386
-
387
- /**
388
- * Switches the camera
389
- *
390
- * @emits {@link Event.CAMERA_SWITCHED}
391
- */
392
- public switchCamera(newCamera: string, reset: boolean = true): Viewer {
393
- const camera = this.scene.getCameraByName(newCamera);
394
- if (camera) {
395
- const activeCamera = this.scene.activeCamera;
396
- if (activeCamera) {
397
- activeCamera.detachControl(this.engine.getRenderingCanvas()!);
398
- }
399
- if (reset) {
400
- camera.restoreState();
401
- }
402
- this.scene.setActiveCameraByName(newCamera);
403
- camera.attachControl(this.engine.getRenderingCanvas()!);
404
- this.broadcastEvent(Event.CAMERA_SWITCHED, camera);
405
- } else {
406
- throw new Error(`Given camera "${newCamera}" not found.`);
407
- }
408
- // TODO: put traceable observers to new camera (@see element)
409
- return this;
410
- }
411
-
412
- /**
413
- * Moves or animates the active camera to given `placement`.
414
- */
415
- public async moveActiveCameraTo(
416
- placement: string | PlacementDefinition,
417
- animation?: string | AnimationDefinition
418
- ): Promise<AnimationInterface> {
419
- return this.animationManager.animateToPlacement(this.sceneManager.activeCamera, placement, animation);
420
- }
421
-
422
- /**
423
- * Takes a sceenshot the the current scene. The result is a string containing a base64 encoded image
424
- */
425
- public screenshot(settings?: ScreenshotSettings): Promise<string> {
426
- return new Promise((resolve, reject) => {
427
- if (!this.engine) {
428
- return reject('Engine is null');
429
- }
430
- if (!this.scene) {
431
- return reject('Scene is null');
432
- }
433
- this.scene.render(); // in combination with a render target, we need to refresh the scene manually to get the latest view
434
- ScreenshotTools.CreateScreenshotUsingRenderTarget(
435
- this.engine,
436
- this.sceneManager.activeCamera,
437
- settings?.size ?? { width: this.canvas.clientWidth, height: this.canvas.clientHeight },
438
- resolve,
439
- settings?.mimeType ?? 'image/png',
440
- settings?.samples ?? 1,
441
- settings?.antialiasing ?? false,
442
- settings?.fileName ?? 'screenshot.png',
443
- settings?.renderSprites ?? false
444
- );
445
- });
446
- }
447
-
448
- /**
449
- * Checks whether the browser is capable of handling XR.
450
- */
451
- public async isBrowserARCapable(): Promise<boolean> {
452
- return await WebXRSessionManager.IsSessionSupportedAsync('immersive-ar');
453
- }
454
-
455
- /**
456
- * Calculates the bounding box from all visible meshes on the scene.
457
- */
458
- public async calculateBoundingBox(excludeGeometry?: ExcludedGeometryList): Promise<Mesh> {
459
- if (this.scene.meshes.length === 0) {
460
- throw new Error('There are currently no meshes on the scene.');
461
- }
462
- this.scene.render(); // CB-6062: workaround for BoundingBox not respecting render loop
463
- const { max, min } = this.scene.meshes
464
- .filter(mesh => {
465
- const isEnabled = mesh.isEnabled();
466
- // ignore the existing bounding box mesh for calculating the current one
467
- const isNotBBoxMesh = Viewer.BOUNDING_BOX_NAME !== mesh.id;
468
- // ignore meshes with invalid bounding infos
469
- const hasValidBBoxInfo = mesh.getBoundingInfo().boundingSphere.radius > 0;
470
- // ignore excluded meshes
471
- const isExcluded = excludeGeometry ? isMeshIncludedInExclusionList(mesh as Mesh, excludeGeometry) : false;
472
- return isEnabled && isNotBBoxMesh && hasValidBBoxInfo && !isExcluded;
473
- })
474
- .reduce(
475
- (accBBoxMinMax, curMesh, idx) => {
476
- const bBox = curMesh.getBoundingInfo().boundingBox;
477
- // use the first entry in the array as default value and get the resulting maximum/minimum values
478
- const max = idx === 0 ? bBox.maximumWorld : Vector3.Maximize(accBBoxMinMax.max, bBox.maximumWorld);
479
- const min = idx === 0 ? bBox.minimumWorld : Vector3.Minimize(accBBoxMinMax.min, bBox.minimumWorld);
480
- return { max, min };
481
- },
482
- { max: new Vector3(), min: new Vector3() }
483
- );
484
-
485
- let boundingBox = this.scene.getMeshByName(Viewer.BOUNDING_BOX_NAME) as Mesh;
486
- if (!boundingBox) {
487
- boundingBox = new Mesh(Viewer.BOUNDING_BOX_NAME, this.scene);
488
- }
489
- boundingBox.setBoundingInfo(new BoundingInfo(min, max));
490
- return boundingBox;
491
- }
492
-
493
- /**
494
- * Focuses the camera to see every visible mesh in scene and tries to optimize wheel precision and panning
495
- */
496
- public async autofocusActiveCamera(settings?: AutofocusSettings) {
497
- // first check some preconditions
498
- const activeCamera = this.scene.activeCamera;
499
- if (!activeCamera) {
500
- throw new Error('No active camera found when using autofocus feature.');
501
- }
502
- if (!(activeCamera instanceof ArcRotateCamera)) {
503
- const cameraClsName = activeCamera.getClassName();
504
- throw new Error(`Camera of type "${cameraClsName}" is not implemented yet to use autofocus feature.`);
505
- }
506
-
507
- let exclude = settings?.exclude || [];
508
-
509
- // Exclude shown photo dome or environment helper from bounding box calculation
510
- const photoDome = this.scene.getNodeByName(backgroundDomeName) as undefined | PhotoDome;
511
- const photoDomeMeshes = photoDome?.getChildMeshes();
512
- if (photoDomeMeshes?.length) {
513
- exclude = [...exclude, ...photoDomeMeshes];
514
- }
515
-
516
- const envHelper = this.scene.metadata?.[envHelperMetadataName] as undefined | EnvironmentHelper;
517
- if (envHelper?.rootMesh) {
518
- exclude = [...exclude, envHelper.rootMesh];
519
- }
520
-
521
- // get bounding box of all visible meshes, this is the base for the autofocus algorithm
522
- const boundingBox = await this.calculateBoundingBox(exclude);
523
-
524
- const radius = boundingBox.getBoundingInfo().boundingSphere.radius;
525
- const center = boundingBox.getBoundingInfo().boundingSphere.center;
526
- const diameter = radius * 2;
527
-
528
- // set lower radius limit on edge of bounding sphere to make sure that we can't dive into the meshes
529
- activeCamera.lowerRadiusLimit = radius;
530
-
531
- // additional settings
532
- // constants for division are taken directly from Babylon.js repository
533
- activeCamera.minZ = Math.min(radius / Viewer._autofocusConstants.minZ, 1);
534
- if (settings?.adjustWheelPrecision !== false) {
535
- activeCamera.wheelPrecision = Viewer._autofocusConstants.wheelPrecision / radius;
536
- }
537
- if (settings?.adjustPanningSensibility !== false) {
538
- activeCamera.panningSensibility = Viewer._autofocusConstants.panningSensibility / diameter;
539
- }
540
- if (settings?.adjustPinchPrecision !== false) {
541
- activeCamera.pinchPrecision = Viewer._autofocusConstants.pinchPrecision / radius;
542
- }
543
-
544
- const radiusFactor = settings?.radiusFactor ?? 1.5;
545
- const alpha = (settings?.alpha ?? -90) * (Math.PI / 180);
546
- const beta = (settings?.beta ?? 90) * (Math.PI / 180);
547
-
548
- const newCameraPosition = {
549
- alpha: alpha,
550
- beta: beta,
551
- // this calculation is a bit "simplified", as it doesn't consider the viewport ratio or the frustum angle
552
- // but it's also done this way in the Babylon.js repository, so it should be fine for us
553
- radius: diameter * radiusFactor,
554
- target: center,
555
- };
556
-
557
- await this.animationManager.animateToPlacement(activeCamera, newCameraPosition, settings?.animation);
558
- }
559
-
560
- /**
561
- * Resets everything by calling {@link destroy} to clear all references and {@link bootstrap} to setup a clean
562
- * environment
563
- */
564
- public async reset(tagManagerParameterValues: TagManagerParameterValue[]): Promise<Viewer> {
565
- await this.destroy();
566
- return this.bootstrap(tagManagerParameterValues);
567
- }
568
-
569
- /**
570
- * Destroys
571
- *
572
- * * all {@link VariantInstance}s using {@link destroyVariantInstances}
573
- * * calling `dispose` on the `Engine` and `Scene`
574
- */
575
- public destroy(): Viewer {
576
- this.destroyVariantInstances();
577
- this.scene.dispose();
578
- SpecStorage.destroy();
579
- return this;
580
- }
581
-
582
- /**
583
- * Show coordinate system with given dimension (for debugging purpose).
584
- */
585
- public showWorldCoordinates(dimension: number) {
586
- const scene = this.scene;
587
- const makeTextPlane = function (text: string, color: string, size: number) {
588
- const dynamicTexture = new DynamicTexture('DynamicTexture', 50, scene, true);
589
- dynamicTexture.hasAlpha = true;
590
- dynamicTexture.drawText(text, 5, 40, 'bold 36px Arial', color, 'transparent', true);
591
- const plane = Mesh.CreatePlane('TextPlane', size, scene, true);
592
- plane.material = new StandardMaterial('TextPlaneMaterial', scene);
593
- plane.material.backFaceCulling = false;
594
- (plane.material as StandardMaterial).specularColor = new Color3(0, 0, 0);
595
- (plane.material as StandardMaterial).diffuseTexture = dynamicTexture;
596
- return plane;
597
- };
598
-
599
- const axisX = Mesh.CreateLines(
600
- 'axisX',
601
- [
602
- Vector3.Zero(),
603
- new Vector3(dimension, 0, 0),
604
- new Vector3(dimension * 0.95, 0.05 * dimension, 0),
605
- new Vector3(dimension, 0, 0),
606
- new Vector3(dimension * 0.95, -0.05 * dimension, 0),
607
- ],
608
- scene,
609
- false
610
- );
611
- axisX.color = new Color3(1, 0, 0);
612
- const xChar = makeTextPlane('X', 'red', dimension / 10);
613
- xChar.position = new Vector3(0.9 * dimension, -0.05 * dimension, 0);
614
- const axisY = Mesh.CreateLines(
615
- 'axisY',
616
- [
617
- Vector3.Zero(),
618
- new Vector3(0, dimension, 0),
619
- new Vector3(-0.05 * dimension, dimension * 0.95, 0),
620
- new Vector3(0, dimension, 0),
621
- new Vector3(0.05 * dimension, dimension * 0.95, 0),
622
- ],
623
- scene,
624
- false
625
- );
626
- axisY.color = new Color3(0, 1, 0);
627
- const yChar = makeTextPlane('Y', 'green', dimension / 10);
628
- yChar.position = new Vector3(0, 0.9 * dimension, -0.05 * dimension);
629
- const axisZ = Mesh.CreateLines(
630
- 'axisZ',
631
- [
632
- Vector3.Zero(),
633
- new Vector3(0, 0, dimension),
634
- new Vector3(0, -0.05 * dimension, dimension * 0.95),
635
- new Vector3(0, 0, dimension),
636
- new Vector3(0, 0.05 * dimension, dimension * 0.95),
637
- ],
638
- scene,
639
- false
640
- );
641
- axisZ.color = new Color3(0, 0, 1);
642
- const zChar = makeTextPlane('Z', 'blue', dimension / 10);
643
- zChar.position = new Vector3(0, 0.05 * dimension, 0.9 * dimension);
644
- }
645
-
646
- /**
647
- * Pause render loop.
648
- */
649
- public pauseRendering() {
650
- this._isRenderLoopPaused = true;
651
- }
652
-
653
- /**
654
- * Resume render loop when paused.
655
- */
656
- public resumeRendering() {
657
- this._isRenderLoopPaused = false;
658
- }
659
-
660
- /**
661
- * @emits {@link Event.SCENE_PROCESSING_START}
662
- * @emits {@link Event.SCENE_PROCESSING_END}
663
- */
664
- protected async initScene(): Promise<Scene> {
665
- const sceneJson = SpecStorage.get<SceneJson>('scene');
666
- this.broadcastEvent(Event.SCENE_PROCESSING_START, sceneJson);
667
- const engine = new Engine(
668
- this.canvas as HTMLCanvasElement,
669
- sceneJson.engine?.antialiasing ?? false,
670
- sceneJson.engine?.options
671
- );
672
- const scene = await sceneSetup(engine, sceneJson);
673
- if (sceneJson.meshPicking) {
674
- new HighlightLayer('default', scene);
675
- scene.onPointerPick = (pointerEvent: IPointerEvent, pickInfo: PickingInfo) => {
676
- if (!pickInfo.hit) {
677
- return;
678
- }
679
- const mesh = pickInfo.pickedMesh;
680
- this.broadcastEvent(Event.MESH_PICKED, mesh, mesh?.metadata.element, mesh?.metadata.variant);
681
- if (mesh?.metadata.element) {
682
- this.broadcastEvent(Event.ELEMENT_PICKED, mesh.metadata.element);
683
- }
684
- if (mesh?.metadata.variant) {
685
- if (mesh.metadata.variant.inheritedParameters[Parameter.HIGHLIGHT_ENABLED]) {
686
- mesh.metadata.variant.toggleHighlight();
687
- }
688
- this.broadcastEvent(Event.VARIANT_PICKED, mesh.metadata.variant);
689
- }
690
- };
691
- }
692
- this._sceneManager = await SceneManager.create(scene);
693
- this._animationManager = await AnimationManager.create(scene);
694
- if (sceneJson.cloneMaterialsOnMutation !== undefined) {
695
- this._cloneMaterialsOnMutation = sceneJson.cloneMaterialsOnMutation;
696
- }
697
- // register observers for tag manager
698
- this.tagManager.registerNewTransformNodeObservers(scene);
699
- this.broadcastEvent(Event.SCENE_PROCESSING_END, scene);
700
- return scene;
701
- }
702
-
703
- /**
704
- * Register custom file loader for babylon files which adds "missing-material" metadata to meshes which reference
705
- * materials that are not present in the `materials` section of the given babylon file.
706
- *
707
- * Required for babylon files & materials loaded from "Combeenation configurator assets".
708
- */
709
- protected initCbnBabylonLoaderPlugin() {
710
- const previousLoaderPlugin = SceneLoader.GetPluginForExtension('babylon') as ISceneLoaderPlugin;
711
- const customLoaderPlugin = getCustomCbnBabylonLoaderPlugin(previousLoaderPlugin);
712
- SceneLoader.RegisterPlugin(customLoaderPlugin);
713
- }
714
-
715
- /**
716
- * Batch creation of multiple {@link VariantInstance} objects with a {@link SetupJson} object passed
717
- */
718
- protected async createVariantInstances(
719
- tagManagerParameterValues?: TagManagerParameterValue[]
720
- ): Promise<VariantInstance[]> {
721
- const setupJson = SpecStorage.get<SetupJson>('setup');
722
- const instances: VariantInstance[] = [];
723
- for (const instanceDefinition of setupJson.instances) {
724
- if (isArray(tagManagerParameterValues)) {
725
- instanceDefinition.tagManagerParameterValues = tagManagerParameterValues;
726
- }
727
- // don't create the instance right away if `lazy` is set, register it for later creation (on first usage) instead
728
- // however if the variant should be `visible` by default, `lazy` loading doesn't make sense and should therefore
729
- // be overruled by the `visible` flag
730
- if (instanceDefinition.lazy && instanceDefinition.parameters?.visible !== true) {
731
- this.variantInstances.register(instanceDefinition);
732
- continue;
733
- }
734
- const instance = await this.variantInstances.create(
735
- instanceDefinition.variant,
736
- instanceDefinition.name,
737
- instanceDefinition.parameters,
738
- instanceDefinition.tagManagerParameterValues
739
- );
740
- instances.push(instance);
741
- }
742
- return instances;
743
- }
744
- }
1
+ import buildInfo from '../../buildinfo.json';
2
+ import { sceneSetup } from '../internal/sceneSetup';
3
+ import { AnimationManager } from '../manager/animationManager';
4
+ import { GltfExportManager } from '../manager/gltfExportManager';
5
+ import { SceneManager } from '../manager/sceneManager';
6
+ import { TagManager } from '../manager/tagManager';
7
+ import { VariantInstanceManager } from '../manager/variantInstanceManager';
8
+ import { SpecStorage } from '../store/specStorage';
9
+ import { backgroundDomeName, envHelperMetadataName } from '../util/babylonHelper';
10
+ import { debounce, loadJavascript, loadJson } from '../util/resourceHelper';
11
+ import { getCustomCbnBabylonLoaderPlugin } from '../util/sceneLoaderHelper';
12
+ import { replaceDots } from '../util/stringHelper';
13
+ import { isMeshIncludedInExclusionList } from '../util/structureHelper';
14
+ import { AnimationInterface } from './animationInterface';
15
+ import { Event } from './event';
16
+ import { EventBroadcaster } from './eventBroadcaster';
17
+ import { Parameter } from './parameter';
18
+ import { VariantInstance } from './variantInstance';
19
+ import { ArcRotateCamera } from '@babylonjs/core/Cameras/arcRotateCamera';
20
+ import { PickingInfo } from '@babylonjs/core/Collisions/pickingInfo';
21
+ import { BoundingInfo } from '@babylonjs/core/Culling/boundingInfo';
22
+ import { DebugLayer } from '@babylonjs/core/Debug/debugLayer';
23
+ import { Engine } from '@babylonjs/core/Engines/engine';
24
+ import { IPointerEvent } from '@babylonjs/core/Events/deviceInputEvents';
25
+ import { EnvironmentHelper } from '@babylonjs/core/Helpers/environmentHelper';
26
+ import { PhotoDome } from '@babylonjs/core/Helpers/photoDome';
27
+ import { HighlightLayer } from '@babylonjs/core/Layers/highlightLayer';
28
+ import { ISceneLoaderPlugin, SceneLoader } from '@babylonjs/core/Loading/sceneLoader';
29
+ import { DynamicTexture } from '@babylonjs/core/Materials/Textures/dynamicTexture';
30
+ import { StandardMaterial } from '@babylonjs/core/Materials/standardMaterial';
31
+ import { Color3 } from '@babylonjs/core/Maths/math.color';
32
+ import { Vector3 } from '@babylonjs/core/Maths/math.vector';
33
+ import { Mesh } from '@babylonjs/core/Meshes/mesh';
34
+ import { ScreenshotTools } from '@babylonjs/core/Misc/screenshotTools';
35
+ import { WebXRSessionManager } from '@babylonjs/core/XR/webXRSessionManager';
36
+ import { Scene } from '@babylonjs/core/scene';
37
+ import { isArray, isString } from 'lodash-es';
38
+
39
+ /**
40
+ * The main exposed object. This is the entry point into the application
41
+ *
42
+ * ```js
43
+ * const canvas = document.getElementById( 'babylon-canvas' );
44
+ * const viewer = Viewer( canvas, '/path/to/index.json' );
45
+ * ```
46
+ * The class does nothing on its own and needs to {@link bootstrap}
47
+ */
48
+ export class Viewer extends EventBroadcaster {
49
+ public static readonly BOUNDING_BOX_NAME = '__bounding_box__';
50
+
51
+ protected _scene: Scene | null = null;
52
+
53
+ protected _animationManager: AnimationManager | null = null;
54
+
55
+ protected _sceneManager: SceneManager | null = null;
56
+
57
+ protected _tagManager: TagManager | null = null;
58
+
59
+ protected _gltfExportManager: GltfExportManager | null = null;
60
+
61
+ protected _variantInstances: VariantInstanceManager | null = null;
62
+
63
+ // defaults to `false` as material cloning should be the edge case
64
+ protected _cloneMaterialsOnMutation: boolean = false;
65
+
66
+ protected _isRenderLoopPaused: boolean = false;
67
+
68
+ protected _inspectorLoaded: boolean = false;
69
+
70
+ protected _nodeNamingStrategyHandler: NodeNamingStrategyHandler | null = null;
71
+
72
+ static version = buildInfo.version;
73
+
74
+ // these are constants for calculating the camera settings, depending on the bounding box size of the meshes to zoom
75
+ // the algorithms and constant values are directly taken from the Babylon.js repository
76
+ private static readonly _autofocusConstants = {
77
+ minZ: 100,
78
+ wheelPrecision: 100,
79
+ panningSensibility: 5000,
80
+ pinchPrecision: 200,
81
+ };
82
+
83
+ /**
84
+ * Help function for automatically creating a valid Spec from a list of variant names and dedicated GLB/babylon URLs.
85
+ * This data is most likely coming from babylon assets from the Combeenation system but other sources are also valid.
86
+ */
87
+ public static generateSpec(genData: SpecGenerationData[]): AutoSpecStructureJson {
88
+ // dots in the variant name indicate inheritance, but this should not be the case for an auto-generated spec
89
+ // therefore dots are exchanged with slashes
90
+ const safeGenData: SpecGenerationData[] = genData.map(data => ({ ...data, name: replaceDots(data.name) }));
91
+
92
+ const spec: AutoSpecStructureJson = {
93
+ // common scene settings as suggested in the viewer docs
94
+ scene: {
95
+ engine: {
96
+ antialiasing: true,
97
+ options: {
98
+ preserveDrawingBuffer: true,
99
+ stencil: true,
100
+ xrCompatible: false,
101
+ },
102
+ },
103
+ scene: {
104
+ globals: {},
105
+ },
106
+ },
107
+ setup: {
108
+ // create one instance for each input entry
109
+ // name and variant are named accordingly from the input, instance will be hidden by default and lazy loading
110
+ // is activated
111
+ instances: safeGenData.map(instanceData => ({
112
+ name: instanceData.name,
113
+ variant: instanceData.name,
114
+ lazy: true,
115
+ parameters: {
116
+ [Parameter.VISIBLE]: false,
117
+ },
118
+ })),
119
+ },
120
+ // variants definition is also mapped to the input array, using the name as key and url as glTF source
121
+ // no elements are created here, since this should be done automatically from the system
122
+ // => create one element which contains all root nodes of the imported 3d file (GLB or babylon)
123
+ variants: safeGenData.reduce((accVariants, curVariant) => {
124
+ accVariants[curVariant.name] = {
125
+ glTF: curVariant.url,
126
+ };
127
+
128
+ return accVariants;
129
+ }, {} as { [id: string]: StructureJson }),
130
+ };
131
+
132
+ return spec;
133
+ }
134
+
135
+ /**
136
+ * Constructor
137
+ */
138
+ public constructor(public readonly canvas: HTMLCanvasElement, protected structureJson: string | StructureJson) {
139
+ super();
140
+ this._tagManager = new TagManager(this);
141
+ this._nodeNamingStrategyHandler = (node, payload) => {
142
+ const suffix: string[] = [];
143
+ // (1) instance part
144
+ const variantInstances = this.variantInstances.allWithVariantName(payload.variant.name);
145
+ if (variantInstances.length > 0) {
146
+ suffix.push(payload.variantInstance.name);
147
+ }
148
+ // (2) element part
149
+ if (payload.variant.elements.length > 0) {
150
+ const nodeElements = payload.variant.elements.filter(
151
+ e => e.nodesFlat.filter(n => n.metadata?.cloneSource?.id === node.id).length > 0
152
+ );
153
+ if (nodeElements.length > 0) {
154
+ suffix.push(payload.variantParameterizable.name);
155
+ }
156
+ }
157
+ // (3) merge together
158
+ const originalName = node.metadata?.originalName || node.name;
159
+ return [originalName, ...suffix.filter(s => !!s)].join('.');
160
+ };
161
+ }
162
+
163
+ /**
164
+ * Gets the Babylon.js Scene that is attached to the instance.
165
+ *
166
+ * @throws Error if the `scene` has not been initialized.
167
+ */
168
+ get scene(): Scene {
169
+ if (!this._scene) {
170
+ throw new Error(`Scene has not been initialized.`);
171
+ }
172
+ return this._scene;
173
+ }
174
+
175
+ /**
176
+ * Gets the {@link SceneManager} attached to the viewer.
177
+ *
178
+ * @throws Error if the {@link SceneManager} has not been initialized.
179
+ */
180
+ get sceneManager(): SceneManager {
181
+ if (!this._sceneManager) {
182
+ throw new Error(`SceneManager has not been initialized.`);
183
+ }
184
+ return this._sceneManager;
185
+ }
186
+
187
+ /**
188
+ * Gets the {@link GltfExportManager} attached to the viewer.
189
+ *
190
+ * @throws Error if the {@link GltfExportManager} has not been initialized.
191
+ */
192
+ get gltfExportManager(): GltfExportManager {
193
+ if (!this._gltfExportManager) {
194
+ throw new Error(`GltfExportManager has not been initialized.`);
195
+ }
196
+ return this._gltfExportManager;
197
+ }
198
+
199
+ /**
200
+ * Gets the Babylon.js Engine that is attached to the viewer.
201
+ */
202
+ get engine(): Engine {
203
+ return this.scene.getEngine();
204
+ }
205
+
206
+ /**
207
+ * Gets the {@link VariantInstanceManager} attached to the viewer.
208
+ *
209
+ * @throws Error if the {@link VariantInstanceManager} has not been initialized.
210
+ */
211
+ get variantInstances(): VariantInstanceManager {
212
+ if (!this._variantInstances) {
213
+ throw Error(`There is no variantInstanceManager.`);
214
+ }
215
+ return this._variantInstances;
216
+ }
217
+
218
+ /**
219
+ * Gets the {@link AnimationManager} attached to the viewer.
220
+ *
221
+ * @throws Error if the {@link AnimationManager} has not been initialized.
222
+ */
223
+ get animationManager(): AnimationManager {
224
+ if (!this._animationManager) {
225
+ throw new Error(`There is no animationManager instance.`);
226
+ }
227
+ return this._animationManager;
228
+ }
229
+
230
+ get tagManager(): TagManager {
231
+ if (!this._tagManager) {
232
+ throw new Error(`There is no tagManager instance.`);
233
+ }
234
+ return this._tagManager;
235
+ }
236
+
237
+ /**
238
+ * Gets the `cloneMaterialsOnMutation` flag, as defined in the spec
239
+ */
240
+ get cloneMaterialsOnMutation(): boolean {
241
+ return this._cloneMaterialsOnMutation;
242
+ }
243
+
244
+ /**
245
+ * Gets the strategy handler for naming cloned nodes.
246
+ */
247
+ get nodeNamingStrategyHandler(): NodeNamingStrategyHandler {
248
+ if (!this._nodeNamingStrategyHandler) {
249
+ throw new Error(`The NodeNamingStrategyHandler has not been registered yet.`);
250
+ }
251
+ return this._nodeNamingStrategyHandler;
252
+ }
253
+
254
+ /**
255
+ * Sets the strategy handler for naming cloned nodes.\
256
+ * Check the docs of the tag managers [renaming](https://3dviewer.docs.combeenation.com/pages/documentation/Tag-Manager.html#uniqueness-of-node-and-tag-names)
257
+ * chapter for further details.
258
+ */
259
+ set nodeNamingStrategyHandler(value: NodeNamingStrategyHandler) {
260
+ if (!value || typeof value !== 'function') {
261
+ throw new Error(`The NodeNamingStrategyHandler is not a callable function.`);
262
+ }
263
+ this._nodeNamingStrategyHandler = value;
264
+ }
265
+
266
+ /**
267
+ * Starts the application. This will
268
+ * * load the given "index" JSON file
269
+ * * setup the scene with the "scene" JSON file
270
+ * * create an (optional) default setup with different variant settings
271
+ * * sets up resizing by attaching a debounced version of {@link resize}
272
+ *
273
+ * @throws Error if any of the files is not found/valid
274
+ *
275
+ * @emits {@link Event.BOOTSTRAP_START}
276
+ * @emits {@link Event.BOOTSTRAP_END}
277
+ */
278
+ public async bootstrap(tagManagerParameterValues?: TagManagerParameterValue[]): Promise<Viewer> {
279
+ this.broadcastEvent(Event.BOOTSTRAP_START, this);
280
+ let indexJson;
281
+ if (isString(this.structureJson)) {
282
+ indexJson = await loadJson<StructureJson>(this.structureJson);
283
+ } else {
284
+ indexJson = this.structureJson;
285
+ }
286
+ if (!indexJson.scene) {
287
+ throw new Error(`No "scene" property found for bootstrapping.`);
288
+ }
289
+ // fill spec store
290
+ SpecStorage.createFromSpec(indexJson);
291
+ // init custom loader plugin
292
+ this.initCbnBabylonLoaderPlugin();
293
+ // load scene
294
+ if (isString(indexJson.scene)) {
295
+ const sceneJson = await loadJson<SceneJson>(indexJson.scene);
296
+ indexJson.scene = sceneJson;
297
+ }
298
+ this._scene = await this.initScene();
299
+ // create instance manager
300
+ this._variantInstances = await VariantInstanceManager.create(this);
301
+ // create optional default instances
302
+ if (indexJson.setup) {
303
+ if (isString(indexJson.setup)) {
304
+ const setupJson = await loadJson<SetupJson>(indexJson.setup);
305
+ indexJson.setup = setupJson;
306
+ }
307
+ this.printInstanceDefinitions(indexJson.setup);
308
+ await this.createVariantInstances(tagManagerParameterValues);
309
+ }
310
+ this.broadcastEvent(Event.VARIANT_INSTANCES_READY, this);
311
+ // create gltf export manager
312
+ this._gltfExportManager = await GltfExportManager.create(this);
313
+ // resize handler
314
+ window.addEventListener('resize', debounce(this.resize.bind(this), 100));
315
+ // wait until scene is completely ready
316
+ await this.scene.whenReadyAsync();
317
+ // event broadcasting
318
+ this.broadcastEvent(Event.BOOTSTRAP_END, this);
319
+ // render loop
320
+ this.engine.runRenderLoop(() => {
321
+ if (!this._isRenderLoopPaused) this.scene.render();
322
+ });
323
+ return this;
324
+ }
325
+
326
+ /**
327
+ * Enables the Babylon.js [Inspector](https://doc.babylonjs.com/toolsAndResources/tools/inspector).\
328
+ * Due to the additional size of the inspector, the CDN version is used instead of shipping the required code with
329
+ * the viewer.\
330
+ * This means that the code will be downloaded only when needed and calling `enableDebugLayer` can take a little while
331
+ * depending on your internet connection etc.
332
+ */
333
+ public async enableDebugLayer(options?: IInspectorOptions) {
334
+ if (!this._inspectorLoaded) {
335
+ // CDN version of inspector requires the Babylon.js core to be available as CDN module as well
336
+ await loadJavascript('https://cdn.jsdelivr.net/npm/babylonjs@5.6.0/babylon.min.js');
337
+
338
+ DebugLayer.InspectorURL =
339
+ 'https://cdn.jsdelivr.net/npm/babylonjs-inspector@5.6.0/babylon.inspector.bundle.max.min.js';
340
+
341
+ this._inspectorLoaded = true;
342
+ }
343
+
344
+ await this.scene.debugLayer.show(options);
345
+ }
346
+
347
+ /**
348
+ * Destroys all registered {@link VariantInstance}s that are registered
349
+ */
350
+ public destroyVariantInstances(): Viewer {
351
+ this.variantInstances.all.forEach(variantInstance => {
352
+ this.variantInstances.destroy(variantInstance.name);
353
+ });
354
+ return this;
355
+ }
356
+
357
+ /**
358
+ * Trigger a resize event for the `Engine`
359
+ */
360
+ public resize(): Viewer {
361
+ this.engine.resize();
362
+ return this;
363
+ }
364
+
365
+ /**
366
+ * A convenience method for directly getting a Node from a {@link VariantInstance} and an {@link Element} by its
367
+ * {@link DottedPath}s.
368
+ */
369
+ public async getNode(
370
+ variantInstanceName: string,
371
+ elementDottedPath: DottedPathArgument,
372
+ nodeDottedPath: DottedPathArgument
373
+ ): Promise<TransformNode> {
374
+ const variantInstance = await this.variantInstances.get(variantInstanceName);
375
+ return variantInstance.getNode(elementDottedPath, nodeDottedPath);
376
+ }
377
+
378
+ /**
379
+ * A convenience method for directly getting a Node from a {@link VariantInstance} and an {@link Element} by its
380
+ * {@link DottedPath}s.
381
+ */
382
+ public async getMesh(
383
+ variantInstanceName: string,
384
+ elementDottedPath: DottedPathArgument,
385
+ meshDottedPath: DottedPathArgument
386
+ ): Promise<Mesh | null> {
387
+ const variantInstance = await this.variantInstances.get(variantInstanceName);
388
+ return variantInstance.getMesh(elementDottedPath, meshDottedPath);
389
+ }
390
+
391
+ /**
392
+ * Switches the camera
393
+ *
394
+ * @emits {@link Event.CAMERA_SWITCHED}
395
+ */
396
+ public switchCamera(newCamera: string, reset: boolean = true): Viewer {
397
+ const camera = this.scene.getCameraByName(newCamera);
398
+ if (camera) {
399
+ const activeCamera = this.scene.activeCamera;
400
+ if (activeCamera) {
401
+ activeCamera.detachControl(this.engine.getRenderingCanvas()!);
402
+ }
403
+ if (reset) {
404
+ camera.restoreState();
405
+ }
406
+ this.scene.setActiveCameraByName(newCamera);
407
+ camera.attachControl(this.engine.getRenderingCanvas()!);
408
+ this.broadcastEvent(Event.CAMERA_SWITCHED, camera);
409
+ } else {
410
+ throw new Error(`Given camera "${newCamera}" not found.`);
411
+ }
412
+ // TODO: put traceable observers to new camera (@see element)
413
+ return this;
414
+ }
415
+
416
+ /**
417
+ * Moves or animates the active camera to given `placement`.
418
+ */
419
+ public async moveActiveCameraTo(
420
+ placement: string | PlacementDefinition,
421
+ animation?: string | AnimationDefinition
422
+ ): Promise<AnimationInterface> {
423
+ return this.animationManager.animateToPlacement(this.sceneManager.activeCamera, placement, animation);
424
+ }
425
+
426
+ /**
427
+ * Takes a sceenshot the the current scene. The result is a string containing a base64 encoded image
428
+ */
429
+ public screenshot(settings?: ScreenshotSettings): Promise<string> {
430
+ return new Promise((resolve, reject) => {
431
+ if (!this.engine) {
432
+ return reject('Engine is null');
433
+ }
434
+ if (!this.scene) {
435
+ return reject('Scene is null');
436
+ }
437
+ this.scene.render(); // in combination with a render target, we need to refresh the scene manually to get the latest view
438
+ ScreenshotTools.CreateScreenshotUsingRenderTarget(
439
+ this.engine,
440
+ this.sceneManager.activeCamera,
441
+ settings?.size ?? { width: this.canvas.clientWidth, height: this.canvas.clientHeight },
442
+ resolve,
443
+ settings?.mimeType ?? 'image/png',
444
+ settings?.samples ?? 1,
445
+ settings?.antialiasing ?? false,
446
+ settings?.fileName ?? 'screenshot.png',
447
+ settings?.renderSprites ?? false
448
+ );
449
+ });
450
+ }
451
+
452
+ /**
453
+ * Checks whether the browser is capable of handling XR.
454
+ */
455
+ public async isBrowserARCapable(): Promise<boolean> {
456
+ return await WebXRSessionManager.IsSessionSupportedAsync('immersive-ar');
457
+ }
458
+
459
+ /**
460
+ * Calculates the bounding box from all visible meshes on the scene.
461
+ */
462
+ public async calculateBoundingBox(excludeGeometry?: ExcludedGeometryList): Promise<Mesh> {
463
+ if (this.scene.meshes.length === 0) {
464
+ throw new Error('There are currently no meshes on the scene.');
465
+ }
466
+ this.scene.render(); // CB-6062: workaround for BoundingBox not respecting render loop
467
+ const { max, min } = this.scene.meshes
468
+ .filter(mesh => {
469
+ const isEnabled = mesh.isEnabled();
470
+ // ignore the existing bounding box mesh for calculating the current one
471
+ const isNotBBoxMesh = Viewer.BOUNDING_BOX_NAME !== mesh.id;
472
+ // ignore meshes with invalid bounding infos
473
+ const hasValidBBoxInfo = mesh.getBoundingInfo().boundingSphere.radius > 0;
474
+ // ignore meshes with infinite distance, typically these are sky boxes
475
+ const hasInfiniteDistance = mesh.infiniteDistance;
476
+ // ignore excluded meshes
477
+ const isExcluded = excludeGeometry ? isMeshIncludedInExclusionList(mesh as Mesh, excludeGeometry) : false;
478
+ return isEnabled && isNotBBoxMesh && hasValidBBoxInfo && !hasInfiniteDistance && !isExcluded;
479
+ })
480
+ .reduce(
481
+ (accBBoxMinMax, curMesh, idx) => {
482
+ const bBox = curMesh.getBoundingInfo().boundingBox;
483
+ // use the first entry in the array as default value and get the resulting maximum/minimum values
484
+ const max = idx === 0 ? bBox.maximumWorld : Vector3.Maximize(accBBoxMinMax.max, bBox.maximumWorld);
485
+ const min = idx === 0 ? bBox.minimumWorld : Vector3.Minimize(accBBoxMinMax.min, bBox.minimumWorld);
486
+ return { max, min };
487
+ },
488
+ { max: new Vector3(), min: new Vector3() }
489
+ );
490
+
491
+ let boundingBox = this.scene.getMeshByName(Viewer.BOUNDING_BOX_NAME) as Mesh;
492
+ if (!boundingBox) {
493
+ boundingBox = new Mesh(Viewer.BOUNDING_BOX_NAME, this.scene);
494
+ }
495
+ boundingBox.setBoundingInfo(new BoundingInfo(min, max));
496
+ return boundingBox;
497
+ }
498
+
499
+ /**
500
+ * Focuses the camera to see every visible mesh in scene and tries to optimize wheel precision and panning
501
+ */
502
+ public async autofocusActiveCamera(settings?: AutofocusSettings) {
503
+ // first check some preconditions
504
+ const activeCamera = this.scene.activeCamera;
505
+ if (!activeCamera) {
506
+ throw new Error('No active camera found when using autofocus feature.');
507
+ }
508
+ if (!(activeCamera instanceof ArcRotateCamera)) {
509
+ const cameraClsName = activeCamera.getClassName();
510
+ throw new Error(`Camera of type "${cameraClsName}" is not implemented yet to use autofocus feature.`);
511
+ }
512
+
513
+ let exclude = settings?.exclude || [];
514
+
515
+ // Exclude shown photo dome or environment helper from bounding box calculation
516
+ const photoDome = this.scene.getNodeByName(backgroundDomeName) as undefined | PhotoDome;
517
+ const photoDomeMeshes = photoDome?.getChildMeshes();
518
+ if (photoDomeMeshes?.length) {
519
+ exclude = [...exclude, ...photoDomeMeshes];
520
+ }
521
+
522
+ const envHelper = this.scene.metadata?.[envHelperMetadataName] as undefined | EnvironmentHelper;
523
+ if (envHelper?.rootMesh) {
524
+ exclude = [...exclude, envHelper.rootMesh];
525
+ }
526
+
527
+ // get bounding box of all visible meshes, this is the base for the autofocus algorithm
528
+ const boundingBox = await this.calculateBoundingBox(exclude);
529
+
530
+ const radius = boundingBox.getBoundingInfo().boundingSphere.radius;
531
+ const center = boundingBox.getBoundingInfo().boundingSphere.center;
532
+ const diameter = radius * 2;
533
+
534
+ // set lower radius limit on edge of bounding sphere to make sure that we can't dive into the meshes
535
+ activeCamera.lowerRadiusLimit = radius;
536
+
537
+ // additional settings
538
+ // constants for division are taken directly from Babylon.js repository
539
+ activeCamera.minZ = Math.min(radius / Viewer._autofocusConstants.minZ, 1);
540
+ if (settings?.adjustWheelPrecision !== false) {
541
+ activeCamera.wheelPrecision = Viewer._autofocusConstants.wheelPrecision / radius;
542
+ }
543
+ if (settings?.adjustPanningSensibility !== false) {
544
+ activeCamera.panningSensibility = Viewer._autofocusConstants.panningSensibility / diameter;
545
+ }
546
+ if (settings?.adjustPinchPrecision !== false) {
547
+ activeCamera.pinchPrecision = Viewer._autofocusConstants.pinchPrecision / radius;
548
+ }
549
+
550
+ const radiusFactor = settings?.radiusFactor ?? 1.5;
551
+ const alpha = (settings?.alpha ?? -90) * (Math.PI / 180);
552
+ const beta = (settings?.beta ?? 90) * (Math.PI / 180);
553
+
554
+ const newCameraPosition = {
555
+ alpha: alpha,
556
+ beta: beta,
557
+ // this calculation is a bit "simplified", as it doesn't consider the viewport ratio or the frustum angle
558
+ // but it's also done this way in the Babylon.js repository, so it should be fine for us
559
+ radius: diameter * radiusFactor,
560
+ target: center,
561
+ };
562
+
563
+ await this.animationManager.animateToPlacement(activeCamera, newCameraPosition, settings?.animation);
564
+ }
565
+
566
+ /**
567
+ * Resets everything by calling {@link destroy} to clear all references and {@link bootstrap} to setup a clean
568
+ * environment
569
+ */
570
+ public async reset(tagManagerParameterValues: TagManagerParameterValue[]): Promise<Viewer> {
571
+ await this.destroy();
572
+ return this.bootstrap(tagManagerParameterValues);
573
+ }
574
+
575
+ /**
576
+ * Destroys
577
+ *
578
+ * * all {@link VariantInstance}s using {@link destroyVariantInstances}
579
+ * * calling `dispose` on the `Engine` and `Scene`
580
+ */
581
+ public destroy(): Viewer {
582
+ this.destroyVariantInstances();
583
+ this.scene.dispose();
584
+ SpecStorage.destroy();
585
+ return this;
586
+ }
587
+
588
+ /**
589
+ * Show coordinate system with given dimension (for debugging purpose).
590
+ */
591
+ public showWorldCoordinates(dimension: number) {
592
+ const scene = this.scene;
593
+ const makeTextPlane = function (text: string, color: string, size: number) {
594
+ const dynamicTexture = new DynamicTexture('DynamicTexture', 50, scene, true);
595
+ dynamicTexture.hasAlpha = true;
596
+ dynamicTexture.drawText(text, 5, 40, 'bold 36px Arial', color, 'transparent', true);
597
+ const plane = Mesh.CreatePlane('TextPlane', size, scene, true);
598
+ plane.material = new StandardMaterial('TextPlaneMaterial', scene);
599
+ plane.material.backFaceCulling = false;
600
+ (plane.material as StandardMaterial).specularColor = new Color3(0, 0, 0);
601
+ (plane.material as StandardMaterial).diffuseTexture = dynamicTexture;
602
+ return plane;
603
+ };
604
+
605
+ const axisX = Mesh.CreateLines(
606
+ 'axisX',
607
+ [
608
+ Vector3.Zero(),
609
+ new Vector3(dimension, 0, 0),
610
+ new Vector3(dimension * 0.95, 0.05 * dimension, 0),
611
+ new Vector3(dimension, 0, 0),
612
+ new Vector3(dimension * 0.95, -0.05 * dimension, 0),
613
+ ],
614
+ scene,
615
+ false
616
+ );
617
+ axisX.color = new Color3(1, 0, 0);
618
+ const xChar = makeTextPlane('X', 'red', dimension / 10);
619
+ xChar.position = new Vector3(0.9 * dimension, -0.05 * dimension, 0);
620
+ const axisY = Mesh.CreateLines(
621
+ 'axisY',
622
+ [
623
+ Vector3.Zero(),
624
+ new Vector3(0, dimension, 0),
625
+ new Vector3(-0.05 * dimension, dimension * 0.95, 0),
626
+ new Vector3(0, dimension, 0),
627
+ new Vector3(0.05 * dimension, dimension * 0.95, 0),
628
+ ],
629
+ scene,
630
+ false
631
+ );
632
+ axisY.color = new Color3(0, 1, 0);
633
+ const yChar = makeTextPlane('Y', 'green', dimension / 10);
634
+ yChar.position = new Vector3(0, 0.9 * dimension, -0.05 * dimension);
635
+ const axisZ = Mesh.CreateLines(
636
+ 'axisZ',
637
+ [
638
+ Vector3.Zero(),
639
+ new Vector3(0, 0, dimension),
640
+ new Vector3(0, -0.05 * dimension, dimension * 0.95),
641
+ new Vector3(0, 0, dimension),
642
+ new Vector3(0, 0.05 * dimension, dimension * 0.95),
643
+ ],
644
+ scene,
645
+ false
646
+ );
647
+ axisZ.color = new Color3(0, 0, 1);
648
+ const zChar = makeTextPlane('Z', 'blue', dimension / 10);
649
+ zChar.position = new Vector3(0, 0.05 * dimension, 0.9 * dimension);
650
+ }
651
+
652
+ /**
653
+ * Pause render loop.
654
+ */
655
+ public pauseRendering() {
656
+ this._isRenderLoopPaused = true;
657
+ }
658
+
659
+ /**
660
+ * Resume render loop when paused.
661
+ */
662
+ public resumeRendering() {
663
+ this._isRenderLoopPaused = false;
664
+ }
665
+
666
+ /**
667
+ * Prints defined instances from SetupJson to the console.
668
+ */
669
+ protected printInstanceDefinitions(setupJson: SetupJson) {
670
+ console.info(
671
+ 'The following variant instances have been read from the given spec: ',
672
+ setupJson.instances.map(o => o.name)
673
+ );
674
+ }
675
+
676
+ /**
677
+ * @emits {@link Event.SCENE_PROCESSING_START}
678
+ * @emits {@link Event.SCENE_PROCESSING_END}
679
+ */
680
+ protected async initScene(): Promise<Scene> {
681
+ const sceneJson = SpecStorage.get<SceneJson>('scene');
682
+ this.broadcastEvent(Event.SCENE_PROCESSING_START, sceneJson);
683
+ const engine = new Engine(
684
+ this.canvas as HTMLCanvasElement,
685
+ sceneJson.engine?.antialiasing ?? false,
686
+ sceneJson.engine?.options
687
+ );
688
+ const scene = await sceneSetup(engine, sceneJson);
689
+ if (sceneJson.meshPicking) {
690
+ new HighlightLayer('default', scene);
691
+ scene.onPointerPick = (pointerEvent: IPointerEvent, pickInfo: PickingInfo) => {
692
+ if (!pickInfo.hit) {
693
+ return;
694
+ }
695
+ const mesh = pickInfo.pickedMesh;
696
+ this.broadcastEvent(Event.MESH_PICKED, mesh, mesh?.metadata.element, mesh?.metadata.variant);
697
+ if (mesh?.metadata.element) {
698
+ this.broadcastEvent(Event.ELEMENT_PICKED, mesh.metadata.element);
699
+ }
700
+ if (mesh?.metadata.variant) {
701
+ if (mesh.metadata.variant.inheritedParameters[Parameter.HIGHLIGHT_ENABLED]) {
702
+ mesh.metadata.variant.toggleHighlight();
703
+ }
704
+ this.broadcastEvent(Event.VARIANT_PICKED, mesh.metadata.variant);
705
+ }
706
+ };
707
+ }
708
+ this._sceneManager = await SceneManager.create(scene);
709
+ this._animationManager = await AnimationManager.create(scene);
710
+ if (sceneJson.cloneMaterialsOnMutation !== undefined) {
711
+ this._cloneMaterialsOnMutation = sceneJson.cloneMaterialsOnMutation;
712
+ }
713
+ // register observers for tag manager
714
+ this.tagManager.registerNewTransformNodeObservers(scene);
715
+ this.broadcastEvent(Event.SCENE_PROCESSING_END, scene);
716
+ return scene;
717
+ }
718
+
719
+ /**
720
+ * Register custom file loader for babylon files which adds "missing-material" metadata to meshes which reference
721
+ * materials that are not present in the `materials` section of the given babylon file.
722
+ *
723
+ * Required for babylon files & materials loaded from "Combeenation configurator assets".
724
+ */
725
+ protected initCbnBabylonLoaderPlugin() {
726
+ const previousLoaderPlugin = SceneLoader.GetPluginForExtension('babylon') as ISceneLoaderPlugin;
727
+ const customLoaderPlugin = getCustomCbnBabylonLoaderPlugin(previousLoaderPlugin);
728
+ SceneLoader.RegisterPlugin(customLoaderPlugin);
729
+ }
730
+
731
+ /**
732
+ * Batch creation of multiple {@link VariantInstance} objects with a {@link SetupJson} object passed
733
+ */
734
+ protected async createVariantInstances(
735
+ tagManagerParameterValues?: TagManagerParameterValue[]
736
+ ): Promise<VariantInstance[]> {
737
+ const setupJson = SpecStorage.get<SetupJson>('setup');
738
+ const instances: VariantInstance[] = [];
739
+ for (const instanceDefinition of setupJson.instances) {
740
+ if (isArray(tagManagerParameterValues)) {
741
+ instanceDefinition.tagManagerParameterValues = tagManagerParameterValues;
742
+ }
743
+ // don't create the instance right away if `lazy` is set, register it for later creation (on first usage) instead
744
+ // however if the variant should be `visible` by default, `lazy` loading doesn't make sense and should therefore
745
+ // be overruled by the `visible` flag
746
+ if (instanceDefinition.lazy && instanceDefinition.parameters?.visible !== true) {
747
+ this.variantInstances.register(instanceDefinition);
748
+ continue;
749
+ }
750
+ const instance = await this.variantInstances.create(
751
+ instanceDefinition.variant,
752
+ instanceDefinition.name,
753
+ instanceDefinition.parameters,
754
+ instanceDefinition.tagManagerParameterValues
755
+ );
756
+ instances.push(instance);
757
+ }
758
+ return instances;
759
+ }
760
+ }