@combeenation/3d-viewer 3.1.1-alpha7 → 3.1.2
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.
- package/README.md +111 -111
- package/dist/lib-cjs/api/classes/animationInterface.d.ts +8 -8
- package/dist/lib-cjs/api/classes/animationInterface.js +1 -1
- package/dist/lib-cjs/api/classes/dottedPath.d.ts +79 -79
- package/dist/lib-cjs/api/classes/dottedPath.js +190 -190
- package/dist/lib-cjs/api/classes/element.d.ts +125 -126
- package/dist/lib-cjs/api/classes/element.js +678 -674
- package/dist/lib-cjs/api/classes/element.js.map +1 -1
- package/dist/lib-cjs/api/classes/elementParameterizable.d.ts +14 -14
- package/dist/lib-cjs/api/classes/elementParameterizable.js +134 -134
- package/dist/lib-cjs/api/classes/event.d.ts +312 -312
- package/dist/lib-cjs/api/classes/event.js +357 -357
- package/dist/lib-cjs/api/classes/eventBroadcaster.d.ts +26 -26
- package/dist/lib-cjs/api/classes/eventBroadcaster.js +53 -53
- package/dist/lib-cjs/api/classes/parameter.d.ts +165 -165
- package/dist/lib-cjs/api/classes/parameter.js +267 -267
- package/dist/lib-cjs/api/classes/parameterObservable.d.ts +36 -36
- package/dist/lib-cjs/api/classes/parameterObservable.js +126 -126
- package/dist/lib-cjs/api/classes/parameterizable.d.ts +15 -15
- package/dist/lib-cjs/api/classes/parameterizable.js +149 -149
- package/dist/lib-cjs/api/classes/placementAnimation.d.ts +38 -38
- package/dist/lib-cjs/api/classes/placementAnimation.js +138 -138
- package/dist/lib-cjs/api/classes/variant.d.ts +180 -190
- package/dist/lib-cjs/api/classes/variant.js +863 -873
- package/dist/lib-cjs/api/classes/variant.js.map +1 -1
- package/dist/lib-cjs/api/classes/variantInstance.d.ts +41 -41
- package/dist/lib-cjs/api/classes/variantInstance.js +98 -98
- package/dist/lib-cjs/api/classes/variantParameterizable.d.ts +17 -17
- package/dist/lib-cjs/api/classes/variantParameterizable.js +92 -92
- package/dist/lib-cjs/api/classes/viewer.d.ts +128 -128
- package/dist/lib-cjs/api/classes/viewer.js +486 -486
- package/dist/lib-cjs/api/classes/viewerLight.d.ts +65 -66
- package/dist/lib-cjs/api/classes/viewerLight.js +322 -389
- package/dist/lib-cjs/api/classes/viewerLight.js.map +1 -1
- package/dist/lib-cjs/api/internal/debugViewer.d.ts +13 -13
- package/dist/lib-cjs/api/internal/debugViewer.js +87 -87
- package/dist/lib-cjs/api/internal/lensRendering.d.ts +8 -8
- package/dist/lib-cjs/api/internal/lensRendering.js +11 -11
- package/dist/lib-cjs/api/internal/sceneSetup.d.ts +6 -6
- package/dist/lib-cjs/api/internal/sceneSetup.js +227 -227
- package/dist/lib-cjs/api/manager/animationManager.d.ts +29 -29
- package/dist/lib-cjs/api/manager/animationManager.js +121 -121
- package/dist/lib-cjs/api/manager/sceneManager.d.ts +32 -32
- package/dist/lib-cjs/api/manager/sceneManager.js +132 -132
- package/dist/lib-cjs/api/manager/variantInstanceManager.d.ts +90 -90
- package/dist/lib-cjs/api/manager/variantInstanceManager.js +321 -321
- package/dist/lib-cjs/api/store/specStorage.d.ts +24 -24
- package/dist/lib-cjs/api/store/specStorage.js +51 -51
- package/dist/lib-cjs/api/util/babylonHelper.d.ts +125 -125
- package/dist/lib-cjs/api/util/babylonHelper.js +368 -368
- package/dist/lib-cjs/api/util/globalTypes.d.ts +275 -275
- package/dist/lib-cjs/api/util/globalTypes.js +1 -1
- package/dist/lib-cjs/api/util/resourceHelper.d.ts +30 -30
- package/dist/lib-cjs/api/util/resourceHelper.js +247 -203
- package/dist/lib-cjs/api/util/resourceHelper.js.map +1 -1
- package/dist/lib-cjs/api/util/stringHelper.d.ts +9 -9
- package/dist/lib-cjs/api/util/stringHelper.js +25 -25
- package/dist/lib-cjs/buildinfo.json +3 -3
- package/dist/lib-cjs/index.d.ts +46 -46
- package/dist/lib-cjs/index.js +82 -82
- package/dist/webpack-stats.json +0 -0
- package/package.json +83 -83
- package/src/api/classes/animationInterface.ts +11 -11
- package/src/api/classes/dottedPath.ts +189 -189
- package/src/api/classes/element.ts +608 -606
- package/src/api/classes/elementParameterizable.ts +78 -78
- package/src/api/classes/event.ts +355 -355
- package/src/api/classes/eventBroadcaster.ts +54 -54
- package/src/api/classes/parameter.ts +277 -277
- package/src/api/classes/parameterObservable.ts +121 -121
- package/src/api/classes/placementAnimation.ts +133 -133
- package/src/api/classes/variant.ts +659 -670
- package/src/api/classes/variantInstance.ts +81 -81
- package/src/api/classes/viewer.ts +421 -421
- package/src/api/internal/debugViewer.ts +81 -81
- package/src/api/internal/lensRendering.ts +10 -10
- package/src/api/internal/sceneSetup.ts +203 -203
- package/src/api/manager/animationManager.ts +116 -116
- package/src/api/manager/sceneManager.ts +105 -105
- package/src/api/manager/variantInstanceManager.ts +236 -236
- package/src/api/store/specStorage.ts +53 -53
- package/src/api/util/babylonHelper.ts +392 -392
- package/src/api/util/globalTypes.ts +314 -314
- package/src/api/util/resourceHelper.ts +168 -155
- package/src/buildinfo.json +2 -2
- package/src/commonjs.tsconfig.json +13 -13
- package/src/declaration.tsconfig.json +10 -10
- package/src/dev.ts +44 -46
- package/src/es6.tsconfig.json +13 -13
- package/src/index.ts +87 -87
- package/src/pagesconfig.json +47 -47
- package/src/tsconfig.json +43 -43
- package/src/tsconfig.types.json +9 -9
- package/src/types.d.ts +4 -4
|
@@ -1,487 +1,487 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Viewer = void 0;
|
|
4
|
-
var tslib_1 = require("tslib");
|
|
5
|
-
var arcRotateCamera_1 = require("@babylonjs/core/Cameras/arcRotateCamera");
|
|
6
|
-
var boundingInfo_1 = require("@babylonjs/core/Culling/boundingInfo");
|
|
7
|
-
var engine_1 = require("@babylonjs/core/Engines/engine");
|
|
8
|
-
var highlightLayer_1 = require("@babylonjs/core/Layers/highlightLayer");
|
|
9
|
-
var 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 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;
|
|
487
487
|
//# sourceMappingURL=viewer.js.map
|