@combeenation/3d-viewer 4.0.0-beta5 → 4.0.0-rc1

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