@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,515 +1,574 @@
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.ParameterManager = exports.BuiltInParameter = void 0;
13
- const index_1 = require("../index");
14
- const metadata_helper_1 = require("../internal/metadata-helper");
15
- const paintable_helper_1 = require("../internal/paintable-helper");
16
- const tags_helper_1 = require("../internal/tags-helper");
17
- const lodash_es_1 = require("lodash-es");
18
- /**
19
- * Parameters with a built in observer implementation
20
- */
21
- exports.BuiltInParameter = {
22
- Visible: 'visible',
23
- Material: 'material',
24
- Position: 'position',
25
- Rotation: 'rotation',
26
- Scaling: 'scaling',
27
- Color: 'color',
28
- Roughness: 'roughness',
29
- Metallic: 'metallic',
30
- Paintable: 'paintable',
31
- };
32
- const isTagParameterSubject = (subject) => 'tagName' in subject && !!subject.tagName;
33
- const isNodeParameterSubject = (subject) => 'nodeName' in subject && !!subject.nodeName;
34
- const isMaterialParameterSubject = (subject) => 'materialName' in subject && !!subject.materialName;
35
- class ParameterManager {
36
- constructor(viewer) {
37
- this.viewer = viewer;
38
- this._parameterEntries = [];
39
- this._parameterObserver = {};
40
- this._addBuiltInParameterObservers();
41
- }
42
- /**
43
- * Parses and converts input to a boolean value, valid values are:
44
- * - true / false
45
- * - 1 / 0
46
- */
47
- static parseBoolean(value) {
48
- if (value.toString() === 'true' || value.toString() === '1') {
49
- return true;
50
- }
51
- else if (value.toString() === 'false' || value.toString() === '0') {
52
- return false;
53
- }
54
- throw new index_1.ViewerError({
55
- id: index_1.ViewerErrorIds.InvalidParameterValue,
56
- message: `Unable to parse "${value}" to a boolean`,
57
- });
58
- }
59
- /**
60
- * Parses and converts input to a number value
61
- */
62
- static parseNumber(value) {
63
- return parseFloat(value.toString());
64
- }
65
- /**
66
- * Parses and converts input to a string value
67
- */
68
- static parseString(value) {
69
- return value.toString();
70
- }
71
- // TODO WTT: enable setting Vector3 on the input directly
72
- /**
73
- * Parses a string of format "(x,y,z)"" to a "Vector3".
74
- */
75
- static parseVector(value) {
76
- if (!(0, lodash_es_1.isString)(value)) {
77
- throw new index_1.ViewerError({
78
- id: index_1.ViewerErrorIds.InvalidParameterValue,
79
- message: `Unable to parse "${value}" to a vector: not a string`,
80
- });
81
- }
82
- let cleanedValue = value.split(' ').join('');
83
- if (cleanedValue.startsWith('(') && cleanedValue.endsWith(')')) {
84
- cleanedValue = cleanedValue.substring(1, cleanedValue.length - 1);
85
- const [x, y, z] = cleanedValue.split(',').map(value => parseFloat(value));
86
- return new index_1.Vector3(x, y, z);
87
- }
88
- else {
89
- throw new index_1.ViewerError({
90
- id: index_1.ViewerErrorIds.InvalidParameterValue,
91
- message: `Unable to parse "${value}" to a vector: expected "(x,y,z)"`,
92
- });
93
- }
94
- }
95
- // TODO WTT: enable setting Vector3 on the input directly (maybe quaternion as well)
96
- /**
97
- * Parses a string of format `'(x,y,z)'` with angular degrees to a `Vector3` with rotation information in radians.
98
- */
99
- static parseRotation(value) {
100
- const rotation = ParameterManager.parseVector(value);
101
- const deg2rad = (deg) => {
102
- return (deg * Math.PI) / 180;
103
- };
104
- return rotation.set(deg2rad(rotation.x), deg2rad(rotation.y), deg2rad(rotation.z));
105
- }
106
- // TODO WTT: enable setting Color3 on the input directly
107
- /**
108
- * Parses a string of format `'#rrggbb'` or `'(r,g,b)'` to a `Color3`.
109
- */
110
- static parseColor(value) {
111
- const cleanedValue = value.toString().split(' ').join('');
112
- if (cleanedValue.startsWith('#')) {
113
- return index_1.Color3.FromHexString(value.toString());
114
- }
115
- if (cleanedValue.startsWith('(') && cleanedValue.endsWith(')')) {
116
- const rgb = cleanedValue.substring(1, cleanedValue.length - 1);
117
- const [r, g, b] = rgb.split(',').map(value => parseFloat(value));
118
- return index_1.Color3.FromInts(r, g, b);
119
- }
120
- const humanReadable = (0, lodash_es_1.capitalize)(cleanedValue);
121
- if (Object.prototype.hasOwnProperty.call(index_1.Color3, humanReadable)) {
122
- return index_1.Color3[humanReadable]();
123
- }
124
- throw new index_1.ViewerError({
125
- id: index_1.ViewerErrorIds.InvalidParameterValue,
126
- message: `Unable to parse "${value}" to a color: expected "#rrggbb", "(r,g,b)" or any human readable (e.g. Red) property implemented in Color3`,
127
- });
128
- }
129
- /**
130
- * Set parameter value for a certain node and calls the corresponding observer if the value has changed
131
- *
132
- * @returns "true" if parameter value has changed
133
- */
134
- setNodeParameterValue(nodeName, parameterName, value) {
135
- return __awaiter(this, void 0, void 0, function* () {
136
- const valueChanged = this._addParameterValue({ nodeName }, parameterName, value);
137
- if (valueChanged) {
138
- yield this._applyParameterValue({ nodeName }, parameterName);
139
- }
140
- return valueChanged;
141
- });
142
- }
143
- /**
144
- * Set parameter value for a certain material and calls the corresponding observer if the value has changed
145
- *
146
- * @returns "true" if parameter value has changed
147
- */
148
- setMaterialParameterValue(materialName, parameterName, value) {
149
- return __awaiter(this, void 0, void 0, function* () {
150
- const valueChanged = this._addParameterValue({ materialName }, parameterName, value);
151
- if (valueChanged) {
152
- yield this._applyParameterValue({ materialName }, parameterName);
153
- }
154
- return valueChanged;
155
- });
156
- }
157
- /**
158
- * Set parameter value for a certain tag and calls the corresponding observer if the value has changed.\
159
- * Setting a parameter value on a tag can affect multiple nodes and tags, depending which of these objects contains
160
- * the desired tag.
161
- *
162
- * @returns "true" if parameter value has changed
163
- */
164
- setTagParameterValue(tagName, parameterName, value) {
165
- return __awaiter(this, void 0, void 0, function* () {
166
- const valueChanged = this._addParameterValue({ tagName }, parameterName, value);
167
- if (valueChanged) {
168
- yield this._applyParameterValue({ tagName }, parameterName);
169
- }
170
- return valueChanged;
171
- });
172
- }
173
- /**
174
- * Set multiple parameter values simultaniously.\
175
- * Tag parameters are applied before node and material parameters, node and materials are more specific and should
176
- * have priority.
177
- *
178
- * @returns Array of parameters, which have changed values
179
- */
180
- setParameterValues(values) {
181
- return __awaiter(this, void 0, void 0, function* () {
182
- const parameterEntries = values.map(valueEntry => {
183
- const subject = isNodeParameterSubject(valueEntry)
184
- ? { nodeName: valueEntry.nodeName }
185
- : isMaterialParameterSubject(valueEntry)
186
- ? { materialName: valueEntry.materialName }
187
- : { tagName: valueEntry.tagName };
188
- return { subject, parameterName: valueEntry.parameterName, value: valueEntry.value, oldValue: undefined };
189
- });
190
- const changedParameterEntries = parameterEntries.filter(paramEntry => this._addParameterValue(paramEntry.subject, paramEntry.parameterName, paramEntry.value));
191
- yield this._applyParameterValues(changedParameterEntries);
192
- // convert back to original typing
193
- const changedParameterValues = changedParameterEntries.map(paramEntry => {
194
- return Object.assign(Object.assign({}, paramEntry.subject), { parameterName: paramEntry.parameterName, value: paramEntry.value });
195
- });
196
- return changedParameterValues;
197
- });
198
- }
199
- /**
200
- * @returns desired parameter value or "undefined" if parameter entry is not available.
201
- */
202
- getParameterValue(subject, parameterName) {
203
- const entry = this._getEntry(subject, parameterName);
204
- return entry === null || entry === void 0 ? void 0 : entry.value;
205
- }
206
- /**
207
- * Define observer callback for certain parameter.\
208
- * There can only be one observer for a certain parameter name.\
209
- * Parameter observers can not be overwritten once they are defined, this also includes system observers for
210
- * {@link BuiltInParameter}.
211
- */
212
- setParameterObserver(parameterName, observer) {
213
- if (this._parameterObserver[parameterName]) {
214
- console.warn(`Observer for parameter "${parameterName}" already set`);
215
- return;
216
- }
217
- this._parameterObserver[parameterName] = observer;
218
- }
219
- /**
220
- * Print all parameter entries in table format into the console
221
- */
222
- printAllParameters() {
223
- const printable = this._parameterEntries.map(entry => (Object.assign(Object.assign({}, entry), { subject: JSON.stringify(entry.subject) })));
224
- console.table(printable);
225
- }
226
- /**
227
- * Applies all existing parameter entries to a certain "model", as defined in the {@link ModelManager}.\
228
- * This can be usefull when updating the model before showing it in the scene.
229
- *
230
- * @internal
231
- */
232
- applyAllParameterValuesToModel(model) {
233
- return __awaiter(this, void 0, void 0, function* () {
234
- const parameterEntriesToApply = this._parameterEntries;
235
- yield this._applyParameterValues(parameterEntriesToApply, model);
236
- });
237
- }
238
- /**
239
- * Applies all parameter values which are targeting a material.\
240
- * This can be usefull when updating a material definition before creating it.
241
- *
242
- * @internal
243
- */
244
- applyParameterValuesToMaterial(material) {
245
- return __awaiter(this, void 0, void 0, function* () {
246
- const tags = (0, tags_helper_1.getTags)(material);
247
- const parameterEntriesToApply = [];
248
- tags.forEach(tagName => {
249
- const tagParamEntries = this._getEntriesOfSubject({ tagName });
250
- parameterEntriesToApply.push(...tagParamEntries);
251
- });
252
- const materialParamEntries = this._getEntriesOfSubject({ materialName: material.id });
253
- parameterEntriesToApply.push(...materialParamEntries);
254
- yield this._applyParameterValues(parameterEntriesToApply);
255
- });
256
- }
257
- /**
258
- * @returns Desired parameter value of a certain node.
259
- * Tags are considered as well but have lower priority than node parameters, as these are more specific.
260
- *
261
- * @internal
262
- */
263
- getParameterValueOfNode(node, parameterName) {
264
- const nodeParamValue = this.getParameterValue({ nodeName: node.name }, parameterName);
265
- if (nodeParamValue !== undefined) {
266
- return nodeParamValue;
267
- }
268
- const tags = (0, tags_helper_1.getTags)(node);
269
- const tagParamValue = tags.reduce((accValue, curTag) => {
270
- // NOTE: it is possible that values are available for multiple tags
271
- // in this case the resulting parameter value is quite "random" as the last tag in the tag string of the node has
272
- // priority
273
- const tagParamValue = this.getParameterValue({ tagName: curTag }, parameterName);
274
- return accValue !== null && accValue !== void 0 ? accValue : tagParamValue;
275
- }, undefined);
276
- return tagParamValue;
277
- }
278
- /**
279
- * @returns Desired parameter value of a certain material.
280
- * Tags are considered as well but have lower priority than material parameters, as these are more specific.
281
- * Unused ATM, but added for consistency as counter part for {@link getParameterValueOfNode}
282
- *
283
- * @internal
284
- */
285
- getParameterValueOfMaterial(material, parameterName) {
286
- const materialParamValue = this.getParameterValue({ materialName: material.name }, parameterName);
287
- if (materialParamValue !== undefined) {
288
- return materialParamValue;
289
- }
290
- const tags = (0, tags_helper_1.getTags)(material);
291
- const tagParamValue = tags.reduce((accValue, curTag) => {
292
- const tagParamValue = this.getParameterValue({ tagName: curTag }, parameterName);
293
- return accValue !== null && accValue !== void 0 ? accValue : tagParamValue;
294
- }, undefined);
295
- return tagParamValue;
296
- }
297
- /**
298
- * Parameter observer implementation of default parameters
299
- */
300
- _addBuiltInParameterObservers() {
301
- this.setParameterObserver(exports.BuiltInParameter.Visible, ({ nodes, newValue }) => __awaiter(this, void 0, void 0, function* () {
302
- const visible = ParameterManager.parseBoolean(newValue);
303
- for (const node of nodes) {
304
- if (visible) {
305
- const deferredMaterial = (0, metadata_helper_1.getInternalMetadataValue)(node, 'deferredMaterial');
306
- if (deferredMaterial) {
307
- yield this.viewer.materialManager.setMaterialOnMesh(deferredMaterial, node);
308
- }
309
- node.setEnabled(true);
310
- }
311
- else {
312
- node.setEnabled(false);
313
- }
314
- }
315
- }));
316
- this.setParameterObserver(exports.BuiltInParameter.Material, ({ nodes, newValue }) => __awaiter(this, void 0, void 0, function* () {
317
- const material = ParameterManager.parseString(newValue);
318
- for (const node of nodes) {
319
- // NOTE: don't use node.isEnabled() as visibility observer is probably called in same cycle but later
320
- // however the parameter value is already correct at this stage
321
- const rawVisibleValue = this.getParameterValueOfNode(node, exports.BuiltInParameter.Visible);
322
- const visible = rawVisibleValue !== undefined ? ParameterManager.parseBoolean(rawVisibleValue) : node.isEnabled();
323
- if (visible) {
324
- // TODO WTT: check mesh type and throw error if it doesn't fit
325
- // think of creating a framework with type guards (isMesh, canHaveMaterial, ...) around this
326
- yield this.viewer.materialManager.setMaterialOnMesh(material, node);
327
- }
328
- else {
329
- (0, metadata_helper_1.setInternalMetadataValue)(node, 'deferredMaterial', material);
330
- }
331
- }
332
- }));
333
- this.setParameterObserver(exports.BuiltInParameter.Position, ({ nodes, newValue }) => __awaiter(this, void 0, void 0, function* () {
334
- const position = ParameterManager.parseVector(newValue);
335
- for (const node of nodes) {
336
- node.position = position;
337
- }
338
- }));
339
- this.setParameterObserver(exports.BuiltInParameter.Rotation, ({ nodes, newValue }) => __awaiter(this, void 0, void 0, function* () {
340
- const rotation = ParameterManager.parseRotation(newValue);
341
- for (const node of nodes) {
342
- node.rotation = rotation;
343
- }
344
- }));
345
- this.setParameterObserver(exports.BuiltInParameter.Scaling, ({ nodes, newValue }) => __awaiter(this, void 0, void 0, function* () {
346
- const scaling = ParameterManager.parseVector(newValue);
347
- for (const node of nodes) {
348
- node.scaling = scaling;
349
- }
350
- }));
351
- this.setParameterObserver(exports.BuiltInParameter.Color, ({ materials, newValue }) => __awaiter(this, void 0, void 0, function* () {
352
- const color = ParameterManager.parseColor(newValue);
353
- for (const material of materials) {
354
- const materialCls = material.getClassName();
355
- switch (materialCls) {
356
- case 'PBRMaterial':
357
- material.albedoColor = color.toLinearSpace();
358
- break;
359
- case 'StandardMaterial':
360
- material.diffuseColor = color;
361
- break;
362
- default:
363
- throw new Error(`Setting color for material of instance "${materialCls}" not implemented`);
364
- }
365
- }
366
- }));
367
- this.setParameterObserver(exports.BuiltInParameter.Roughness, ({ materials, newValue }) => __awaiter(this, void 0, void 0, function* () {
368
- const roughness = ParameterManager.parseNumber(newValue);
369
- for (const material of materials) {
370
- const materialCls = material.getClassName();
371
- switch (materialCls) {
372
- case 'PBRMaterial':
373
- material.roughness = roughness;
374
- break;
375
- case 'StandardMaterial':
376
- material.roughness = roughness;
377
- break;
378
- default:
379
- throw new Error(`Setting rougness for material of instance "${materialCls}" not implemented`);
380
- }
381
- }
382
- }));
383
- this.setParameterObserver(exports.BuiltInParameter.Metallic, ({ materials, newValue }) => __awaiter(this, void 0, void 0, function* () {
384
- const metallic = ParameterManager.parseNumber(newValue);
385
- for (const material of materials) {
386
- const materialCls = material.getClassName();
387
- switch (materialCls) {
388
- case 'PBRMaterial':
389
- material.metallic = metallic;
390
- break;
391
- default:
392
- throw new Error(`Setting metallic for material of instance "${materialCls}" not implemented`);
393
- }
394
- }
395
- }));
396
- this.setParameterObserver(exports.BuiltInParameter.Paintable, ({ newValue, materials }) => __awaiter(this, void 0, void 0, function* () {
397
- (0, paintable_helper_1.paintableParameterObserver)(newValue, materials, this.viewer.scene);
398
- }));
399
- }
400
- /**
401
- * Change parameter value in array of existing parameter entries or create a new entry
402
- *
403
- * @returns "true" if parameter has changed or wasn't available before
404
- */
405
- _addParameterValue(subject, parameterName, value) {
406
- const existingEntry = this._getEntry(subject, parameterName);
407
- if ((existingEntry === null || existingEntry === void 0 ? void 0 : existingEntry.value) === value) {
408
- return false;
409
- }
410
- if (existingEntry) {
411
- existingEntry.oldValue = existingEntry.value;
412
- existingEntry.value = value;
413
- }
414
- else {
415
- this._parameterEntries.push({ subject, parameterName, value, oldValue: undefined });
416
- }
417
- return true;
418
- }
419
- /**
420
- * Call parameter observer of desired parameter which usually apply the new values to the scene.
421
- *
422
- * @param assetContainer Asset container in which to look for the paramter entries subjects (e.g. the nodes and
423
- * materials to which the parameter values should be applied to).\
424
- * Defaults to `viewer.scene`.
425
- */
426
- _applyParameterValues(parameterEntries, assetContainer) {
427
- return __awaiter(this, void 0, void 0, function* () {
428
- const tagParamEntries = parameterEntries.filter(entry => isTagParameterSubject(entry.subject));
429
- const nonTagParamEntries = parameterEntries.filter(entry => !isTagParameterSubject(entry.subject));
430
- for (const entry of tagParamEntries) {
431
- yield this._applyParameterValue(entry.subject, entry.parameterName, assetContainer);
432
- }
433
- for (const entry of nonTagParamEntries) {
434
- yield this._applyParameterValue(entry.subject, entry.parameterName, assetContainer);
435
- }
436
- });
437
- }
438
- /**
439
- * Call parameter observer of desired parameter
440
- *
441
- * @param assetContainer Optionally add an "asset container", which actually represents a model in the
442
- * {@link ModelManager}. Viewer scene is used if left empty.
443
- */
444
- _applyParameterValue(subject, parameterName, assetContainer) {
445
- return __awaiter(this, void 0, void 0, function* () {
446
- const parameterEntry = this._getEntry(subject, parameterName);
447
- const observer = this._parameterObserver[parameterName];
448
- if (!parameterEntry || !observer) {
449
- return;
450
- }
451
- const nodes = this._getAffectedNodes(subject, assetContainer);
452
- const materials = this._getAffectedMaterials(subject, assetContainer);
453
- yield observer({
454
- subject,
455
- newValue: parameterEntry.value,
456
- oldValue: parameterEntry.oldValue,
457
- nodes,
458
- materials,
459
- });
460
- });
461
- }
462
- _getAffectedMaterials(subject, assetContainer) {
463
- assetContainer = assetContainer !== null && assetContainer !== void 0 ? assetContainer : this.viewer.scene;
464
- let materials = [];
465
- // materials have priority over tags
466
- if (isMaterialParameterSubject(subject)) {
467
- materials = assetContainer.materials.filter(material => material.name === subject.materialName);
468
- if (materials.length > 1) {
469
- console.warn(`Multiple materials for material name "${subject.materialName}" have been found`);
470
- }
471
- }
472
- else if (isTagParameterSubject(subject)) {
473
- materials = assetContainer.materials.filter(material => (0, tags_helper_1.hasTag)(material, subject.tagName));
474
- }
475
- return materials;
476
- }
477
- _getAffectedNodes(subject, assetContainer) {
478
- assetContainer = assetContainer !== null && assetContainer !== void 0 ? assetContainer : this.viewer.scene;
479
- const allNodes = [...assetContainer.meshes, ...assetContainer.transformNodes];
480
- let nodes = [];
481
- // nodes have priority over tags
482
- if (isNodeParameterSubject(subject)) {
483
- nodes = allNodes.filter(node => node.name === subject.nodeName);
484
- if (nodes.length > 1) {
485
- console.warn(`Multiple nodes for node name "${subject.nodeName}" have been found`);
486
- }
487
- }
488
- else if (isTagParameterSubject(subject)) {
489
- nodes = allNodes.filter(node => (0, tags_helper_1.hasTag)(node, subject.tagName));
490
- }
491
- return nodes;
492
- }
493
- _getEntry(subject, parameterName) {
494
- const entriesOfSubject = this._getEntriesOfSubject(subject);
495
- const entry = entriesOfSubject.find(entry => entry.parameterName === parameterName);
496
- return entry;
497
- }
498
- _getEntriesOfSubject(subject) {
499
- const entries = this._parameterEntries.filter(entry => {
500
- const nodeNameMatches = isNodeParameterSubject(entry.subject) &&
501
- isNodeParameterSubject(subject) &&
502
- entry.subject.nodeName === subject.nodeName;
503
- const materialNameMatches = isMaterialParameterSubject(entry.subject) &&
504
- isMaterialParameterSubject(subject) &&
505
- entry.subject.materialName === subject.materialName;
506
- const tagNameMatches = isTagParameterSubject(entry.subject) &&
507
- isTagParameterSubject(subject) &&
508
- entry.subject.tagName === subject.tagName;
509
- return nodeNameMatches || materialNameMatches || tagNameMatches;
510
- });
511
- return entries;
512
- }
513
- }
514
- exports.ParameterManager = ParameterManager;
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.ParameterManager = exports.LegacyParameter = exports.BuiltInParameter = void 0;
13
+ const index_1 = require("../index");
14
+ const metadata_helper_1 = require("../internal/metadata-helper");
15
+ const paintable_helper_1 = require("../internal/paintable-helper");
16
+ const tags_helper_1 = require("../internal/tags-helper");
17
+ const texture_parameter_helper_1 = require("../internal/texture-parameter-helper");
18
+ const lodash_es_1 = require("lodash-es");
19
+ /**
20
+ * Parameters with a built in observer implementation
21
+ */
22
+ exports.BuiltInParameter = {
23
+ Visible: 'visible',
24
+ Material: 'material',
25
+ Position: 'position',
26
+ Rotation: 'rotation',
27
+ Scaling: 'scaling',
28
+ Color: 'color',
29
+ Roughness: 'roughness',
30
+ Metallic: 'metallic',
31
+ /**
32
+ * Texture parameters are always a combination of the channel (e.g. `albedoTexture`) and a sub parameter
33
+ * (e.g. `uScale`). Use this function to create the parameter accordingly
34
+ * (e.g. createTextureParameter("albedoTexture", "uScale")).
35
+ */
36
+ createTextureParameter: (channel, parameter) => `${channel}.${parameter}`,
37
+ UseDetailmap: 'useDetailmap',
38
+ };
39
+ /** @ignore @deprected Use "texture parameters" {@link BuiltInParameter}.`createTextureParameter` instead */
40
+ exports.LegacyParameter = {
41
+ Paintable: 'paintable',
42
+ };
43
+ const isTagParameterSubject = (subject) => 'tagName' in subject && !!subject.tagName;
44
+ const isNodeParameterSubject = (subject) => 'nodeName' in subject && !!subject.nodeName;
45
+ const isMaterialParameterSubject = (subject) => 'materialName' in subject && !!subject.materialName;
46
+ class ParameterManager {
47
+ constructor(viewer) {
48
+ this.viewer = viewer;
49
+ this._parameterEntries = [];
50
+ this._parameterObserver = {};
51
+ this._addBuiltInParameterObservers();
52
+ }
53
+ /**
54
+ * Parses and converts input to a boolean value, valid values are:
55
+ * - true / false
56
+ * - 1 / 0
57
+ */
58
+ static parseBoolean(value) {
59
+ if (value.toString() === 'true' || value.toString() === '1') {
60
+ return true;
61
+ }
62
+ else if (value.toString() === 'false' || value.toString() === '0') {
63
+ return false;
64
+ }
65
+ throw new index_1.ViewerError({
66
+ id: index_1.ViewerErrorIds.InvalidParameterValue,
67
+ message: `Unable to parse "${value}" to a boolean`,
68
+ });
69
+ }
70
+ /**
71
+ * Parses and converts input to a number value
72
+ */
73
+ static parseNumber(value) {
74
+ return parseFloat(value.toString());
75
+ }
76
+ /**
77
+ * Parses and converts input to a string value
78
+ */
79
+ static parseString(value) {
80
+ return value.toString();
81
+ }
82
+ // TODO WTT: enable setting Vector3 on the input directly
83
+ /**
84
+ * Parses a string of format "(x,y,z)"" to a "Vector3".
85
+ */
86
+ static parseVector(value) {
87
+ if (!(0, lodash_es_1.isString)(value)) {
88
+ throw new index_1.ViewerError({
89
+ id: index_1.ViewerErrorIds.InvalidParameterValue,
90
+ message: `Unable to parse "${value}" to a vector: not a string`,
91
+ });
92
+ }
93
+ let cleanedValue = value.split(' ').join('');
94
+ if (cleanedValue.startsWith('(') && cleanedValue.endsWith(')')) {
95
+ cleanedValue = cleanedValue.substring(1, cleanedValue.length - 1);
96
+ const [x, y, z] = cleanedValue.split(',').map(value => parseFloat(value));
97
+ return new index_1.Vector3(x, y, z);
98
+ }
99
+ else {
100
+ throw new index_1.ViewerError({
101
+ id: index_1.ViewerErrorIds.InvalidParameterValue,
102
+ message: `Unable to parse "${value}" to a vector: expected "(x,y,z)"`,
103
+ });
104
+ }
105
+ }
106
+ // TODO WTT: enable setting Vector3 on the input directly (maybe quaternion as well)
107
+ /**
108
+ * Parses a string of format `'(x,y,z)'` with angular degrees to a `Vector3` with rotation information in radians.
109
+ */
110
+ static parseRotation(value) {
111
+ const rotation = ParameterManager.parseVector(value);
112
+ const deg2rad = (deg) => {
113
+ return (deg * Math.PI) / 180;
114
+ };
115
+ return rotation.set(deg2rad(rotation.x), deg2rad(rotation.y), deg2rad(rotation.z));
116
+ }
117
+ // TODO WTT: enable setting Color3 on the input directly
118
+ /**
119
+ * Parses a string of format `'#rrggbb'` or `'(r,g,b)'` to a `Color3`.
120
+ */
121
+ static parseColor(value) {
122
+ const cleanedValue = value.toString().split(' ').join('');
123
+ if (cleanedValue.startsWith('#')) {
124
+ return index_1.Color3.FromHexString(value.toString());
125
+ }
126
+ if (cleanedValue.startsWith('(') && cleanedValue.endsWith(')')) {
127
+ const rgb = cleanedValue.substring(1, cleanedValue.length - 1);
128
+ const [r, g, b] = rgb.split(',').map(value => parseFloat(value));
129
+ return index_1.Color3.FromInts(r, g, b);
130
+ }
131
+ const humanReadable = (0, lodash_es_1.capitalize)(cleanedValue);
132
+ if (Object.prototype.hasOwnProperty.call(index_1.Color3, humanReadable)) {
133
+ return index_1.Color3[humanReadable]();
134
+ }
135
+ throw new index_1.ViewerError({
136
+ id: index_1.ViewerErrorIds.InvalidParameterValue,
137
+ message: `Unable to parse "${value}" to a color: expected "#rrggbb", "(r,g,b)" or any human readable (e.g. Red) property implemented in Color3`,
138
+ });
139
+ }
140
+ /**
141
+ * Set parameter value for a certain node and calls the corresponding observer if the value has changed
142
+ *
143
+ * @returns "true" if parameter value has changed
144
+ */
145
+ setNodeParameterValue(nodeName, parameterName, value) {
146
+ return __awaiter(this, void 0, void 0, function* () {
147
+ const valueChanged = this._addParameterValue({ nodeName }, parameterName, value);
148
+ if (valueChanged) {
149
+ yield this._applyParameterValue({ nodeName }, parameterName);
150
+ }
151
+ return valueChanged;
152
+ });
153
+ }
154
+ /**
155
+ * Set parameter value for a certain material and calls the corresponding observer if the value has changed
156
+ *
157
+ * @returns "true" if parameter value has changed
158
+ */
159
+ setMaterialParameterValue(materialName, parameterName, value) {
160
+ return __awaiter(this, void 0, void 0, function* () {
161
+ const valueChanged = this._addParameterValue({ materialName }, parameterName, value);
162
+ if (valueChanged) {
163
+ yield this._applyParameterValue({ materialName }, parameterName);
164
+ }
165
+ return valueChanged;
166
+ });
167
+ }
168
+ /**
169
+ * Set parameter value for a certain tag and calls the corresponding observer if the value has changed.\
170
+ * Setting a parameter value on a tag can affect multiple nodes and tags, depending which of these objects contains
171
+ * the desired tag.
172
+ *
173
+ * @returns "true" if parameter value has changed
174
+ */
175
+ setTagParameterValue(tagName, parameterName, value) {
176
+ return __awaiter(this, void 0, void 0, function* () {
177
+ const valueChanged = this._addParameterValue({ tagName }, parameterName, value);
178
+ if (valueChanged) {
179
+ yield this._applyParameterValue({ tagName }, parameterName);
180
+ }
181
+ return valueChanged;
182
+ });
183
+ }
184
+ /**
185
+ * Set multiple parameter values simultaniously.\
186
+ * Tag parameters are applied before node and material parameters, node and materials are more specific and should
187
+ * have priority.
188
+ *
189
+ * @returns Array of parameters, which have changed values
190
+ */
191
+ setParameterValues(values) {
192
+ return __awaiter(this, void 0, void 0, function* () {
193
+ const parameterEntries = values.map(valueEntry => {
194
+ const subject = isNodeParameterSubject(valueEntry)
195
+ ? { nodeName: valueEntry.nodeName }
196
+ : isMaterialParameterSubject(valueEntry)
197
+ ? { materialName: valueEntry.materialName }
198
+ : { tagName: valueEntry.tagName };
199
+ return { subject, parameterName: valueEntry.parameterName, value: valueEntry.value, oldValue: undefined };
200
+ });
201
+ const changedParameterEntries = parameterEntries.filter(paramEntry => this._addParameterValue(paramEntry.subject, paramEntry.parameterName, paramEntry.value));
202
+ yield this._applyParameterValues(changedParameterEntries);
203
+ // convert back to original typing
204
+ const changedParameterValues = changedParameterEntries.map(paramEntry => {
205
+ return Object.assign(Object.assign({}, paramEntry.subject), { parameterName: paramEntry.parameterName, value: paramEntry.value });
206
+ });
207
+ return changedParameterValues;
208
+ });
209
+ }
210
+ /**
211
+ * @returns desired parameter value or "undefined" if parameter entry is not available.
212
+ */
213
+ getParameterValue(subject, parameterName) {
214
+ const entry = this._getEntry(subject, parameterName);
215
+ return entry === null || entry === void 0 ? void 0 : entry.value;
216
+ }
217
+ /**
218
+ * Define observer callback for certain parameter.\
219
+ * There can only be one observer for a certain parameter name.\
220
+ * Parameter observers can not be overwritten once they are defined, this also includes system observers for
221
+ * {@link BuiltInParameter}.
222
+ */
223
+ setParameterObserver(parameterName, observer) {
224
+ if (this._parameterObserver[parameterName]) {
225
+ console.warn(`Observer for parameter "${parameterName}" already set`);
226
+ return;
227
+ }
228
+ this._parameterObserver[parameterName] = observer;
229
+ }
230
+ /**
231
+ * Print all parameter entries in table format into the console
232
+ */
233
+ printAllParameters() {
234
+ const printable = this._parameterEntries.map(entry => (Object.assign(Object.assign({}, entry), { subject: JSON.stringify(entry.subject) })));
235
+ console.table(printable);
236
+ }
237
+ /**
238
+ * Applies all existing parameter entries to a certain "model", as defined in the {@link ModelManager}.\
239
+ * This can be usefull when updating the model before showing it in the scene.
240
+ *
241
+ * @internal
242
+ */
243
+ applyAllParameterValuesToModel(model) {
244
+ return __awaiter(this, void 0, void 0, function* () {
245
+ const parameterEntriesToApply = this._parameterEntries;
246
+ yield this._applyParameterValues(parameterEntriesToApply, model);
247
+ });
248
+ }
249
+ /**
250
+ * Applies all parameter values which are targeting a material.\
251
+ * This can be usefull when updating a material definition before creating it.
252
+ *
253
+ * @internal
254
+ */
255
+ applyParameterValuesToMaterial(material) {
256
+ return __awaiter(this, void 0, void 0, function* () {
257
+ const tags = (0, tags_helper_1.getTags)(material);
258
+ const parameterEntriesToApply = [];
259
+ tags.forEach(tagName => {
260
+ const tagParamEntries = this._getEntriesOfSubject({ tagName });
261
+ parameterEntriesToApply.push(...tagParamEntries);
262
+ });
263
+ const materialParamEntries = this._getEntriesOfSubject({ materialName: material.id });
264
+ parameterEntriesToApply.push(...materialParamEntries);
265
+ yield this._applyParameterValues(parameterEntriesToApply);
266
+ });
267
+ }
268
+ /**
269
+ * Applies subset of texture parameter values which are targeting a certain texture channel in a material.\
270
+ * This can be useful when updating texture settings (e.g. uScale) before the texture is actually created.
271
+ *
272
+ * @internal
273
+ */
274
+ applyTextureSettingsParameter(material, channel) {
275
+ return __awaiter(this, void 0, void 0, function* () {
276
+ const tags = (0, tags_helper_1.getTags)(material);
277
+ const parameterEntriesToApply = [];
278
+ tags.forEach(tagName => {
279
+ const tagParamEntries = this._getEntriesOfSubject({ tagName });
280
+ parameterEntriesToApply.push(...tagParamEntries);
281
+ });
282
+ const materialParamEntries = this._getEntriesOfSubject({ materialName: material.id });
283
+ parameterEntriesToApply.push(...materialParamEntries);
284
+ const textureParameterEntriesToApply = parameterEntriesToApply.filter(parameterEntry => {
285
+ const paramPath = parameterEntry.parameterName.split('.');
286
+ // find texture parameters of this channel, e.g. "albedoTexture.uScale"
287
+ // "image" should not be applied here as this is part of the texture initialization anyway
288
+ const isTextureParamOfChannel = paramPath.length === 2 && paramPath[0] === channel && paramPath[1] !== texture_parameter_helper_1.BuiltInTextureParameter.image;
289
+ return isTextureParamOfChannel;
290
+ });
291
+ yield this._applyParameterValues(textureParameterEntriesToApply);
292
+ });
293
+ }
294
+ /**
295
+ * @returns Desired parameter value of a certain node.
296
+ * Tags are considered as well but have lower priority than node parameters, as these are more specific.
297
+ *
298
+ * @internal
299
+ */
300
+ getParameterValueOfNode(node, parameterName) {
301
+ const nodeParamValue = this.getParameterValue({ nodeName: node.name }, parameterName);
302
+ if (nodeParamValue !== undefined) {
303
+ return nodeParamValue;
304
+ }
305
+ const tags = (0, tags_helper_1.getTags)(node);
306
+ const tagParamValue = tags.reduce((accValue, curTag) => {
307
+ // NOTE: it is possible that values are available for multiple tags
308
+ // in this case the resulting parameter value is quite "random" as the last tag in the tag string of the node has
309
+ // priority
310
+ const tagParamValue = this.getParameterValue({ tagName: curTag }, parameterName);
311
+ return accValue !== null && accValue !== void 0 ? accValue : tagParamValue;
312
+ }, undefined);
313
+ return tagParamValue;
314
+ }
315
+ /**
316
+ * @returns Desired parameter value of a certain material.
317
+ * Tags are considered as well but have lower priority than material parameters, as these are more specific.
318
+ * Unused ATM, but added for consistency as counter part for {@link getParameterValueOfNode}
319
+ *
320
+ * @internal
321
+ */
322
+ getParameterValueOfMaterial(material, parameterName) {
323
+ const materialParamValue = this.getParameterValue({ materialName: material.name }, parameterName);
324
+ if (materialParamValue !== undefined) {
325
+ return materialParamValue;
326
+ }
327
+ const tags = (0, tags_helper_1.getTags)(material);
328
+ const tagParamValue = tags.reduce((accValue, curTag) => {
329
+ const tagParamValue = this.getParameterValue({ tagName: curTag }, parameterName);
330
+ return accValue !== null && accValue !== void 0 ? accValue : tagParamValue;
331
+ }, undefined);
332
+ return tagParamValue;
333
+ }
334
+ /**
335
+ * Parameter observer implementation of default parameters
336
+ */
337
+ _addBuiltInParameterObservers() {
338
+ this.setParameterObserver(exports.BuiltInParameter.Visible, ({ nodes, newValue }) => __awaiter(this, void 0, void 0, function* () {
339
+ const visible = ParameterManager.parseBoolean(newValue);
340
+ for (const node of nodes) {
341
+ if (visible) {
342
+ // if a mesh gets visible by this operation we have to activate the assigned material which is stored in the
343
+ // internal metadata
344
+ // => consider child meshes as well (CB-10143)
345
+ const activatedNodes = [node, ...node.getChildMeshes(false)];
346
+ for (const activatedNode of activatedNodes) {
347
+ const deferredMaterial = (0, metadata_helper_1.getInternalMetadataValue)(activatedNode, 'deferredMaterial');
348
+ if (deferredMaterial) {
349
+ yield this.viewer.materialManager.setMaterialOnMesh(deferredMaterial,
350
+ // this cast is fine, as deferred material can only be set on meshes
351
+ activatedNode);
352
+ }
353
+ }
354
+ node.setEnabled(true);
355
+ }
356
+ else {
357
+ node.setEnabled(false);
358
+ }
359
+ }
360
+ }));
361
+ this.setParameterObserver(exports.BuiltInParameter.Material, ({ nodes, newValue }) => __awaiter(this, void 0, void 0, function* () {
362
+ const material = ParameterManager.parseString(newValue);
363
+ for (const node of nodes) {
364
+ // NOTE: don't use node.isEnabled() as visibility observer is probably called in same cycle but later
365
+ // however the parameter value is already correct at this stage
366
+ // we have to go through all parents as well, because a parent may have been set to false, which also disables
367
+ // this child node
368
+ let curNode = node;
369
+ let visibleByParameter = undefined;
370
+ while (curNode && visibleByParameter !== false) {
371
+ const curNodeVisibleByParameter = this.getParameterValueOfNode(curNode, exports.BuiltInParameter.Visible);
372
+ if (curNodeVisibleByParameter !== undefined) {
373
+ visibleByParameter = curNodeVisibleByParameter;
374
+ }
375
+ curNode = curNode.parent;
376
+ }
377
+ // parameter visibility has priority, but if the visiblity is not controlled by the parameter use the plain
378
+ // Babylon.js isEnabled() check
379
+ const visible = visibleByParameter !== undefined ? ParameterManager.parseBoolean(visibleByParameter) : node.isEnabled();
380
+ if (visible) {
381
+ // TODO WTT: check mesh type and throw error if it doesn't fit
382
+ // think of creating a framework with type guards (isMesh, canHaveMaterial, ...) around this
383
+ yield this.viewer.materialManager.setMaterialOnMesh(material, node);
384
+ }
385
+ else {
386
+ (0, metadata_helper_1.setInternalMetadataValue)(node, 'deferredMaterial', material);
387
+ }
388
+ }
389
+ }));
390
+ this.setParameterObserver(exports.BuiltInParameter.Position, ({ nodes, newValue }) => __awaiter(this, void 0, void 0, function* () {
391
+ const position = ParameterManager.parseVector(newValue);
392
+ for (const node of nodes) {
393
+ node.position = position;
394
+ }
395
+ }));
396
+ this.setParameterObserver(exports.BuiltInParameter.Rotation, ({ nodes, newValue }) => __awaiter(this, void 0, void 0, function* () {
397
+ const rotation = ParameterManager.parseRotation(newValue);
398
+ for (const node of nodes) {
399
+ node.rotation = rotation;
400
+ }
401
+ }));
402
+ this.setParameterObserver(exports.BuiltInParameter.Scaling, ({ nodes, newValue }) => __awaiter(this, void 0, void 0, function* () {
403
+ const scaling = ParameterManager.parseVector(newValue);
404
+ for (const node of nodes) {
405
+ node.scaling = scaling;
406
+ }
407
+ }));
408
+ this.setParameterObserver(exports.BuiltInParameter.Color, ({ materials, newValue }) => __awaiter(this, void 0, void 0, function* () {
409
+ const color = ParameterManager.parseColor(newValue);
410
+ for (const material of materials) {
411
+ const materialCls = material.getClassName();
412
+ switch (materialCls) {
413
+ case 'PBRMaterial':
414
+ material.albedoColor = color.toLinearSpace();
415
+ break;
416
+ case 'StandardMaterial':
417
+ material.diffuseColor = color;
418
+ break;
419
+ default:
420
+ throw new Error(`Setting color for material of instance "${materialCls}" not implemented`);
421
+ }
422
+ }
423
+ }));
424
+ this.setParameterObserver(exports.BuiltInParameter.Roughness, ({ materials, newValue }) => __awaiter(this, void 0, void 0, function* () {
425
+ const roughness = ParameterManager.parseNumber(newValue);
426
+ for (const material of materials) {
427
+ const materialCls = material.getClassName();
428
+ switch (materialCls) {
429
+ case 'PBRMaterial':
430
+ material.roughness = roughness;
431
+ break;
432
+ case 'StandardMaterial':
433
+ material.roughness = roughness;
434
+ break;
435
+ default:
436
+ throw new Error(`Setting rougness for material of instance "${materialCls}" not implemented`);
437
+ }
438
+ }
439
+ }));
440
+ this.setParameterObserver(exports.BuiltInParameter.Metallic, ({ materials, newValue }) => __awaiter(this, void 0, void 0, function* () {
441
+ const metallic = ParameterManager.parseNumber(newValue);
442
+ for (const material of materials) {
443
+ const materialCls = material.getClassName();
444
+ switch (materialCls) {
445
+ case 'PBRMaterial':
446
+ material.metallic = metallic;
447
+ break;
448
+ default:
449
+ throw new Error(`Setting metallic for material of instance "${materialCls}" not implemented`);
450
+ }
451
+ }
452
+ }));
453
+ this.setParameterObserver(exports.LegacyParameter.Paintable, ({ newValue, materials }) => __awaiter(this, void 0, void 0, function* () {
454
+ (0, paintable_helper_1.paintableParameterObserver)(newValue, materials, this.viewer.scene);
455
+ }));
456
+ // texture parameter use a more generic approach (`channel`.`parameter`) and are therefore coded in a dedicated file
457
+ (0, texture_parameter_helper_1.createBuiltInTextureParameter)(this, this.viewer.scene);
458
+ }
459
+ /**
460
+ * Change parameter value in array of existing parameter entries or create a new entry
461
+ *
462
+ * @returns "true" if parameter has changed or wasn't available before
463
+ */
464
+ _addParameterValue(subject, parameterName, value) {
465
+ const existingEntry = this._getEntry(subject, parameterName);
466
+ if ((existingEntry === null || existingEntry === void 0 ? void 0 : existingEntry.value) === value) {
467
+ return false;
468
+ }
469
+ if (existingEntry) {
470
+ existingEntry.oldValue = existingEntry.value;
471
+ existingEntry.value = value;
472
+ }
473
+ else {
474
+ this._parameterEntries.push({ subject, parameterName, value, oldValue: undefined });
475
+ }
476
+ return true;
477
+ }
478
+ /**
479
+ * Call parameter observer of desired parameter which usually apply the new values to the scene.
480
+ *
481
+ * @param assetContainer Asset container in which to look for the paramter entries subjects (e.g. the nodes and
482
+ * materials to which the parameter values should be applied to).\
483
+ * Defaults to `viewer.scene`.
484
+ */
485
+ _applyParameterValues(parameterEntries, assetContainer) {
486
+ return __awaiter(this, void 0, void 0, function* () {
487
+ const tagParamEntries = parameterEntries.filter(entry => isTagParameterSubject(entry.subject));
488
+ const nonTagParamEntries = parameterEntries.filter(entry => !isTagParameterSubject(entry.subject));
489
+ for (const entry of tagParamEntries) {
490
+ yield this._applyParameterValue(entry.subject, entry.parameterName, assetContainer);
491
+ }
492
+ for (const entry of nonTagParamEntries) {
493
+ yield this._applyParameterValue(entry.subject, entry.parameterName, assetContainer);
494
+ }
495
+ });
496
+ }
497
+ /**
498
+ * Call parameter observer of desired parameter
499
+ *
500
+ * @param assetContainer Optionally add an "asset container", which actually represents a model in the
501
+ * {@link ModelManager}. Viewer scene is used if left empty.
502
+ */
503
+ _applyParameterValue(subject, parameterName, assetContainer) {
504
+ return __awaiter(this, void 0, void 0, function* () {
505
+ const parameterEntry = this._getEntry(subject, parameterName);
506
+ const observer = this._parameterObserver[parameterName];
507
+ if (!parameterEntry || !observer) {
508
+ return;
509
+ }
510
+ const nodes = this._getAffectedNodes(subject, assetContainer);
511
+ const materials = this._getAffectedMaterials(subject, assetContainer);
512
+ yield observer({
513
+ subject,
514
+ newValue: parameterEntry.value,
515
+ oldValue: parameterEntry.oldValue,
516
+ nodes,
517
+ materials,
518
+ });
519
+ });
520
+ }
521
+ _getAffectedMaterials(subject, assetContainer) {
522
+ assetContainer = assetContainer !== null && assetContainer !== void 0 ? assetContainer : this.viewer.scene;
523
+ let materials = [];
524
+ // materials have priority over tags
525
+ if (isMaterialParameterSubject(subject)) {
526
+ materials = assetContainer.materials.filter(material => material.name === subject.materialName);
527
+ if (materials.length > 1) {
528
+ console.warn(`Multiple materials for material name "${subject.materialName}" have been found`);
529
+ }
530
+ }
531
+ else if (isTagParameterSubject(subject)) {
532
+ materials = assetContainer.materials.filter(material => (0, tags_helper_1.hasTag)(material, subject.tagName));
533
+ }
534
+ return materials;
535
+ }
536
+ _getAffectedNodes(subject, assetContainer) {
537
+ assetContainer = assetContainer !== null && assetContainer !== void 0 ? assetContainer : this.viewer.scene;
538
+ const allNodes = [...assetContainer.meshes, ...assetContainer.transformNodes];
539
+ let nodes = [];
540
+ // nodes have priority over tags
541
+ if (isNodeParameterSubject(subject)) {
542
+ nodes = allNodes.filter(node => node.name === subject.nodeName);
543
+ if (nodes.length > 1) {
544
+ console.warn(`Multiple nodes for node name "${subject.nodeName}" have been found`);
545
+ }
546
+ }
547
+ else if (isTagParameterSubject(subject)) {
548
+ nodes = allNodes.filter(node => (0, tags_helper_1.hasTag)(node, subject.tagName));
549
+ }
550
+ return nodes;
551
+ }
552
+ _getEntry(subject, parameterName) {
553
+ const entriesOfSubject = this._getEntriesOfSubject(subject);
554
+ const entry = entriesOfSubject.find(entry => entry.parameterName === parameterName);
555
+ return entry;
556
+ }
557
+ _getEntriesOfSubject(subject) {
558
+ const entries = this._parameterEntries.filter(entry => {
559
+ const nodeNameMatches = isNodeParameterSubject(entry.subject) &&
560
+ isNodeParameterSubject(subject) &&
561
+ entry.subject.nodeName === subject.nodeName;
562
+ const materialNameMatches = isMaterialParameterSubject(entry.subject) &&
563
+ isMaterialParameterSubject(subject) &&
564
+ entry.subject.materialName === subject.materialName;
565
+ const tagNameMatches = isTagParameterSubject(entry.subject) &&
566
+ isTagParameterSubject(subject) &&
567
+ entry.subject.tagName === subject.tagName;
568
+ return nodeNameMatches || materialNameMatches || tagNameMatches;
569
+ });
570
+ return entries;
571
+ }
572
+ }
573
+ exports.ParameterManager = ParameterManager;
515
574
  //# sourceMappingURL=parameter-manager.js.map