@combeenation/3d-viewer 5.0.1 → 5.0.3-beta2

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 (109) hide show
  1. package/README.md +111 -114
  2. package/dist/lib-cjs/api/classes/animationInterface.d.ts +8 -8
  3. package/dist/lib-cjs/api/classes/animationInterface.js +1 -1
  4. package/dist/lib-cjs/api/classes/dottedPath.d.ts +79 -79
  5. package/dist/lib-cjs/api/classes/dottedPath.js +187 -187
  6. package/dist/lib-cjs/api/classes/element.d.ts +139 -139
  7. package/dist/lib-cjs/api/classes/element.js +794 -794
  8. package/dist/lib-cjs/api/classes/element.js.map +1 -1
  9. package/dist/lib-cjs/api/classes/elementParameterizable.d.ts +14 -0
  10. package/dist/lib-cjs/api/classes/elementParameterizable.js +135 -0
  11. package/dist/lib-cjs/api/classes/elementParameterizable.js.map +1 -0
  12. package/dist/lib-cjs/api/classes/event.d.ts +326 -326
  13. package/dist/lib-cjs/api/classes/event.js +371 -371
  14. package/dist/lib-cjs/api/classes/eventBroadcaster.d.ts +26 -26
  15. package/dist/lib-cjs/api/classes/eventBroadcaster.js +53 -53
  16. package/dist/lib-cjs/api/classes/parameter.d.ts +316 -259
  17. package/dist/lib-cjs/api/classes/parameter.js +451 -388
  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 +101 -101
  21. package/dist/lib-cjs/api/classes/parameterizable.d.ts +15 -15
  22. package/dist/lib-cjs/api/classes/parameterizable.js +149 -149
  23. package/dist/lib-cjs/api/classes/placementAnimation.d.ts +44 -44
  24. package/dist/lib-cjs/api/classes/placementAnimation.js +163 -163
  25. package/dist/lib-cjs/api/classes/variant.d.ts +234 -234
  26. package/dist/lib-cjs/api/classes/variant.js +1154 -1154
  27. package/dist/lib-cjs/api/classes/variantInstance.d.ts +45 -45
  28. package/dist/lib-cjs/api/classes/variantInstance.js +108 -108
  29. package/dist/lib-cjs/api/classes/variantParameterizable.d.ts +17 -17
  30. package/dist/lib-cjs/api/classes/variantParameterizable.js +99 -99
  31. package/dist/lib-cjs/api/classes/viewer.d.ts +177 -175
  32. package/dist/lib-cjs/api/classes/viewer.js +717 -701
  33. package/dist/lib-cjs/api/classes/viewer.js.map +1 -1
  34. package/dist/lib-cjs/api/classes/viewerLight.d.ts +66 -66
  35. package/dist/lib-cjs/api/classes/viewerLight.js +392 -392
  36. package/dist/lib-cjs/api/internal/debugViewer.d.ts +13 -13
  37. package/dist/lib-cjs/api/internal/debugViewer.js +86 -86
  38. package/dist/lib-cjs/api/internal/lensRendering.d.ts +8 -8
  39. package/dist/lib-cjs/api/internal/lensRendering.js +11 -11
  40. package/dist/lib-cjs/api/internal/sceneSetup.d.ts +13 -10
  41. package/dist/lib-cjs/api/internal/sceneSetup.js +238 -234
  42. package/dist/lib-cjs/api/internal/sceneSetup.js.map +1 -1
  43. package/dist/lib-cjs/api/manager/animationManager.d.ts +29 -29
  44. package/dist/lib-cjs/api/manager/animationManager.js +130 -130
  45. package/dist/lib-cjs/api/manager/gltfExportManager.d.ts +65 -65
  46. package/dist/lib-cjs/api/manager/gltfExportManager.js +223 -222
  47. package/dist/lib-cjs/api/manager/gltfExportManager.js.map +1 -1
  48. package/dist/lib-cjs/api/manager/sceneManager.d.ts +31 -32
  49. package/dist/lib-cjs/api/manager/sceneManager.js +153 -132
  50. package/dist/lib-cjs/api/manager/sceneManager.js.map +1 -1
  51. package/dist/lib-cjs/api/manager/variantInstanceManager.d.ts +92 -92
  52. package/dist/lib-cjs/api/manager/variantInstanceManager.js +335 -335
  53. package/dist/lib-cjs/api/store/specStorage.d.ts +24 -24
  54. package/dist/lib-cjs/api/store/specStorage.js +51 -51
  55. package/dist/lib-cjs/api/util/babylonHelper.d.ts +174 -166
  56. package/dist/lib-cjs/api/util/babylonHelper.js +575 -497
  57. package/dist/lib-cjs/api/util/babylonHelper.js.map +1 -1
  58. package/dist/lib-cjs/api/util/globalTypes.d.ts +366 -356
  59. package/dist/lib-cjs/api/util/globalTypes.js +1 -1
  60. package/dist/lib-cjs/api/util/resourceHelper.d.ts +58 -53
  61. package/dist/lib-cjs/api/util/resourceHelper.js +257 -242
  62. package/dist/lib-cjs/api/util/resourceHelper.js.map +1 -1
  63. package/dist/lib-cjs/api/util/stringHelper.d.ts +9 -9
  64. package/dist/lib-cjs/api/util/stringHelper.js +25 -25
  65. package/dist/lib-cjs/api/util/structureHelper.d.ts +9 -9
  66. package/dist/lib-cjs/api/util/structureHelper.js +48 -44
  67. package/dist/lib-cjs/api/util/structureHelper.js.map +1 -1
  68. package/dist/lib-cjs/buildinfo.json +3 -3
  69. package/dist/lib-cjs/index.d.ts +49 -49
  70. package/dist/lib-cjs/index.js +89 -89
  71. package/dist/webpack-stats.json +0 -0
  72. package/package.json +87 -87
  73. package/src/api/classes/animationInterface.ts +10 -10
  74. package/src/api/classes/dottedPath.ts +181 -181
  75. package/src/api/classes/element.ts +690 -692
  76. package/src/api/classes/event.ts +367 -367
  77. package/src/api/classes/eventBroadcaster.ts +52 -52
  78. package/src/api/classes/parameter.ts +474 -405
  79. package/src/api/classes/parameterObservable.ts +100 -100
  80. package/src/api/classes/parameterizable.ts +87 -87
  81. package/src/api/classes/placementAnimation.ts +160 -160
  82. package/src/api/classes/variant.ts +845 -845
  83. package/src/api/classes/variantInstance.ts +97 -97
  84. package/src/api/classes/variantParameterizable.ts +85 -85
  85. package/src/api/classes/viewer.ts +650 -624
  86. package/src/api/classes/viewerLight.ts +334 -334
  87. package/src/api/internal/debugViewer.ts +90 -90
  88. package/src/api/internal/lensRendering.ts +10 -10
  89. package/src/api/internal/sceneSetup.ts +204 -201
  90. package/src/api/manager/animationManager.ts +142 -142
  91. package/src/api/manager/gltfExportManager.ts +191 -191
  92. package/src/api/manager/sceneManager.ts +128 -102
  93. package/src/api/manager/variantInstanceManager.ts +265 -265
  94. package/src/api/store/specStorage.ts +51 -51
  95. package/src/api/util/babylonHelper.ts +628 -538
  96. package/src/api/util/globalTypes.ts +413 -402
  97. package/src/api/util/resourceHelper.ts +189 -173
  98. package/src/api/util/stringHelper.ts +23 -23
  99. package/src/api/util/structureHelper.ts +49 -43
  100. package/src/buildinfo.json +3 -3
  101. package/src/commonjs.tsconfig.json +10 -10
  102. package/src/declaration.tsconfig.json +8 -8
  103. package/src/dev.ts +42 -42
  104. package/src/es6.tsconfig.json +10 -10
  105. package/src/index.ts +94 -94
  106. package/src/pagesconfig.json +77 -73
  107. package/src/tsconfig.json +32 -32
  108. package/src/tsconfig.types.json +9 -9
  109. package/src/types.d.ts +3 -3
@@ -1,624 +1,650 @@
1
- import { AbstractMesh, IPointerEvent } from '@babylonjs/core';
2
- import { ArcRotateCamera } from '@babylonjs/core/Cameras/arcRotateCamera';
3
- import { PickingInfo } from '@babylonjs/core/Collisions/pickingInfo';
4
- import { BoundingInfo } from '@babylonjs/core/Culling/boundingInfo';
5
- import { Engine } from '@babylonjs/core/Engines/engine';
6
- import { HighlightLayer } from '@babylonjs/core/Layers/highlightLayer';
7
- import { StandardMaterial } from '@babylonjs/core/Materials/standardMaterial';
8
- import { DynamicTexture } from '@babylonjs/core/Materials/Textures/dynamicTexture';
9
- import { Color3 } from '@babylonjs/core/Maths/math.color';
10
- import { Vector3 } from '@babylonjs/core/Maths/math.vector';
11
- import { Mesh } from '@babylonjs/core/Meshes/mesh';
12
- import { ScreenshotTools } from '@babylonjs/core/Misc/screenshotTools';
13
- import { Scene } from '@babylonjs/core/scene';
14
- import { WebXRSessionManager } from '@babylonjs/core/XR/webXRSessionManager';
15
- import { isMeshIncludedInExclusionList } from '../util/structureHelper';
16
- import { isString } from 'lodash-es';
17
- import { version } from '../../buildinfo.json';
18
- import { sceneSetup } from '../internal/sceneSetup';
19
- import { AnimationManager } from '../manager/animationManager';
20
- import { GltfExportManager } from '../manager/gltfExportManager';
21
- import { SceneManager } from '../manager/sceneManager';
22
- import { VariantInstanceManager } from '../manager/variantInstanceManager';
23
- import { SpecStorage } from '../store/specStorage';
24
- import { debounce, loadJson } from '../util/resourceHelper';
25
- import { Event } from './event';
26
- import { EventBroadcaster } from './eventBroadcaster';
27
- import { Parameter } from './parameter';
28
- import { Variant } from './variant';
29
- import { VariantInstance } from './variantInstance';
30
-
31
- /**
32
- * The main exposed object. This is the entry point into the application
33
- *
34
- * ```js
35
- * const canvas = document.getElementById( 'babylon-canvas' );
36
- * const viewer = Viewer( canvas, '/path/to/index.json' );
37
- * ```
38
- * The class does nothing on its own and needs to {@link bootstrap}
39
- */
40
- export class Viewer extends EventBroadcaster {
41
- protected _scene: Scene | null = null;
42
-
43
- protected _animationManager: AnimationManager | null = null;
44
-
45
- protected _sceneManager: SceneManager | null = null;
46
-
47
- protected _gltfExportManager: GltfExportManager | null = null;
48
-
49
- protected _variantInstances: VariantInstanceManager | null = null;
50
-
51
- // default value is `true` ATM for compatibility reasons
52
- // in the future material cloning should be the edge case
53
- protected _cloneMaterialsOnMutation: boolean = true;
54
-
55
- protected _isRenderLoopPaused: boolean = false;
56
-
57
- static version = version;
58
-
59
- /**
60
- * Constructor
61
- */
62
- public constructor(public readonly canvas: HTMLCanvasElement, protected structureJson: string | StructureJson) {
63
- super();
64
- }
65
-
66
- /**
67
- * Gets the BabylonJS Scene that is attached to the instance.
68
- *
69
- * @throws Error if the `scene` has not been initialized.
70
- */
71
- get scene(): Scene {
72
- if (!this._scene) {
73
- throw new Error(`Scene has not been initialized.`);
74
- }
75
- return this._scene;
76
- }
77
-
78
- /**
79
- * Gets the {@link SceneManager} attached to the viewer.
80
- *
81
- * @throws Error if the {@link SceneManager} has not been initialized.
82
- */
83
- get sceneManager(): SceneManager {
84
- if (!this._sceneManager) {
85
- throw new Error(`SceneManager has not been initialized.`);
86
- }
87
- return this._sceneManager;
88
- }
89
-
90
- /**
91
- * Gets the {@link GltfExportManager} attached to the viewer.
92
- *
93
- * @throws Error if the {@link GltfExportManager} has not been initialized.
94
- */
95
- get gltfExportManager(): GltfExportManager {
96
- if (!this._gltfExportManager) {
97
- throw new Error(`GltfExportManager has not been initialized.`);
98
- }
99
- return this._gltfExportManager;
100
- }
101
-
102
- /**
103
- * Gets the BabylonJS Engine that is attached to the viewer.
104
- */
105
- get engine(): Engine {
106
- return this.scene.getEngine();
107
- }
108
-
109
- /**
110
- * Gets the {@link VariantInstanceManager} attached to the viewer.
111
- *
112
- * @throws Error if the {@link VariantInstanceManager} has not been initialized.
113
- */
114
- get variantInstances(): VariantInstanceManager {
115
- if (!this._variantInstances) {
116
- throw Error(`There is no variantInstanceManager.`);
117
- }
118
- return this._variantInstances;
119
- }
120
-
121
- /**
122
- * Gets the {@link AnimationManager} attached to the viewer.
123
- *
124
- * @throws Error if the {@link AnimationManager} has not been initialized.
125
- */
126
- get animationManager(): AnimationManager {
127
- if (!this._animationManager) {
128
- throw new Error(`There is no animationManager instance.`);
129
- }
130
- return this._animationManager;
131
- }
132
-
133
- /**
134
- * Gets the `cloneMaterialsOnMutation` flag, as defined in the spec
135
- */
136
- get cloneMaterialsOnMutation(): boolean {
137
- return this._cloneMaterialsOnMutation;
138
- }
139
-
140
- /**
141
- * Starts the application. This will
142
- * * load the given "index" JSON file
143
- * * setup the scene with the "scene" JSON file
144
- * * create an (optional) default setup with different variant settings
145
- * * sets up resizing by attaching a debounced version of {@link resize}
146
- *
147
- * @throws Error if any of the files is not found/valid
148
- *
149
- * @emits {@link Event.BOOTSTRAP_START}
150
- * @emits {@link Event.BOOTSTRAP_END}
151
- */
152
- public async bootstrap(): Promise<Viewer> {
153
- this.broadcastEvent(Event.BOOTSTRAP_START, this);
154
- let indexJson;
155
- if (isString(this.structureJson)) {
156
- indexJson = await loadJson<StructureJson>(this.structureJson);
157
- } else {
158
- indexJson = this.structureJson;
159
- }
160
- if (!indexJson.scene) {
161
- throw new Error(`No "scene" property found for bootstrapping.`);
162
- }
163
- // fill spec store
164
- SpecStorage.createFromSpec(indexJson);
165
- // load scene
166
- if (isString(indexJson.scene)) {
167
- const sceneJson = await loadJson<SceneJson>(indexJson.scene);
168
- indexJson.scene = sceneJson;
169
- }
170
- this._scene = await this.initScene();
171
- // create instance manager
172
- const rootVariant = await Variant.create('_', indexJson, this);
173
- this._variantInstances = await VariantInstanceManager.create(rootVariant);
174
- // create optional default instances
175
- if (indexJson.setup) {
176
- if (isString(indexJson.setup)) {
177
- const setupJson = await loadJson<SetupJson>(indexJson.setup);
178
- indexJson.setup = setupJson;
179
- }
180
- await this.createVariantInstances();
181
- }
182
- // create gltf export manager
183
- this._gltfExportManager = await GltfExportManager.create(this);
184
- // resize handler
185
- window.addEventListener('resize', debounce(this.resize.bind(this), 100));
186
- // wait until scene is completely ready
187
- await this.scene.whenReadyAsync();
188
- // event broadcasting
189
- this.broadcastEvent(Event.BOOTSTRAP_END, this);
190
- // render loop
191
- this.engine.runRenderLoop(() => {
192
- if (!this._isRenderLoopPaused) this.scene.render();
193
- });
194
- return this;
195
- }
196
-
197
- /**
198
- * Enables the BabylonJS [Inspector](https://doc.babylonjs.com/toolsAndResources/tools/inspector).\
199
- * Due to the enormous additional package size of the inspector, this feature is only available in development builds.
200
- */
201
- public async enableDebugLayer(options?: IInspectorOptions) {
202
- if (process.env.NODE_ENV?.toLowerCase().includes('dev')) {
203
- await import(/* webpackChunkName: "debug-inspector" */ '@babylonjs/inspector');
204
- await this.scene.debugLayer.show(options);
205
- } else {
206
- console.warn('BabylonJS inspector is not supported in production builds!');
207
- }
208
- }
209
-
210
- /**
211
- * Destroys all registered {@link VariantInstance}s that are registered
212
- */
213
- public destroyVariantInstances(): Viewer {
214
- this.variantInstances.all.forEach(variantInstance => {
215
- this.variantInstances.destroy(variantInstance.name);
216
- });
217
- return this;
218
- }
219
-
220
- /**
221
- * Trigger a resize event for the `Engine`
222
- */
223
- public resize(): Viewer {
224
- this.engine.resize();
225
- return this;
226
- }
227
-
228
- /**
229
- * A convenience method for directly getting a Node from a {@link VariantInstance} and an {@link Element} by its
230
- * {@link DottedPath}s.
231
- */
232
- public async getNode(
233
- variantInstanceName: string,
234
- elementDottedPath: DottedPathArgument,
235
- nodeDottedPath: DottedPathArgument
236
- ): Promise<TransformNode> {
237
- const variantInstance = await this.variantInstances.get(variantInstanceName);
238
- return variantInstance.getNode(elementDottedPath, nodeDottedPath);
239
- }
240
-
241
- /**
242
- * A convenience method for directly getting a Node from a {@link VariantInstance} and an {@link Element} by its
243
- * {@link DottedPath}s.
244
- */
245
- public async getMesh(
246
- variantInstanceName: string,
247
- elementDottedPath: DottedPathArgument,
248
- meshDottedPath: DottedPathArgument
249
- ): Promise<Mesh | null> {
250
- const variantInstance = await this.variantInstances.get(variantInstanceName);
251
- return variantInstance.getMesh(elementDottedPath, meshDottedPath);
252
- }
253
-
254
- /**
255
- * Switches the camera
256
- *
257
- * @emits {@link Event.CAMERA_SWITCHED}
258
- */
259
- public switchCamera(newCamera: string, reset: boolean = true): Viewer {
260
- const camera = this.scene.getCameraByName(newCamera);
261
- if (camera) {
262
- const activeCamera = this.scene.activeCamera;
263
- if (activeCamera) {
264
- activeCamera.detachControl(this.engine.getRenderingCanvas()!);
265
- }
266
- if (reset) {
267
- camera.restoreState();
268
- }
269
- this.scene.setActiveCameraByName(newCamera);
270
- camera.attachControl(this.engine.getRenderingCanvas()!);
271
- this.broadcastEvent(Event.CAMERA_SWITCHED, camera);
272
- } else {
273
- throw new Error(`Given camera "${newCamera}" not found.`);
274
- }
275
- // TODO: put traceable observers to new camera (@see element)
276
- return this;
277
- }
278
-
279
- /**
280
- * Moves or animates the active camera to given `placement`.
281
- */
282
- public async moveActiveCameraTo(
283
- placement: string | PlacementDefinition,
284
- animation?: string | AnimationDefinition
285
- ): Promise<AnimationInterface> {
286
- return this.animationManager.animateToPlacement(this.sceneManager.activeCamera, placement, animation);
287
- }
288
-
289
- /**
290
- * Takes a sceenshot the the current scene. The result is a string containing a base64 encoded image
291
- */
292
- public screenshot(settings?: ScreenshotSettings): Promise<string> {
293
- return new Promise((resolve, reject) => {
294
- if (!this.engine) {
295
- return reject('Engine is null');
296
- }
297
- if (!this.scene) {
298
- return reject('Scene is null');
299
- }
300
- this.scene.render(); // in combination with a render target, we need to refresh the scene manually to get the latest view
301
- ScreenshotTools.CreateScreenshotUsingRenderTarget(
302
- this.engine,
303
- this.sceneManager.activeCamera,
304
- settings?.size ?? { width: this.canvas.clientWidth, height: this.canvas.clientHeight },
305
- resolve,
306
- settings?.mimeType ?? 'image/png',
307
- settings?.samples ?? 1,
308
- settings?.antialiasing ?? false,
309
- settings?.fileName ?? 'screenshot.png',
310
- settings?.renderSprites ?? false
311
- );
312
- });
313
- }
314
-
315
- /**
316
- * Checks whether the browser is capable of handling XR.
317
- */
318
- public async isBrowserARCapable(): Promise<boolean> {
319
- return await WebXRSessionManager.IsSessionSupportedAsync('immersive-ar');
320
- }
321
-
322
- /**
323
- * Calculates the bounding box from all visible meshes on the scene.
324
- */
325
- public async calculateBoundingBox(excludeGeometry?: ExcludedGeometry): Promise<Mesh> {
326
- if (this.scene.meshes.length === 0) {
327
- throw new Error('There are currently no meshes on the scene.');
328
- }
329
- this.scene.render(); // CB-6062: workaround for BoundingBox not respecting render loop
330
- const bbName = '__bounding_box__';
331
-
332
- const { max, min } = this.scene.meshes
333
- .filter(mesh => {
334
- const isEnabled = mesh.isEnabled();
335
- // ignore the existing bounding box mesh for calculating the current one
336
- const isNotBBoxMesh = bbName !== mesh.id;
337
- // ignore meshes with invalid bounding infos
338
- const hasValidBBoxInfo = mesh.getBoundingInfo().boundingSphere.radius > 0;
339
- // ignore excluded meshes
340
- const isExcluded = excludeGeometry ? isMeshIncludedInExclusionList(mesh as Mesh, excludeGeometry) : false;
341
- return isEnabled && isNotBBoxMesh && hasValidBBoxInfo && !isExcluded;
342
- })
343
- .reduce(
344
- (accBBoxMinMax, curMesh, idx) => {
345
- const bBox = curMesh.getBoundingInfo().boundingBox;
346
- // use the first entry in the array as default value and get the resulting maximum/minimum values
347
- const max = idx === 0 ? bBox.maximumWorld : Vector3.Maximize(accBBoxMinMax.max, bBox.maximumWorld);
348
- const min = idx === 0 ? bBox.minimumWorld : Vector3.Minimize(accBBoxMinMax.min, bBox.minimumWorld);
349
- return { max, min };
350
- },
351
- { max: new Vector3(), min: new Vector3() }
352
- );
353
-
354
- let boundingBox = this.scene.getMeshByName(bbName) as Mesh;
355
- if (!boundingBox) {
356
- boundingBox = new Mesh(bbName, this.scene);
357
- }
358
- boundingBox.setBoundingInfo(new BoundingInfo(min, max));
359
- return boundingBox;
360
- }
361
-
362
- /**
363
- * Focuses the camera to see every visible mesh in scene and tries to optimize wheel precision and panning
364
- */
365
- public async autofocusActiveCamera(settings?: AutofocusSettings) {
366
- // first check some preconditions
367
- const activeCamera = this.scene.activeCamera;
368
- if (!activeCamera) {
369
- throw new Error('No active camera found when using autofocus feature.');
370
- }
371
- if (!(activeCamera instanceof ArcRotateCamera)) {
372
- const cameraClsName = activeCamera.getClassName();
373
- throw new Error(`Camera of type "${cameraClsName}" is not implemented yet to use autofocus feature.`);
374
- }
375
-
376
- // get bounding box of all visible meshes, this is the base for the autofocus algorithm
377
- const boundingBox = await this.calculateBoundingBox(settings?.exclude);
378
-
379
- // focus the helper camera and set the calculated camera data to the real camera
380
- const helperCamera = this.getFocusedHelperCamera(boundingBox, settings);
381
- await this.applyFocusedHelperCameraData(activeCamera, helperCamera, settings);
382
-
383
- // remove the helper camera
384
- helperCamera.dispose();
385
- }
386
-
387
- /**
388
- * Resets everything by calling {@link destroy} to clear all references and {@link bootstrap} to setup a clean
389
- * environment
390
- */
391
- public async reset(): Promise<Viewer> {
392
- await this.destroy();
393
- return this.bootstrap();
394
- }
395
-
396
- /**
397
- * Destroys
398
- *
399
- * * all {@link VariantInstance}s using {@link destroyVariantInstances}
400
- * * calling `dispose` on the `Engine` and `Scene`
401
- */
402
- public destroy(): Viewer {
403
- this.destroyVariantInstances();
404
- this.scene.dispose();
405
- SpecStorage.destroy();
406
- return this;
407
- }
408
-
409
- /**
410
- * Show coordinate system with given dimension (for debugging purpose).
411
- */
412
- public showWorldCoordinates(dimension: number) {
413
- const scene = this.scene;
414
- const makeTextPlane = function (text: string, color: string, size: number) {
415
- const dynamicTexture = new DynamicTexture('DynamicTexture', 50, scene, true);
416
- dynamicTexture.hasAlpha = true;
417
- dynamicTexture.drawText(text, 5, 40, 'bold 36px Arial', color, 'transparent', true);
418
- const plane = Mesh.CreatePlane('TextPlane', size, scene, true);
419
- plane.material = new StandardMaterial('TextPlaneMaterial', scene);
420
- plane.material.backFaceCulling = false;
421
- // @ts-ignore
422
- plane.material.specularColor = new Color3(0, 0, 0);
423
- // @ts-ignore
424
- plane.material.diffuseTexture = dynamicTexture;
425
- return plane;
426
- };
427
-
428
- const axisX = Mesh.CreateLines(
429
- 'axisX',
430
- [
431
- Vector3.Zero(),
432
- new Vector3(dimension, 0, 0),
433
- new Vector3(dimension * 0.95, 0.05 * dimension, 0),
434
- new Vector3(dimension, 0, 0),
435
- new Vector3(dimension * 0.95, -0.05 * dimension, 0),
436
- ],
437
- scene,
438
- false
439
- );
440
- axisX.color = new Color3(1, 0, 0);
441
- const xChar = makeTextPlane('X', 'red', dimension / 10);
442
- xChar.position = new Vector3(0.9 * dimension, -0.05 * dimension, 0);
443
- const axisY = Mesh.CreateLines(
444
- 'axisY',
445
- [
446
- Vector3.Zero(),
447
- new Vector3(0, dimension, 0),
448
- new Vector3(-0.05 * dimension, dimension * 0.95, 0),
449
- new Vector3(0, dimension, 0),
450
- new Vector3(0.05 * dimension, dimension * 0.95, 0),
451
- ],
452
- scene,
453
- false
454
- );
455
- axisY.color = new Color3(0, 1, 0);
456
- const yChar = makeTextPlane('Y', 'green', dimension / 10);
457
- yChar.position = new Vector3(0, 0.9 * dimension, -0.05 * dimension);
458
- const axisZ = Mesh.CreateLines(
459
- 'axisZ',
460
- [
461
- Vector3.Zero(),
462
- new Vector3(0, 0, dimension),
463
- new Vector3(0, -0.05 * dimension, dimension * 0.95),
464
- new Vector3(0, 0, dimension),
465
- new Vector3(0, 0.05 * dimension, dimension * 0.95),
466
- ],
467
- scene,
468
- false
469
- );
470
- axisZ.color = new Color3(0, 0, 1);
471
- const zChar = makeTextPlane('Z', 'blue', dimension / 10);
472
- zChar.position = new Vector3(0, 0.05 * dimension, 0.9 * dimension);
473
- }
474
-
475
- /**
476
- * Pause render loop.
477
- */
478
- public pauseRendering() {
479
- this._isRenderLoopPaused = true;
480
- }
481
-
482
- /**
483
- * Resume render loop when paused.
484
- */
485
- public resumeRendering() {
486
- this._isRenderLoopPaused = false;
487
- }
488
-
489
- /**
490
- * @emits {@link Event.SCENE_PROCESSING_START}
491
- * @emits {@link Event.SCENE_PROCESSING_END}
492
- */
493
- protected async initScene(): Promise<Scene> {
494
- const sceneJson = SpecStorage.get<SceneJson>('scene');
495
- this.broadcastEvent(Event.SCENE_PROCESSING_START, sceneJson);
496
- const engine = new Engine(
497
- this.canvas as HTMLCanvasElement,
498
- sceneJson.engine?.antialiasing ?? false,
499
- sceneJson.engine?.options
500
- );
501
- const scene = await sceneSetup(engine, sceneJson);
502
- if (sceneJson.meshPicking) {
503
- new HighlightLayer('default', scene);
504
- scene.onPointerPick = (pointerEvent: IPointerEvent, pickInfo: PickingInfo) => {
505
- if (!pickInfo.hit) {
506
- return;
507
- }
508
- const mesh = pickInfo.pickedMesh;
509
- this.broadcastEvent(Event.MESH_PICKED, mesh, mesh?.metadata.element, mesh?.metadata.variant);
510
- if (mesh?.metadata.element) {
511
- this.broadcastEvent(Event.ELEMENT_PICKED, mesh.metadata.element);
512
- }
513
- if (mesh?.metadata.variant) {
514
- if (mesh.metadata.variant.inheritedParameters[Parameter.HIGHLIGHT_ENABLED]) {
515
- mesh.metadata.variant.toggleHighlight();
516
- }
517
- this.broadcastEvent(Event.VARIANT_PICKED, mesh.metadata.variant);
518
- }
519
- };
520
- }
521
- this._sceneManager = await SceneManager.create(scene);
522
- this._animationManager = await AnimationManager.create(scene);
523
- if (sceneJson.cloneMaterialsOnMutation !== undefined) {
524
- this._cloneMaterialsOnMutation = sceneJson.cloneMaterialsOnMutation;
525
- }
526
- this.broadcastEvent(Event.SCENE_PROCESSING_END, scene);
527
- return scene;
528
- }
529
-
530
- /**
531
- * Batch creation of multiple {@link VariantInstance} objects with a {@link SetupJson} object passed
532
- */
533
- protected async createVariantInstances(): Promise<VariantInstance[]> {
534
- const setupJson = SpecStorage.get<SetupJson>('setup');
535
- const instances = [];
536
- for (const instanceDefinition of setupJson.instances) {
537
- if (instanceDefinition.lazy) {
538
- this.variantInstances.register(instanceDefinition);
539
- continue;
540
- }
541
- instances.push(
542
- await this.variantInstances.create(
543
- instanceDefinition.variant,
544
- instanceDefinition.name,
545
- instanceDefinition.parameters
546
- )
547
- );
548
- }
549
- return instances;
550
- }
551
-
552
- /**
553
- * Help function for focusing a helper camera exactly onto the given bounding box
554
- */
555
- private getFocusedHelperCamera(boundingBox: Mesh, settings?: AutofocusSettings): ArcRotateCamera {
556
- // use helper camera to get some default values and set the values of the real camera accordingly
557
- const helperCamera = new ArcRotateCamera(
558
- '__helper_camera__',
559
- 0, // camera angles will be overwritten after the target has been set
560
- 0,
561
- 0, // radius will be calculated, so we can set to 0 here
562
- Vector3.Zero(),
563
- this.scene
564
- );
565
- // this is required for automatically calculating the `lowerRadiusLimit`, so that we don't "dive" into meshes
566
- // see https://doc.babylonjs.com/divingDeeper/behaviors/cameraBehaviors#framing-behavior
567
- helperCamera.useFramingBehavior = true;
568
-
569
- // `minZ` is the camera distance beyond which the mesh will be clipped
570
- // this should be very low, but can't be zero
571
- // a good value seems to be 1% of the bounding box size (= radius), whereas the value shouldn't go above 1, which is also the default value
572
- const radius = boundingBox.getBoundingInfo().boundingSphere.radius;
573
- helperCamera.minZ = Math.min(radius / 100, 1);
574
-
575
- // set desired camera data, these won't be changed by the autofocus function!
576
- // default values should focus the element exactly from the front (= XY Plane)
577
- helperCamera.setTarget(boundingBox, true);
578
- helperCamera.alpha = (settings?.alpha ?? -90) * (Math.PI / 180);
579
- helperCamera.beta = (settings?.beta ?? 90) * (Math.PI / 180);
580
-
581
- // finally zoom to the bounding box
582
- // also apply a zoom factor, this adjusts the borders around the model in the viewport
583
- helperCamera.zoomOnFactor = settings?.radiusFactor || 1;
584
- helperCamera.zoomOn([boundingBox], true);
585
-
586
- return helperCamera;
587
- }
588
-
589
- /**
590
- * Help function for applying the relevant data of the focused helper camera to the real camera
591
- */
592
- private async applyFocusedHelperCameraData(
593
- activeCamera: ArcRotateCamera,
594
- helperCamera: ArcRotateCamera,
595
- settings?: AutofocusSettings
596
- ) {
597
- // limits
598
- activeCamera.minZ = helperCamera.minZ;
599
- activeCamera.maxZ = helperCamera.maxZ;
600
- activeCamera.lowerRadiusLimit = helperCamera.lowerRadiusLimit;
601
- activeCamera.upperRadiusLimit = helperCamera.upperRadiusLimit;
602
-
603
- // additional settings
604
- if (settings?.adjustWheelPrecision !== false) {
605
- activeCamera.wheelPrecision = helperCamera.wheelPrecision;
606
- }
607
- if (settings?.adjustPanningSensibility !== false) {
608
- activeCamera.panningSensibility = helperCamera.panningSensibility;
609
- }
610
- if (settings?.adjustPinchPrecision !== false) {
611
- activeCamera.pinchPrecision = helperCamera.pinchPrecision;
612
- }
613
-
614
- // finally move the camera
615
- // do this at last, so that all camera settings are already considered
616
- const newCameraPosition: PlacementDefinition = {
617
- alpha: helperCamera.alpha,
618
- beta: helperCamera.beta,
619
- radius: helperCamera.radius,
620
- target: helperCamera.target,
621
- };
622
- await this.animationManager.animateToPlacement(activeCamera, newCameraPosition, settings?.animation);
623
- }
624
- }
1
+ import { EnvironmentHelper } from '@babylonjs/core/Helpers/environmentHelper';
2
+ import { ArcRotateCamera } from '@babylonjs/core/Cameras/arcRotateCamera';
3
+ import { PickingInfo } from '@babylonjs/core/Collisions/pickingInfo';
4
+ import { BoundingInfo } from '@babylonjs/core/Culling/boundingInfo';
5
+ import { Engine } from '@babylonjs/core/Engines/engine';
6
+ import { HighlightLayer } from '@babylonjs/core/Layers/highlightLayer';
7
+ import { StandardMaterial } from '@babylonjs/core/Materials/standardMaterial';
8
+ import { DynamicTexture } from '@babylonjs/core/Materials/Textures/dynamicTexture';
9
+ import { Color3 } from '@babylonjs/core/Maths/math.color';
10
+ import { Vector3 } from '@babylonjs/core/Maths/math.vector';
11
+ import { Mesh } from '@babylonjs/core/Meshes/mesh';
12
+ import { ScreenshotTools } from '@babylonjs/core/Misc/screenshotTools';
13
+ import { Scene } from '@babylonjs/core/scene';
14
+ import { WebXRSessionManager } from '@babylonjs/core/XR/webXRSessionManager';
15
+ import { isMeshIncludedInExclusionList } from '../util/structureHelper';
16
+ import { isString } from 'lodash-es';
17
+ import { version } from '../../buildinfo.json';
18
+ import { sceneSetup } from '../internal/sceneSetup';
19
+ import { AnimationManager } from '../manager/animationManager';
20
+ import { GltfExportManager } from '../manager/gltfExportManager';
21
+ import { SceneManager } from '../manager/sceneManager';
22
+ import { VariantInstanceManager } from '../manager/variantInstanceManager';
23
+ import { SpecStorage } from '../store/specStorage';
24
+ import { debounce, loadJavascript, loadJson } from '../util/resourceHelper';
25
+ import { Event } from './event';
26
+ import { EventBroadcaster } from './eventBroadcaster';
27
+ import { Parameter } from './parameter';
28
+ import { Variant } from './variant';
29
+ import { VariantInstance } from './variantInstance';
30
+ import { IPointerEvent } from '@babylonjs/core/Events/deviceInputEvents';
31
+ import { DebugLayer } from '@babylonjs/core/Debug/debugLayer';
32
+ import { PhotoDome } from '@babylonjs/core/Helpers/photoDome';
33
+ import { backgroundDomeName, envHelperMetadataName } from '../util/babylonHelper';
34
+
35
+ /**
36
+ * The main exposed object. This is the entry point into the application
37
+ *
38
+ * ```js
39
+ * const canvas = document.getElementById( 'babylon-canvas' );
40
+ * const viewer = Viewer( canvas, '/path/to/index.json' );
41
+ * ```
42
+ * The class does nothing on its own and needs to {@link bootstrap}
43
+ */
44
+ export class Viewer extends EventBroadcaster {
45
+ protected _scene: Scene | null = null;
46
+
47
+ protected _animationManager: AnimationManager | null = null;
48
+
49
+ protected _sceneManager: SceneManager | null = null;
50
+
51
+ protected _gltfExportManager: GltfExportManager | null = null;
52
+
53
+ protected _variantInstances: VariantInstanceManager | null = null;
54
+
55
+ // default value is `true` ATM for compatibility reasons
56
+ // in the future material cloning should be the edge case
57
+ protected _cloneMaterialsOnMutation: boolean = true;
58
+
59
+ protected _isRenderLoopPaused: boolean = false;
60
+
61
+ protected _inspectorLoaded: boolean = false;
62
+
63
+ static version = version;
64
+
65
+ /**
66
+ * Constructor
67
+ */
68
+ public constructor(public readonly canvas: HTMLCanvasElement, protected structureJson: string | StructureJson) {
69
+ super();
70
+ }
71
+
72
+ /**
73
+ * Gets the BabylonJS Scene that is attached to the instance.
74
+ *
75
+ * @throws Error if the `scene` has not been initialized.
76
+ */
77
+ get scene(): Scene {
78
+ if (!this._scene) {
79
+ throw new Error(`Scene has not been initialized.`);
80
+ }
81
+ return this._scene;
82
+ }
83
+
84
+ /**
85
+ * Gets the {@link SceneManager} attached to the viewer.
86
+ *
87
+ * @throws Error if the {@link SceneManager} has not been initialized.
88
+ */
89
+ get sceneManager(): SceneManager {
90
+ if (!this._sceneManager) {
91
+ throw new Error(`SceneManager has not been initialized.`);
92
+ }
93
+ return this._sceneManager;
94
+ }
95
+
96
+ /**
97
+ * Gets the {@link GltfExportManager} attached to the viewer.
98
+ *
99
+ * @throws Error if the {@link GltfExportManager} has not been initialized.
100
+ */
101
+ get gltfExportManager(): GltfExportManager {
102
+ if (!this._gltfExportManager) {
103
+ throw new Error(`GltfExportManager has not been initialized.`);
104
+ }
105
+ return this._gltfExportManager;
106
+ }
107
+
108
+ /**
109
+ * Gets the BabylonJS Engine that is attached to the viewer.
110
+ */
111
+ get engine(): Engine {
112
+ return this.scene.getEngine();
113
+ }
114
+
115
+ /**
116
+ * Gets the {@link VariantInstanceManager} attached to the viewer.
117
+ *
118
+ * @throws Error if the {@link VariantInstanceManager} has not been initialized.
119
+ */
120
+ get variantInstances(): VariantInstanceManager {
121
+ if (!this._variantInstances) {
122
+ throw Error(`There is no variantInstanceManager.`);
123
+ }
124
+ return this._variantInstances;
125
+ }
126
+
127
+ /**
128
+ * Gets the {@link AnimationManager} attached to the viewer.
129
+ *
130
+ * @throws Error if the {@link AnimationManager} has not been initialized.
131
+ */
132
+ get animationManager(): AnimationManager {
133
+ if (!this._animationManager) {
134
+ throw new Error(`There is no animationManager instance.`);
135
+ }
136
+ return this._animationManager;
137
+ }
138
+
139
+ /**
140
+ * Gets the `cloneMaterialsOnMutation` flag, as defined in the spec
141
+ */
142
+ get cloneMaterialsOnMutation(): boolean {
143
+ return this._cloneMaterialsOnMutation;
144
+ }
145
+
146
+ /**
147
+ * Starts the application. This will
148
+ * * load the given "index" JSON file
149
+ * * setup the scene with the "scene" JSON file
150
+ * * create an (optional) default setup with different variant settings
151
+ * * sets up resizing by attaching a debounced version of {@link resize}
152
+ *
153
+ * @throws Error if any of the files is not found/valid
154
+ *
155
+ * @emits {@link Event.BOOTSTRAP_START}
156
+ * @emits {@link Event.BOOTSTRAP_END}
157
+ */
158
+ public async bootstrap(): Promise<Viewer> {
159
+ this.broadcastEvent(Event.BOOTSTRAP_START, this);
160
+ let indexJson;
161
+ if (isString(this.structureJson)) {
162
+ indexJson = await loadJson<StructureJson>(this.structureJson);
163
+ } else {
164
+ indexJson = this.structureJson;
165
+ }
166
+ if (!indexJson.scene) {
167
+ throw new Error(`No "scene" property found for bootstrapping.`);
168
+ }
169
+ // fill spec store
170
+ SpecStorage.createFromSpec(indexJson);
171
+ // load scene
172
+ if (isString(indexJson.scene)) {
173
+ const sceneJson = await loadJson<SceneJson>(indexJson.scene);
174
+ indexJson.scene = sceneJson;
175
+ }
176
+ this._scene = await this.initScene();
177
+ // create instance manager
178
+ const rootVariant = await Variant.create('_', indexJson, this);
179
+ this._variantInstances = await VariantInstanceManager.create(rootVariant);
180
+ // create optional default instances
181
+ if (indexJson.setup) {
182
+ if (isString(indexJson.setup)) {
183
+ const setupJson = await loadJson<SetupJson>(indexJson.setup);
184
+ indexJson.setup = setupJson;
185
+ }
186
+ await this.createVariantInstances();
187
+ }
188
+ // create gltf export manager
189
+ this._gltfExportManager = await GltfExportManager.create(this);
190
+ // resize handler
191
+ window.addEventListener('resize', debounce(this.resize.bind(this), 100));
192
+ // wait until scene is completely ready
193
+ await this.scene.whenReadyAsync();
194
+ // event broadcasting
195
+ this.broadcastEvent(Event.BOOTSTRAP_END, this);
196
+ // render loop
197
+ this.engine.runRenderLoop(() => {
198
+ if (!this._isRenderLoopPaused) this.scene.render();
199
+ });
200
+ return this;
201
+ }
202
+
203
+ /**
204
+ * Enables the BabylonJS [Inspector](https://doc.babylonjs.com/toolsAndResources/tools/inspector).\
205
+ * Due to the additional size of the inspector, the CDN version is used instead of shipping the required code with the viewer.\
206
+ * This means that the code will be downloaded only when needed and calling `enableDebugLayer` can take a little while depending on your internet connection etc.
207
+ */
208
+ public async enableDebugLayer(options?: IInspectorOptions) {
209
+ if (!this._inspectorLoaded) {
210
+ // CDN version of inspector requires the BabylonJS core to be available as CDN module as well
211
+ await loadJavascript('https://cdn.jsdelivr.net/npm/babylonjs@5.6.0/babylon.min.js');
212
+
213
+ DebugLayer.InspectorURL =
214
+ 'https://cdn.jsdelivr.net/npm/babylonjs-inspector@5.6.0/babylon.inspector.bundle.max.min.js';
215
+
216
+ this._inspectorLoaded = true;
217
+ }
218
+
219
+ await this.scene.debugLayer.show(options);
220
+ }
221
+
222
+ /**
223
+ * Destroys all registered {@link VariantInstance}s that are registered
224
+ */
225
+ public destroyVariantInstances(): Viewer {
226
+ this.variantInstances.all.forEach(variantInstance => {
227
+ this.variantInstances.destroy(variantInstance.name);
228
+ });
229
+ return this;
230
+ }
231
+
232
+ /**
233
+ * Trigger a resize event for the `Engine`
234
+ */
235
+ public resize(): Viewer {
236
+ this.engine.resize();
237
+ return this;
238
+ }
239
+
240
+ /**
241
+ * A convenience method for directly getting a Node from a {@link VariantInstance} and an {@link Element} by its
242
+ * {@link DottedPath}s.
243
+ */
244
+ public async getNode(
245
+ variantInstanceName: string,
246
+ elementDottedPath: DottedPathArgument,
247
+ nodeDottedPath: DottedPathArgument
248
+ ): Promise<TransformNode> {
249
+ const variantInstance = await this.variantInstances.get(variantInstanceName);
250
+ return variantInstance.getNode(elementDottedPath, nodeDottedPath);
251
+ }
252
+
253
+ /**
254
+ * A convenience method for directly getting a Node from a {@link VariantInstance} and an {@link Element} by its
255
+ * {@link DottedPath}s.
256
+ */
257
+ public async getMesh(
258
+ variantInstanceName: string,
259
+ elementDottedPath: DottedPathArgument,
260
+ meshDottedPath: DottedPathArgument
261
+ ): Promise<Mesh | null> {
262
+ const variantInstance = await this.variantInstances.get(variantInstanceName);
263
+ return variantInstance.getMesh(elementDottedPath, meshDottedPath);
264
+ }
265
+
266
+ /**
267
+ * Switches the camera
268
+ *
269
+ * @emits {@link Event.CAMERA_SWITCHED}
270
+ */
271
+ public switchCamera(newCamera: string, reset: boolean = true): Viewer {
272
+ const camera = this.scene.getCameraByName(newCamera);
273
+ if (camera) {
274
+ const activeCamera = this.scene.activeCamera;
275
+ if (activeCamera) {
276
+ activeCamera.detachControl(this.engine.getRenderingCanvas()!);
277
+ }
278
+ if (reset) {
279
+ camera.restoreState();
280
+ }
281
+ this.scene.setActiveCameraByName(newCamera);
282
+ camera.attachControl(this.engine.getRenderingCanvas()!);
283
+ this.broadcastEvent(Event.CAMERA_SWITCHED, camera);
284
+ } else {
285
+ throw new Error(`Given camera "${newCamera}" not found.`);
286
+ }
287
+ // TODO: put traceable observers to new camera (@see element)
288
+ return this;
289
+ }
290
+
291
+ /**
292
+ * Moves or animates the active camera to given `placement`.
293
+ */
294
+ public async moveActiveCameraTo(
295
+ placement: string | PlacementDefinition,
296
+ animation?: string | AnimationDefinition
297
+ ): Promise<AnimationInterface> {
298
+ return this.animationManager.animateToPlacement(this.sceneManager.activeCamera, placement, animation);
299
+ }
300
+
301
+ /**
302
+ * Takes a sceenshot the the current scene. The result is a string containing a base64 encoded image
303
+ */
304
+ public screenshot(settings?: ScreenshotSettings): Promise<string> {
305
+ return new Promise((resolve, reject) => {
306
+ if (!this.engine) {
307
+ return reject('Engine is null');
308
+ }
309
+ if (!this.scene) {
310
+ return reject('Scene is null');
311
+ }
312
+ this.scene.render(); // in combination with a render target, we need to refresh the scene manually to get the latest view
313
+ ScreenshotTools.CreateScreenshotUsingRenderTarget(
314
+ this.engine,
315
+ this.sceneManager.activeCamera,
316
+ settings?.size ?? { width: this.canvas.clientWidth, height: this.canvas.clientHeight },
317
+ resolve,
318
+ settings?.mimeType ?? 'image/png',
319
+ settings?.samples ?? 1,
320
+ settings?.antialiasing ?? false,
321
+ settings?.fileName ?? 'screenshot.png',
322
+ settings?.renderSprites ?? false
323
+ );
324
+ });
325
+ }
326
+
327
+ /**
328
+ * Checks whether the browser is capable of handling XR.
329
+ */
330
+ public async isBrowserARCapable(): Promise<boolean> {
331
+ return await WebXRSessionManager.IsSessionSupportedAsync('immersive-ar');
332
+ }
333
+
334
+ /**
335
+ * Calculates the bounding box from all visible meshes on the scene.
336
+ */
337
+ public async calculateBoundingBox(excludeGeometry?: ExcludedGeometryList): Promise<Mesh> {
338
+ if (this.scene.meshes.length === 0) {
339
+ throw new Error('There are currently no meshes on the scene.');
340
+ }
341
+ this.scene.render(); // CB-6062: workaround for BoundingBox not respecting render loop
342
+ const bbName = '__bounding_box__';
343
+
344
+ const { max, min } = this.scene.meshes
345
+ .filter(mesh => {
346
+ const isEnabled = mesh.isEnabled();
347
+ // ignore the existing bounding box mesh for calculating the current one
348
+ const isNotBBoxMesh = bbName !== mesh.id;
349
+ // ignore meshes with invalid bounding infos
350
+ const hasValidBBoxInfo = mesh.getBoundingInfo().boundingSphere.radius > 0;
351
+ // ignore excluded meshes
352
+ const isExcluded = excludeGeometry ? isMeshIncludedInExclusionList(mesh as Mesh, excludeGeometry) : false;
353
+ return isEnabled && isNotBBoxMesh && hasValidBBoxInfo && !isExcluded;
354
+ })
355
+ .reduce(
356
+ (accBBoxMinMax, curMesh, idx) => {
357
+ const bBox = curMesh.getBoundingInfo().boundingBox;
358
+ // use the first entry in the array as default value and get the resulting maximum/minimum values
359
+ const max = idx === 0 ? bBox.maximumWorld : Vector3.Maximize(accBBoxMinMax.max, bBox.maximumWorld);
360
+ const min = idx === 0 ? bBox.minimumWorld : Vector3.Minimize(accBBoxMinMax.min, bBox.minimumWorld);
361
+ return { max, min };
362
+ },
363
+ { max: new Vector3(), min: new Vector3() }
364
+ );
365
+
366
+ let boundingBox = this.scene.getMeshByName(bbName) as Mesh;
367
+ if (!boundingBox) {
368
+ boundingBox = new Mesh(bbName, this.scene);
369
+ }
370
+ boundingBox.setBoundingInfo(new BoundingInfo(min, max));
371
+ return boundingBox;
372
+ }
373
+
374
+ /**
375
+ * Focuses the camera to see every visible mesh in scene and tries to optimize wheel precision and panning
376
+ */
377
+ public async autofocusActiveCamera(settings?: AutofocusSettings) {
378
+ // first check some preconditions
379
+ const activeCamera = this.scene.activeCamera;
380
+ if (!activeCamera) {
381
+ throw new Error('No active camera found when using autofocus feature.');
382
+ }
383
+ if (!(activeCamera instanceof ArcRotateCamera)) {
384
+ const cameraClsName = activeCamera.getClassName();
385
+ throw new Error(`Camera of type "${cameraClsName}" is not implemented yet to use autofocus feature.`);
386
+ }
387
+
388
+ let exclude = settings?.exclude || [];
389
+
390
+ // Exclude shown photo dome or environment helper from bounding box calculation
391
+ const photoDome = this.scene.getNodeByName(backgroundDomeName) as undefined | PhotoDome;
392
+ const photoDomeMeshes = photoDome?.getChildMeshes();
393
+ if (photoDomeMeshes?.length) {
394
+ exclude = [...exclude, ...photoDomeMeshes];
395
+ }
396
+
397
+ const envHelper = this.scene.metadata?.[envHelperMetadataName] as undefined | EnvironmentHelper;
398
+ if (envHelper?.rootMesh) {
399
+ exclude = [...exclude, envHelper.rootMesh];
400
+ }
401
+
402
+ // get bounding box of all visible meshes, this is the base for the autofocus algorithm
403
+ const boundingBox = await this.calculateBoundingBox(exclude);
404
+
405
+ // focus the helper camera and set the calculated camera data to the real camera
406
+ const helperCamera = this.getFocusedHelperCamera(boundingBox, settings);
407
+ await this.applyFocusedHelperCameraData(activeCamera, helperCamera, settings);
408
+
409
+ // remove the helper camera
410
+ helperCamera.dispose();
411
+ }
412
+
413
+ /**
414
+ * Resets everything by calling {@link destroy} to clear all references and {@link bootstrap} to setup a clean
415
+ * environment
416
+ */
417
+ public async reset(): Promise<Viewer> {
418
+ await this.destroy();
419
+ return this.bootstrap();
420
+ }
421
+
422
+ /**
423
+ * Destroys
424
+ *
425
+ * * all {@link VariantInstance}s using {@link destroyVariantInstances}
426
+ * * calling `dispose` on the `Engine` and `Scene`
427
+ */
428
+ public destroy(): Viewer {
429
+ this.destroyVariantInstances();
430
+ this.scene.dispose();
431
+ SpecStorage.destroy();
432
+ return this;
433
+ }
434
+
435
+ /**
436
+ * Show coordinate system with given dimension (for debugging purpose).
437
+ */
438
+ public showWorldCoordinates(dimension: number) {
439
+ const scene = this.scene;
440
+ const makeTextPlane = function (text: string, color: string, size: number) {
441
+ const dynamicTexture = new DynamicTexture('DynamicTexture', 50, scene, true);
442
+ dynamicTexture.hasAlpha = true;
443
+ dynamicTexture.drawText(text, 5, 40, 'bold 36px Arial', color, 'transparent', true);
444
+ const plane = Mesh.CreatePlane('TextPlane', size, scene, true);
445
+ plane.material = new StandardMaterial('TextPlaneMaterial', scene);
446
+ plane.material.backFaceCulling = false;
447
+ // @ts-ignore
448
+ plane.material.specularColor = new Color3(0, 0, 0);
449
+ // @ts-ignore
450
+ plane.material.diffuseTexture = dynamicTexture;
451
+ return plane;
452
+ };
453
+
454
+ const axisX = Mesh.CreateLines(
455
+ 'axisX',
456
+ [
457
+ Vector3.Zero(),
458
+ new Vector3(dimension, 0, 0),
459
+ new Vector3(dimension * 0.95, 0.05 * dimension, 0),
460
+ new Vector3(dimension, 0, 0),
461
+ new Vector3(dimension * 0.95, -0.05 * dimension, 0),
462
+ ],
463
+ scene,
464
+ false
465
+ );
466
+ axisX.color = new Color3(1, 0, 0);
467
+ const xChar = makeTextPlane('X', 'red', dimension / 10);
468
+ xChar.position = new Vector3(0.9 * dimension, -0.05 * dimension, 0);
469
+ const axisY = Mesh.CreateLines(
470
+ 'axisY',
471
+ [
472
+ Vector3.Zero(),
473
+ new Vector3(0, dimension, 0),
474
+ new Vector3(-0.05 * dimension, dimension * 0.95, 0),
475
+ new Vector3(0, dimension, 0),
476
+ new Vector3(0.05 * dimension, dimension * 0.95, 0),
477
+ ],
478
+ scene,
479
+ false
480
+ );
481
+ axisY.color = new Color3(0, 1, 0);
482
+ const yChar = makeTextPlane('Y', 'green', dimension / 10);
483
+ yChar.position = new Vector3(0, 0.9 * dimension, -0.05 * dimension);
484
+ const axisZ = Mesh.CreateLines(
485
+ 'axisZ',
486
+ [
487
+ Vector3.Zero(),
488
+ new Vector3(0, 0, dimension),
489
+ new Vector3(0, -0.05 * dimension, dimension * 0.95),
490
+ new Vector3(0, 0, dimension),
491
+ new Vector3(0, 0.05 * dimension, dimension * 0.95),
492
+ ],
493
+ scene,
494
+ false
495
+ );
496
+ axisZ.color = new Color3(0, 0, 1);
497
+ const zChar = makeTextPlane('Z', 'blue', dimension / 10);
498
+ zChar.position = new Vector3(0, 0.05 * dimension, 0.9 * dimension);
499
+ }
500
+
501
+ /**
502
+ * Pause render loop.
503
+ */
504
+ public pauseRendering() {
505
+ this._isRenderLoopPaused = true;
506
+ }
507
+
508
+ /**
509
+ * Resume render loop when paused.
510
+ */
511
+ public resumeRendering() {
512
+ this._isRenderLoopPaused = false;
513
+ }
514
+
515
+ /**
516
+ * @emits {@link Event.SCENE_PROCESSING_START}
517
+ * @emits {@link Event.SCENE_PROCESSING_END}
518
+ */
519
+ protected async initScene(): Promise<Scene> {
520
+ const sceneJson = SpecStorage.get<SceneJson>('scene');
521
+ this.broadcastEvent(Event.SCENE_PROCESSING_START, sceneJson);
522
+ const engine = new Engine(
523
+ this.canvas as HTMLCanvasElement,
524
+ sceneJson.engine?.antialiasing ?? false,
525
+ sceneJson.engine?.options
526
+ );
527
+ const scene = await sceneSetup(engine, sceneJson);
528
+ if (sceneJson.meshPicking) {
529
+ new HighlightLayer('default', scene);
530
+ scene.onPointerPick = (pointerEvent: IPointerEvent, pickInfo: PickingInfo) => {
531
+ if (!pickInfo.hit) {
532
+ return;
533
+ }
534
+ const mesh = pickInfo.pickedMesh;
535
+ this.broadcastEvent(Event.MESH_PICKED, mesh, mesh?.metadata.element, mesh?.metadata.variant);
536
+ if (mesh?.metadata.element) {
537
+ this.broadcastEvent(Event.ELEMENT_PICKED, mesh.metadata.element);
538
+ }
539
+ if (mesh?.metadata.variant) {
540
+ if (mesh.metadata.variant.inheritedParameters[Parameter.HIGHLIGHT_ENABLED]) {
541
+ mesh.metadata.variant.toggleHighlight();
542
+ }
543
+ this.broadcastEvent(Event.VARIANT_PICKED, mesh.metadata.variant);
544
+ }
545
+ };
546
+ }
547
+ this._sceneManager = await SceneManager.create(scene);
548
+ this._animationManager = await AnimationManager.create(scene);
549
+ if (sceneJson.cloneMaterialsOnMutation !== undefined) {
550
+ this._cloneMaterialsOnMutation = sceneJson.cloneMaterialsOnMutation;
551
+ }
552
+ this.broadcastEvent(Event.SCENE_PROCESSING_END, scene);
553
+ return scene;
554
+ }
555
+
556
+ /**
557
+ * Batch creation of multiple {@link VariantInstance} objects with a {@link SetupJson} object passed
558
+ */
559
+ protected async createVariantInstances(): Promise<VariantInstance[]> {
560
+ const setupJson = SpecStorage.get<SetupJson>('setup');
561
+ const instances = [];
562
+ for (const instanceDefinition of setupJson.instances) {
563
+ if (instanceDefinition.lazy) {
564
+ this.variantInstances.register(instanceDefinition);
565
+ continue;
566
+ }
567
+ instances.push(
568
+ await this.variantInstances.create(
569
+ instanceDefinition.variant,
570
+ instanceDefinition.name,
571
+ instanceDefinition.parameters
572
+ )
573
+ );
574
+ }
575
+ return instances;
576
+ }
577
+
578
+ /**
579
+ * Help function for focusing a helper camera exactly onto the given bounding box
580
+ */
581
+ private getFocusedHelperCamera(boundingBox: Mesh, settings?: AutofocusSettings): ArcRotateCamera {
582
+ // use helper camera to get some default values and set the values of the real camera accordingly
583
+ const helperCamera = new ArcRotateCamera(
584
+ '__helper_camera__',
585
+ 0, // camera angles will be overwritten after the target has been set
586
+ 0,
587
+ 0, // radius will be calculated, so we can set to 0 here
588
+ Vector3.Zero(),
589
+ this.scene
590
+ );
591
+ // this is required for automatically calculating the `lowerRadiusLimit`, so that we don't "dive" into meshes
592
+ // see https://doc.babylonjs.com/divingDeeper/behaviors/cameraBehaviors#framing-behavior
593
+ helperCamera.useFramingBehavior = true;
594
+
595
+ // `minZ` is the camera distance beyond which the mesh will be clipped
596
+ // this should be very low, but can't be zero
597
+ // a good value seems to be 1% of the bounding box size (= radius), whereas the value shouldn't go above 1, which is also the default value
598
+ const radius = boundingBox.getBoundingInfo().boundingSphere.radius;
599
+ helperCamera.minZ = Math.min(radius / 100, 1);
600
+
601
+ // set desired camera data, these won't be changed by the autofocus function!
602
+ // default values should focus the element exactly from the front (= XY Plane)
603
+ helperCamera.setTarget(boundingBox, true);
604
+ helperCamera.alpha = (settings?.alpha ?? -90) * (Math.PI / 180);
605
+ helperCamera.beta = (settings?.beta ?? 90) * (Math.PI / 180);
606
+
607
+ // finally zoom to the bounding box
608
+ // also apply a zoom factor, this adjusts the borders around the model in the viewport
609
+ helperCamera.zoomOnFactor = settings?.radiusFactor || 1;
610
+ helperCamera.zoomOn([boundingBox], true);
611
+
612
+ return helperCamera;
613
+ }
614
+
615
+ /**
616
+ * Help function for applying the relevant data of the focused helper camera to the real camera
617
+ */
618
+ private async applyFocusedHelperCameraData(
619
+ activeCamera: ArcRotateCamera,
620
+ helperCamera: ArcRotateCamera,
621
+ settings?: AutofocusSettings
622
+ ) {
623
+ // limits
624
+ activeCamera.minZ = helperCamera.minZ;
625
+ activeCamera.maxZ = helperCamera.maxZ;
626
+ activeCamera.lowerRadiusLimit = helperCamera.lowerRadiusLimit;
627
+ activeCamera.upperRadiusLimit = helperCamera.upperRadiusLimit;
628
+
629
+ // additional settings
630
+ if (settings?.adjustWheelPrecision !== false) {
631
+ activeCamera.wheelPrecision = helperCamera.wheelPrecision;
632
+ }
633
+ if (settings?.adjustPanningSensibility !== false) {
634
+ activeCamera.panningSensibility = helperCamera.panningSensibility;
635
+ }
636
+ if (settings?.adjustPinchPrecision !== false) {
637
+ activeCamera.pinchPrecision = helperCamera.pinchPrecision;
638
+ }
639
+
640
+ // finally move the camera
641
+ // do this at last, so that all camera settings are already considered
642
+ const newCameraPosition: PlacementDefinition = {
643
+ alpha: helperCamera.alpha,
644
+ beta: helperCamera.beta,
645
+ radius: helperCamera.radius,
646
+ target: helperCamera.target,
647
+ };
648
+ await this.animationManager.animateToPlacement(activeCamera, newCameraPosition, settings?.animation);
649
+ }
650
+ }