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

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 (106) hide show
  1. package/README.md +114 -112
  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 -125
  7. package/dist/lib-cjs/api/classes/element.js +743 -682
  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 +326 -312
  12. package/dist/lib-cjs/api/classes/event.js +371 -357
  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 +259 -165
  17. package/dist/lib-cjs/api/classes/parameter.js +387 -267
  18. package/dist/lib-cjs/api/classes/parameter.js.map +1 -1
  19. package/dist/lib-cjs/api/classes/parameterObservable.d.ts +36 -36
  20. package/dist/lib-cjs/api/classes/parameterObservable.js +101 -126
  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 +149 -138
  26. package/dist/lib-cjs/api/classes/placementAnimation.js.map +1 -1
  27. package/dist/lib-cjs/api/classes/variant.d.ts +234 -190
  28. package/dist/lib-cjs/api/classes/variant.js +1147 -873
  29. package/dist/lib-cjs/api/classes/variant.js.map +1 -1
  30. package/dist/lib-cjs/api/classes/variantInstance.d.ts +45 -41
  31. package/dist/lib-cjs/api/classes/variantInstance.js +108 -98
  32. package/dist/lib-cjs/api/classes/variantInstance.js.map +1 -1
  33. package/dist/lib-cjs/api/classes/variantParameterizable.d.ts +17 -17
  34. package/dist/lib-cjs/api/classes/variantParameterizable.js +92 -92
  35. package/dist/lib-cjs/api/classes/viewer.d.ts +131 -128
  36. package/dist/lib-cjs/api/classes/viewer.js +560 -486
  37. package/dist/lib-cjs/api/classes/viewer.js.map +1 -1
  38. package/dist/lib-cjs/api/classes/viewerLight.d.ts +66 -65
  39. package/dist/lib-cjs/api/classes/viewerLight.js +389 -322
  40. package/dist/lib-cjs/api/classes/viewerLight.js.map +1 -1
  41. package/dist/lib-cjs/api/internal/debugViewer.d.ts +13 -13
  42. package/dist/lib-cjs/api/internal/debugViewer.js +87 -87
  43. package/dist/lib-cjs/api/internal/lensRendering.d.ts +8 -8
  44. package/dist/lib-cjs/api/internal/lensRendering.js +11 -11
  45. package/dist/lib-cjs/api/internal/sceneSetup.d.ts +10 -6
  46. package/dist/lib-cjs/api/internal/sceneSetup.js +231 -227
  47. package/dist/lib-cjs/api/internal/sceneSetup.js.map +1 -1
  48. package/dist/lib-cjs/api/manager/animationManager.d.ts +29 -29
  49. package/dist/lib-cjs/api/manager/animationManager.js +121 -121
  50. package/dist/lib-cjs/api/manager/sceneManager.d.ts +32 -32
  51. package/dist/lib-cjs/api/manager/sceneManager.js +132 -132
  52. package/dist/lib-cjs/api/manager/variantInstanceManager.d.ts +90 -90
  53. package/dist/lib-cjs/api/manager/variantInstanceManager.js +321 -321
  54. package/dist/lib-cjs/api/store/specStorage.d.ts +24 -24
  55. package/dist/lib-cjs/api/store/specStorage.js +51 -51
  56. package/dist/lib-cjs/api/util/babylonHelper.d.ts +175 -125
  57. package/dist/lib-cjs/api/util/babylonHelper.js +520 -368
  58. package/dist/lib-cjs/api/util/babylonHelper.js.map +1 -1
  59. package/dist/lib-cjs/api/util/globalTypes.d.ts +333 -279
  60. package/dist/lib-cjs/api/util/globalTypes.js +1 -1
  61. package/dist/lib-cjs/api/util/resourceHelper.d.ts +53 -51
  62. package/dist/lib-cjs/api/util/resourceHelper.js +240 -243
  63. package/dist/lib-cjs/api/util/resourceHelper.js.map +1 -1
  64. package/dist/lib-cjs/api/util/stringHelper.d.ts +9 -9
  65. package/dist/lib-cjs/api/util/stringHelper.js +25 -25
  66. package/dist/lib-cjs/buildinfo.json +3 -3
  67. package/dist/lib-cjs/index.d.ts +48 -46
  68. package/dist/lib-cjs/index.js +86 -82
  69. package/dist/lib-cjs/index.js.map +1 -1
  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 +644 -617
  74. package/src/api/classes/event.ts +370 -355
  75. package/src/api/classes/eventBroadcaster.ts +54 -54
  76. package/src/api/classes/parameter.ts +408 -277
  77. package/src/api/classes/parameterObservable.ts +99 -121
  78. package/src/api/classes/{elementParameterizable.ts → parameterizable.ts} +89 -78
  79. package/src/api/classes/placementAnimation.ts +143 -133
  80. package/src/api/classes/variant.ts +817 -670
  81. package/src/api/classes/variantInstance.ts +88 -81
  82. package/src/api/classes/variantParameterizable.ts +73 -0
  83. package/src/api/classes/viewer.ts +478 -421
  84. package/src/api/classes/viewerLight.ts +330 -0
  85. package/src/api/internal/debugViewer.ts +81 -81
  86. package/src/api/internal/lensRendering.ts +10 -10
  87. package/src/api/internal/sceneSetup.ts +194 -204
  88. package/src/api/manager/animationManager.ts +116 -116
  89. package/src/api/manager/sceneManager.ts +105 -105
  90. package/src/api/manager/variantInstanceManager.ts +236 -236
  91. package/src/api/store/specStorage.ts +53 -53
  92. package/src/api/util/babylonHelper.ts +553 -392
  93. package/src/api/util/globalTypes.ts +380 -319
  94. package/src/api/util/resourceHelper.ts +184 -187
  95. package/src/api/util/stringHelper.ts +26 -0
  96. package/src/buildinfo.json +2 -2
  97. package/src/commonjs.tsconfig.json +13 -13
  98. package/src/declaration.tsconfig.json +10 -10
  99. package/src/dev.ts +42 -46
  100. package/src/es6.tsconfig.json +13 -13
  101. package/src/index.ts +91 -87
  102. package/src/pagesconfig.json +61 -57
  103. package/src/tsconfig.json +43 -43
  104. package/src/tsconfig.types.json +9 -9
  105. package/src/types.d.ts +4 -4
  106. package/dist/webpack-stats.json +0 -0
@@ -1,487 +1,561 @@
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;
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;
487
561
  //# sourceMappingURL=viewer.js.map