@combeenation/3d-viewer 14.0.1-rc1 → 15.0.0

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 (81) hide show
  1. package/README.md +9 -9
  2. package/dist/lib-cjs/buildinfo.json +3 -3
  3. package/dist/lib-cjs/commonjs.tsconfig.tsbuildinfo +1 -1
  4. package/dist/lib-cjs/index.d.ts +51 -62
  5. package/dist/lib-cjs/index.js +84 -94
  6. package/dist/lib-cjs/index.js.map +1 -1
  7. package/dist/lib-cjs/internal/cbn-custom-babylon-loader-plugin.d.ts +10 -10
  8. package/dist/lib-cjs/internal/cbn-custom-babylon-loader-plugin.js +131 -131
  9. package/dist/lib-cjs/internal/cbn-custom-babylon-loader-plugin.js.map +1 -1
  10. package/dist/lib-cjs/internal/cloning-helper.d.ts +19 -19
  11. package/dist/lib-cjs/internal/cloning-helper.js +163 -163
  12. package/dist/lib-cjs/internal/device-helper.d.ts +9 -9
  13. package/dist/lib-cjs/internal/device-helper.js +24 -24
  14. package/dist/lib-cjs/internal/geometry-helper.d.ts +21 -21
  15. package/dist/lib-cjs/internal/geometry-helper.js +145 -145
  16. package/dist/lib-cjs/internal/metadata-helper.d.ts +26 -26
  17. package/dist/lib-cjs/internal/metadata-helper.js +50 -50
  18. package/dist/lib-cjs/internal/paintable-helper.d.ts +40 -40
  19. package/dist/lib-cjs/internal/paintable-helper.js +234 -286
  20. package/dist/lib-cjs/internal/paintable-helper.js.map +1 -1
  21. package/dist/lib-cjs/internal/svg-helper.d.ts +4 -0
  22. package/dist/lib-cjs/internal/svg-helper.js +67 -0
  23. package/dist/lib-cjs/internal/svg-helper.js.map +1 -0
  24. package/dist/lib-cjs/internal/tags-helper.d.ts +12 -12
  25. package/dist/lib-cjs/internal/tags-helper.js +39 -37
  26. package/dist/lib-cjs/internal/tags-helper.js.map +1 -1
  27. package/dist/lib-cjs/internal/texture-parameter-helper.d.ts +37 -0
  28. package/dist/lib-cjs/internal/texture-parameter-helper.js +287 -0
  29. package/dist/lib-cjs/internal/texture-parameter-helper.js.map +1 -0
  30. package/dist/lib-cjs/manager/camera-manager.d.ts +110 -110
  31. package/dist/lib-cjs/manager/camera-manager.js +209 -206
  32. package/dist/lib-cjs/manager/camera-manager.js.map +1 -1
  33. package/dist/lib-cjs/manager/debug-manager.d.ts +60 -60
  34. package/dist/lib-cjs/manager/debug-manager.js +217 -217
  35. package/dist/lib-cjs/manager/event-manager.d.ts +52 -52
  36. package/dist/lib-cjs/manager/event-manager.js +71 -71
  37. package/dist/lib-cjs/manager/gltf-export-manager.d.ts +75 -84
  38. package/dist/lib-cjs/manager/gltf-export-manager.js +286 -290
  39. package/dist/lib-cjs/manager/gltf-export-manager.js.map +1 -1
  40. package/dist/lib-cjs/manager/material-manager.d.ts +35 -35
  41. package/dist/lib-cjs/manager/material-manager.js +125 -125
  42. package/dist/lib-cjs/manager/model-manager.d.ts +145 -145
  43. package/dist/lib-cjs/manager/model-manager.js +382 -382
  44. package/dist/lib-cjs/manager/parameter-manager.d.ts +228 -210
  45. package/dist/lib-cjs/manager/parameter-manager.js +573 -514
  46. package/dist/lib-cjs/manager/parameter-manager.js.map +1 -1
  47. package/dist/lib-cjs/manager/scene-manager.d.ts +45 -45
  48. package/dist/lib-cjs/manager/scene-manager.js +64 -64
  49. package/dist/lib-cjs/manager/texture-manager.d.ts +12 -12
  50. package/dist/lib-cjs/manager/texture-manager.js +43 -43
  51. package/dist/lib-cjs/viewer-error.d.ts +49 -48
  52. package/dist/lib-cjs/viewer-error.js +61 -60
  53. package/dist/lib-cjs/viewer-error.js.map +1 -1
  54. package/dist/lib-cjs/viewer.d.ts +115 -115
  55. package/dist/lib-cjs/viewer.js +217 -217
  56. package/dist/lib-cjs/viewer.js.map +1 -1
  57. package/package.json +94 -91
  58. package/src/buildinfo.json +3 -3
  59. package/src/dev.ts +47 -47
  60. package/src/global-types.d.ts +39 -39
  61. package/src/index.ts +71 -81
  62. package/src/internal/cbn-custom-babylon-loader-plugin.ts +159 -159
  63. package/src/internal/cloning-helper.ts +225 -225
  64. package/src/internal/device-helper.ts +25 -25
  65. package/src/internal/geometry-helper.ts +181 -181
  66. package/src/internal/metadata-helper.ts +63 -63
  67. package/src/internal/paintable-helper.ts +258 -310
  68. package/src/internal/svg-helper.ts +52 -0
  69. package/src/internal/tags-helper.ts +43 -41
  70. package/src/internal/texture-parameter-helper.ts +353 -0
  71. package/src/manager/camera-manager.ts +368 -365
  72. package/src/manager/debug-manager.ts +245 -245
  73. package/src/manager/event-manager.ts +72 -72
  74. package/src/manager/gltf-export-manager.ts +356 -357
  75. package/src/manager/material-manager.ts +135 -135
  76. package/src/manager/model-manager.ts +458 -458
  77. package/src/manager/parameter-manager.ts +730 -652
  78. package/src/manager/scene-manager.ts +101 -101
  79. package/src/manager/texture-manager.ts +32 -32
  80. package/src/viewer-error.ts +69 -68
  81. package/src/viewer.ts +290 -290
@@ -1,383 +1,383 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.ModelManager = void 0;
13
- const index_1 = require("../index");
14
- const cloning_helper_1 = require("../internal/cloning-helper");
15
- const metadata_helper_1 = require("../internal/metadata-helper");
16
- const lodash_es_1 = require("lodash-es");
17
- /**
18
- * Manager for handling 3d models.\
19
- * Responsible for loading models and handling their visibility.\
20
- * Also contains advanced features like model cloning.
21
- */
22
- class ModelManager {
23
- constructor(viewer) {
24
- this.viewer = viewer;
25
- this._modelAssets = {};
26
- this._fallbackModelAsset = {
27
- name: ModelManager.CBN_FALLBACK_MODEL_ASSET_NAME,
28
- url: '',
29
- state: 'loaded',
30
- assetContainer: new index_1.AssetContainer(),
31
- isClone: false,
32
- };
33
- this._loadModelPromises = {};
34
- }
35
- get modelNames() {
36
- return Object.keys(this._modelAssets);
37
- }
38
- /**
39
- * Nested models were targeted with slash notation previously, as Spec couldn't work with dots.
40
- * Dots were interpreted as inherited model.
41
- * Now the Hive viewer parameters are returning slash notated asset names, whereas the asset names from the
42
- * prepacked asset manager are dot notated.
43
- * This function converts slash to dot notation, so that models can be target with slash and dot notation alike.
44
- */
45
- static _replaceSlashes(inputStr) {
46
- return inputStr.split('/').join('.');
47
- }
48
- /**
49
- * Creates content for the fallback model, which is actually just a black cube.
50
- * This is used for 3d assets that are linked in a data source but not available (yet).
51
- * The Combeenation backend exchanges the missing 3d asset name with "$fallback", which indicates that we shouldn't
52
- * throw an error but just show the fallback model instead.
53
- *
54
- * @internal
55
- */
56
- createFallbackModel() {
57
- const fallbackCube = index_1.MeshBuilder.CreateBox('$fallbackCube', { size: 1 }, this.viewer.scene);
58
- fallbackCube._parentContainer = this._fallbackModelAsset.assetContainer;
59
- this._fallbackModelAsset.assetContainer.meshes.push(fallbackCube);
60
- // NOTE: this is theoretically async, but in reality it's not when getting the material description from the CBN
61
- // server
62
- // also this is just a fallback that shouldn't be relevant in productive configurators
63
- this.viewer.materialManager.setMaterialOnMesh(index_1.MaterialManager.CBN_FALLBACK_MATERIAL_NAME, fallbackCube);
64
- this._fallbackModelAsset.assetContainer.removeFromScene();
65
- }
66
- /**
67
- * Register models for 3d viewer, this is required for each model before it can be loaded/shown.\
68
- * The "viewer control" inside the Combeenation framework calls this function automatically with all assigned
69
- * 3d assets.
70
- */
71
- registerModels(modelAssets) {
72
- for (const { name, url } of modelAssets) {
73
- const existingModel = this._modelAssets[name];
74
- if (existingModel) {
75
- console.warn(`Model ${name} is already registered`);
76
- return;
77
- }
78
- const model = { name, url, state: 'notLoaded', assetContainer: new index_1.AssetContainer(), isClone: false };
79
- this._modelAssets[name] = model;
80
- }
81
- }
82
- /**
83
- * Loads the model and all it's assigned materials.\
84
- * This function should be used for "eager loading" of models, so that model switching will be instant in the
85
- * process.\
86
- * If you don't plan to do this, just use {@link setModelVisibility} as this function will load the model under the
87
- * hood the first time the model gets visible.
88
- */
89
- loadModel(name) {
90
- return __awaiter(this, void 0, void 0, function* () {
91
- const model = this._getModel(name);
92
- if (!model) {
93
- throw new index_1.ViewerError({
94
- id: index_1.ViewerErrorIds.ModelNotRegistered,
95
- message: `Can't load model "${name}" as model is not registered`,
96
- });
97
- }
98
- if (model.state !== 'notLoaded') {
99
- console.warn(`Model ${name} is already loaded or currently loading`);
100
- return;
101
- }
102
- yield this._loadModel(model);
103
- yield this._prepareModelForScene(model);
104
- });
105
- }
106
- /**
107
- * Enables/disables multiple one or multiple models simultaniously.\
108
- * All required models are loaded before adjusting the visibility to avoid flashing in the 3d scene.
109
- *
110
- * @returns Array of changed visibility status, combined with the corresponding model name
111
- */
112
- setModelVisibility(modelVisibility) {
113
- var _a;
114
- return __awaiter(this, void 0, void 0, function* () {
115
- const showModels = [];
116
- const hideModels = [];
117
- if (!(0, lodash_es_1.isArray)(modelVisibility)) {
118
- modelVisibility = [modelVisibility];
119
- }
120
- for (const entry of modelVisibility) {
121
- const model = this._getModel(entry.name);
122
- if (!model) {
123
- throw new index_1.ViewerError({
124
- id: index_1.ViewerErrorIds.ModelNotRegistered,
125
- message: `Can't set visibility of model "${entry.name}" as model is not registered`,
126
- });
127
- }
128
- // there can be multiple "setModelVisibility" calls while the model is loading
129
- // loading can be awaited, but it has to be ensured, that the last call of "setModelVisibility" has priority
130
- // therefore we store the id of the call in the model
131
- const curVisibilityCallId = ((_a = model.visibilityCallId) !== null && _a !== void 0 ? _a : 0) + 1;
132
- model.visibilityCallId = curVisibilityCallId;
133
- if (entry.visible) {
134
- if (model.state === 'notLoaded') {
135
- yield this._loadModel(model);
136
- // check if this is still the latest visibility call
137
- if (model.visibilityCallId === curVisibilityCallId) {
138
- showModels.push(model);
139
- }
140
- }
141
- else if (model.state === 'loading') {
142
- yield this._loadModelPromises[model.name];
143
- if (model.visibilityCallId === curVisibilityCallId) {
144
- showModels.push(model);
145
- }
146
- }
147
- else if (model.state === 'loaded') {
148
- showModels.push(model);
149
- }
150
- }
151
- else {
152
- if (model.state === 'loading') {
153
- yield this._loadModelPromises[model.name];
154
- if (model.visibilityCallId === curVisibilityCallId) {
155
- hideModels.push(model);
156
- }
157
- }
158
- else if (model.state === 'inScene') {
159
- hideModels.push(model);
160
- }
161
- }
162
- }
163
- for (const showModel of showModels) {
164
- yield this._prepareModelForScene(showModel);
165
- }
166
- for (const showModel of showModels) {
167
- yield this._showModel(showModel, true);
168
- }
169
- for (const hideModel of hideModels) {
170
- this._hideModel(hideModel);
171
- }
172
- const returnVal = [];
173
- showModels.forEach(showModel => returnVal.push({ name: showModel.name, visible: true }));
174
- hideModels.forEach(hideModel => returnVal.push({ name: hideModel.name, visible: false }));
175
- return returnVal;
176
- });
177
- }
178
- /**
179
- * Create a clone of an existing model.\
180
- * Only the geometry (meshes, transform nodes) gets cloned, as materials are typically shared across the whole scene.
181
- *
182
- * @param show show model immediately after cloning
183
- * @param options additional options for the cloning procedure, like renaming algorithms
184
- */
185
- cloneModel(name, newModelName, show = true, options) {
186
- return __awaiter(this, void 0, void 0, function* () {
187
- const sourceModel = this._getModel(name);
188
- if (!sourceModel) {
189
- throw new index_1.ViewerError({
190
- id: index_1.ViewerErrorIds.ModelNotRegistered,
191
- message: `Can't clone model "${name}" as model is not registered`,
192
- });
193
- }
194
- const existingModel = this._getModel(newModelName);
195
- if (existingModel) {
196
- throw new index_1.ViewerError({
197
- id: index_1.ViewerErrorIds.ModelAlreadyExists,
198
- message: `Can't create clone as model "${newModelName}" already exists`,
199
- });
200
- }
201
- if (sourceModel.state === 'notLoaded') {
202
- yield this._loadModel(sourceModel);
203
- }
204
- else if (sourceModel.state === 'loading') {
205
- yield this._loadModelPromises[sourceModel.name];
206
- }
207
- const clonedModel = {
208
- name: newModelName,
209
- url: sourceModel.url,
210
- state: 'loaded',
211
- assetContainer: (0, cloning_helper_1.cloneModelAssetContainer)(sourceModel.assetContainer, newModelName, {
212
- nodeNamingStrategy: options === null || options === void 0 ? void 0 : options.nodeNamingStrategy,
213
- tagNamingStrategy: options === null || options === void 0 ? void 0 : options.tagNamingStrategy,
214
- }, this.viewer.scene),
215
- isClone: true,
216
- };
217
- this._modelAssets[newModelName] = clonedModel;
218
- if (show) {
219
- yield this._showModel(clonedModel);
220
- }
221
- });
222
- }
223
- /**
224
- * Removes a cloned model from the scene and from the internal model storage.\
225
- * Deleted clones can not be shown again.
226
- */
227
- deleteClonedModel(name) {
228
- const model = this._getModel(name);
229
- if (!model) {
230
- throw new index_1.ViewerError({
231
- id: index_1.ViewerErrorIds.ModelNotRegistered,
232
- message: `Can't delete cloned model "${name}" as model is not registered`,
233
- });
234
- }
235
- if (!model.isClone) {
236
- throw new index_1.ViewerError({
237
- id: index_1.ViewerErrorIds.ModelIsNotAClone,
238
- message: `Can't delete model "${name}" as model is not a clone`,
239
- });
240
- }
241
- model.assetContainer.dispose();
242
- delete this._modelAssets[name];
243
- }
244
- /**
245
- * Removes all cloned models from the scene and the internal model storage.
246
- */
247
- deleteAllClonedModels() {
248
- const clonedModelNames = Object.values(this._modelAssets)
249
- .filter(model => model.isClone)
250
- .map(model => model.name);
251
- clonedModelNames.forEach(modelName => this.deleteClonedModel(modelName));
252
- }
253
- /**
254
- * Loads and returns the asset container of a certain model.\
255
- * This can be used to access the models content without having to add it to the scene.\
256
- * A typical use case is to clone or instantiate a node from a "library" model.
257
- */
258
- getAssetContainerOfModel(name) {
259
- return __awaiter(this, void 0, void 0, function* () {
260
- const model = this._getModel(name);
261
- if (!model) {
262
- throw new index_1.ViewerError({
263
- id: index_1.ViewerErrorIds.ModelNotRegistered,
264
- message: `Can't get asset container of model "${name}" as model is not registered`,
265
- });
266
- }
267
- if (model.state === 'notLoaded') {
268
- yield this._loadModel(model);
269
- }
270
- else if (model.state === 'loading') {
271
- yield this._loadModelPromises[model.name];
272
- }
273
- if (model.state !== 'inScene') {
274
- yield this._prepareModelForScene(model);
275
- }
276
- return model.assetContainer;
277
- });
278
- }
279
- /**
280
- * Get model by name
281
- */
282
- _getModel(name) {
283
- if (name === '$fallback') {
284
- return this._fallbackModelAsset;
285
- }
286
- const replacedName = ModelManager._replaceSlashes(name);
287
- const model = this._modelAssets[replacedName];
288
- return model;
289
- }
290
- /**
291
- * Load model into scene, but don't show it immediately
292
- */
293
- _loadModel(model) {
294
- return __awaiter(this, void 0, void 0, function* () {
295
- const loadModelPromise = () => __awaiter(this, void 0, void 0, function* () {
296
- model.state = 'loading';
297
- const curEnvTexture = this.viewer.scene.environmentTexture;
298
- const curEnvIntensity = this.viewer.scene.environmentIntensity;
299
- let assetContainer;
300
- try {
301
- assetContainer = yield index_1.SceneLoader.LoadAssetContainerAsync('', model.url, this.viewer.scene);
302
- }
303
- catch (e) {
304
- throw new index_1.ViewerError({
305
- id: index_1.ViewerErrorIds.AssetLoadingFailed,
306
- message: `Error loading model "${model.name}" from url "${model.url}":\n${e.message}`,
307
- });
308
- }
309
- // remove all lights and cameras from the asset, as this data should be handled globally in the scene instead of
310
- // being in a model context
311
- [...assetContainer.lights].forEach(light => light.dispose());
312
- [...assetContainer.cameras].forEach(camera => camera.dispose());
313
- // environment texture and intensity has been overwritten by load asset container function
314
- this.viewer.scene.environmentTexture = curEnvTexture;
315
- this.viewer.scene.environmentIntensity = curEnvIntensity;
316
- model.assetContainer = assetContainer;
317
- model.state = 'loaded';
318
- delete this._loadModelPromises[model.name];
319
- });
320
- this._loadModelPromises[model.name] = loadModelPromise();
321
- return this._loadModelPromises[model.name];
322
- });
323
- }
324
- /**
325
- * Apply parameter to the model in the asset container.
326
- * This is typically done before the model is added to the scene to avoid changes on the visible model, which ensures
327
- * a smooth model switch behaviour.
328
- */
329
- _prepareModelForScene(model) {
330
- return __awaiter(this, void 0, void 0, function* () {
331
- yield this.viewer.parameterManager.applyAllParameterValuesToModel(model.assetContainer);
332
- // parameter manager did his job, now apply the deferred materials to all meshes that are not explicitely hidden by
333
- // the parameter manager
334
- yield this._applyDeferredMaterialsForAllVisibleMeshes(model);
335
- });
336
- }
337
- /**
338
- * Add assets of asset container to scene, do the scene preparation if required.
339
- * CAUTION: model is expected to be in the correct loading state already.
340
- *
341
- * @param skipPreparation optionally skip applying parameter values to model before showing it
342
- * (see {@link _prepareModelForScene})
343
- */
344
- _showModel(model, skipPreparation) {
345
- return __awaiter(this, void 0, void 0, function* () {
346
- if (!skipPreparation) {
347
- yield this._prepareModelForScene(model);
348
- }
349
- model.assetContainer.addToScene();
350
- model.state = 'inScene';
351
- });
352
- }
353
- /**
354
- * Remove assets of asset container from scene
355
- */
356
- _hideModel(model) {
357
- model.assetContainer.removeFromScene();
358
- model.state = 'loaded';
359
- }
360
- /**
361
- * Creates and assigns each "deferred" material to the corresponding mesh, if the mesh is visible.
362
- * Model should be ready to use immediately after this function has done it's job.
363
- */
364
- _applyDeferredMaterialsForAllVisibleMeshes(model) {
365
- return __awaiter(this, void 0, void 0, function* () {
366
- for (const mesh of model.assetContainer.meshes) {
367
- const deferredMaterial = (0, metadata_helper_1.getInternalMetadataValue)(mesh, 'deferredMaterial');
368
- const rawVisibleValue = this.viewer.parameterManager.getParameterValueOfNode(mesh, index_1.BuiltInParameter.Visible);
369
- const visible = rawVisibleValue === undefined || index_1.ParameterManager.parseBoolean(rawVisibleValue) === true;
370
- if (deferredMaterial && visible) {
371
- yield this.viewer.materialManager.setMaterialOnMesh(deferredMaterial, mesh);
372
- }
373
- }
374
- });
375
- }
376
- }
377
- exports.ModelManager = ModelManager;
378
- /**
379
- * CAUTION: this has to be in sync with the Combeenation backend!
380
- * @internal
381
- */
382
- ModelManager.CBN_FALLBACK_MODEL_ASSET_NAME = '$fallback';
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.ModelManager = void 0;
13
+ const index_1 = require("../index");
14
+ const cloning_helper_1 = require("../internal/cloning-helper");
15
+ const metadata_helper_1 = require("../internal/metadata-helper");
16
+ const lodash_es_1 = require("lodash-es");
17
+ /**
18
+ * Manager for handling 3d models.\
19
+ * Responsible for loading models and handling their visibility.\
20
+ * Also contains advanced features like model cloning.
21
+ */
22
+ class ModelManager {
23
+ constructor(viewer) {
24
+ this.viewer = viewer;
25
+ this._modelAssets = {};
26
+ this._fallbackModelAsset = {
27
+ name: ModelManager.CBN_FALLBACK_MODEL_ASSET_NAME,
28
+ url: '',
29
+ state: 'loaded',
30
+ assetContainer: new index_1.AssetContainer(),
31
+ isClone: false,
32
+ };
33
+ this._loadModelPromises = {};
34
+ }
35
+ get modelNames() {
36
+ return Object.keys(this._modelAssets);
37
+ }
38
+ /**
39
+ * Nested models were targeted with slash notation previously, as Spec couldn't work with dots.
40
+ * Dots were interpreted as inherited model.
41
+ * Now the Hive viewer parameters are returning slash notated asset names, whereas the asset names from the
42
+ * prepacked asset manager are dot notated.
43
+ * This function converts slash to dot notation, so that models can be target with slash and dot notation alike.
44
+ */
45
+ static _replaceSlashes(inputStr) {
46
+ return inputStr.split('/').join('.');
47
+ }
48
+ /**
49
+ * Creates content for the fallback model, which is actually just a black cube.
50
+ * This is used for 3d assets that are linked in a data source but not available (yet).
51
+ * The Combeenation backend exchanges the missing 3d asset name with "$fallback", which indicates that we shouldn't
52
+ * throw an error but just show the fallback model instead.
53
+ *
54
+ * @internal
55
+ */
56
+ createFallbackModel() {
57
+ const fallbackCube = index_1.MeshBuilder.CreateBox('$fallbackCube', { size: 1 }, this.viewer.scene);
58
+ fallbackCube._parentContainer = this._fallbackModelAsset.assetContainer;
59
+ this._fallbackModelAsset.assetContainer.meshes.push(fallbackCube);
60
+ // NOTE: this is theoretically async, but in reality it's not when getting the material description from the CBN
61
+ // server
62
+ // also this is just a fallback that shouldn't be relevant in productive configurators
63
+ this.viewer.materialManager.setMaterialOnMesh(index_1.MaterialManager.CBN_FALLBACK_MATERIAL_NAME, fallbackCube);
64
+ this._fallbackModelAsset.assetContainer.removeFromScene();
65
+ }
66
+ /**
67
+ * Register models for 3d viewer, this is required for each model before it can be loaded/shown.\
68
+ * The "viewer control" inside the Combeenation framework calls this function automatically with all assigned
69
+ * 3d assets.
70
+ */
71
+ registerModels(modelAssets) {
72
+ for (const { name, url } of modelAssets) {
73
+ const existingModel = this._modelAssets[name];
74
+ if (existingModel) {
75
+ console.warn(`Model ${name} is already registered`);
76
+ return;
77
+ }
78
+ const model = { name, url, state: 'notLoaded', assetContainer: new index_1.AssetContainer(), isClone: false };
79
+ this._modelAssets[name] = model;
80
+ }
81
+ }
82
+ /**
83
+ * Loads the model and all it's assigned materials.\
84
+ * This function should be used for "eager loading" of models, so that model switching will be instant in the
85
+ * process.\
86
+ * If you don't plan to do this, just use {@link setModelVisibility} as this function will load the model under the
87
+ * hood the first time the model gets visible.
88
+ */
89
+ loadModel(name) {
90
+ return __awaiter(this, void 0, void 0, function* () {
91
+ const model = this._getModel(name);
92
+ if (!model) {
93
+ throw new index_1.ViewerError({
94
+ id: index_1.ViewerErrorIds.ModelNotRegistered,
95
+ message: `Can't load model "${name}" as model is not registered`,
96
+ });
97
+ }
98
+ if (model.state !== 'notLoaded') {
99
+ console.warn(`Model ${name} is already loaded or currently loading`);
100
+ return;
101
+ }
102
+ yield this._loadModel(model);
103
+ yield this._prepareModelForScene(model);
104
+ });
105
+ }
106
+ /**
107
+ * Enables/disables multiple one or multiple models simultaniously.\
108
+ * All required models are loaded before adjusting the visibility to avoid flashing in the 3d scene.
109
+ *
110
+ * @returns Array of changed visibility status, combined with the corresponding model name
111
+ */
112
+ setModelVisibility(modelVisibility) {
113
+ var _a;
114
+ return __awaiter(this, void 0, void 0, function* () {
115
+ const showModels = [];
116
+ const hideModels = [];
117
+ if (!(0, lodash_es_1.isArray)(modelVisibility)) {
118
+ modelVisibility = [modelVisibility];
119
+ }
120
+ for (const entry of modelVisibility) {
121
+ const model = this._getModel(entry.name);
122
+ if (!model) {
123
+ throw new index_1.ViewerError({
124
+ id: index_1.ViewerErrorIds.ModelNotRegistered,
125
+ message: `Can't set visibility of model "${entry.name}" as model is not registered`,
126
+ });
127
+ }
128
+ // there can be multiple "setModelVisibility" calls while the model is loading
129
+ // loading can be awaited, but it has to be ensured, that the last call of "setModelVisibility" has priority
130
+ // therefore we store the id of the call in the model
131
+ const curVisibilityCallId = ((_a = model.visibilityCallId) !== null && _a !== void 0 ? _a : 0) + 1;
132
+ model.visibilityCallId = curVisibilityCallId;
133
+ if (entry.visible) {
134
+ if (model.state === 'notLoaded') {
135
+ yield this._loadModel(model);
136
+ // check if this is still the latest visibility call
137
+ if (model.visibilityCallId === curVisibilityCallId) {
138
+ showModels.push(model);
139
+ }
140
+ }
141
+ else if (model.state === 'loading') {
142
+ yield this._loadModelPromises[model.name];
143
+ if (model.visibilityCallId === curVisibilityCallId) {
144
+ showModels.push(model);
145
+ }
146
+ }
147
+ else if (model.state === 'loaded') {
148
+ showModels.push(model);
149
+ }
150
+ }
151
+ else {
152
+ if (model.state === 'loading') {
153
+ yield this._loadModelPromises[model.name];
154
+ if (model.visibilityCallId === curVisibilityCallId) {
155
+ hideModels.push(model);
156
+ }
157
+ }
158
+ else if (model.state === 'inScene') {
159
+ hideModels.push(model);
160
+ }
161
+ }
162
+ }
163
+ for (const showModel of showModels) {
164
+ yield this._prepareModelForScene(showModel);
165
+ }
166
+ for (const showModel of showModels) {
167
+ yield this._showModel(showModel, true);
168
+ }
169
+ for (const hideModel of hideModels) {
170
+ this._hideModel(hideModel);
171
+ }
172
+ const returnVal = [];
173
+ showModels.forEach(showModel => returnVal.push({ name: showModel.name, visible: true }));
174
+ hideModels.forEach(hideModel => returnVal.push({ name: hideModel.name, visible: false }));
175
+ return returnVal;
176
+ });
177
+ }
178
+ /**
179
+ * Create a clone of an existing model.\
180
+ * Only the geometry (meshes, transform nodes) gets cloned, as materials are typically shared across the whole scene.
181
+ *
182
+ * @param show show model immediately after cloning
183
+ * @param options additional options for the cloning procedure, like renaming algorithms
184
+ */
185
+ cloneModel(name, newModelName, show = true, options) {
186
+ return __awaiter(this, void 0, void 0, function* () {
187
+ const sourceModel = this._getModel(name);
188
+ if (!sourceModel) {
189
+ throw new index_1.ViewerError({
190
+ id: index_1.ViewerErrorIds.ModelNotRegistered,
191
+ message: `Can't clone model "${name}" as model is not registered`,
192
+ });
193
+ }
194
+ const existingModel = this._getModel(newModelName);
195
+ if (existingModel) {
196
+ throw new index_1.ViewerError({
197
+ id: index_1.ViewerErrorIds.ModelAlreadyExists,
198
+ message: `Can't create clone as model "${newModelName}" already exists`,
199
+ });
200
+ }
201
+ if (sourceModel.state === 'notLoaded') {
202
+ yield this._loadModel(sourceModel);
203
+ }
204
+ else if (sourceModel.state === 'loading') {
205
+ yield this._loadModelPromises[sourceModel.name];
206
+ }
207
+ const clonedModel = {
208
+ name: newModelName,
209
+ url: sourceModel.url,
210
+ state: 'loaded',
211
+ assetContainer: (0, cloning_helper_1.cloneModelAssetContainer)(sourceModel.assetContainer, newModelName, {
212
+ nodeNamingStrategy: options === null || options === void 0 ? void 0 : options.nodeNamingStrategy,
213
+ tagNamingStrategy: options === null || options === void 0 ? void 0 : options.tagNamingStrategy,
214
+ }, this.viewer.scene),
215
+ isClone: true,
216
+ };
217
+ this._modelAssets[newModelName] = clonedModel;
218
+ if (show) {
219
+ yield this._showModel(clonedModel);
220
+ }
221
+ });
222
+ }
223
+ /**
224
+ * Removes a cloned model from the scene and from the internal model storage.\
225
+ * Deleted clones can not be shown again.
226
+ */
227
+ deleteClonedModel(name) {
228
+ const model = this._getModel(name);
229
+ if (!model) {
230
+ throw new index_1.ViewerError({
231
+ id: index_1.ViewerErrorIds.ModelNotRegistered,
232
+ message: `Can't delete cloned model "${name}" as model is not registered`,
233
+ });
234
+ }
235
+ if (!model.isClone) {
236
+ throw new index_1.ViewerError({
237
+ id: index_1.ViewerErrorIds.ModelIsNotAClone,
238
+ message: `Can't delete model "${name}" as model is not a clone`,
239
+ });
240
+ }
241
+ model.assetContainer.dispose();
242
+ delete this._modelAssets[name];
243
+ }
244
+ /**
245
+ * Removes all cloned models from the scene and the internal model storage.
246
+ */
247
+ deleteAllClonedModels() {
248
+ const clonedModelNames = Object.values(this._modelAssets)
249
+ .filter(model => model.isClone)
250
+ .map(model => model.name);
251
+ clonedModelNames.forEach(modelName => this.deleteClonedModel(modelName));
252
+ }
253
+ /**
254
+ * Loads and returns the asset container of a certain model.\
255
+ * This can be used to access the models content without having to add it to the scene.\
256
+ * A typical use case is to clone or instantiate a node from a "library" model.
257
+ */
258
+ getAssetContainerOfModel(name) {
259
+ return __awaiter(this, void 0, void 0, function* () {
260
+ const model = this._getModel(name);
261
+ if (!model) {
262
+ throw new index_1.ViewerError({
263
+ id: index_1.ViewerErrorIds.ModelNotRegistered,
264
+ message: `Can't get asset container of model "${name}" as model is not registered`,
265
+ });
266
+ }
267
+ if (model.state === 'notLoaded') {
268
+ yield this._loadModel(model);
269
+ }
270
+ else if (model.state === 'loading') {
271
+ yield this._loadModelPromises[model.name];
272
+ }
273
+ if (model.state !== 'inScene') {
274
+ yield this._prepareModelForScene(model);
275
+ }
276
+ return model.assetContainer;
277
+ });
278
+ }
279
+ /**
280
+ * Get model by name
281
+ */
282
+ _getModel(name) {
283
+ if (name === '$fallback') {
284
+ return this._fallbackModelAsset;
285
+ }
286
+ const replacedName = ModelManager._replaceSlashes(name);
287
+ const model = this._modelAssets[replacedName];
288
+ return model;
289
+ }
290
+ /**
291
+ * Load model into scene, but don't show it immediately
292
+ */
293
+ _loadModel(model) {
294
+ return __awaiter(this, void 0, void 0, function* () {
295
+ const loadModelPromise = () => __awaiter(this, void 0, void 0, function* () {
296
+ model.state = 'loading';
297
+ const curEnvTexture = this.viewer.scene.environmentTexture;
298
+ const curEnvIntensity = this.viewer.scene.environmentIntensity;
299
+ let assetContainer;
300
+ try {
301
+ assetContainer = yield index_1.SceneLoader.LoadAssetContainerAsync('', model.url, this.viewer.scene);
302
+ }
303
+ catch (e) {
304
+ throw new index_1.ViewerError({
305
+ id: index_1.ViewerErrorIds.AssetLoadingFailed,
306
+ message: `Error loading model "${model.name}" from url "${model.url}":\n${e.message}`,
307
+ });
308
+ }
309
+ // remove all lights and cameras from the asset, as this data should be handled globally in the scene instead of
310
+ // being in a model context
311
+ [...assetContainer.lights].forEach(light => light.dispose());
312
+ [...assetContainer.cameras].forEach(camera => camera.dispose());
313
+ // environment texture and intensity has been overwritten by load asset container function
314
+ this.viewer.scene.environmentTexture = curEnvTexture;
315
+ this.viewer.scene.environmentIntensity = curEnvIntensity;
316
+ model.assetContainer = assetContainer;
317
+ model.state = 'loaded';
318
+ delete this._loadModelPromises[model.name];
319
+ });
320
+ this._loadModelPromises[model.name] = loadModelPromise();
321
+ return this._loadModelPromises[model.name];
322
+ });
323
+ }
324
+ /**
325
+ * Apply parameter to the model in the asset container.
326
+ * This is typically done before the model is added to the scene to avoid changes on the visible model, which ensures
327
+ * a smooth model switch behaviour.
328
+ */
329
+ _prepareModelForScene(model) {
330
+ return __awaiter(this, void 0, void 0, function* () {
331
+ yield this.viewer.parameterManager.applyAllParameterValuesToModel(model.assetContainer);
332
+ // parameter manager did his job, now apply the deferred materials to all meshes that are not explicitely hidden by
333
+ // the parameter manager
334
+ yield this._applyDeferredMaterialsForAllVisibleMeshes(model);
335
+ });
336
+ }
337
+ /**
338
+ * Add assets of asset container to scene, do the scene preparation if required.
339
+ * CAUTION: model is expected to be in the correct loading state already.
340
+ *
341
+ * @param skipPreparation optionally skip applying parameter values to model before showing it
342
+ * (see {@link _prepareModelForScene})
343
+ */
344
+ _showModel(model, skipPreparation) {
345
+ return __awaiter(this, void 0, void 0, function* () {
346
+ if (!skipPreparation) {
347
+ yield this._prepareModelForScene(model);
348
+ }
349
+ model.assetContainer.addToScene();
350
+ model.state = 'inScene';
351
+ });
352
+ }
353
+ /**
354
+ * Remove assets of asset container from scene
355
+ */
356
+ _hideModel(model) {
357
+ model.assetContainer.removeFromScene();
358
+ model.state = 'loaded';
359
+ }
360
+ /**
361
+ * Creates and assigns each "deferred" material to the corresponding mesh, if the mesh is visible.
362
+ * Model should be ready to use immediately after this function has done it's job.
363
+ */
364
+ _applyDeferredMaterialsForAllVisibleMeshes(model) {
365
+ return __awaiter(this, void 0, void 0, function* () {
366
+ for (const mesh of model.assetContainer.meshes) {
367
+ const deferredMaterial = (0, metadata_helper_1.getInternalMetadataValue)(mesh, 'deferredMaterial');
368
+ const rawVisibleValue = this.viewer.parameterManager.getParameterValueOfNode(mesh, index_1.BuiltInParameter.Visible);
369
+ const visible = rawVisibleValue === undefined || index_1.ParameterManager.parseBoolean(rawVisibleValue) === true;
370
+ if (deferredMaterial && visible) {
371
+ yield this.viewer.materialManager.setMaterialOnMesh(deferredMaterial, mesh);
372
+ }
373
+ }
374
+ });
375
+ }
376
+ }
377
+ exports.ModelManager = ModelManager;
378
+ /**
379
+ * CAUTION: this has to be in sync with the Combeenation backend!
380
+ * @internal
381
+ */
382
+ ModelManager.CBN_FALLBACK_MODEL_ASSET_NAME = '$fallback';
383
383
  //# sourceMappingURL=model-manager.js.map