@combeenation/3d-viewer 4.0.0-alpha9 → 4.0.0-beta4

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 (105) hide show
  1. package/README.md +112 -111
  2. package/dist/lib-cjs/api/classes/animationInterface.d.ts +8 -8
  3. package/dist/lib-cjs/api/classes/animationInterface.js +1 -1
  4. package/dist/lib-cjs/api/classes/dottedPath.d.ts +79 -79
  5. package/dist/lib-cjs/api/classes/dottedPath.js +190 -190
  6. package/dist/lib-cjs/api/classes/element.d.ts +125 -130
  7. package/dist/lib-cjs/api/classes/element.js +682 -739
  8. package/dist/lib-cjs/api/classes/element.js.map +1 -1
  9. package/dist/lib-cjs/api/classes/elementParameterizable.d.ts +14 -14
  10. package/dist/lib-cjs/api/classes/elementParameterizable.js +134 -134
  11. package/dist/lib-cjs/api/classes/event.d.ts +312 -326
  12. package/dist/lib-cjs/api/classes/event.js +357 -371
  13. package/dist/lib-cjs/api/classes/event.js.map +1 -1
  14. package/dist/lib-cjs/api/classes/eventBroadcaster.d.ts +26 -26
  15. package/dist/lib-cjs/api/classes/eventBroadcaster.js +53 -53
  16. package/dist/lib-cjs/api/classes/parameter.d.ts +165 -259
  17. package/dist/lib-cjs/api/classes/parameter.js +267 -387
  18. package/dist/lib-cjs/api/classes/parameter.js.map +1 -1
  19. package/dist/lib-cjs/api/classes/parameterObservable.d.ts +36 -36
  20. package/dist/lib-cjs/api/classes/parameterObservable.js +126 -101
  21. package/dist/lib-cjs/api/classes/parameterObservable.js.map +1 -1
  22. package/dist/lib-cjs/api/classes/parameterizable.d.ts +15 -15
  23. package/dist/lib-cjs/api/classes/parameterizable.js +149 -149
  24. package/dist/lib-cjs/api/classes/placementAnimation.d.ts +38 -38
  25. package/dist/lib-cjs/api/classes/placementAnimation.js +138 -138
  26. package/dist/lib-cjs/api/classes/variant.d.ts +190 -224
  27. package/dist/lib-cjs/api/classes/variant.js +873 -1137
  28. package/dist/lib-cjs/api/classes/variant.js.map +1 -1
  29. package/dist/lib-cjs/api/classes/variantInstance.d.ts +41 -45
  30. package/dist/lib-cjs/api/classes/variantInstance.js +98 -108
  31. package/dist/lib-cjs/api/classes/variantInstance.js.map +1 -1
  32. package/dist/lib-cjs/api/classes/variantParameterizable.d.ts +17 -17
  33. package/dist/lib-cjs/api/classes/variantParameterizable.js +92 -92
  34. package/dist/lib-cjs/api/classes/viewer.d.ts +128 -131
  35. package/dist/lib-cjs/api/classes/viewer.js +486 -538
  36. package/dist/lib-cjs/api/classes/viewer.js.map +1 -1
  37. package/dist/lib-cjs/api/classes/viewerLight.d.ts +65 -66
  38. package/dist/lib-cjs/api/classes/viewerLight.js +322 -389
  39. package/dist/lib-cjs/api/classes/viewerLight.js.map +1 -1
  40. package/dist/lib-cjs/api/internal/debugViewer.d.ts +13 -13
  41. package/dist/lib-cjs/api/internal/debugViewer.js +87 -87
  42. package/dist/lib-cjs/api/internal/lensRendering.d.ts +8 -8
  43. package/dist/lib-cjs/api/internal/lensRendering.js +11 -11
  44. package/dist/lib-cjs/api/internal/sceneSetup.d.ts +6 -10
  45. package/dist/lib-cjs/api/internal/sceneSetup.js +227 -231
  46. package/dist/lib-cjs/api/internal/sceneSetup.js.map +1 -1
  47. package/dist/lib-cjs/api/manager/animationManager.d.ts +29 -29
  48. package/dist/lib-cjs/api/manager/animationManager.js +121 -121
  49. package/dist/lib-cjs/api/manager/sceneManager.d.ts +32 -32
  50. package/dist/lib-cjs/api/manager/sceneManager.js +132 -132
  51. package/dist/lib-cjs/api/manager/variantInstanceManager.d.ts +90 -90
  52. package/dist/lib-cjs/api/manager/variantInstanceManager.js +321 -321
  53. package/dist/lib-cjs/api/store/specStorage.d.ts +24 -24
  54. package/dist/lib-cjs/api/store/specStorage.js +51 -51
  55. package/dist/lib-cjs/api/util/babylonHelper.d.ts +125 -175
  56. package/dist/lib-cjs/api/util/babylonHelper.js +368 -520
  57. package/dist/lib-cjs/api/util/babylonHelper.js.map +1 -1
  58. package/dist/lib-cjs/api/util/globalTypes.d.ts +279 -321
  59. package/dist/lib-cjs/api/util/globalTypes.js +1 -1
  60. package/dist/lib-cjs/api/util/resourceHelper.d.ts +51 -26
  61. package/dist/lib-cjs/api/util/resourceHelper.js +243 -237
  62. package/dist/lib-cjs/api/util/resourceHelper.js.map +1 -1
  63. package/dist/lib-cjs/api/util/stringHelper.d.ts +9 -9
  64. package/dist/lib-cjs/api/util/stringHelper.js +25 -25
  65. package/dist/lib-cjs/buildinfo.json +3 -3
  66. package/dist/lib-cjs/index.d.ts +46 -48
  67. package/dist/lib-cjs/index.js +82 -86
  68. package/dist/lib-cjs/index.js.map +1 -1
  69. package/dist/webpack-stats.json +0 -0
  70. package/package.json +83 -83
  71. package/src/api/classes/animationInterface.ts +11 -11
  72. package/src/api/classes/dottedPath.ts +189 -189
  73. package/src/api/classes/element.ts +617 -635
  74. package/src/api/classes/{parameterizable.ts → elementParameterizable.ts} +78 -89
  75. package/src/api/classes/event.ts +355 -370
  76. package/src/api/classes/eventBroadcaster.ts +54 -54
  77. package/src/api/classes/parameter.ts +277 -408
  78. package/src/api/classes/parameterObservable.ts +121 -99
  79. package/src/api/classes/placementAnimation.ts +133 -133
  80. package/src/api/classes/variant.ts +670 -806
  81. package/src/api/classes/variantInstance.ts +81 -88
  82. package/src/api/classes/viewer.ts +421 -473
  83. package/src/api/internal/debugViewer.ts +81 -81
  84. package/src/api/internal/lensRendering.ts +10 -10
  85. package/src/api/internal/sceneSetup.ts +204 -194
  86. package/src/api/manager/animationManager.ts +116 -116
  87. package/src/api/manager/sceneManager.ts +105 -105
  88. package/src/api/manager/variantInstanceManager.ts +236 -236
  89. package/src/api/store/specStorage.ts +53 -53
  90. package/src/api/util/babylonHelper.ts +392 -553
  91. package/src/api/util/globalTypes.ts +318 -369
  92. package/src/api/util/resourceHelper.ts +187 -157
  93. package/src/buildinfo.json +2 -2
  94. package/src/commonjs.tsconfig.json +13 -13
  95. package/src/declaration.tsconfig.json +10 -10
  96. package/src/dev.ts +46 -44
  97. package/src/es6.tsconfig.json +13 -13
  98. package/src/index.ts +87 -91
  99. package/src/pagesconfig.json +57 -52
  100. package/src/tsconfig.json +43 -43
  101. package/src/tsconfig.types.json +9 -9
  102. package/src/types.d.ts +4 -4
  103. package/src/api/classes/variantParameterizable.ts +0 -73
  104. package/src/api/classes/viewerLight.ts +0 -330
  105. package/src/api/util/stringHelper.ts +0 -26
@@ -1,539 +1,487 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Viewer = void 0;
4
- var tslib_1 = require("tslib");
5
- var arcRotateCamera_1 = require("@babylonjs/core/Cameras/arcRotateCamera");
6
- var boundingInfo_1 = require("@babylonjs/core/Culling/boundingInfo");
7
- var engine_1 = require("@babylonjs/core/Engines/engine");
8
- var highlightLayer_1 = require("@babylonjs/core/Layers/highlightLayer");
9
- var standardMaterial_1 = require("@babylonjs/core/Materials/standardMaterial");
10
- var dynamicTexture_1 = require("@babylonjs/core/Materials/Textures/dynamicTexture");
11
- var math_color_1 = require("@babylonjs/core/Maths/math.color");
12
- var math_vector_1 = require("@babylonjs/core/Maths/math.vector");
13
- var mesh_1 = require("@babylonjs/core/Meshes/mesh");
14
- var screenshotTools_1 = require("@babylonjs/core/Misc/screenshotTools");
15
- var lodash_es_1 = require("lodash-es");
16
- var buildinfo_json_1 = require("../../buildinfo.json");
17
- var sceneSetup_1 = require("../internal/sceneSetup");
18
- var animationManager_1 = require("../manager/animationManager");
19
- var sceneManager_1 = require("../manager/sceneManager");
20
- var variantInstanceManager_1 = require("../manager/variantInstanceManager");
21
- var specStorage_1 = require("../store/specStorage");
22
- var resourceHelper_1 = require("../util/resourceHelper");
23
- var event_1 = require("./event");
24
- var eventBroadcaster_1 = require("./eventBroadcaster");
25
- var parameter_1 = require("./parameter");
26
- var variant_1 = require("./variant");
27
- /**
28
- * The main exposed object. This is the entry point into the application
29
- *
30
- * ```js
31
- * const canvas = document.getElementById( 'babylon-canvas' );
32
- * const viewer = Viewer( canvas, '/path/to/index.json' );
33
- * ```
34
- * The class does nothing on its own and needs to {@link bootstrap}
35
- */
36
- var Viewer = /** @class */ (function (_super) {
37
- tslib_1.__extends(Viewer, _super);
38
- /**
39
- * Constructor
40
- */
41
- function Viewer(canvas, structureJson) {
42
- var _this = _super.call(this) || this;
43
- _this.canvas = canvas;
44
- _this.structureJson = structureJson;
45
- _this._scene = null;
46
- _this._animationManager = null;
47
- _this._sceneManager = null;
48
- _this._variantInstances = null;
49
- return _this;
50
- }
51
- Object.defineProperty(Viewer.prototype, "scene", {
52
- /**
53
- * Gets the BabylonJS Scene that is attached to the instance.
54
- *
55
- * @throws Error if the `scene` has not been initialized.
56
- */
57
- get: function () {
58
- if (!this._scene) {
59
- throw new Error("Scene has not been initialized.");
60
- }
61
- return this._scene;
62
- },
63
- enumerable: false,
64
- configurable: true
65
- });
66
- Object.defineProperty(Viewer.prototype, "sceneManager", {
67
- get: function () {
68
- if (!this._sceneManager) {
69
- throw new Error("Environment has not been initialized.");
70
- }
71
- return this._sceneManager;
72
- },
73
- enumerable: false,
74
- configurable: true
75
- });
76
- Object.defineProperty(Viewer.prototype, "engine", {
77
- /**
78
- * Gets the BabylonJS Engine that is attached to the viewer.
79
- */
80
- get: function () {
81
- return this.scene.getEngine();
82
- },
83
- enumerable: false,
84
- configurable: true
85
- });
86
- Object.defineProperty(Viewer.prototype, "variantInstances", {
87
- /**
88
- * Gets the {@link VariantInstanceManager} attached to the viewer.
89
- *
90
- * @throws Error if the {@link VariantInstanceManager} has not been initialized.
91
- */
92
- get: function () {
93
- if (!this._variantInstances) {
94
- throw Error("There is no variantInstanceManager.");
95
- }
96
- return this._variantInstances;
97
- },
98
- enumerable: false,
99
- configurable: true
100
- });
101
- Object.defineProperty(Viewer.prototype, "animationManager", {
102
- /**
103
- * Gets the {@link AnimationManager} attached to the viewer.
104
- *
105
- * @throws Error if the {@link AnimationManager} has not been initialized.
106
- */
107
- get: function () {
108
- if (!this._animationManager) {
109
- throw new Error("There is no animationManager instance.");
110
- }
111
- return this._animationManager;
112
- },
113
- enumerable: false,
114
- configurable: true
115
- });
116
- /**
117
- * Starts the application. This will
118
- * * load the given "index" JSON file
119
- * * setup the scene with the "scene" JSON file
120
- * * create an (optional) default setup with different variant settings
121
- * * sets up resizing by attaching a debounced version of {@link resize}
122
- *
123
- * @throws Error if any of the files is not found/valid
124
- *
125
- * @emits {@link Event.BOOTSTRAP_START}
126
- * @emits {@link Event.BOOTSTRAP_END}
127
- */
128
- Viewer.prototype.bootstrap = function () {
129
- return tslib_1.__awaiter(this, void 0, void 0, function () {
130
- var indexJson, sceneJson, _a, rootVariant, _b, setupJson;
131
- return tslib_1.__generator(this, function (_c) {
132
- switch (_c.label) {
133
- case 0:
134
- this.broadcastEvent(event_1.Event.BOOTSTRAP_START, this);
135
- if (!lodash_es_1.isString(this.structureJson)) return [3 /*break*/, 2];
136
- return [4 /*yield*/, resourceHelper_1.loadJson(this.structureJson)];
137
- case 1:
138
- indexJson = _c.sent();
139
- return [3 /*break*/, 3];
140
- case 2:
141
- indexJson = this.structureJson;
142
- _c.label = 3;
143
- case 3:
144
- if (!indexJson.scene) {
145
- throw new Error("No \"scene\" property found for bootstrapping.");
146
- }
147
- // fill spec store
148
- specStorage_1.SpecStorage.createFromSpec(indexJson);
149
- if (!lodash_es_1.isString(indexJson.scene)) return [3 /*break*/, 5];
150
- return [4 /*yield*/, resourceHelper_1.loadJson(indexJson.scene)];
151
- case 4:
152
- sceneJson = _c.sent();
153
- indexJson.scene = sceneJson;
154
- _c.label = 5;
155
- case 5:
156
- _a = this;
157
- return [4 /*yield*/, this.initScene()];
158
- case 6:
159
- _a._scene = _c.sent();
160
- return [4 /*yield*/, variant_1.Variant.create('_', indexJson, this)];
161
- case 7:
162
- rootVariant = _c.sent();
163
- _b = this;
164
- return [4 /*yield*/, variantInstanceManager_1.VariantInstanceManager.create(rootVariant)];
165
- case 8:
166
- _b._variantInstances = _c.sent();
167
- if (!indexJson.setup) return [3 /*break*/, 12];
168
- if (!lodash_es_1.isString(indexJson.setup)) return [3 /*break*/, 10];
169
- return [4 /*yield*/, resourceHelper_1.loadJson(indexJson.setup)];
170
- case 9:
171
- setupJson = _c.sent();
172
- indexJson.setup = setupJson;
173
- _c.label = 10;
174
- case 10: return [4 /*yield*/, this.createVariantInstances()];
175
- case 11:
176
- _c.sent();
177
- _c.label = 12;
178
- case 12:
179
- // resize handler
180
- window.addEventListener('resize', resourceHelper_1.debounce(this.resize.bind(this), 100));
181
- // event broadcasting
182
- this.broadcastEvent(event_1.Event.BOOTSTRAP_END, this);
183
- return [2 /*return*/, this];
184
- }
185
- });
186
- });
187
- };
188
- // Disabled for now to decrease bundle size.
189
- // See https://github.com/Combeenation/3d-viewer/issues/37
190
- // /**
191
- // * Enables the BabylonJS DebugLayer. If you pass options, be sure to pass an Object of type `IInspectorOptions`.
192
- // */
193
- // public async enableDebugLayer(options?: any) {
194
- // // @ts-ignore
195
- // await import(/* webpackChunkName: "debug-inspector" */'@babylonjs/inspector');
196
- // await this.scene.debugLayer.show(options);
197
- // return this;
198
- // }
199
- /**
200
- * Destroys all registered {@link VariantInstance}s that are registered
201
- */
202
- Viewer.prototype.destroyVariantInstances = function () {
203
- var _this = this;
204
- this.variantInstances.all.forEach(function (variantInstance) {
205
- _this.variantInstances.destroy(variantInstance.name);
206
- });
207
- return this;
208
- };
209
- /**
210
- * Trigger a resize event for the `Engine`
211
- */
212
- Viewer.prototype.resize = function () {
213
- this.engine.resize();
214
- return this;
215
- };
216
- /**
217
- * A convenience method for directly getting a Node from a {@link VariantInstance} and an {@link Element} by its
218
- * {@link DottedPath}s.
219
- */
220
- Viewer.prototype.getNode = function (variantInstanceName, elementDottedPath, nodeDottedPath) {
221
- return tslib_1.__awaiter(this, void 0, void 0, function () {
222
- var variantInstance;
223
- return tslib_1.__generator(this, function (_a) {
224
- switch (_a.label) {
225
- case 0: return [4 /*yield*/, this.variantInstances.get(variantInstanceName)];
226
- case 1:
227
- variantInstance = _a.sent();
228
- return [2 /*return*/, variantInstance.getNode(elementDottedPath, nodeDottedPath)];
229
- }
230
- });
231
- });
232
- };
233
- /**
234
- * A convenience method for directly getting a Node from a {@link VariantInstance} and an {@link Element} by its
235
- * {@link DottedPath}s.
236
- */
237
- Viewer.prototype.getMesh = function (variantInstanceName, elementDottedPath, meshDottedPath) {
238
- return tslib_1.__awaiter(this, void 0, void 0, function () {
239
- var variantInstance;
240
- return tslib_1.__generator(this, function (_a) {
241
- switch (_a.label) {
242
- case 0: return [4 /*yield*/, this.variantInstances.get(variantInstanceName)];
243
- case 1:
244
- variantInstance = _a.sent();
245
- return [2 /*return*/, variantInstance.getMesh(elementDottedPath, meshDottedPath)];
246
- }
247
- });
248
- });
249
- };
250
- /**
251
- * Switches the camera
252
- *
253
- * @emits {@link Event.CAMERA_SWITCHED}
254
- */
255
- Viewer.prototype.switchCamera = function (newCamera, reset) {
256
- if (reset === void 0) { reset = true; }
257
- var camera = this.scene.getCameraByName(newCamera);
258
- if (camera) {
259
- var activeCamera = this.scene.activeCamera;
260
- if (activeCamera) {
261
- activeCamera.detachControl(this.engine.getRenderingCanvas());
262
- }
263
- if (reset) {
264
- camera.restoreState();
265
- }
266
- this.scene.setActiveCameraByName(newCamera);
267
- camera.attachControl(this.engine.getRenderingCanvas());
268
- this.broadcastEvent(event_1.Event.CAMERA_SWITCHED, camera);
269
- }
270
- else {
271
- throw new Error("Given camera \"" + newCamera + "\" not found.");
272
- }
273
- // TODO: put traceable observers to new camera (@see element)
274
- return this;
275
- };
276
- /**
277
- * Moves or animates the active camera to given `placement`.
278
- */
279
- Viewer.prototype.moveActiveCameraTo = function (placement, animation) {
280
- return tslib_1.__awaiter(this, void 0, void 0, function () {
281
- return tslib_1.__generator(this, function (_a) {
282
- return [2 /*return*/, this.animationManager.animateToPlacement(this.sceneManager.activeCamera, placement, animation)];
283
- });
284
- });
285
- };
286
- /**
287
- * Takes a sceenshot the the current scene. The result is a string containing a base64 encoded image
288
- */
289
- Viewer.prototype.screenshot = function (settings) {
290
- var _this = this;
291
- return new Promise(function (resolve, reject) {
292
- var _a, _b, _c, _d, _e, _f;
293
- if (!_this.engine) {
294
- return reject('Engine is null');
295
- }
296
- if (!_this.scene) {
297
- return reject('Scene is null');
298
- }
299
- _this.scene.render(); // in combination with a render target, we need to refresh the scene manually to get the latest view
300
- 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);
301
- });
302
- };
303
- /**
304
- * Calculates the bounding box from all visible meshes on the scene.
305
- */
306
- Viewer.prototype.calculateBoundingBox = function () {
307
- if (this.scene.meshes.length === 0) {
308
- throw new Error('There are currently no meshes on the scene.');
309
- }
310
- var max = new math_vector_1.Vector3(0, 0, 0);
311
- var min = new math_vector_1.Vector3(0, 0, 0);
312
- this.scene.meshes.filter(function (mesh) { return mesh.isVisible; }).forEach(function (mesh) {
313
- max = math_vector_1.Vector3.Maximize(max, mesh.getBoundingInfo().boundingBox.maximumWorld);
314
- min = math_vector_1.Vector3.Minimize(min, mesh.getBoundingInfo().boundingBox.minimumWorld);
315
- });
316
- var boundingBox = this.scene.getMeshByName('__bounding_box__');
317
- if (!boundingBox) {
318
- boundingBox = new mesh_1.Mesh('__bounding_box__', this.scene);
319
- }
320
- boundingBox.setBoundingInfo(new boundingInfo_1.BoundingInfo(min, max));
321
- //boundingBox.showBoundingBox = true;
322
- return boundingBox;
323
- };
324
- /**
325
- * Focuses the camera to see every visible mesh in scene and tries to optimize wheel precision and panning.
326
- */
327
- Viewer.prototype.autofocusActiveCamera = function (settings) {
328
- var _a;
329
- var activeCamera = this.scene.activeCamera;
330
- if (!activeCamera) {
331
- throw new Error('No active camera found when using autofocus feature.');
332
- }
333
- if (activeCamera instanceof arcRotateCamera_1.ArcRotateCamera) {
334
- // calculate some values
335
- var boundingBox = this.calculateBoundingBox();
336
- var size = boundingBox.getBoundingInfo().maximum.subtract(boundingBox.getBoundingInfo().minimum);
337
- var radius = size.length() * ((_a = settings === null || settings === void 0 ? void 0 : settings.radiusFactor) !== null && _a !== void 0 ? _a : 1.5);
338
- if (!isFinite(radius)) {
339
- radius = 1;
340
- }
341
- // use helper camera to get some default values
342
- var helperCamera = new arcRotateCamera_1.ArcRotateCamera('__helper_camera__', Math.PI / -2, Math.PI / 2, radius, math_vector_1.Vector3.Zero(), this.scene);
343
- helperCamera.useFramingBehavior = true;
344
- helperCamera.setTarget(boundingBox);
345
- // translate values from helper to active camera
346
- activeCamera.setTarget(mesh_1.Mesh.Center([boundingBox]));
347
- activeCamera.alpha = helperCamera.alpha;
348
- activeCamera.beta = helperCamera.beta;
349
- activeCamera.minZ = helperCamera.minZ;
350
- activeCamera.maxZ = helperCamera.maxZ;
351
- activeCamera.radius = helperCamera.radius;
352
- activeCamera.lowerRadiusLimit = helperCamera.lowerRadiusLimit;
353
- activeCamera.upperRadiusLimit = helperCamera.upperRadiusLimit;
354
- if ((settings === null || settings === void 0 ? void 0 : settings.adjustWheelPrecision) !== false) {
355
- activeCamera.wheelPrecision = helperCamera.wheelPrecision;
356
- }
357
- if ((settings === null || settings === void 0 ? void 0 : settings.adjustPanningSensibility) !== false) {
358
- activeCamera.panningSensibility = helperCamera.panningSensibility;
359
- }
360
- if ((settings === null || settings === void 0 ? void 0 : settings.adjustPinchPrecision) !== false) {
361
- activeCamera.pinchPrecision = helperCamera.pinchPrecision;
362
- }
363
- // remove the helper camera
364
- helperCamera.dispose();
365
- }
366
- else {
367
- var cameraClsName = activeCamera.getClassName();
368
- throw new Error("Camera of type \"" + cameraClsName + "\" is not implemented yet to use autofocus feature.");
369
- }
370
- };
371
- /**
372
- * Resets everything by calling {@link destroy} to clear all references and {@link bootstrap} to setup a clean
373
- * environment
374
- */
375
- Viewer.prototype.reset = function () {
376
- return tslib_1.__awaiter(this, void 0, void 0, function () {
377
- return tslib_1.__generator(this, function (_a) {
378
- switch (_a.label) {
379
- case 0: return [4 /*yield*/, this.destroy()];
380
- case 1:
381
- _a.sent();
382
- return [2 /*return*/, this.bootstrap()];
383
- }
384
- });
385
- });
386
- };
387
- /**
388
- * Destroys
389
- *
390
- * * all {@link VariantInstance}s using {@link destroyVariantInstances}
391
- * * calling `dispose` on the `Engine` and `Scene`
392
- */
393
- Viewer.prototype.destroy = function () {
394
- this.destroyVariantInstances();
395
- this.scene.dispose();
396
- specStorage_1.SpecStorage.destroy();
397
- return this;
398
- };
399
- /**
400
- * Show coordinate system with given dimension (for debugging purpose).
401
- */
402
- Viewer.prototype.showWorldCoordinates = function (dimension) {
403
- var scene = this.scene;
404
- var makeTextPlane = function (text, color, size) {
405
- var dynamicTexture = new dynamicTexture_1.DynamicTexture('DynamicTexture', 50, scene, true);
406
- dynamicTexture.hasAlpha = true;
407
- dynamicTexture.drawText(text, 5, 40, 'bold 36px Arial', color, 'transparent', true);
408
- var plane = mesh_1.Mesh.CreatePlane('TextPlane', size, scene, true);
409
- plane.material = new standardMaterial_1.StandardMaterial('TextPlaneMaterial', scene);
410
- plane.material.backFaceCulling = false;
411
- // @ts-ignore
412
- plane.material.specularColor = new math_color_1.Color3(0, 0, 0);
413
- // @ts-ignore
414
- plane.material.diffuseTexture = dynamicTexture;
415
- return plane;
416
- };
417
- var axisX = mesh_1.Mesh.CreateLines('axisX', [
418
- math_vector_1.Vector3.Zero(),
419
- new math_vector_1.Vector3(dimension, 0, 0),
420
- new math_vector_1.Vector3(dimension * 0.95, 0.05 * dimension, 0),
421
- new math_vector_1.Vector3(dimension, 0, 0),
422
- new math_vector_1.Vector3(dimension * 0.95, -0.05 * dimension, 0)
423
- ], scene);
424
- axisX.color = new math_color_1.Color3(1, 0, 0);
425
- var xChar = makeTextPlane('X', 'red', dimension / 10);
426
- xChar.position = new math_vector_1.Vector3(0.9 * dimension, -0.05 * dimension, 0);
427
- var axisY = mesh_1.Mesh.CreateLines('axisY', [
428
- math_vector_1.Vector3.Zero(),
429
- new math_vector_1.Vector3(0, dimension, 0),
430
- new math_vector_1.Vector3(-0.05 * dimension, dimension * 0.95, 0),
431
- new math_vector_1.Vector3(0, dimension, 0),
432
- new math_vector_1.Vector3(0.05 * dimension, dimension * 0.95, 0)
433
- ], scene);
434
- axisY.color = new math_color_1.Color3(0, 1, 0);
435
- var yChar = makeTextPlane('Y', 'green', dimension / 10);
436
- yChar.position = new math_vector_1.Vector3(0, 0.9 * dimension, -0.05 * dimension);
437
- var axisZ = mesh_1.Mesh.CreateLines('axisZ', [
438
- math_vector_1.Vector3.Zero(),
439
- new math_vector_1.Vector3(0, 0, dimension),
440
- new math_vector_1.Vector3(0, -0.05 * dimension, dimension * 0.95),
441
- new math_vector_1.Vector3(0, 0, dimension),
442
- new math_vector_1.Vector3(0, 0.05 * dimension, dimension * 0.95)
443
- ], scene);
444
- axisZ.color = new math_color_1.Color3(0, 0, 1);
445
- var zChar = makeTextPlane('Z', 'blue', dimension / 10);
446
- zChar.position = new math_vector_1.Vector3(0, 0.05 * dimension, 0.9 * dimension);
447
- };
448
- /**
449
- * @emits {@link Event.SCENE_PROCESSING_START}
450
- * @emits {@link Event.SCENE_PROCESSING_END}
451
- */
452
- Viewer.prototype.initScene = function () {
453
- var _a, _b, _c;
454
- return tslib_1.__awaiter(this, void 0, void 0, function () {
455
- var sceneJson, engine, scene, _d, _e;
456
- var _this = this;
457
- return tslib_1.__generator(this, function (_f) {
458
- switch (_f.label) {
459
- case 0:
460
- sceneJson = specStorage_1.SpecStorage.get('scene');
461
- this.broadcastEvent(event_1.Event.SCENE_PROCESSING_START, sceneJson);
462
- 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);
463
- return [4 /*yield*/, sceneSetup_1.sceneSetup(engine, sceneJson)];
464
- case 1:
465
- scene = _f.sent();
466
- if (sceneJson.meshPicking) {
467
- new highlightLayer_1.HighlightLayer('default', scene);
468
- scene.onPointerPick = function (pointerEvent, pickInfo) {
469
- if (!pickInfo.hit) {
470
- return;
471
- }
472
- var mesh = pickInfo.pickedMesh;
473
- _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);
474
- if (mesh === null || mesh === void 0 ? void 0 : mesh.metadata.element) {
475
- _this.broadcastEvent(event_1.Event.ELEMENT_PICKED, mesh.metadata.element);
476
- }
477
- if (mesh === null || mesh === void 0 ? void 0 : mesh.metadata.variant) {
478
- if (mesh.metadata.variant.inheritedParameters[parameter_1.Parameter.HIGHLIGHT_ENABLED]) {
479
- mesh.metadata.variant.toggleHighlight();
480
- }
481
- _this.broadcastEvent(event_1.Event.VARIANT_PICKED, mesh.metadata.variant);
482
- }
483
- };
484
- }
485
- _d = this;
486
- return [4 /*yield*/, sceneManager_1.SceneManager.create(scene)];
487
- case 2:
488
- _d._sceneManager = _f.sent();
489
- _e = this;
490
- return [4 /*yield*/, animationManager_1.AnimationManager.create(scene)];
491
- case 3:
492
- _e._animationManager = _f.sent();
493
- this.broadcastEvent(event_1.Event.SCENE_PROCESSING_END, scene);
494
- engine.runRenderLoop(function () {
495
- scene.render();
496
- });
497
- return [2 /*return*/, scene];
498
- }
499
- });
500
- });
501
- };
502
- /**
503
- * Batch creation of multiple {@link VariantInstance} objects with a {@link SetupJson} object passed
504
- */
505
- Viewer.prototype.createVariantInstances = function () {
506
- return tslib_1.__awaiter(this, void 0, void 0, function () {
507
- var setupJson, instances, _i, _a, instanceDefinition, _b, _c;
508
- return tslib_1.__generator(this, function (_d) {
509
- switch (_d.label) {
510
- case 0:
511
- setupJson = specStorage_1.SpecStorage.get('setup');
512
- instances = [];
513
- _i = 0, _a = setupJson.instances;
514
- _d.label = 1;
515
- case 1:
516
- if (!(_i < _a.length)) return [3 /*break*/, 4];
517
- instanceDefinition = _a[_i];
518
- if (instanceDefinition.lazy) {
519
- this.variantInstances.register(instanceDefinition);
520
- return [3 /*break*/, 3];
521
- }
522
- _c = (_b = instances).push;
523
- return [4 /*yield*/, this.variantInstances.create(instanceDefinition.variant, instanceDefinition.name, instanceDefinition.parameters)];
524
- case 2:
525
- _c.apply(_b, [_d.sent()]);
526
- _d.label = 3;
527
- case 3:
528
- _i++;
529
- return [3 /*break*/, 1];
530
- case 4: return [2 /*return*/, instances];
531
- }
532
- });
533
- });
534
- };
535
- Viewer.version = buildinfo_json_1.version;
536
- return Viewer;
537
- }(eventBroadcaster_1.EventBroadcaster));
538
- exports.Viewer = Viewer;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Viewer = void 0;
4
+ var tslib_1 = require("tslib");
5
+ var arcRotateCamera_1 = require("@babylonjs/core/Cameras/arcRotateCamera");
6
+ var boundingInfo_1 = require("@babylonjs/core/Culling/boundingInfo");
7
+ var engine_1 = require("@babylonjs/core/Engines/engine");
8
+ var highlightLayer_1 = require("@babylonjs/core/Layers/highlightLayer");
9
+ var math_vector_1 = require("@babylonjs/core/Maths/math.vector");
10
+ var mesh_1 = require("@babylonjs/core/Meshes/mesh");
11
+ var screenshotTools_1 = require("@babylonjs/core/Misc/screenshotTools");
12
+ var lodash_es_1 = require("lodash-es");
13
+ var buildinfo_json_1 = require("../../buildinfo.json");
14
+ var sceneSetup_1 = require("../internal/sceneSetup");
15
+ var animationManager_1 = require("../manager/animationManager");
16
+ var sceneManager_1 = require("../manager/sceneManager");
17
+ var variantInstanceManager_1 = require("../manager/variantInstanceManager");
18
+ var specStorage_1 = require("../store/specStorage");
19
+ var resourceHelper_1 = require("../util/resourceHelper");
20
+ var event_1 = require("./event");
21
+ var eventBroadcaster_1 = require("./eventBroadcaster");
22
+ var parameter_1 = require("./parameter");
23
+ var variant_1 = require("./variant");
24
+ /**
25
+ * The main exposed object. This is the entry point into the application
26
+ *
27
+ * ```js
28
+ * const canvas = document.getElementById( 'babylon-canvas' );
29
+ * const viewer = Viewer( canvas, '/path/to/index.json' );
30
+ * ```
31
+ * The class does nothing on its own and needs to {@link bootstrap}
32
+ */
33
+ var Viewer = /** @class */ (function (_super) {
34
+ tslib_1.__extends(Viewer, _super);
35
+ /**
36
+ * Constructor
37
+ */
38
+ function Viewer(canvas, structureJson) {
39
+ var _this = _super.call(this) || this;
40
+ _this.canvas = canvas;
41
+ _this.structureJson = structureJson;
42
+ _this._scene = null;
43
+ _this._animationManager = null;
44
+ _this._sceneManager = null;
45
+ _this._variantInstances = null;
46
+ return _this;
47
+ }
48
+ Object.defineProperty(Viewer.prototype, "scene", {
49
+ /**
50
+ * Gets the BabylonJS Scene that is attached to the instance.
51
+ *
52
+ * @throws Error if the `scene` has not been initialized.
53
+ */
54
+ get: function () {
55
+ if (!this._scene) {
56
+ throw new Error("Scene has not been initialized.");
57
+ }
58
+ return this._scene;
59
+ },
60
+ enumerable: false,
61
+ configurable: true
62
+ });
63
+ Object.defineProperty(Viewer.prototype, "sceneManager", {
64
+ get: function () {
65
+ if (!this._sceneManager) {
66
+ throw new Error("Environment has not been initialized.");
67
+ }
68
+ return this._sceneManager;
69
+ },
70
+ enumerable: false,
71
+ configurable: true
72
+ });
73
+ Object.defineProperty(Viewer.prototype, "engine", {
74
+ /**
75
+ * Gets the BabylonJS Engine that is attached to the viewer.
76
+ */
77
+ get: function () {
78
+ return this.scene.getEngine();
79
+ },
80
+ enumerable: false,
81
+ configurable: true
82
+ });
83
+ Object.defineProperty(Viewer.prototype, "variantInstances", {
84
+ /**
85
+ * Gets the {@link VariantInstanceManager} attached to the viewer.
86
+ *
87
+ * @throws Error if the {@link VariantInstanceManager} has not been initialized.
88
+ */
89
+ get: function () {
90
+ if (!this._variantInstances) {
91
+ throw Error("There is no variantInstanceManager.");
92
+ }
93
+ return this._variantInstances;
94
+ },
95
+ enumerable: false,
96
+ configurable: true
97
+ });
98
+ Object.defineProperty(Viewer.prototype, "animationManager", {
99
+ /**
100
+ * Gets the {@link AnimationManager} attached to the viewer.
101
+ *
102
+ * @throws Error if the {@link AnimationManager} has not been initialized.
103
+ */
104
+ get: function () {
105
+ if (!this._animationManager) {
106
+ throw new Error("There is no animationManager instance.");
107
+ }
108
+ return this._animationManager;
109
+ },
110
+ enumerable: false,
111
+ configurable: true
112
+ });
113
+ /**
114
+ * Starts the application. This will
115
+ * * load the given "index" JSON file
116
+ * * setup the scene with the "scene" JSON file
117
+ * * create an (optional) default setup with different variant settings
118
+ * * sets up resizing by attaching a debounced version of {@link resize}
119
+ *
120
+ * @throws Error if any of the files is not found/valid
121
+ *
122
+ * @emits {@link Event.BOOTSTRAP_START}
123
+ * @emits {@link Event.BOOTSTRAP_END}
124
+ */
125
+ Viewer.prototype.bootstrap = function () {
126
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
127
+ var indexJson, sceneJson, _a, rootVariant, _b, setupJson;
128
+ return tslib_1.__generator(this, function (_c) {
129
+ switch (_c.label) {
130
+ case 0:
131
+ this.broadcastEvent(event_1.Event.BOOTSTRAP_START, this);
132
+ if (!lodash_es_1.isString(this.structureJson)) return [3 /*break*/, 2];
133
+ return [4 /*yield*/, resourceHelper_1.loadJson(this.structureJson)];
134
+ case 1:
135
+ indexJson = _c.sent();
136
+ return [3 /*break*/, 3];
137
+ case 2:
138
+ indexJson = this.structureJson;
139
+ _c.label = 3;
140
+ case 3:
141
+ if (!indexJson.scene) {
142
+ throw new Error("No \"scene\" property found for bootstrapping.");
143
+ }
144
+ // fill spec store
145
+ specStorage_1.SpecStorage.createFromSpec(indexJson);
146
+ if (!lodash_es_1.isString(indexJson.scene)) return [3 /*break*/, 5];
147
+ return [4 /*yield*/, resourceHelper_1.loadJson(indexJson.scene)];
148
+ case 4:
149
+ sceneJson = _c.sent();
150
+ indexJson.scene = sceneJson;
151
+ _c.label = 5;
152
+ case 5:
153
+ _a = this;
154
+ return [4 /*yield*/, this.initScene()];
155
+ case 6:
156
+ _a._scene = _c.sent();
157
+ return [4 /*yield*/, variant_1.Variant.create('_', indexJson, this)];
158
+ case 7:
159
+ rootVariant = _c.sent();
160
+ _b = this;
161
+ return [4 /*yield*/, variantInstanceManager_1.VariantInstanceManager.create(rootVariant)];
162
+ case 8:
163
+ _b._variantInstances = _c.sent();
164
+ if (!indexJson.setup) return [3 /*break*/, 12];
165
+ if (!lodash_es_1.isString(indexJson.setup)) return [3 /*break*/, 10];
166
+ return [4 /*yield*/, resourceHelper_1.loadJson(indexJson.setup)];
167
+ case 9:
168
+ setupJson = _c.sent();
169
+ indexJson.setup = setupJson;
170
+ _c.label = 10;
171
+ case 10: return [4 /*yield*/, this.createVariantInstances()];
172
+ case 11:
173
+ _c.sent();
174
+ _c.label = 12;
175
+ case 12:
176
+ // resize handler
177
+ window.addEventListener('resize', resourceHelper_1.debounce(this.resize.bind(this), 100));
178
+ // event broadcasting
179
+ this.broadcastEvent(event_1.Event.BOOTSTRAP_END, this);
180
+ return [2 /*return*/, this];
181
+ }
182
+ });
183
+ });
184
+ };
185
+ // Disabled for now to decrease bundle size.
186
+ // See https://github.com/Combeenation/3d-viewer/issues/37
187
+ // /**
188
+ // * Enables the BabylonJS DebugLayer. If you pass options, be sure to pass an Object of type `IInspectorOptions`.
189
+ // */
190
+ // public async enableDebugLayer(options?: any) {
191
+ // // @ts-ignore
192
+ // await import(/* webpackChunkName: "debug-inspector" */'@babylonjs/inspector');
193
+ // await this.scene.debugLayer.show(options);
194
+ // return this;
195
+ // }
196
+ /**
197
+ * Destroys all registered {@link VariantInstance}s that are registered
198
+ */
199
+ Viewer.prototype.destroyVariantInstances = function () {
200
+ var _this = this;
201
+ this.variantInstances.all.forEach(function (variantInstance) {
202
+ _this.variantInstances.destroy(variantInstance.name);
203
+ });
204
+ return this;
205
+ };
206
+ /**
207
+ * Trigger a resize event for the `Engine`
208
+ */
209
+ Viewer.prototype.resize = function () {
210
+ this.engine.resize();
211
+ return this;
212
+ };
213
+ /**
214
+ * A convenience method for directly getting a Node from a {@link VariantInstance} and an {@link Element} by its
215
+ * {@link DottedPath}s.
216
+ */
217
+ Viewer.prototype.getNode = function (variantInstanceName, elementDottedPath, nodeDottedPath) {
218
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
219
+ var variantInstance;
220
+ return tslib_1.__generator(this, function (_a) {
221
+ switch (_a.label) {
222
+ case 0: return [4 /*yield*/, this.variantInstances.get(variantInstanceName)];
223
+ case 1:
224
+ variantInstance = _a.sent();
225
+ return [2 /*return*/, variantInstance.getNode(elementDottedPath, nodeDottedPath)];
226
+ }
227
+ });
228
+ });
229
+ };
230
+ /**
231
+ * A convenience method for directly getting a Node from a {@link VariantInstance} and an {@link Element} by its
232
+ * {@link DottedPath}s.
233
+ */
234
+ Viewer.prototype.getMesh = function (variantInstanceName, elementDottedPath, meshDottedPath) {
235
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
236
+ var variantInstance;
237
+ return tslib_1.__generator(this, function (_a) {
238
+ switch (_a.label) {
239
+ case 0: return [4 /*yield*/, this.variantInstances.get(variantInstanceName)];
240
+ case 1:
241
+ variantInstance = _a.sent();
242
+ return [2 /*return*/, variantInstance.getMesh(elementDottedPath, meshDottedPath)];
243
+ }
244
+ });
245
+ });
246
+ };
247
+ /**
248
+ * Switches the camera
249
+ *
250
+ * @emits {@link Event.CAMERA_SWITCHED}
251
+ */
252
+ Viewer.prototype.switchCamera = function (newCamera, reset) {
253
+ if (reset === void 0) { reset = true; }
254
+ var camera = this.scene.getCameraByName(newCamera);
255
+ if (camera) {
256
+ var activeCamera = this.scene.activeCamera;
257
+ if (activeCamera) {
258
+ activeCamera.detachControl(this.engine.getRenderingCanvas());
259
+ }
260
+ if (reset) {
261
+ camera.restoreState();
262
+ }
263
+ this.scene.setActiveCameraByName(newCamera);
264
+ camera.attachControl(this.engine.getRenderingCanvas());
265
+ this.broadcastEvent(event_1.Event.CAMERA_SWITCHED, camera);
266
+ }
267
+ else {
268
+ throw new Error("Given camera \"" + newCamera + "\" not found.");
269
+ }
270
+ // TODO: put traceable observers to new camera (@see element)
271
+ return this;
272
+ };
273
+ /**
274
+ * Moves or animates the active camera to given `placement`.
275
+ */
276
+ Viewer.prototype.moveActiveCameraTo = function (placement, animation) {
277
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
278
+ return tslib_1.__generator(this, function (_a) {
279
+ return [2 /*return*/, this.animationManager.animateToPlacement(this.sceneManager.activeCamera, placement, animation)];
280
+ });
281
+ });
282
+ };
283
+ /**
284
+ * Takes a sceenshot the the current scene. The result is a string containing a base64 encoded image
285
+ */
286
+ Viewer.prototype.screenshot = function (settings) {
287
+ var _this = this;
288
+ return new Promise(function (resolve, reject) {
289
+ var _a, _b, _c, _d, _e, _f;
290
+ if (!_this.engine) {
291
+ return reject('Engine is null');
292
+ }
293
+ if (!_this.scene) {
294
+ return reject('Scene is null');
295
+ }
296
+ _this.scene.render(); // in combination with a render target, we need to refresh the scene manually to get the latest view
297
+ 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);
298
+ });
299
+ };
300
+ /**
301
+ * Calculates the bounding box from all visible meshes on the scene.
302
+ */
303
+ Viewer.prototype.calculateBoundingBox = function () {
304
+ if (this.scene.meshes.length === 0) {
305
+ throw new Error('There are currently no meshes on the scene.');
306
+ }
307
+ var max = new math_vector_1.Vector3(0, 0, 0);
308
+ var min = new math_vector_1.Vector3(0, 0, 0);
309
+ this.scene.meshes.filter(function (mesh) { return mesh.isVisible; }).forEach(function (mesh) {
310
+ max = math_vector_1.Vector3.Maximize(max, mesh.getBoundingInfo().boundingBox.maximumWorld);
311
+ min = math_vector_1.Vector3.Minimize(min, mesh.getBoundingInfo().boundingBox.minimumWorld);
312
+ });
313
+ var boundingBox = this.scene.getMeshByName('__bounding_box__');
314
+ if (!boundingBox) {
315
+ boundingBox = new mesh_1.Mesh('__bounding_box__', this.scene);
316
+ }
317
+ boundingBox.setBoundingInfo(new boundingInfo_1.BoundingInfo(min, max));
318
+ //boundingBox.showBoundingBox = true;
319
+ return boundingBox;
320
+ };
321
+ /**
322
+ * Focuses the camera to see every visible mesh in scene and tries to optimize wheel precision and panning.
323
+ */
324
+ Viewer.prototype.autofocusActiveCamera = function (settings) {
325
+ var _a;
326
+ var activeCamera = this.scene.activeCamera;
327
+ if (!activeCamera) {
328
+ throw new Error('No active camera found when using autofocus feature.');
329
+ }
330
+ if (activeCamera instanceof arcRotateCamera_1.ArcRotateCamera) {
331
+ // calculate some values
332
+ var boundingBox = this.calculateBoundingBox();
333
+ var size = boundingBox.getBoundingInfo().maximum.subtract(boundingBox.getBoundingInfo().minimum);
334
+ var radius = size.length() * ((_a = settings === null || settings === void 0 ? void 0 : settings.radiusFactor) !== null && _a !== void 0 ? _a : 1.5);
335
+ if (!isFinite(radius)) {
336
+ radius = 1;
337
+ }
338
+ // use helper camera to get some default values
339
+ var helperCamera = new arcRotateCamera_1.ArcRotateCamera('__helper_camera__', Math.PI / -2, Math.PI / 2, radius, math_vector_1.Vector3.Zero(), this.scene);
340
+ helperCamera.useFramingBehavior = true;
341
+ helperCamera.setTarget(boundingBox);
342
+ // translate values from helper to active camera
343
+ activeCamera.setTarget(mesh_1.Mesh.Center([boundingBox]));
344
+ activeCamera.alpha = helperCamera.alpha;
345
+ activeCamera.beta = helperCamera.beta;
346
+ activeCamera.minZ = helperCamera.minZ;
347
+ activeCamera.maxZ = helperCamera.maxZ;
348
+ activeCamera.radius = helperCamera.radius;
349
+ activeCamera.lowerRadiusLimit = helperCamera.lowerRadiusLimit;
350
+ activeCamera.upperRadiusLimit = helperCamera.upperRadiusLimit;
351
+ if ((settings === null || settings === void 0 ? void 0 : settings.adjustWheelPrecision) !== false) {
352
+ activeCamera.wheelPrecision = helperCamera.wheelPrecision;
353
+ }
354
+ if ((settings === null || settings === void 0 ? void 0 : settings.adjustPanningSensibility) !== false) {
355
+ activeCamera.panningSensibility = helperCamera.panningSensibility;
356
+ }
357
+ if ((settings === null || settings === void 0 ? void 0 : settings.adjustPinchPrecision) !== false) {
358
+ activeCamera.pinchPrecision = helperCamera.pinchPrecision;
359
+ }
360
+ // remove the helper camera
361
+ helperCamera.dispose();
362
+ }
363
+ else {
364
+ var cameraClsName = activeCamera.getClassName();
365
+ throw new Error("Camera of type \"" + cameraClsName + "\" is not implemented yet to use autofocus feature.");
366
+ }
367
+ };
368
+ /**
369
+ * Resets everything by calling {@link destroy} to clear all references and {@link bootstrap} to setup a clean
370
+ * environment
371
+ */
372
+ Viewer.prototype.reset = function () {
373
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
374
+ return tslib_1.__generator(this, function (_a) {
375
+ switch (_a.label) {
376
+ case 0: return [4 /*yield*/, this.destroy()];
377
+ case 1:
378
+ _a.sent();
379
+ return [2 /*return*/, this.bootstrap()];
380
+ }
381
+ });
382
+ });
383
+ };
384
+ /**
385
+ * Destroys
386
+ *
387
+ * * all {@link VariantInstance}s using {@link destroyVariantInstances}
388
+ * * calling `dispose` on the `Engine` and `Scene`
389
+ */
390
+ Viewer.prototype.destroy = function () {
391
+ this.destroyVariantInstances();
392
+ this.scene.dispose();
393
+ specStorage_1.SpecStorage.destroy();
394
+ return this;
395
+ };
396
+ /**
397
+ * @emits {@link Event.SCENE_PROCESSING_START}
398
+ * @emits {@link Event.SCENE_PROCESSING_END}
399
+ */
400
+ Viewer.prototype.initScene = function () {
401
+ var _a, _b, _c;
402
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
403
+ var sceneJson, engine, scene, _d, _e;
404
+ var _this = this;
405
+ return tslib_1.__generator(this, function (_f) {
406
+ switch (_f.label) {
407
+ case 0:
408
+ sceneJson = specStorage_1.SpecStorage.get('scene');
409
+ this.broadcastEvent(event_1.Event.SCENE_PROCESSING_START, sceneJson);
410
+ 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);
411
+ return [4 /*yield*/, sceneSetup_1.sceneSetup(engine, sceneJson)];
412
+ case 1:
413
+ scene = _f.sent();
414
+ if (sceneJson.meshPicking) {
415
+ new highlightLayer_1.HighlightLayer('default', scene);
416
+ scene.onPointerPick = function (pointerEvent, pickInfo) {
417
+ if (!pickInfo.hit) {
418
+ return;
419
+ }
420
+ var mesh = pickInfo.pickedMesh;
421
+ _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);
422
+ if (mesh === null || mesh === void 0 ? void 0 : mesh.metadata.element) {
423
+ _this.broadcastEvent(event_1.Event.ELEMENT_PICKED, mesh.metadata.element);
424
+ }
425
+ if (mesh === null || mesh === void 0 ? void 0 : mesh.metadata.variant) {
426
+ if (mesh.metadata.variant.inheritedParameters[parameter_1.Parameter.HIGHLIGHT_ENABLED]) {
427
+ mesh.metadata.variant.toggleHighlight();
428
+ }
429
+ _this.broadcastEvent(event_1.Event.VARIANT_PICKED, mesh.metadata.variant);
430
+ }
431
+ };
432
+ }
433
+ _d = this;
434
+ return [4 /*yield*/, sceneManager_1.SceneManager.create(scene)];
435
+ case 2:
436
+ _d._sceneManager = _f.sent();
437
+ _e = this;
438
+ return [4 /*yield*/, animationManager_1.AnimationManager.create(scene)];
439
+ case 3:
440
+ _e._animationManager = _f.sent();
441
+ this.broadcastEvent(event_1.Event.SCENE_PROCESSING_END, scene);
442
+ engine.runRenderLoop(function () {
443
+ scene.render();
444
+ });
445
+ return [2 /*return*/, scene];
446
+ }
447
+ });
448
+ });
449
+ };
450
+ /**
451
+ * Batch creation of multiple {@link VariantInstance} objects with a {@link SetupJson} object passed
452
+ */
453
+ Viewer.prototype.createVariantInstances = function () {
454
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
455
+ var setupJson, instances, _i, _a, instanceDefinition, _b, _c;
456
+ return tslib_1.__generator(this, function (_d) {
457
+ switch (_d.label) {
458
+ case 0:
459
+ setupJson = specStorage_1.SpecStorage.get('setup');
460
+ instances = [];
461
+ _i = 0, _a = setupJson.instances;
462
+ _d.label = 1;
463
+ case 1:
464
+ if (!(_i < _a.length)) return [3 /*break*/, 4];
465
+ instanceDefinition = _a[_i];
466
+ if (instanceDefinition.lazy) {
467
+ this.variantInstances.register(instanceDefinition);
468
+ return [3 /*break*/, 3];
469
+ }
470
+ _c = (_b = instances).push;
471
+ return [4 /*yield*/, this.variantInstances.create(instanceDefinition.variant, instanceDefinition.name, instanceDefinition.parameters)];
472
+ case 2:
473
+ _c.apply(_b, [_d.sent()]);
474
+ _d.label = 3;
475
+ case 3:
476
+ _i++;
477
+ return [3 /*break*/, 1];
478
+ case 4: return [2 /*return*/, instances];
479
+ }
480
+ });
481
+ });
482
+ };
483
+ Viewer.version = buildinfo_json_1.version;
484
+ return Viewer;
485
+ }(eventBroadcaster_1.EventBroadcaster));
486
+ exports.Viewer = Viewer;
539
487
  //# sourceMappingURL=viewer.js.map