@combeenation/3d-viewer 12.4.1 → 12.4.3

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