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