@combeenation/3d-viewer 5.0.1 → 5.0.3-beta2

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 (109) hide show
  1. package/README.md +111 -114
  2. package/dist/lib-cjs/api/classes/animationInterface.d.ts +8 -8
  3. package/dist/lib-cjs/api/classes/animationInterface.js +1 -1
  4. package/dist/lib-cjs/api/classes/dottedPath.d.ts +79 -79
  5. package/dist/lib-cjs/api/classes/dottedPath.js +187 -187
  6. package/dist/lib-cjs/api/classes/element.d.ts +139 -139
  7. package/dist/lib-cjs/api/classes/element.js +794 -794
  8. package/dist/lib-cjs/api/classes/element.js.map +1 -1
  9. package/dist/lib-cjs/api/classes/elementParameterizable.d.ts +14 -0
  10. package/dist/lib-cjs/api/classes/elementParameterizable.js +135 -0
  11. package/dist/lib-cjs/api/classes/elementParameterizable.js.map +1 -0
  12. package/dist/lib-cjs/api/classes/event.d.ts +326 -326
  13. package/dist/lib-cjs/api/classes/event.js +371 -371
  14. package/dist/lib-cjs/api/classes/eventBroadcaster.d.ts +26 -26
  15. package/dist/lib-cjs/api/classes/eventBroadcaster.js +53 -53
  16. package/dist/lib-cjs/api/classes/parameter.d.ts +316 -259
  17. package/dist/lib-cjs/api/classes/parameter.js +451 -388
  18. package/dist/lib-cjs/api/classes/parameter.js.map +1 -1
  19. package/dist/lib-cjs/api/classes/parameterObservable.d.ts +36 -36
  20. package/dist/lib-cjs/api/classes/parameterObservable.js +101 -101
  21. package/dist/lib-cjs/api/classes/parameterizable.d.ts +15 -15
  22. package/dist/lib-cjs/api/classes/parameterizable.js +149 -149
  23. package/dist/lib-cjs/api/classes/placementAnimation.d.ts +44 -44
  24. package/dist/lib-cjs/api/classes/placementAnimation.js +163 -163
  25. package/dist/lib-cjs/api/classes/variant.d.ts +234 -234
  26. package/dist/lib-cjs/api/classes/variant.js +1154 -1154
  27. package/dist/lib-cjs/api/classes/variantInstance.d.ts +45 -45
  28. package/dist/lib-cjs/api/classes/variantInstance.js +108 -108
  29. package/dist/lib-cjs/api/classes/variantParameterizable.d.ts +17 -17
  30. package/dist/lib-cjs/api/classes/variantParameterizable.js +99 -99
  31. package/dist/lib-cjs/api/classes/viewer.d.ts +177 -175
  32. package/dist/lib-cjs/api/classes/viewer.js +717 -701
  33. package/dist/lib-cjs/api/classes/viewer.js.map +1 -1
  34. package/dist/lib-cjs/api/classes/viewerLight.d.ts +66 -66
  35. package/dist/lib-cjs/api/classes/viewerLight.js +392 -392
  36. package/dist/lib-cjs/api/internal/debugViewer.d.ts +13 -13
  37. package/dist/lib-cjs/api/internal/debugViewer.js +86 -86
  38. package/dist/lib-cjs/api/internal/lensRendering.d.ts +8 -8
  39. package/dist/lib-cjs/api/internal/lensRendering.js +11 -11
  40. package/dist/lib-cjs/api/internal/sceneSetup.d.ts +13 -10
  41. package/dist/lib-cjs/api/internal/sceneSetup.js +238 -234
  42. package/dist/lib-cjs/api/internal/sceneSetup.js.map +1 -1
  43. package/dist/lib-cjs/api/manager/animationManager.d.ts +29 -29
  44. package/dist/lib-cjs/api/manager/animationManager.js +130 -130
  45. package/dist/lib-cjs/api/manager/gltfExportManager.d.ts +65 -65
  46. package/dist/lib-cjs/api/manager/gltfExportManager.js +223 -222
  47. package/dist/lib-cjs/api/manager/gltfExportManager.js.map +1 -1
  48. package/dist/lib-cjs/api/manager/sceneManager.d.ts +31 -32
  49. package/dist/lib-cjs/api/manager/sceneManager.js +153 -132
  50. package/dist/lib-cjs/api/manager/sceneManager.js.map +1 -1
  51. package/dist/lib-cjs/api/manager/variantInstanceManager.d.ts +92 -92
  52. package/dist/lib-cjs/api/manager/variantInstanceManager.js +335 -335
  53. package/dist/lib-cjs/api/store/specStorage.d.ts +24 -24
  54. package/dist/lib-cjs/api/store/specStorage.js +51 -51
  55. package/dist/lib-cjs/api/util/babylonHelper.d.ts +174 -166
  56. package/dist/lib-cjs/api/util/babylonHelper.js +575 -497
  57. package/dist/lib-cjs/api/util/babylonHelper.js.map +1 -1
  58. package/dist/lib-cjs/api/util/globalTypes.d.ts +366 -356
  59. package/dist/lib-cjs/api/util/globalTypes.js +1 -1
  60. package/dist/lib-cjs/api/util/resourceHelper.d.ts +58 -53
  61. package/dist/lib-cjs/api/util/resourceHelper.js +257 -242
  62. package/dist/lib-cjs/api/util/resourceHelper.js.map +1 -1
  63. package/dist/lib-cjs/api/util/stringHelper.d.ts +9 -9
  64. package/dist/lib-cjs/api/util/stringHelper.js +25 -25
  65. package/dist/lib-cjs/api/util/structureHelper.d.ts +9 -9
  66. package/dist/lib-cjs/api/util/structureHelper.js +48 -44
  67. package/dist/lib-cjs/api/util/structureHelper.js.map +1 -1
  68. package/dist/lib-cjs/buildinfo.json +3 -3
  69. package/dist/lib-cjs/index.d.ts +49 -49
  70. package/dist/lib-cjs/index.js +89 -89
  71. package/dist/webpack-stats.json +0 -0
  72. package/package.json +87 -87
  73. package/src/api/classes/animationInterface.ts +10 -10
  74. package/src/api/classes/dottedPath.ts +181 -181
  75. package/src/api/classes/element.ts +690 -692
  76. package/src/api/classes/event.ts +367 -367
  77. package/src/api/classes/eventBroadcaster.ts +52 -52
  78. package/src/api/classes/parameter.ts +474 -405
  79. package/src/api/classes/parameterObservable.ts +100 -100
  80. package/src/api/classes/parameterizable.ts +87 -87
  81. package/src/api/classes/placementAnimation.ts +160 -160
  82. package/src/api/classes/variant.ts +845 -845
  83. package/src/api/classes/variantInstance.ts +97 -97
  84. package/src/api/classes/variantParameterizable.ts +85 -85
  85. package/src/api/classes/viewer.ts +650 -624
  86. package/src/api/classes/viewerLight.ts +334 -334
  87. package/src/api/internal/debugViewer.ts +90 -90
  88. package/src/api/internal/lensRendering.ts +10 -10
  89. package/src/api/internal/sceneSetup.ts +204 -201
  90. package/src/api/manager/animationManager.ts +142 -142
  91. package/src/api/manager/gltfExportManager.ts +191 -191
  92. package/src/api/manager/sceneManager.ts +128 -102
  93. package/src/api/manager/variantInstanceManager.ts +265 -265
  94. package/src/api/store/specStorage.ts +51 -51
  95. package/src/api/util/babylonHelper.ts +628 -538
  96. package/src/api/util/globalTypes.ts +413 -402
  97. package/src/api/util/resourceHelper.ts +189 -173
  98. package/src/api/util/stringHelper.ts +23 -23
  99. package/src/api/util/structureHelper.ts +49 -43
  100. package/src/buildinfo.json +3 -3
  101. package/src/commonjs.tsconfig.json +10 -10
  102. package/src/declaration.tsconfig.json +8 -8
  103. package/src/dev.ts +42 -42
  104. package/src/es6.tsconfig.json +10 -10
  105. package/src/index.ts +94 -94
  106. package/src/pagesconfig.json +77 -73
  107. package/src/tsconfig.json +32 -32
  108. package/src/tsconfig.types.json +9 -9
  109. package/src/types.d.ts +3 -3
@@ -1,405 +1,474 @@
1
- import { Color3 } from '@babylonjs/core/Maths/math.color';
2
- import { Vector3 } from '@babylonjs/core/Maths/math.vector';
3
- import { get, isEmpty, isNumber, isString } from 'lodash-es';
4
-
5
- /**
6
- * This `Parameter` class defines all parameters that can be consumed and committed by a {@link ParameterObservable}.
7
- * Use {@link ParameterObservable.addParameterObserver} or {@link ParameterObservable.commitParameters} to either add or
8
- * commit parameters on your observable objects.
9
- */
10
- export class Parameter {
11
- /**
12
- * Constructor.
13
- */
14
- private constructor() {}
15
-
16
- /**
17
- * Mutates the visibility. Helper methods are {@link VariantParameterizable.show} and
18
- * {@link VariantParameterizable.hide}. Getter and setter is {@link VariantParameterizable.visible}.
19
- *
20
- * Scopes:
21
- * * {@link Element}
22
- * * {@link Variant}
23
- * * {@link ViewerLight} Available for all light types
24
- *
25
- * @parser {@link parseBoolean}
26
- */
27
- public static readonly VISIBLE = 'visible';
28
-
29
- /**
30
- * Stretches or compresses some meshes. Getter and setter is {@link VariantParameterizable.scaling}.
31
- *
32
- * Scopes:
33
- * * {@link Element}
34
- * * {@link Variant}
35
- *
36
- * @parser {@link parseScaling}
37
- */
38
- public static readonly SCALING = 'scaling';
39
-
40
- /**
41
- * Mutates the material. Getter and setter is {@link VariantParameterizable.material}. The value of this parameter
42
- * represents the ID of a baked-in glTF or custom created material in the {@link Variant} tree.
43
- *
44
- * Scopes:
45
- * * {@link Element}
46
- * * {@link Variant}
47
- *
48
- * @parser string
49
- */
50
- public static readonly MATERIAL = 'material';
51
-
52
- /**
53
- * Mutates the material color.
54
- *
55
- * Scopes:
56
- * * {@link Element}
57
- * * {@link Variant}
58
- *
59
- * @parser {@link parseColor}
60
- */
61
- public static readonly MATERIAL_COLOR = 'material.color';
62
-
63
- /**
64
- * Mutates the material metallness.
65
- *
66
- * Scopes:
67
- * * {@link Element}
68
- * * {@link Variant}
69
- *
70
- * @parser {@link parseNumber}
71
- */
72
- public static readonly MATERIAL_METALLNESS = 'material.metallness';
73
-
74
- /**
75
- * Mutates the material roughness.
76
- *
77
- * Scopes:
78
- * * {@link Element}
79
- * * {@link Variant}
80
- *
81
- * @parser {@link parseNumber}
82
- */
83
- public static readonly MATERIAL_ROUGHNESS = 'material.roughness';
84
-
85
- /**
86
- * Mutates whether a highlight layer should be enabled.
87
- *
88
- * Scopes:
89
- * * {@link Element}
90
- * * {@link Variant}
91
- *
92
- * @parser {@link parseBoolean}
93
- */
94
- public static readonly HIGHLIGHT_ENABLED = 'highlight.enabled';
95
-
96
- /**
97
- * Changes the highlight color.
98
- *
99
- * Scopes:
100
- * * {@link Element}
101
- * * {@link Variant}
102
- *
103
- * @parser {@link parseColor}
104
- */
105
- public static readonly HIGHLIGHT_COLOR = 'highlight.color';
106
-
107
- /**
108
- * Mutates the highlighted state. Getter and setter is {@link VariantParameterizable.highlighted}.
109
- *
110
- * Scopes:
111
- * * {@link Element}
112
- * * {@link Variant}
113
- *
114
- * @parser {@link parseBoolean}
115
- */
116
- public static readonly HIGHLIGHTED = 'highlighted';
117
-
118
- /**
119
- * Mutates the position relative to its origin position. Getter and setter is {@link VariantParameterizable.position}.
120
- *
121
- * Scopes:
122
- * * {@link Element}
123
- * * {@link Variant}
124
- * * {@link ViewerLight} Available for [PointLight](https://doc.babylonjs.com/typedoc/classes/babylon.pointlight#position), [SpotLight](https://doc.babylonjs.com/typedoc/classes/babylon.spotlight#position), [DirectionalLight](https://doc.babylonjs.com/typedoc/classes/babylon.directionallight#position)
125
- *
126
- * @parser {@link parseVector}
127
- */
128
- public static readonly POSITION = 'position';
129
-
130
- /**
131
- * Mutates the rotation relative to its origin rotation. Getter and setter is {@link VariantParameterizable.rotation}.
132
- *
133
- * Scopes:
134
- * * {@link Element}
135
- * * {@link Variant}
136
- *
137
- * @parser {@link parseRotation}
138
- */
139
- public static readonly ROTATION = 'rotation';
140
-
141
- /**
142
- * Mutates whether a shadow should be casted or not.
143
- *
144
- * Scopes:
145
- * * {@link Element}
146
- * * {@link Variant}
147
- *
148
- * @parser {@link parseBoolean}
149
- */
150
- public static readonly CAST_SHADOW = 'castShadow';
151
-
152
- /**
153
- * Mutates from which lights a shadow should be casted.
154
- *
155
- * Scopes:
156
- * * {@link Element}
157
- * * {@link Variant}
158
- *
159
- * @parser {@link parseCommaSeparatedList}
160
- */
161
- public static readonly CAST_SHADOW_FROM_LIGHTS = 'castShadow.fromLights';
162
-
163
- /**
164
- * Mutates whether to receive shadows or not.
165
- *
166
- * Scopes:
167
- * * {@link Element}
168
- * * {@link Variant}
169
- *
170
- * @parser {@link parseBoolean}
171
- */
172
- public static readonly RECEIVE_SHADOWS = 'receiveShadows';
173
-
174
- /**
175
- * Mutates the intensity of Lights.
176
- *
177
- * Scopes:
178
- * * {@link ViewerLight} Available for [PointLight](https://doc.babylonjs.com/typedoc/classes/babylon.pointlight#intensity), [SpotLight](https://doc.babylonjs.com/typedoc/classes/babylon.spotlight#intensity), [DirectionalLight](https://doc.babylonjs.com/typedoc/classes/babylon.directionallight#intensity), [HemisphericLight](https://doc.babylonjs.com/typedoc/classes/babylon.hemisphericlight#intensity)
179
- *
180
- * @parser {@link parseNumber}
181
- */
182
- public static readonly INTENSITY = 'intensity';
183
-
184
- /**
185
- * Mutates the direction of Lights.
186
- *
187
- * Scopes:
188
- * * {@link ViewerLight} Available for [PointLight](https://doc.babylonjs.com/typedoc/classes/babylon.pointlight#direction), [SpotLight](https://doc.babylonjs.com/typedoc/classes/babylon.spotlight#direction), [DirectionalLight](https://doc.babylonjs.com/typedoc/classes/babylon.directionallight#direction), [HemisphericLight](https://doc.babylonjs.com/typedoc/classes/babylon.hemisphericlight#direction)
189
- *
190
- * @parser {@link parseNumber}
191
- */
192
- public static readonly DIRECTION = 'direction';
193
-
194
- /**
195
- * Mutates the angle of Lights.
196
- *
197
- * Scopes:
198
- * * {@link ViewerLight} Available for [SpotLight](https://doc.babylonjs.com/typedoc/classes/babylon.spotlight#angle)
199
- *
200
- * @parser {@link parseNumber}
201
- */
202
- public static readonly ANGLE = 'angle';
203
-
204
- /**
205
- * Mutates the exponent of Lights.
206
- *
207
- * Scopes:
208
- * * {@link ViewerLight} Available for [SpotLight](https://doc.babylonjs.com/typedoc/classes/babylon.spotlight#exponent)
209
- *
210
- * @parser {@link parseNumber}
211
- */
212
- public static readonly EXPONENT = 'exponent';
213
-
214
- /**
215
- * Mutates the diffuse color of Lights.
216
- *
217
- * Scopes:
218
- * * {@link ViewerLight} Available for [PointLight](https://doc.babylonjs.com/typedoc/classes/babylon.pointlight#diffuse), [SpotLight](https://doc.babylonjs.com/typedoc/classes/babylon.spotlight#diffuse), [DirectionalLight](https://doc.babylonjs.com/typedoc/classes/babylon.directionallight#diffuse), [HemisphericLight](https://doc.babylonjs.com/typedoc/classes/babylon.hemisphericlight#diffuse)
219
- *
220
- * @parser {@link parseNumber}
221
- */
222
- public static readonly DIFFUSE = 'diffuse';
223
-
224
- /**
225
- * Mutates the specular color of Lights.
226
- *
227
- * Scopes:
228
- * * {@link ViewerLight} Available for [PointLight](https://doc.babylonjs.com/typedoc/classes/babylon.pointlight#specular), [SpotLight](https://doc.babylonjs.com/typedoc/classes/babylon.spotlight#specular), [DirectionalLight](https://doc.babylonjs.com/typedoc/classes/babylon.directionallight#specular), [HemisphericLight](https://doc.babylonjs.com/typedoc/classes/babylon.hemisphericlight#specular)
229
- *
230
- * @parser {@link parseNumber}
231
- */
232
- public static readonly SPECULAR = 'specular';
233
-
234
- /**
235
- * The {@link ParameterDeclarations} for all parameters in this class.
236
- */
237
- public static get declarations(): ParameterDeclarations {
238
- const declarations: ParameterDeclarations = {};
239
- declarations[Parameter.VISIBLE] = { type: 'boolean', parser: Parameter.parseBoolean };
240
- declarations[Parameter.SCALING] = { type: 'number', parser: Parameter.parseNumber };
241
- declarations[Parameter.POSITION] = { type: 'vector', parser: Parameter.parseVector };
242
- declarations[Parameter.ROTATION] = { type: 'vector', parser: Parameter.parseVector };
243
- declarations[Parameter.HIGHLIGHTED] = { type: 'boolean', parser: Parameter.parseBoolean };
244
- declarations[Parameter.MATERIAL] = { type: 'string' };
245
- declarations[Parameter.MATERIAL_COLOR] = { type: 'color', parser: Parameter.parseColor };
246
- declarations[Parameter.MATERIAL_METALLNESS] = { type: 'number', parser: Parameter.parseNumber };
247
- declarations[Parameter.MATERIAL_ROUGHNESS] = { type: 'number', parser: Parameter.parseNumber };
248
- declarations[Parameter.HIGHLIGHT_ENABLED] = { type: 'boolean', parser: Parameter.parseBoolean };
249
- declarations[Parameter.HIGHLIGHT_COLOR] = { type: 'color', parser: Parameter.parseColor };
250
- declarations[Parameter.HIGHLIGHTED] = { type: 'boolean', parser: Parameter.parseBoolean };
251
- declarations[Parameter.CAST_SHADOW] = { type: 'boolean', parser: Parameter.parseBoolean };
252
- declarations[Parameter.CAST_SHADOW_FROM_LIGHTS] = { type: 'csl', parser: Parameter.parseCommaSeparatedList };
253
- declarations[Parameter.RECEIVE_SHADOWS] = { type: 'boolean', parser: Parameter.parseBoolean };
254
- declarations[Parameter.INTENSITY] = { type: 'number', parser: Parameter.parseNumber };
255
- declarations[Parameter.DIRECTION] = { type: 'vector', parser: Parameter.parseVector };
256
- declarations[Parameter.ANGLE] = { type: 'number', parser: Parameter.parseNumber };
257
- declarations[Parameter.EXPONENT] = { type: 'number', parser: Parameter.parseNumber };
258
- declarations[Parameter.DIFFUSE] = { type: 'color', parser: Parameter.parseColor };
259
- declarations[Parameter.SPECULAR] = { type: 'color', parser: Parameter.parseColor };
260
- return declarations;
261
- }
262
-
263
- /**
264
- * Gets the {@link ParameterDeclaration} for a given {@link Parameter} declared in this class.
265
- */
266
- public static getDeclaration(parameter: string): ParameterDeclaration {
267
- return Parameter.declarations[parameter];
268
- }
269
-
270
- /**
271
- * All parameters defined in this class.
272
- */
273
- public static get all() {
274
- const all: string[] = [];
275
- for (const parameter in Parameter) {
276
- const property = Object.getOwnPropertyDescriptor(Parameter, parameter);
277
- if (parameter.toUpperCase() === parameter && property && isString(property.value)) {
278
- all.push(property.value);
279
- }
280
- }
281
- return all;
282
- }
283
-
284
- /**
285
- * Parses given {@link ParameterBag} with given {@link ParameterDeclarations}.
286
- */
287
- public static parseFromDeclarations(
288
- declarations: ParameterDeclarations,
289
- parameterBag: ParameterBag
290
- ): ParsedParameterBag {
291
- const parameters: ParsedParameterBag = {};
292
- for (const parameter in parameterBag) {
293
- const value = get(parameterBag, parameter);
294
- parameters[parameter] = value;
295
- const declaration: ParameterDeclaration = declarations[parameter];
296
- if (declaration) {
297
- if (!isEmpty(value) && declaration.parser) {
298
- parameters[parameter] = declaration.parser(value);
299
- }
300
- }
301
- }
302
- return parameters;
303
- }
304
-
305
- /**
306
- * Parses a string of format `'(x,y,z)'` to a `Vector3`.
307
- */
308
- public static parseVector(value: ParameterValue): Vector3 {
309
- if (!isString(value)) {
310
- throw new Error(`Unable to convert "${value}" into a Vector: not a string.`);
311
- }
312
- let cleanedValue = value.split(' ').join('');
313
- if (cleanedValue.startsWith('(') && cleanedValue.endsWith(')')) {
314
- cleanedValue = cleanedValue.substring(1, cleanedValue.length - 1);
315
- let [x, y, z] = cleanedValue.split(',').map(value => parseFloat(value));
316
- return new Vector3(x, y, z);
317
- } else {
318
- throw new Error(`Unable to parse "${value}" to a Vector: expected "(x,y,z)".`);
319
- }
320
- }
321
-
322
- /**
323
- * Stringifies a Vector3 to format `(x,y,z)`.
324
- */
325
- public static stringifyVector(value: Vector3): string {
326
- return `(${value.x},${value.y},${value.z})`;
327
- }
328
-
329
- /**
330
- * Parses a string of format `'(x,y,z)'` with angular degrees to a `Vector3` with rotation information in radians.
331
- */
332
- public static parseRotation(value: ParameterValue): Vector3 {
333
- const rotation = Parameter.parseVector(value);
334
- const deg2rad = function (deg: number) {
335
- return (deg * Math.PI) / 180;
336
- };
337
- return rotation.set(deg2rad(rotation.x), deg2rad(rotation.y), deg2rad(rotation.z));
338
- }
339
-
340
- /**
341
- * Parses a string of format `'(x,y,z)'` or a simple `number` to a `Vector3` for scaling.
342
- */
343
- public static parseScaling(value: ParameterValue): Vector3 {
344
- let scaling = new Vector3();
345
- if (isString(value)) {
346
- const cleanedFactor = value.split(' ').join('');
347
- if (cleanedFactor.startsWith('(') && cleanedFactor.endsWith(')')) {
348
- scaling = Parameter.parseVector(cleanedFactor);
349
- } else {
350
- const _factor = parseFloat(value);
351
- scaling = new Vector3(_factor, _factor, _factor);
352
- }
353
- } else if (isNumber(value)) {
354
- scaling = new Vector3(value, value, value);
355
- } else {
356
- throw new Error(`Unable to convert "${value}" to a Vector: expected "(x,y,z)".`);
357
- }
358
- return scaling;
359
- }
360
-
361
- /**
362
- * Parses a string of format `'#rrggbb'` or `'(r,g,b)'` to a `Color3`.
363
- */
364
- public static parseColor(value: ParameterValue): Color3 {
365
- let cleanedValue = value.toString().split(' ').join('');
366
- if (cleanedValue.startsWith('#')) {
367
- return Color3.FromHexString(value.toString());
368
- }
369
- if (cleanedValue.startsWith('(') && cleanedValue.endsWith(')')) {
370
- cleanedValue = cleanedValue.substring(1, cleanedValue.length - 1);
371
- let [r, g, b] = cleanedValue.split(',').map(value => parseFloat(value));
372
- return Color3.FromInts(r, g, b);
373
- }
374
- throw new Error(`Unable to parse "${value}" to a Color: expected "#rrggbb" or "(r,g,b)".`);
375
- }
376
-
377
- /**
378
- * Parses a `boolean` string (`'true'`/`'1'` or `'false'`/`'0'`) or `number` (`1` or `0`) to a `boolean` or `undefined`.
379
- */
380
- public static parseBoolean(value: ParameterValue): boolean {
381
- if (value.toString() === 'true' || value.toString() === '1') {
382
- return true;
383
- } else if (value.toString() === 'false' || value.toString() === '0') {
384
- return false;
385
- }
386
- throw new Error(`Unable to parse "${value}" to a boolean.`);
387
- }
388
-
389
- /**
390
- * Parses a number `string` to a `number`.
391
- */
392
- public static parseNumber(value: ParameterValue): number {
393
- return parseFloat(value.toString());
394
- }
395
-
396
- /**
397
- * Parses a string with comma separated values to a list of strings.
398
- */
399
- public static parseCommaSeparatedList(value: ParameterValue): string[] {
400
- return value
401
- .toString()
402
- .split(',')
403
- .map(s => s.trim());
404
- }
405
- }
1
+ import { Color3 } from '@babylonjs/core/Maths/math.color';
2
+ import { Vector3 } from '@babylonjs/core/Maths/math.vector';
3
+ import { get, isEmpty, isNumber, isString } from 'lodash-es';
4
+
5
+ /**
6
+ * This `Parameter` class defines all parameters that can be consumed and committed by a {@link ParameterObservable}.
7
+ * Use {@link ParameterObservable.addParameterObserver} or {@link ParameterObservable.commitParameters} to either add or
8
+ * commit parameters on your observable objects.
9
+ */
10
+ export class Parameter {
11
+ /**
12
+ * Constructor.
13
+ */
14
+ private constructor() {}
15
+
16
+ /**
17
+ * Mutates the visibility. Helper methods are {@link VariantParameterizable.show} and
18
+ * {@link VariantParameterizable.hide}. Getter and setter is {@link VariantParameterizable.visible}.
19
+ *
20
+ * Scopes:
21
+ * * {@link Element}
22
+ * * {@link Variant}
23
+ * * {@link ViewerLight} Available for all light types
24
+ *
25
+ * @parser {@link parseBoolean}
26
+ */
27
+ public static readonly VISIBLE = 'visible';
28
+
29
+ /**
30
+ * Stretches or compresses some meshes. Getter and setter is {@link VariantParameterizable.scaling}.
31
+ *
32
+ * Scopes:
33
+ * * {@link Element}
34
+ * * {@link Variant}
35
+ *
36
+ * @parser {@link parseScaling}
37
+ */
38
+ public static readonly SCALING = 'scaling';
39
+
40
+ /**
41
+ * Mutates the material. Getter and setter is {@link VariantParameterizable.material}. The value of this parameter
42
+ * represents the ID of a baked-in glTF or custom created material in the {@link Variant} tree.
43
+ *
44
+ * Scopes:
45
+ * * {@link Element}
46
+ * * {@link Variant}
47
+ *
48
+ * @parser string
49
+ */
50
+ public static readonly MATERIAL = 'material';
51
+
52
+ /**
53
+ * Mutates the material color.
54
+ *
55
+ * Scopes:
56
+ * * {@link Element}
57
+ * * {@link Variant}
58
+ *
59
+ * @parser {@link parseColor}
60
+ */
61
+ public static readonly MATERIAL_COLOR = 'material.color';
62
+
63
+ /**
64
+ * Mutates the material metallness.
65
+ *
66
+ * Scopes:
67
+ * * {@link Element}
68
+ * * {@link Variant}
69
+ *
70
+ * @parser {@link parseNumber}
71
+ */
72
+ public static readonly MATERIAL_METALLNESS = 'material.metallness';
73
+
74
+ /**
75
+ * Mutates the material roughness.
76
+ *
77
+ * Scopes:
78
+ * * {@link Element}
79
+ * * {@link Variant}
80
+ *
81
+ * @parser {@link parseNumber}
82
+ */
83
+ public static readonly MATERIAL_ROUGHNESS = 'material.roughness';
84
+
85
+ /**
86
+ * Mutates whether a highlight layer should be enabled.
87
+ *
88
+ * Scopes:
89
+ * * {@link Element}
90
+ * * {@link Variant}
91
+ *
92
+ * @parser {@link parseBoolean}
93
+ */
94
+ public static readonly HIGHLIGHT_ENABLED = 'highlight.enabled';
95
+
96
+ /**
97
+ * Changes the highlight color.
98
+ *
99
+ * Scopes:
100
+ * * {@link Element}
101
+ * * {@link Variant}
102
+ *
103
+ * @parser {@link parseColor}
104
+ */
105
+ public static readonly HIGHLIGHT_COLOR = 'highlight.color';
106
+
107
+ /**
108
+ * Mutates the highlighted state. Getter and setter is {@link VariantParameterizable.highlighted}.
109
+ *
110
+ * Scopes:
111
+ * * {@link Element}
112
+ * * {@link Variant}
113
+ *
114
+ * @parser {@link parseBoolean}
115
+ */
116
+ public static readonly HIGHLIGHTED = 'highlighted';
117
+
118
+ /**
119
+ * Mutates the position relative to its origin position. Getter and setter is {@link VariantParameterizable.position}.
120
+ *
121
+ * Scopes:
122
+ * * {@link Element}
123
+ * * {@link Variant}
124
+ * * {@link ViewerLight} Available for [PointLight](https://doc.babylonjs.com/typedoc/classes/babylon.pointlight#position), [SpotLight](https://doc.babylonjs.com/typedoc/classes/babylon.spotlight#position), [DirectionalLight](https://doc.babylonjs.com/typedoc/classes/babylon.directionallight#position)
125
+ *
126
+ * @parser {@link parseVector}
127
+ */
128
+ public static readonly POSITION = 'position';
129
+
130
+ /**
131
+ * Mutates the rotation relative to its origin rotation. Getter and setter is {@link VariantParameterizable.rotation}.
132
+ *
133
+ * Scopes:
134
+ * * {@link Element}
135
+ * * {@link Variant}
136
+ *
137
+ * @parser {@link parseRotation}
138
+ */
139
+ public static readonly ROTATION = 'rotation';
140
+
141
+ /**
142
+ * Mutates whether a shadow should be casted or not.
143
+ *
144
+ * Scopes:
145
+ * * {@link Element}
146
+ * * {@link Variant}
147
+ *
148
+ * @parser {@link parseBoolean}
149
+ */
150
+ public static readonly CAST_SHADOW = 'castShadow';
151
+
152
+ /**
153
+ * Mutates from which lights a shadow should be casted.
154
+ *
155
+ * Scopes:
156
+ * * {@link Element}
157
+ * * {@link Variant}
158
+ *
159
+ * @parser {@link parseCommaSeparatedList}
160
+ */
161
+ public static readonly CAST_SHADOW_FROM_LIGHTS = 'castShadow.fromLights';
162
+
163
+ /**
164
+ * Mutates whether to receive shadows or not.
165
+ *
166
+ * Scopes:
167
+ * * {@link Element}
168
+ * * {@link Variant}
169
+ *
170
+ * @parser {@link parseBoolean}
171
+ */
172
+ public static readonly RECEIVE_SHADOWS = 'receiveShadows';
173
+
174
+ /**
175
+ * Mutates the intensity of Lights.
176
+ *
177
+ * Scopes:
178
+ * * {@link ViewerLight} Available for [PointLight](https://doc.babylonjs.com/typedoc/classes/babylon.pointlight#intensity), [SpotLight](https://doc.babylonjs.com/typedoc/classes/babylon.spotlight#intensity), [DirectionalLight](https://doc.babylonjs.com/typedoc/classes/babylon.directionallight#intensity), [HemisphericLight](https://doc.babylonjs.com/typedoc/classes/babylon.hemisphericlight#intensity)
179
+ *
180
+ * @parser {@link parseNumber}
181
+ */
182
+ public static readonly INTENSITY = 'intensity';
183
+
184
+ /**
185
+ * Mutates the direction of Lights.
186
+ *
187
+ * Scopes:
188
+ * * {@link ViewerLight} Available for [PointLight](https://doc.babylonjs.com/typedoc/classes/babylon.pointlight#direction), [SpotLight](https://doc.babylonjs.com/typedoc/classes/babylon.spotlight#direction), [DirectionalLight](https://doc.babylonjs.com/typedoc/classes/babylon.directionallight#direction), [HemisphericLight](https://doc.babylonjs.com/typedoc/classes/babylon.hemisphericlight#direction)
189
+ *
190
+ * @parser {@link parseNumber}
191
+ */
192
+ public static readonly DIRECTION = 'direction';
193
+
194
+ /**
195
+ * Mutates the angle of Lights.
196
+ *
197
+ * Scopes:
198
+ * * {@link ViewerLight} Available for [SpotLight](https://doc.babylonjs.com/typedoc/classes/babylon.spotlight#angle)
199
+ *
200
+ * @parser {@link parseNumber}
201
+ */
202
+ public static readonly ANGLE = 'angle';
203
+
204
+ /**
205
+ * Mutates the exponent of Lights.
206
+ *
207
+ * Scopes:
208
+ * * {@link ViewerLight} Available for [SpotLight](https://doc.babylonjs.com/typedoc/classes/babylon.spotlight#exponent)
209
+ *
210
+ * @parser {@link parseNumber}
211
+ */
212
+ public static readonly EXPONENT = 'exponent';
213
+
214
+ /**
215
+ * Mutates the diffuse color of Lights.
216
+ *
217
+ * Scopes:
218
+ * * {@link ViewerLight} Available for [PointLight](https://doc.babylonjs.com/typedoc/classes/babylon.pointlight#diffuse), [SpotLight](https://doc.babylonjs.com/typedoc/classes/babylon.spotlight#diffuse), [DirectionalLight](https://doc.babylonjs.com/typedoc/classes/babylon.directionallight#diffuse), [HemisphericLight](https://doc.babylonjs.com/typedoc/classes/babylon.hemisphericlight#diffuse)
219
+ *
220
+ * @parser {@link parseNumber}
221
+ */
222
+ public static readonly DIFFUSE = 'diffuse';
223
+
224
+ /**
225
+ * Mutates the specular color of Lights.
226
+ *
227
+ * Scopes:
228
+ * * {@link ViewerLight} Available for [PointLight](https://doc.babylonjs.com/typedoc/classes/babylon.pointlight#specular), [SpotLight](https://doc.babylonjs.com/typedoc/classes/babylon.spotlight#specular), [DirectionalLight](https://doc.babylonjs.com/typedoc/classes/babylon.directionallight#specular), [HemisphericLight](https://doc.babylonjs.com/typedoc/classes/babylon.hemisphericlight#specular)
229
+ *
230
+ * @parser {@link parseNumber}
231
+ */
232
+ public static readonly SPECULAR = 'specular';
233
+
234
+ /**
235
+ * Sets the environment. See [BABYLON.Scene.environmentTexture](https://doc.babylonjs.com/typedoc/classes/BABYLON.Scene#environmentTexture)
236
+ *
237
+ * Scopes:
238
+ * *{@link SceneManager}
239
+ *
240
+ * @parser string
241
+ */
242
+ public static readonly ENVIRONMENT = 'environment';
243
+
244
+ /**
245
+ * Sets [BABYLON.Scene.clearColor](https://doc.babylonjs.com/typedoc/classes/BABYLON.Scene#clearColor)
246
+ *
247
+ * Scopes:
248
+ * *{@link SceneManager}
249
+ *
250
+ * @parser {@link parseColor}
251
+ */
252
+ public static readonly ENVIRONMENT_COLOR = 'environment.color';
253
+
254
+ /**
255
+ * Rotates connected textures, i.e. IBL, reflection, background, ..
256
+ *
257
+ * Scopes:
258
+ * *{@link SceneManager}
259
+ *
260
+ * @parser {@link parseNumber}
261
+ */
262
+ public static readonly ENVIRONMENT_ROTATION = 'environment.rotation';
263
+
264
+ /**
265
+ * Intensity of IBL
266
+ *
267
+ * Scopes:
268
+ * *{@link SceneManager}
269
+ *
270
+ * @parser {@link parseNumber}
271
+ */
272
+ public static readonly ENVIRONMENT_INTENSITY = 'environment.intensity';
273
+
274
+ /**
275
+ * Background texture on a PhotoDome. Just background, no IBL or reflection.
276
+ *
277
+ * Scopes:
278
+ * *{@link SceneManager}
279
+ *
280
+ * @parser string
281
+ */
282
+ public static readonly ENVIRONMENT_BACKGROUND = 'environment.background';
283
+
284
+ /**
285
+ * If TRUE, sets a `scene.environmentTexture` and creates
286
+ * a *BackgroundHelper* Node (via `scene.createDefaultEnvironment()) with two subnodes/meshes:
287
+ * - BackgroundPlane
288
+ * - BackgroundSkybox
289
+ *
290
+ * Scopes:
291
+ * *{@link SceneManager}
292
+ *
293
+ * @parser {@link parseBoolean}
294
+ */
295
+ public static readonly ENVIRONMENT_USEDEFAULT = 'environment.usedefault';
296
+
297
+ /**
298
+ * The {@link ParameterDeclarations} for all parameters in this class.
299
+ */
300
+ public static get declarations(): ParameterDeclarations {
301
+ const declarations: ParameterDeclarations = {};
302
+ declarations[Parameter.VISIBLE] = { type: 'boolean', parser: Parameter.parseBoolean };
303
+ declarations[Parameter.SCALING] = { type: 'number', parser: Parameter.parseNumber };
304
+ declarations[Parameter.POSITION] = { type: 'vector', parser: Parameter.parseVector };
305
+ declarations[Parameter.ROTATION] = { type: 'vector', parser: Parameter.parseVector };
306
+ declarations[Parameter.HIGHLIGHTED] = { type: 'boolean', parser: Parameter.parseBoolean };
307
+ declarations[Parameter.MATERIAL] = { type: 'string' };
308
+ declarations[Parameter.MATERIAL_COLOR] = { type: 'color', parser: Parameter.parseColor };
309
+ declarations[Parameter.MATERIAL_METALLNESS] = { type: 'number', parser: Parameter.parseNumber };
310
+ declarations[Parameter.MATERIAL_ROUGHNESS] = { type: 'number', parser: Parameter.parseNumber };
311
+ declarations[Parameter.HIGHLIGHT_ENABLED] = { type: 'boolean', parser: Parameter.parseBoolean };
312
+ declarations[Parameter.HIGHLIGHT_COLOR] = { type: 'color', parser: Parameter.parseColor };
313
+ declarations[Parameter.HIGHLIGHTED] = { type: 'boolean', parser: Parameter.parseBoolean };
314
+ declarations[Parameter.CAST_SHADOW] = { type: 'boolean', parser: Parameter.parseBoolean };
315
+ declarations[Parameter.CAST_SHADOW_FROM_LIGHTS] = { type: 'csl', parser: Parameter.parseCommaSeparatedList };
316
+ declarations[Parameter.RECEIVE_SHADOWS] = { type: 'boolean', parser: Parameter.parseBoolean };
317
+ declarations[Parameter.INTENSITY] = { type: 'number', parser: Parameter.parseNumber };
318
+ declarations[Parameter.DIRECTION] = { type: 'vector', parser: Parameter.parseVector };
319
+ declarations[Parameter.ANGLE] = { type: 'number', parser: Parameter.parseNumber };
320
+ declarations[Parameter.EXPONENT] = { type: 'number', parser: Parameter.parseNumber };
321
+ declarations[Parameter.DIFFUSE] = { type: 'color', parser: Parameter.parseColor };
322
+ declarations[Parameter.SPECULAR] = { type: 'color', parser: Parameter.parseColor };
323
+ declarations[Parameter.ENVIRONMENT] = { type: 'string' };
324
+ declarations[Parameter.ENVIRONMENT_COLOR] = { type: 'color', parser: Parameter.parseColor };
325
+ declarations[Parameter.ENVIRONMENT_ROTATION] = { type: 'number', parser: Parameter.parseNumber };
326
+ declarations[Parameter.ENVIRONMENT_INTENSITY] = { type: 'number', parser: Parameter.parseNumber };
327
+ declarations[Parameter.ENVIRONMENT_BACKGROUND] = { type: 'string' };
328
+ declarations[Parameter.ENVIRONMENT_USEDEFAULT] = { type: 'boolean', parser: Parameter.parseBoolean };
329
+ return declarations;
330
+ }
331
+
332
+ /**
333
+ * Gets the {@link ParameterDeclaration} for a given {@link Parameter} declared in this class.
334
+ */
335
+ public static getDeclaration(parameter: string): ParameterDeclaration {
336
+ return Parameter.declarations[parameter];
337
+ }
338
+
339
+ /**
340
+ * All parameters defined in this class.
341
+ */
342
+ public static get all() {
343
+ const all: string[] = [];
344
+ for (const parameter in Parameter) {
345
+ const property = Object.getOwnPropertyDescriptor(Parameter, parameter);
346
+ if (parameter.toUpperCase() === parameter && property && isString(property.value)) {
347
+ all.push(property.value);
348
+ }
349
+ }
350
+ return all;
351
+ }
352
+
353
+ /**
354
+ * Parses given {@link ParameterBag} with given {@link ParameterDeclarations}.
355
+ */
356
+ public static parseFromDeclarations(
357
+ declarations: ParameterDeclarations,
358
+ parameterBag: ParameterBag
359
+ ): ParsedParameterBag {
360
+ const parameters: ParsedParameterBag = {};
361
+ for (const parameter in parameterBag) {
362
+ const value = get(parameterBag, parameter);
363
+ parameters[parameter] = value;
364
+ const declaration: ParameterDeclaration = declarations[parameter];
365
+ if (declaration) {
366
+ if (!isEmpty(value) && declaration.parser) {
367
+ parameters[parameter] = declaration.parser(value);
368
+ }
369
+ }
370
+ }
371
+ return parameters;
372
+ }
373
+
374
+ /**
375
+ * Parses a string of format `'(x,y,z)'` to a `Vector3`.
376
+ */
377
+ public static parseVector(value: ParameterValue): Vector3 {
378
+ if (!isString(value)) {
379
+ throw new Error(`Unable to convert "${value}" into a Vector: not a string.`);
380
+ }
381
+ let cleanedValue = value.split(' ').join('');
382
+ if (cleanedValue.startsWith('(') && cleanedValue.endsWith(')')) {
383
+ cleanedValue = cleanedValue.substring(1, cleanedValue.length - 1);
384
+ let [x, y, z] = cleanedValue.split(',').map(value => parseFloat(value));
385
+ return new Vector3(x, y, z);
386
+ } else {
387
+ throw new Error(`Unable to parse "${value}" to a Vector: expected "(x,y,z)".`);
388
+ }
389
+ }
390
+
391
+ /**
392
+ * Stringifies a Vector3 to format `(x,y,z)`.
393
+ */
394
+ public static stringifyVector(value: Vector3): string {
395
+ return `(${value.x},${value.y},${value.z})`;
396
+ }
397
+
398
+ /**
399
+ * Parses a string of format `'(x,y,z)'` with angular degrees to a `Vector3` with rotation information in radians.
400
+ */
401
+ public static parseRotation(value: ParameterValue): Vector3 {
402
+ const rotation = Parameter.parseVector(value);
403
+ const deg2rad = function (deg: number) {
404
+ return (deg * Math.PI) / 180;
405
+ };
406
+ return rotation.set(deg2rad(rotation.x), deg2rad(rotation.y), deg2rad(rotation.z));
407
+ }
408
+
409
+ /**
410
+ * Parses a string of format `'(x,y,z)'` or a simple `number` to a `Vector3` for scaling.
411
+ */
412
+ public static parseScaling(value: ParameterValue): Vector3 {
413
+ let scaling = new Vector3();
414
+ if (isString(value)) {
415
+ const cleanedFactor = value.split(' ').join('');
416
+ if (cleanedFactor.startsWith('(') && cleanedFactor.endsWith(')')) {
417
+ scaling = Parameter.parseVector(cleanedFactor);
418
+ } else {
419
+ const _factor = parseFloat(value);
420
+ scaling = new Vector3(_factor, _factor, _factor);
421
+ }
422
+ } else if (isNumber(value)) {
423
+ scaling = new Vector3(value, value, value);
424
+ } else {
425
+ throw new Error(`Unable to convert "${value}" to a Vector: expected "(x,y,z)".`);
426
+ }
427
+ return scaling;
428
+ }
429
+
430
+ /**
431
+ * Parses a string of format `'#rrggbb'` or `'(r,g,b)'` to a `Color3`.
432
+ */
433
+ public static parseColor(value: ParameterValue): Color3 {
434
+ let cleanedValue = value.toString().split(' ').join('');
435
+ if (cleanedValue.startsWith('#')) {
436
+ return Color3.FromHexString(value.toString());
437
+ }
438
+ if (cleanedValue.startsWith('(') && cleanedValue.endsWith(')')) {
439
+ cleanedValue = cleanedValue.substring(1, cleanedValue.length - 1);
440
+ let [r, g, b] = cleanedValue.split(',').map(value => parseFloat(value));
441
+ return Color3.FromInts(r, g, b);
442
+ }
443
+ throw new Error(`Unable to parse "${value}" to a Color: expected "#rrggbb" or "(r,g,b)".`);
444
+ }
445
+
446
+ /**
447
+ * Parses a `boolean` string (`'true'`/`'1'` or `'false'`/`'0'`) or `number` (`1` or `0`) to a `boolean` or `undefined`.
448
+ */
449
+ public static parseBoolean(value: ParameterValue): boolean {
450
+ if (value.toString() === 'true' || value.toString() === '1') {
451
+ return true;
452
+ } else if (value.toString() === 'false' || value.toString() === '0') {
453
+ return false;
454
+ }
455
+ throw new Error(`Unable to parse "${value}" to a boolean.`);
456
+ }
457
+
458
+ /**
459
+ * Parses a number `string` to a `number`.
460
+ */
461
+ public static parseNumber(value: ParameterValue): number {
462
+ return parseFloat(value.toString());
463
+ }
464
+
465
+ /**
466
+ * Parses a string with comma separated values to a list of strings.
467
+ */
468
+ public static parseCommaSeparatedList(value: ParameterValue): string[] {
469
+ return value
470
+ .toString()
471
+ .split(',')
472
+ .map(s => s.trim());
473
+ }
474
+ }