@combeenation/3d-viewer 9.3.0 → 10.0.0-alpha3

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