@combeenation/3d-viewer 9.0.0 → 9.0.1

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 (107) hide show
  1. package/README.md +111 -111
  2. package/dist/lib-cjs/api/classes/animationInterface.d.ts +8 -8
  3. package/dist/lib-cjs/api/classes/animationInterface.js +2 -2
  4. package/dist/lib-cjs/api/classes/dottedPath.d.ts +79 -79
  5. package/dist/lib-cjs/api/classes/dottedPath.js +166 -166
  6. package/dist/lib-cjs/api/classes/element.d.ts +153 -153
  7. package/dist/lib-cjs/api/classes/element.js +667 -667
  8. package/dist/lib-cjs/api/classes/event.d.ts +401 -401
  9. package/dist/lib-cjs/api/classes/event.js +424 -424
  10. package/dist/lib-cjs/api/classes/eventBroadcaster.d.ts +26 -26
  11. package/dist/lib-cjs/api/classes/eventBroadcaster.js +49 -49
  12. package/dist/lib-cjs/api/classes/fuzzyMap.d.ts +7 -7
  13. package/dist/lib-cjs/api/classes/fuzzyMap.js +21 -21
  14. package/dist/lib-cjs/api/classes/parameter.d.ts +410 -397
  15. package/dist/lib-cjs/api/classes/parameter.js +642 -627
  16. package/dist/lib-cjs/api/classes/parameter.js.map +1 -1
  17. package/dist/lib-cjs/api/classes/parameterObservable.d.ts +36 -36
  18. package/dist/lib-cjs/api/classes/parameterObservable.js +72 -72
  19. package/dist/lib-cjs/api/classes/parameterizable.d.ts +15 -15
  20. package/dist/lib-cjs/api/classes/parameterizable.js +102 -102
  21. package/dist/lib-cjs/api/classes/placementAnimation.d.ts +45 -45
  22. package/dist/lib-cjs/api/classes/placementAnimation.js +176 -176
  23. package/dist/lib-cjs/api/classes/variant.d.ts +261 -261
  24. package/dist/lib-cjs/api/classes/variant.js +870 -870
  25. package/dist/lib-cjs/api/classes/variantInstance.d.ts +53 -53
  26. package/dist/lib-cjs/api/classes/variantInstance.js +125 -125
  27. package/dist/lib-cjs/api/classes/variantParameterizable.d.ts +17 -17
  28. package/dist/lib-cjs/api/classes/variantParameterizable.js +86 -86
  29. package/dist/lib-cjs/api/classes/viewer.d.ts +213 -213
  30. package/dist/lib-cjs/api/classes/viewer.js +717 -717
  31. package/dist/lib-cjs/api/classes/viewerError.d.ts +43 -43
  32. package/dist/lib-cjs/api/classes/viewerError.js +55 -55
  33. package/dist/lib-cjs/api/classes/viewerLight.d.ts +66 -66
  34. package/dist/lib-cjs/api/classes/viewerLight.js +348 -348
  35. package/dist/lib-cjs/api/internal/lensRendering.d.ts +8 -8
  36. package/dist/lib-cjs/api/internal/lensRendering.js +11 -11
  37. package/dist/lib-cjs/api/internal/sceneSetup.d.ts +13 -13
  38. package/dist/lib-cjs/api/internal/sceneSetup.js +226 -226
  39. package/dist/lib-cjs/api/manager/animationManager.d.ts +30 -30
  40. package/dist/lib-cjs/api/manager/animationManager.js +126 -126
  41. package/dist/lib-cjs/api/manager/gltfExportManager.d.ts +103 -103
  42. package/dist/lib-cjs/api/manager/gltfExportManager.js +325 -325
  43. package/dist/lib-cjs/api/manager/sceneManager.d.ts +33 -33
  44. package/dist/lib-cjs/api/manager/sceneManager.js +128 -128
  45. package/dist/lib-cjs/api/manager/tagManager.d.ts +118 -118
  46. package/dist/lib-cjs/api/manager/tagManager.js +522 -522
  47. package/dist/lib-cjs/api/manager/tagManager.js.map +1 -1
  48. package/dist/lib-cjs/api/manager/textureLoadManager.d.ts +22 -22
  49. package/dist/lib-cjs/api/manager/textureLoadManager.js +97 -97
  50. package/dist/lib-cjs/api/manager/variantInstanceManager.d.ts +106 -106
  51. package/dist/lib-cjs/api/manager/variantInstanceManager.js +290 -290
  52. package/dist/lib-cjs/api/store/specStorage.d.ts +32 -32
  53. package/dist/lib-cjs/api/store/specStorage.js +65 -65
  54. package/dist/lib-cjs/api/util/babylonHelper.d.ts +237 -237
  55. package/dist/lib-cjs/api/util/babylonHelper.js +825 -790
  56. package/dist/lib-cjs/api/util/babylonHelper.js.map +1 -1
  57. package/dist/lib-cjs/api/util/globalTypes.d.ts +479 -479
  58. package/dist/lib-cjs/api/util/globalTypes.js +1 -1
  59. package/dist/lib-cjs/api/util/resourceHelper.d.ts +58 -58
  60. package/dist/lib-cjs/api/util/resourceHelper.js +211 -211
  61. package/dist/lib-cjs/api/util/sceneLoaderHelper.d.ts +44 -44
  62. package/dist/lib-cjs/api/util/sceneLoaderHelper.js +175 -175
  63. package/dist/lib-cjs/api/util/stringHelper.d.ts +13 -13
  64. package/dist/lib-cjs/api/util/stringHelper.js +32 -32
  65. package/dist/lib-cjs/api/util/structureHelper.d.ts +9 -9
  66. package/dist/lib-cjs/api/util/structureHelper.js +60 -60
  67. package/dist/lib-cjs/buildinfo.json +3 -3
  68. package/dist/lib-cjs/commonjs.tsconfig.tsbuildinfo +1 -1
  69. package/dist/lib-cjs/index.d.ts +54 -54
  70. package/dist/lib-cjs/index.js +117 -117
  71. package/package.json +82 -82
  72. package/src/api/classes/animationInterface.ts +10 -10
  73. package/src/api/classes/dottedPath.ts +181 -181
  74. package/src/api/classes/element.ts +723 -723
  75. package/src/api/classes/event.ts +457 -457
  76. package/src/api/classes/eventBroadcaster.ts +52 -52
  77. package/src/api/classes/fuzzyMap.ts +21 -21
  78. package/src/api/classes/parameter.ts +686 -670
  79. package/src/api/classes/parameterObservable.ts +73 -73
  80. package/src/api/classes/parameterizable.ts +87 -87
  81. package/src/api/classes/placementAnimation.ts +162 -162
  82. package/src/api/classes/variant.ts +963 -963
  83. package/src/api/classes/variantInstance.ts +123 -123
  84. package/src/api/classes/variantParameterizable.ts +83 -83
  85. package/src/api/classes/viewer.ts +770 -770
  86. package/src/api/classes/viewerError.ts +63 -63
  87. package/src/api/classes/viewerLight.ts +339 -339
  88. package/src/api/internal/debugViewer.ts +90 -90
  89. package/src/api/internal/lensRendering.ts +9 -9
  90. package/src/api/internal/sceneSetup.ts +205 -205
  91. package/src/api/manager/animationManager.ts +143 -143
  92. package/src/api/manager/gltfExportManager.ts +302 -302
  93. package/src/api/manager/sceneManager.ts +134 -134
  94. package/src/api/manager/tagManager.ts +564 -564
  95. package/src/api/manager/textureLoadManager.ts +95 -95
  96. package/src/api/manager/variantInstanceManager.ts +306 -306
  97. package/src/api/store/specStorage.ts +68 -68
  98. package/src/api/util/babylonHelper.ts +913 -870
  99. package/src/api/util/globalTypes.ts +555 -555
  100. package/src/api/util/resourceHelper.ts +198 -198
  101. package/src/api/util/sceneLoaderHelper.ts +170 -170
  102. package/src/api/util/stringHelper.ts +30 -30
  103. package/src/api/util/structureHelper.ts +60 -60
  104. package/src/buildinfo.json +3 -3
  105. package/src/dev.ts +62 -62
  106. package/src/index.ts +103 -103
  107. package/src/types.d.ts +38 -38
@@ -1,670 +1,686 @@
1
- import { ViewerError, ViewerErrorIds } from './viewerError';
2
- import { Color3 } from '@babylonjs/core/Maths/math.color';
3
- import { Vector3 } from '@babylonjs/core/Maths/math.vector';
4
- import { capitalize, get, isEmpty, isNumber, isString } from 'lodash-es';
5
-
6
- /**
7
- * This `Parameter` class defines all parameters that can be consumed and committed by a {@link ParameterObservable}.
8
- * Use {@link ParameterObservable.addParameterObserver} or {@link ParameterObservable.commitParameters} to either add or
9
- * commit parameters on your observable objects.
10
- */
11
- export class Parameter {
12
- /**
13
- * Constructor.
14
- */
15
- private constructor() {}
16
-
17
- /**
18
- * Mutates the visibility. Helper methods are {@link VariantParameterizable.show} and
19
- * {@link VariantParameterizable.hide}. Getter and setter is {@link VariantParameterizable.visible}.
20
- *
21
- * Scopes:
22
- * * {@link Element}
23
- * * {@link Variant}
24
- * * {@link ViewerLight} Available for all light types
25
- *
26
- * @parser {@link parseBoolean}
27
- */
28
- public static readonly VISIBLE = 'visible';
29
-
30
- /**
31
- * Stretches or compresses some meshes. Getter and setter is {@link VariantParameterizable.scaling}.
32
- *
33
- * Scopes:
34
- * * {@link Element}
35
- * * {@link Variant}
36
- *
37
- * @parser {@link parseScaling}
38
- */
39
- public static readonly SCALING = 'scaling';
40
-
41
- /**
42
- * Mutates the material. Getter and setter is {@link VariantParameterizable.material}. The value of this parameter
43
- * represents the ID of a baked-in glTF or custom created material in the {@link Variant} tree.
44
- *
45
- * Scopes:
46
- * * {@link Element}
47
- * * {@link Variant}
48
- *
49
- * @parser string
50
- */
51
- public static readonly MATERIAL = 'material';
52
-
53
- /**
54
- * Mutates the material color.
55
- *
56
- * Scopes:
57
- * * {@link Element}
58
- * * {@link Variant}
59
- *
60
- * @parser {@link parseColor}
61
- */
62
- public static readonly MATERIAL_COLOR = 'material.color';
63
-
64
- /**
65
- * Mutates the material metallness.
66
- *
67
- * Scopes:
68
- * * {@link Element}
69
- * * {@link Variant}
70
- *
71
- * @parser {@link parseNumber}
72
- */
73
- public static readonly MATERIAL_METALLNESS = 'material.metallness';
74
-
75
- /**
76
- * Mutates the material roughness.
77
- *
78
- * Scopes:
79
- * * {@link Element}
80
- * * {@link Variant}
81
- *
82
- * @parser {@link parseNumber}
83
- */
84
- public static readonly MATERIAL_ROUGHNESS = 'material.roughness';
85
-
86
- /**
87
- * Mutates whether a highlight layer should be enabled.
88
- *
89
- * Scopes:
90
- * * {@link Element}
91
- * * {@link Variant}
92
- *
93
- * @parser {@link parseBoolean}
94
- */
95
- public static readonly HIGHLIGHT_ENABLED = 'highlight.enabled';
96
-
97
- /**
98
- * Changes the highlight color.
99
- *
100
- * Scopes:
101
- * * {@link Element}
102
- * * {@link Variant}
103
- *
104
- * @parser {@link parseColor}
105
- */
106
- public static readonly HIGHLIGHT_COLOR = 'highlight.color';
107
-
108
- /**
109
- * Mutates the highlighted state. Getter and setter is {@link VariantParameterizable.highlighted}.
110
- *
111
- * Scopes:
112
- * * {@link Element}
113
- * * {@link Variant}
114
- *
115
- * @parser {@link parseBoolean}
116
- */
117
- public static readonly HIGHLIGHTED = 'highlighted';
118
-
119
- /**
120
- * Mutates the position relative to its origin position. Getter and setter is {@link VariantParameterizable.position}.
121
- *
122
- * Scopes:
123
- * * {@link Element}
124
- * * {@link Variant}
125
- * * {@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)
126
- *
127
- * @parser {@link parseVector}
128
- */
129
- public static readonly POSITION = 'position';
130
-
131
- /**
132
- * Mutates the rotation relative to its origin rotation. Getter and setter is {@link VariantParameterizable.rotation}.
133
- *
134
- * Scopes:
135
- * * {@link Element}
136
- * * {@link Variant}
137
- *
138
- * @parser {@link parseRotation}
139
- */
140
- public static readonly ROTATION = 'rotation';
141
-
142
- /**
143
- * Mutates the color.
144
- *
145
- * Scopes:
146
- * * {@link Material} via {@link TagManager}
147
- *
148
- * @parser {@link parseColor}
149
- */
150
- public static readonly COLOR = 'color';
151
-
152
- /**
153
- * Mutates the metallic.
154
- *
155
- * Scopes:
156
- * * {@link Material} via {@link TagManager}
157
- *
158
- * @parser {@link parseNumber}
159
- */
160
- public static readonly METALLIC = 'metallic';
161
-
162
- /**
163
- * Mutates the texture of a material, based on an svg string, image path or image data.
164
- *
165
- * Scopes:
166
- * * {@link Material} via {@link TagManager}
167
- *
168
- */
169
- public static readonly PAINTABLE = 'paintable';
170
-
171
- /**
172
- * Mutates the roughness.
173
- *
174
- * Scopes:
175
- * * {@link Material} via {@link TagManager}
176
- *
177
- * @parser {@link parseNumber}
178
- */
179
- public static readonly ROUGHNESS = 'roughness';
180
-
181
- /**
182
- * Mutates whether a shadow should be casted or not.
183
- *
184
- * Scopes:
185
- * * {@link Element}
186
- * * {@link Variant}
187
- *
188
- * @parser {@link parseBoolean}
189
- */
190
- public static readonly CAST_SHADOW = 'castShadow';
191
-
192
- /**
193
- * Mutates from which lights a shadow should be casted.
194
- *
195
- * Scopes:
196
- * * {@link Element}
197
- * * {@link Variant}
198
- *
199
- * @parser {@link parseCommaSeparatedList}
200
- */
201
- public static readonly CAST_SHADOW_FROM_LIGHTS = 'castShadow.fromLights';
202
-
203
- /**
204
- * Mutates whether to receive shadows or not.
205
- *
206
- * Scopes:
207
- * * {@link Element}
208
- * * {@link Variant}
209
- *
210
- * @parser {@link parseBoolean}
211
- */
212
- public static readonly RECEIVE_SHADOWS = 'receiveShadows';
213
-
214
- /**
215
- * Mutates the intensity of Lights.
216
- *
217
- * Scopes:
218
- * * {@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)
219
- *
220
- * @parser {@link parseNumber}
221
- */
222
- public static readonly INTENSITY = 'intensity';
223
-
224
- /**
225
- * Mutates the direction of Lights.
226
- *
227
- * Scopes:
228
- * * {@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)
229
- *
230
- * @parser {@link parseNumber}
231
- */
232
- public static readonly DIRECTION = 'direction';
233
-
234
- /**
235
- * Mutates the angle of Lights.
236
- *
237
- * Scopes:
238
- * * {@link ViewerLight} Available for [SpotLight](https://doc.babylonjs.com/typedoc/classes/babylon.spotlight#angle)
239
- *
240
- * @parser {@link parseNumber}
241
- */
242
- public static readonly ANGLE = 'angle';
243
-
244
- /**
245
- * Mutates the exponent of Lights.
246
- *
247
- * Scopes:
248
- * * {@link ViewerLight} Available for [SpotLight](https://doc.babylonjs.com/typedoc/classes/babylon.spotlight#exponent)
249
- *
250
- * @parser {@link parseNumber}
251
- */
252
- public static readonly EXPONENT = 'exponent';
253
-
254
- /**
255
- * Mutates the diffuse color of Lights.
256
- *
257
- * Scopes:
258
- * * {@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)
259
- *
260
- * @parser {@link parseNumber}
261
- */
262
- public static readonly DIFFUSE = 'diffuse';
263
-
264
- /**
265
- * Mutates the specular color of Lights.
266
- *
267
- * Scopes:
268
- * * {@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)
269
- *
270
- * @parser {@link parseNumber}
271
- */
272
- public static readonly SPECULAR = 'specular';
273
-
274
- /**
275
- * Sets the environment. See [BABYLON.Scene.environmentTexture](https://doc.babylonjs.com/typedoc/classes/BABYLON.Scene#environmentTexture).
276
- *
277
- * Usually the URL to an `*.env` file.
278
- *
279
- * See [environment page](./../pages/Documentation/Environment.html) for details on how to use this.
280
- *
281
- * Scopes:
282
- * * {@link SceneManager}
283
- *
284
- * @parser string
285
- */
286
- public static readonly ENVIRONMENT = 'environment';
287
-
288
- /**
289
- * Sets [BABYLON.Scene.clearColor](https://doc.babylonjs.com/typedoc/classes/BABYLON.Scene#clearColor)
290
- *
291
- * See [environment page](./../pages/Documentation/Environment.html) for details on how to use this.
292
- *
293
- * Scopes:
294
- * * {@link SceneManager}
295
- *
296
- * @parser {@link parseColor}
297
- */
298
- public static readonly ENVIRONMENT_COLOR = 'environment.color';
299
-
300
- /**
301
- * Rotates connected textures, i.e. IBL, reflection, background, ...
302
- * Rotation angle in degree.
303
- *
304
- * See [environment page](./../pages/Documentation/Environment.html) for details on how to use this.
305
- *
306
- * Scopes:
307
- * * {@link SceneManager}
308
- *
309
- * @parser {@link parseNumber}
310
- */
311
- public static readonly ENVIRONMENT_ROTATION = 'environment.rotation';
312
-
313
- /**
314
- * Intensity of IBL. Defaults to 1, usually between 0 & 2.
315
- *
316
- * See [environment page](./../pages/Documentation/Environment.html) for details on how to use this.
317
- *
318
- * Scopes:
319
- * * {@link SceneManager}
320
- *
321
- * @parser {@link parseNumber}
322
- */
323
- public static readonly ENVIRONMENT_INTENSITY = 'environment.intensity';
324
-
325
- /**
326
- * Background texture on a [PhotoDome](https://doc.babylonjs.com/divingDeeper/environment/360PhotoDome).
327
- * Just background, no IBL or reflection.
328
- *
329
- * Recommended format is `*.jpg`. HDR files are not supported as background but can be converted to `*.jpg` using
330
- * tools like Photoshop, Gimp or various free online converters.
331
- *
332
- * See [environment page](./../pages/Documentation/Environment.html) for details on how to use this.
333
- *
334
- * Scopes:
335
- * * {@link SceneManager}
336
- *
337
- * @parser string
338
- */
339
- public static readonly ENVIRONMENT_BACKGROUND = 'environment.background';
340
-
341
- /**
342
- * If `true`, sets a `scene.environmentTexture` and creates a *BackgroundHelper* Node (via
343
- * [scene.createDefaultEnvironment](https://doc.babylonjs.com/typedoc/classes/BABYLON.Scene#createDefaultEnvironment))
344
- * with two subnodes/meshes:
345
- * - BackgroundPlane
346
- * - BackgroundSkybox
347
- *
348
- * The value of this parameter is ignored if {@link ENVIRONMENT_BACKGROUND} is also set.
349
- *
350
- * See [environment page](./../pages/Documentation/Environment.html) for details on how to use this.
351
- *
352
- * Scopes:
353
- * * {@link SceneManager}
354
- *
355
- * @parser {@link parseBoolean}
356
- */
357
- public static readonly ENVIRONMENT_USEDEFAULT = 'environment.usedefault';
358
-
359
- /**
360
- * The {@link ParameterDeclarations} for all parameters in this class.
361
- */
362
- public static get declarations(): ParameterDeclarations {
363
- return {
364
- [Parameter.VISIBLE]: { type: 'boolean', parser: Parameter.parseBoolean },
365
- [Parameter.SCALING]: { type: 'number', parser: Parameter.parseNumber },
366
- [Parameter.POSITION]: { type: 'vector', parser: Parameter.parseVector },
367
- [Parameter.ROTATION]: { type: 'vector', parser: Parameter.parseVector },
368
- [Parameter.HIGHLIGHTED]: { type: 'boolean', parser: Parameter.parseBoolean },
369
- [Parameter.MATERIAL]: { type: 'string' },
370
- [Parameter.MATERIAL_COLOR]: { type: 'color', parser: Parameter.parseColor },
371
- [Parameter.MATERIAL_METALLNESS]: { type: 'number', parser: Parameter.parseNumber },
372
- [Parameter.MATERIAL_ROUGHNESS]: { type: 'number', parser: Parameter.parseNumber },
373
- [Parameter.HIGHLIGHT_ENABLED]: { type: 'boolean', parser: Parameter.parseBoolean },
374
- [Parameter.HIGHLIGHT_COLOR]: { type: 'color', parser: Parameter.parseColor },
375
- [Parameter.HIGHLIGHTED]: { type: 'boolean', parser: Parameter.parseBoolean },
376
- [Parameter.CAST_SHADOW]: { type: 'boolean', parser: Parameter.parseBoolean },
377
- [Parameter.CAST_SHADOW_FROM_LIGHTS]: { type: 'csl', parser: Parameter.parseCommaSeparatedList },
378
- [Parameter.RECEIVE_SHADOWS]: { type: 'boolean', parser: Parameter.parseBoolean },
379
- [Parameter.INTENSITY]: { type: 'number', parser: Parameter.parseNumber },
380
- [Parameter.DIRECTION]: { type: 'vector', parser: Parameter.parseVector },
381
- [Parameter.ANGLE]: { type: 'number', parser: Parameter.parseNumber },
382
- [Parameter.EXPONENT]: { type: 'number', parser: Parameter.parseNumber },
383
- [Parameter.DIFFUSE]: { type: 'color', parser: Parameter.parseColor },
384
- [Parameter.SPECULAR]: { type: 'color', parser: Parameter.parseColor },
385
- [Parameter.ENVIRONMENT]: { type: 'string' },
386
- [Parameter.ENVIRONMENT_COLOR]: { type: 'color', parser: Parameter.parseColor },
387
- [Parameter.ENVIRONMENT_ROTATION]: { type: 'number', parser: Parameter.parseNumber },
388
- [Parameter.ENVIRONMENT_INTENSITY]: { type: 'number', parser: Parameter.parseNumber },
389
- [Parameter.ENVIRONMENT_BACKGROUND]: { type: 'string' },
390
- [Parameter.ENVIRONMENT_USEDEFAULT]: { type: 'boolean', parser: Parameter.parseBoolean },
391
- };
392
- }
393
-
394
- /**
395
- * The default {@link ParameterValue}s returned as {@link ParameterBag} for a set of parameters in this class.
396
- */
397
- public static get defaultValues(): ParameterBag {
398
- return {
399
- [Parameter.SCALING]: '(1, 1, 1)',
400
- [Parameter.POSITION]: '(0, 0, 0)',
401
- [Parameter.ROTATION]: '(0, 0, 0)',
402
- };
403
- }
404
-
405
- /**
406
- * Gets the {@link ParameterDeclaration} for a given {@link Parameter} declared in this class.
407
- */
408
- public static getDeclaration(parameter: string): ParameterDeclaration {
409
- return Parameter.declarations[parameter];
410
- }
411
-
412
- /**
413
- * Gets the default {@link ParameterValue} for a given {@link Parameter} declared in this class.
414
- */
415
- public static getDefaultValue(parameter: string): ParameterValue {
416
- return Parameter.defaultValues[parameter];
417
- }
418
-
419
- /**
420
- * All parameters defined in this class.
421
- */
422
- public static get all() {
423
- const all: string[] = [];
424
- for (const parameter in Parameter) {
425
- const property = Object.getOwnPropertyDescriptor(Parameter, parameter);
426
- if (parameter.toUpperCase() === parameter && property && isString(property.value)) {
427
- all.push(property.value);
428
- }
429
- }
430
- return all;
431
- }
432
-
433
- /**
434
- * Asserts whether given parameter is declared and valid.
435
- */
436
- public static assertParameter(parameterDeclaration: ParameterDeclarations, parameter: string, value: ParameterValue) {
437
- if (!(parameter in parameterDeclaration)) {
438
- // This is a valid case since we are not able to check parameters that are not declared.
439
- // We just ignore parameters that are not declared.
440
- return;
441
- }
442
- const declaration = parameterDeclaration[parameter];
443
- const genericError = `"${value}" is not a valid value for parameter "${parameter}" of type "${declaration.type}".`;
444
- if (declaration.parser) {
445
- try {
446
- declaration.parser(value);
447
- } catch (e: any) {
448
- throw new ViewerError({
449
- id: ViewerErrorIds.InvalidParameterValue,
450
- message: `${genericError} ${e.message}`,
451
- });
452
- }
453
- }
454
- switch (declaration.type) {
455
- case 'select':
456
- if (!declaration.options) {
457
- throw new ViewerError({
458
- id: ViewerErrorIds.UnknownParameterOption,
459
- message: `No options defined for parameter declaration "${parameter}" of type "${declaration.type}".`,
460
- });
461
- }
462
- if (declaration.options.indexOf(value) === -1) {
463
- throw Error(genericError + ` Valid values are: "${declaration.options.join('", "')}".`);
464
- }
465
- break;
466
- }
467
- }
468
-
469
- /**
470
- * Parses given {@link ParameterBag} with given {@link ParameterDeclarations}.
471
- */
472
- public static parseFromDeclarations(
473
- declarations: ParameterDeclarations,
474
- parameterBag: ParameterBag
475
- ): ParsedParameterBag {
476
- const parameters: ParsedParameterBag = {};
477
- for (const parameter in parameterBag) {
478
- const value = get(parameterBag, parameter);
479
- parameters[parameter] = value;
480
- const declaration: ParameterDeclaration = declarations[parameter];
481
- if (declaration) {
482
- if (!isEmpty(value) && declaration.parser) {
483
- parameters[parameter] = declaration.parser(value);
484
- }
485
- }
486
- }
487
- return parameters;
488
- }
489
-
490
- /**
491
- * Parses a string of format `'(x,y,z)'` to a `Vector3`.
492
- */
493
- public static parseVector(value: ParameterValue): Vector3 {
494
- if (!isString(value)) {
495
- throw new Error(`Unable to convert "${value}" into a Vector: not a string.`);
496
- }
497
- let cleanedValue = value.split(' ').join('');
498
- if (cleanedValue.startsWith('(') && cleanedValue.endsWith(')')) {
499
- cleanedValue = cleanedValue.substring(1, cleanedValue.length - 1);
500
- const [x, y, z] = cleanedValue.split(',').map(value => parseFloat(value));
501
- return new Vector3(x, y, z);
502
- } else {
503
- throw new Error(`Unable to parse "${value}" to a Vector: expected "(x,y,z)".`);
504
- }
505
- }
506
-
507
- /**
508
- * Stringifies a Vector3 to format `(x,y,z)`.
509
- */
510
- public static stringifyVector(value: Vector3): string {
511
- return `(${value.x},${value.y},${value.z})`;
512
- }
513
-
514
- /**
515
- * Parses a string of format `'(x,y,z)'` with angular degrees to a `Vector3` with rotation information in radians.
516
- */
517
- public static parseRotation(value: ParameterValue): Vector3 {
518
- const rotation = Parameter.parseVector(value);
519
- const deg2rad = function (deg: number) {
520
- return (deg * Math.PI) / 180;
521
- };
522
- return rotation.set(deg2rad(rotation.x), deg2rad(rotation.y), deg2rad(rotation.z));
523
- }
524
-
525
- /**
526
- * Parses a string of format `'(x,y,z)'` or a simple `number` to a `Vector3` for scaling.
527
- */
528
- public static parseScaling(value: ParameterValue): Vector3 {
529
- let scaling = new Vector3();
530
- if (isString(value)) {
531
- const cleanedFactor = value.split(' ').join('');
532
- if (cleanedFactor.startsWith('(') && cleanedFactor.endsWith(')')) {
533
- scaling = Parameter.parseVector(cleanedFactor);
534
- } else {
535
- const _factor = parseFloat(value);
536
- scaling = new Vector3(_factor, _factor, _factor);
537
- }
538
- } else if (isNumber(value)) {
539
- scaling = new Vector3(value, value, value);
540
- } else {
541
- throw new Error(`Unable to convert "${value}" to a Vector: expected "(x,y,z)".`);
542
- }
543
- return scaling;
544
- }
545
-
546
- /**
547
- * Parses a string of format `'#rrggbb'` or `'(r,g,b)'` to a `Color3`.
548
- */
549
- public static parseColor(value: ParameterValue): Color3 {
550
- const cleanedValue = value.toString().split(' ').join('');
551
- if (cleanedValue.startsWith('#')) {
552
- return Color3.FromHexString(value.toString());
553
- }
554
- if (cleanedValue.startsWith('(') && cleanedValue.endsWith(')')) {
555
- const rgb = cleanedValue.substring(1, cleanedValue.length - 1);
556
- const [r, g, b] = rgb.split(',').map(value => parseFloat(value));
557
- return Color3.FromInts(r, g, b);
558
- }
559
- const humanReadable = capitalize(cleanedValue);
560
- // disable lint for this rule as there is no smoother option for checking static class functions (eg Color.Red())
561
- // eslint-disable-next-line no-prototype-builtins
562
- if (Color3.hasOwnProperty(humanReadable)) {
563
- // @ts-ignore same as above
564
- return Color3[humanReadable]();
565
- }
566
- throw new Error(
567
- `Unable to parse "${value}" to a Color: expected "#rrggbb", "(r,g,b)" or any human readable property implemented in Color3.`
568
- );
569
- }
570
-
571
- /**
572
- * Parses a `boolean` string (`'true'`/`'1'` or `'false'`/`'0'`) or `number` (`1` or `0`) to a `boolean` or
573
- * `undefined`.
574
- */
575
- public static parseBoolean(value: ParameterValue): boolean {
576
- if (value.toString() === 'true' || value.toString() === '1') {
577
- return true;
578
- } else if (value.toString() === 'false' || value.toString() === '0') {
579
- return false;
580
- }
581
- throw new Error(`Unable to parse "${value}" to a boolean.`);
582
- }
583
-
584
- /**
585
- * Parses a number `string` to a `number`.
586
- */
587
- public static parseNumber(value: ParameterValue): number {
588
- return parseFloat(value.toString());
589
- }
590
-
591
- /**
592
- * Parses string defintion of a paintable into a paintable object.
593
- * Here are some examples:
594
- * - '{ "src": "pathToCertain.jpg", "uScale": 0.5 }' => default definition as JSON object
595
- * - 'pathToCertain.jpg' => short hand definition, only contains source string
596
- * - '{ "src": "pathToCertain.jpg", "uScale": -1, "vScale": -1, "uOffset": 0, "vOffset": 0 }' => full content,
597
- * paintable texture is flipped in both directions
598
- * - '<svg>...</svg>' => SVG content can be used directly
599
- * - { "src": "<svg>...</svg>", "uScale": 0.5 } => SVG in src property works as well
600
- */
601
- public static parsePaintableValue(value: ParameterValue): PaintableValue {
602
- if (!isString(value)) {
603
- throw new Error(`Unable to parse "${value}" to a PaintableValue: not a string.`);
604
- }
605
-
606
- const paintableValue: PaintableValue = { src: '' };
607
- let valObj: { [key: string]: any } | null = null;
608
-
609
- try {
610
- valObj = JSON.parse(value);
611
- } catch {
612
- // use string directly
613
- paintableValue.src = value;
614
-
615
- if (value.startsWith('{')) {
616
- // seems like the user tried to use a JSON string, as the input starts with {
617
- throw new Error(`Unable to parse "${value}" to a PaintableValue: not a valid JSON string.`);
618
- }
619
- }
620
-
621
- if (valObj) {
622
- // input string is JSON, src attribute is required
623
- if (!valObj.src) {
624
- throw new Error(`Unable to parse "${value}" to a PaintableValue: property "src" is missing.`);
625
- }
626
-
627
- if (!isString(valObj.src)) {
628
- throw new Error(`Unable to parse "${value}" to a PaintableValue: property "src" is not a string.`);
629
- }
630
-
631
- // split src and options
632
- const { src, ...options } = valObj;
633
- paintableValue.src = src;
634
-
635
- // only forward valid paintable options
636
- const validOptionKeys = ['uScale', 'vScale', 'uOffset', 'vOffset'];
637
- const { validOptions, invalidKeys } = Object.entries(options).reduce(
638
- (accRes, [curKey, curValue]) => {
639
- const isValidKey = validOptionKeys.includes(curKey);
640
- if (isValidKey) {
641
- accRes.validOptions[curKey] = curValue;
642
- } else {
643
- accRes.invalidKeys.push(curKey);
644
- }
645
-
646
- return accRes;
647
- },
648
- { validOptions: {} as { [key: string]: any }, invalidKeys: [] as string[] }
649
- );
650
-
651
- if (invalidKeys.length) {
652
- console.warn('parsePaintableValue: invalid paintable options provided: ' + invalidKeys.toString());
653
- }
654
-
655
- paintableValue.options = validOptions;
656
- }
657
-
658
- return paintableValue;
659
- }
660
-
661
- /**
662
- * Parses a string with comma separated values to a list of strings.
663
- */
664
- public static parseCommaSeparatedList(value: ParameterValue): string[] {
665
- return value
666
- .toString()
667
- .split(',')
668
- .map(s => s.trim());
669
- }
670
- }
1
+ import { ViewerError, ViewerErrorIds } from './viewerError';
2
+ import { Color3 } from '@babylonjs/core/Maths/math.color';
3
+ import { Vector3 } from '@babylonjs/core/Maths/math.vector';
4
+ import { capitalize, get, isEmpty, isNumber, isString } from 'lodash-es';
5
+
6
+ /**
7
+ * This `Parameter` class defines all parameters that can be consumed and committed by a {@link ParameterObservable}.
8
+ * Use {@link ParameterObservable.addParameterObserver} or {@link ParameterObservable.commitParameters} to either add or
9
+ * commit parameters on your observable objects.
10
+ */
11
+ export class Parameter {
12
+ /**
13
+ * Constructor.
14
+ */
15
+ private constructor() {}
16
+
17
+ /**
18
+ * Mutates the visibility. Helper methods are {@link VariantParameterizable.show} and
19
+ * {@link VariantParameterizable.hide}. Getter and setter is {@link VariantParameterizable.visible}.
20
+ *
21
+ * Scopes:
22
+ * * {@link Element}
23
+ * * {@link Variant}
24
+ * * {@link ViewerLight} Available for all light types
25
+ *
26
+ * @parser {@link parseBoolean}
27
+ */
28
+ public static readonly VISIBLE = 'visible';
29
+
30
+ /**
31
+ * Stretches or compresses some meshes. Getter and setter is {@link VariantParameterizable.scaling}.
32
+ *
33
+ * Scopes:
34
+ * * {@link Element}
35
+ * * {@link Variant}
36
+ *
37
+ * @parser {@link parseScaling}
38
+ */
39
+ public static readonly SCALING = 'scaling';
40
+
41
+ /**
42
+ * Mutates the material. Getter and setter is {@link VariantParameterizable.material}. The value of this parameter
43
+ * represents the ID of a baked-in glTF or custom created material in the {@link Variant} tree.
44
+ *
45
+ * Scopes:
46
+ * * {@link Element}
47
+ * * {@link Variant}
48
+ *
49
+ * @parser string
50
+ */
51
+ public static readonly MATERIAL = 'material';
52
+
53
+ /**
54
+ * Mutates the material color.
55
+ *
56
+ * Scopes:
57
+ * * {@link Element}
58
+ * * {@link Variant}
59
+ *
60
+ * @parser {@link parseColor}
61
+ */
62
+ public static readonly MATERIAL_COLOR = 'material.color';
63
+
64
+ /**
65
+ * Mutates the material metallness.
66
+ *
67
+ * Scopes:
68
+ * * {@link Element}
69
+ * * {@link Variant}
70
+ *
71
+ * @parser {@link parseNumber}
72
+ */
73
+ public static readonly MATERIAL_METALLNESS = 'material.metallness';
74
+
75
+ /**
76
+ * Mutates the material roughness.
77
+ *
78
+ * Scopes:
79
+ * * {@link Element}
80
+ * * {@link Variant}
81
+ *
82
+ * @parser {@link parseNumber}
83
+ */
84
+ public static readonly MATERIAL_ROUGHNESS = 'material.roughness';
85
+
86
+ /**
87
+ * Mutates whether a highlight layer should be enabled.
88
+ *
89
+ * Scopes:
90
+ * * {@link Element}
91
+ * * {@link Variant}
92
+ *
93
+ * @parser {@link parseBoolean}
94
+ */
95
+ public static readonly HIGHLIGHT_ENABLED = 'highlight.enabled';
96
+
97
+ /**
98
+ * Changes the highlight color.
99
+ *
100
+ * Scopes:
101
+ * * {@link Element}
102
+ * * {@link Variant}
103
+ *
104
+ * @parser {@link parseColor}
105
+ */
106
+ public static readonly HIGHLIGHT_COLOR = 'highlight.color';
107
+
108
+ /**
109
+ * Mutates the highlighted state. Getter and setter is {@link VariantParameterizable.highlighted}.
110
+ *
111
+ * Scopes:
112
+ * * {@link Element}
113
+ * * {@link Variant}
114
+ *
115
+ * @parser {@link parseBoolean}
116
+ */
117
+ public static readonly HIGHLIGHTED = 'highlighted';
118
+
119
+ /**
120
+ * Mutates the position relative to its origin position. Getter and setter is {@link VariantParameterizable.position}.
121
+ *
122
+ * Scopes:
123
+ * * {@link Element}
124
+ * * {@link Variant}
125
+ * * {@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)
126
+ *
127
+ * @parser {@link parseVector}
128
+ */
129
+ public static readonly POSITION = 'position';
130
+
131
+ /**
132
+ * Mutates the rotation relative to its origin rotation. Getter and setter is {@link VariantParameterizable.rotation}.
133
+ *
134
+ * Scopes:
135
+ * * {@link Element}
136
+ * * {@link Variant}
137
+ *
138
+ * @parser {@link parseRotation}
139
+ */
140
+ public static readonly ROTATION = 'rotation';
141
+
142
+ /**
143
+ * Mutates the color.
144
+ *
145
+ * Scopes:
146
+ * * {@link Material} via {@link TagManager}
147
+ *
148
+ * @parser {@link parseColor}
149
+ */
150
+ public static readonly COLOR = 'color';
151
+
152
+ /**
153
+ * Mutates the metallic.
154
+ *
155
+ * Scopes:
156
+ * * {@link Material} via {@link TagManager}
157
+ *
158
+ * @parser {@link parseNumber}
159
+ */
160
+ public static readonly METALLIC = 'metallic';
161
+
162
+ /**
163
+ * Paintables can be used to let the client/browser draw graphics on a 3d model by mutating the texture of a
164
+ * material.\
165
+ * For detailed information have a look at the [paintables](./../pages/documentation/Paintables.html) page.
166
+ *
167
+ * Also see {@link parsePaintable} for detailed information on which values you can pass to the `paintable`
168
+ * parameter.
169
+ *
170
+ * Scopes:
171
+ * * {@link Material} via {@link TagManager}
172
+ *
173
+ * @parser {@link parsePaintable}
174
+ *
175
+ */
176
+ public static readonly PAINTABLE = 'paintable';
177
+
178
+ /**
179
+ * Mutates the roughness.
180
+ *
181
+ * Scopes:
182
+ * * {@link Material} via {@link TagManager}
183
+ *
184
+ * @parser {@link parseNumber}
185
+ */
186
+ public static readonly ROUGHNESS = 'roughness';
187
+
188
+ /**
189
+ * Mutates whether a shadow should be casted or not.
190
+ *
191
+ * Scopes:
192
+ * * {@link Element}
193
+ * * {@link Variant}
194
+ *
195
+ * @parser {@link parseBoolean}
196
+ */
197
+ public static readonly CAST_SHADOW = 'castShadow';
198
+
199
+ /**
200
+ * Mutates from which lights a shadow should be casted.
201
+ *
202
+ * Scopes:
203
+ * * {@link Element}
204
+ * * {@link Variant}
205
+ *
206
+ * @parser {@link parseCommaSeparatedList}
207
+ */
208
+ public static readonly CAST_SHADOW_FROM_LIGHTS = 'castShadow.fromLights';
209
+
210
+ /**
211
+ * Mutates whether to receive shadows or not.
212
+ *
213
+ * Scopes:
214
+ * * {@link Element}
215
+ * * {@link Variant}
216
+ *
217
+ * @parser {@link parseBoolean}
218
+ */
219
+ public static readonly RECEIVE_SHADOWS = 'receiveShadows';
220
+
221
+ /**
222
+ * Mutates the intensity of Lights.
223
+ *
224
+ * Scopes:
225
+ * * {@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)
226
+ *
227
+ * @parser {@link parseNumber}
228
+ */
229
+ public static readonly INTENSITY = 'intensity';
230
+
231
+ /**
232
+ * Mutates the direction of Lights.
233
+ *
234
+ * Scopes:
235
+ * * {@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)
236
+ *
237
+ * @parser {@link parseNumber}
238
+ */
239
+ public static readonly DIRECTION = 'direction';
240
+
241
+ /**
242
+ * Mutates the angle of Lights.
243
+ *
244
+ * Scopes:
245
+ * * {@link ViewerLight} Available for [SpotLight](https://doc.babylonjs.com/typedoc/classes/babylon.spotlight#angle)
246
+ *
247
+ * @parser {@link parseNumber}
248
+ */
249
+ public static readonly ANGLE = 'angle';
250
+
251
+ /**
252
+ * Mutates the exponent of Lights.
253
+ *
254
+ * Scopes:
255
+ * * {@link ViewerLight} Available for [SpotLight](https://doc.babylonjs.com/typedoc/classes/babylon.spotlight#exponent)
256
+ *
257
+ * @parser {@link parseNumber}
258
+ */
259
+ public static readonly EXPONENT = 'exponent';
260
+
261
+ /**
262
+ * Mutates the diffuse color of Lights.
263
+ *
264
+ * Scopes:
265
+ * * {@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)
266
+ *
267
+ * @parser {@link parseNumber}
268
+ */
269
+ public static readonly DIFFUSE = 'diffuse';
270
+
271
+ /**
272
+ * Mutates the specular color of Lights.
273
+ *
274
+ * Scopes:
275
+ * * {@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)
276
+ *
277
+ * @parser {@link parseNumber}
278
+ */
279
+ public static readonly SPECULAR = 'specular';
280
+
281
+ /**
282
+ * Sets the environment. See [BABYLON.Scene.environmentTexture](https://doc.babylonjs.com/typedoc/classes/BABYLON.Scene#environmentTexture).
283
+ *
284
+ * Usually the URL to an `*.env` file.
285
+ *
286
+ * See [environment page](./../pages/documentation/Environment.html) for details on how to use this.
287
+ *
288
+ * Scopes:
289
+ * * {@link SceneManager}
290
+ *
291
+ * @parser string
292
+ */
293
+ public static readonly ENVIRONMENT = 'environment';
294
+
295
+ /**
296
+ * Sets [BABYLON.Scene.clearColor](https://doc.babylonjs.com/typedoc/classes/BABYLON.Scene#clearColor)
297
+ *
298
+ * See [environment page](./../pages/documentation/Environment.html) for details on how to use this.
299
+ *
300
+ * Scopes:
301
+ * * {@link SceneManager}
302
+ *
303
+ * @parser {@link parseColor}
304
+ */
305
+ public static readonly ENVIRONMENT_COLOR = 'environment.color';
306
+
307
+ /**
308
+ * Rotates connected textures, i.e. IBL, reflection, background, ...
309
+ * Rotation angle in degree.
310
+ *
311
+ * See [environment page](./../pages/documentation/Environment.html) for details on how to use this.
312
+ *
313
+ * Scopes:
314
+ * * {@link SceneManager}
315
+ *
316
+ * @parser {@link parseNumber}
317
+ */
318
+ public static readonly ENVIRONMENT_ROTATION = 'environment.rotation';
319
+
320
+ /**
321
+ * Intensity of IBL. Defaults to 1, usually between 0 & 2.
322
+ *
323
+ * See [environment page](./../pages/documentation/Environment.html) for details on how to use this.
324
+ *
325
+ * Scopes:
326
+ * * {@link SceneManager}
327
+ *
328
+ * @parser {@link parseNumber}
329
+ */
330
+ public static readonly ENVIRONMENT_INTENSITY = 'environment.intensity';
331
+
332
+ /**
333
+ * Background texture on a [PhotoDome](https://doc.babylonjs.com/divingDeeper/environment/360PhotoDome).
334
+ * Just background, no IBL or reflection.
335
+ *
336
+ * Recommended format is `*.jpg`. HDR files are not supported as background but can be converted to `*.jpg` using
337
+ * tools like Photoshop, Gimp or various free online converters.
338
+ *
339
+ * See [environment page](./../pages/documentation/Environment.html) for details on how to use this.
340
+ *
341
+ * Scopes:
342
+ * * {@link SceneManager}
343
+ *
344
+ * @parser string
345
+ */
346
+ public static readonly ENVIRONMENT_BACKGROUND = 'environment.background';
347
+
348
+ /**
349
+ * If `true`, sets a `scene.environmentTexture` and creates a *BackgroundHelper* Node (via
350
+ * [scene.createDefaultEnvironment](https://doc.babylonjs.com/typedoc/classes/BABYLON.Scene#createDefaultEnvironment))
351
+ * with two subnodes/meshes:
352
+ * - BackgroundPlane
353
+ * - BackgroundSkybox
354
+ *
355
+ * The value of this parameter is ignored if {@link ENVIRONMENT_BACKGROUND} is also set.
356
+ *
357
+ * See [environment page](./../pages/documentation/Environment.html) for details on how to use this.
358
+ *
359
+ * Scopes:
360
+ * * {@link SceneManager}
361
+ *
362
+ * @parser {@link parseBoolean}
363
+ */
364
+ public static readonly ENVIRONMENT_USEDEFAULT = 'environment.usedefault';
365
+
366
+ /**
367
+ * The {@link ParameterDeclarations} for all parameters in this class.
368
+ */
369
+ public static get declarations(): ParameterDeclarations {
370
+ return {
371
+ [Parameter.VISIBLE]: { type: 'boolean', parser: Parameter.parseBoolean },
372
+ [Parameter.SCALING]: { type: 'number', parser: Parameter.parseNumber },
373
+ [Parameter.POSITION]: { type: 'vector', parser: Parameter.parseVector },
374
+ [Parameter.ROTATION]: { type: 'vector', parser: Parameter.parseVector },
375
+ [Parameter.HIGHLIGHTED]: { type: 'boolean', parser: Parameter.parseBoolean },
376
+ [Parameter.MATERIAL]: { type: 'string' },
377
+ [Parameter.MATERIAL_COLOR]: { type: 'color', parser: Parameter.parseColor },
378
+ [Parameter.MATERIAL_METALLNESS]: { type: 'number', parser: Parameter.parseNumber },
379
+ [Parameter.MATERIAL_ROUGHNESS]: { type: 'number', parser: Parameter.parseNumber },
380
+ [Parameter.HIGHLIGHT_ENABLED]: { type: 'boolean', parser: Parameter.parseBoolean },
381
+ [Parameter.HIGHLIGHT_COLOR]: { type: 'color', parser: Parameter.parseColor },
382
+ [Parameter.COLOR]: { type: 'color', parser: Parameter.parseColor },
383
+ [Parameter.METALLIC]: { type: 'number', parser: Parameter.parseNumber },
384
+ [Parameter.ROUGHNESS]: { type: 'number', parser: Parameter.parseNumber },
385
+ [Parameter.PAINTABLE]: { type: 'paintable', parser: Parameter.parsePaintable },
386
+ [Parameter.HIGHLIGHTED]: { type: 'boolean', parser: Parameter.parseBoolean },
387
+ [Parameter.CAST_SHADOW]: { type: 'boolean', parser: Parameter.parseBoolean },
388
+ [Parameter.CAST_SHADOW_FROM_LIGHTS]: { type: 'csl', parser: Parameter.parseCommaSeparatedList },
389
+ [Parameter.RECEIVE_SHADOWS]: { type: 'boolean', parser: Parameter.parseBoolean },
390
+ [Parameter.INTENSITY]: { type: 'number', parser: Parameter.parseNumber },
391
+ [Parameter.DIRECTION]: { type: 'vector', parser: Parameter.parseVector },
392
+ [Parameter.ANGLE]: { type: 'number', parser: Parameter.parseNumber },
393
+ [Parameter.EXPONENT]: { type: 'number', parser: Parameter.parseNumber },
394
+ [Parameter.DIFFUSE]: { type: 'color', parser: Parameter.parseColor },
395
+ [Parameter.SPECULAR]: { type: 'color', parser: Parameter.parseColor },
396
+ [Parameter.ENVIRONMENT]: { type: 'string' },
397
+ [Parameter.ENVIRONMENT_COLOR]: { type: 'color', parser: Parameter.parseColor },
398
+ [Parameter.ENVIRONMENT_ROTATION]: { type: 'number', parser: Parameter.parseNumber },
399
+ [Parameter.ENVIRONMENT_INTENSITY]: { type: 'number', parser: Parameter.parseNumber },
400
+ [Parameter.ENVIRONMENT_BACKGROUND]: { type: 'string' },
401
+ [Parameter.ENVIRONMENT_USEDEFAULT]: { type: 'boolean', parser: Parameter.parseBoolean },
402
+ };
403
+ }
404
+
405
+ /**
406
+ * The default {@link ParameterValue}s returned as {@link ParameterBag} for a set of parameters in this class.
407
+ */
408
+ public static get defaultValues(): ParameterBag {
409
+ return {
410
+ [Parameter.SCALING]: '(1, 1, 1)',
411
+ [Parameter.POSITION]: '(0, 0, 0)',
412
+ [Parameter.ROTATION]: '(0, 0, 0)',
413
+ };
414
+ }
415
+
416
+ /**
417
+ * Gets the default {@link ParameterValue} for a given {@link Parameter} declared in this class.
418
+ */
419
+ public static getDefaultValue(parameter: string): ParameterValue {
420
+ return Parameter.defaultValues[parameter];
421
+ }
422
+
423
+ /**
424
+ * All parameters defined in this class.
425
+ */
426
+ public static get all() {
427
+ const all: string[] = [];
428
+ for (const parameter in Parameter) {
429
+ const property = Object.getOwnPropertyDescriptor(Parameter, parameter);
430
+ if (parameter.toUpperCase() === parameter && property && isString(property.value)) {
431
+ all.push(property.value);
432
+ }
433
+ }
434
+ return all;
435
+ }
436
+
437
+ /**
438
+ * Asserts whether given parameter is declared and valid.
439
+ */
440
+ public static assertParameter(parameterDeclaration: ParameterDeclarations, parameter: string, value: ParameterValue) {
441
+ if (!(parameter in parameterDeclaration)) {
442
+ // This is a valid case since we are not able to check parameters that are not declared.
443
+ // We just ignore parameters that are not declared.
444
+ return;
445
+ }
446
+ const declaration = parameterDeclaration[parameter];
447
+ const errorPrefix = `Invalid value for parameter "${parameter}" of type "${declaration.type}" given:`;
448
+ if (declaration.parser) {
449
+ try {
450
+ declaration.parser(value);
451
+ } catch (e: any) {
452
+ throw new ViewerError({
453
+ id: ViewerErrorIds.InvalidParameterValue,
454
+ message: `${errorPrefix}\n${e.message}.\nGiven value: ${value}`,
455
+ });
456
+ }
457
+ }
458
+ switch (declaration.type) {
459
+ case 'select':
460
+ if (!declaration.options) {
461
+ throw new ViewerError({
462
+ id: ViewerErrorIds.UnknownParameterOption,
463
+ message: `No options defined for parameter declaration "${parameter}" of type "${declaration.type}".`,
464
+ });
465
+ }
466
+ if (declaration.options.indexOf(value) === -1) {
467
+ throw Error(
468
+ `${errorPrefix}\nValid values are: "${declaration.options.join('", "')}".\nGiven value: ${value}`
469
+ );
470
+ }
471
+ break;
472
+ }
473
+ }
474
+
475
+ /**
476
+ * Parses given {@link ParameterBag} with given {@link ParameterDeclarations}.
477
+ */
478
+ public static parseFromDeclarations(
479
+ declarations: ParameterDeclarations,
480
+ parameterBag: ParameterBag
481
+ ): ParsedParameterBag {
482
+ const parameters: ParsedParameterBag = {};
483
+ for (const parameter in parameterBag) {
484
+ const value = get(parameterBag, parameter);
485
+ parameters[parameter] = value;
486
+ const declaration: ParameterDeclaration = declarations[parameter];
487
+ if (declaration) {
488
+ if (!isEmpty(value) && declaration.parser) {
489
+ parameters[parameter] = declaration.parser(value);
490
+ }
491
+ }
492
+ }
493
+ return parameters;
494
+ }
495
+
496
+ /**
497
+ * Parses a string of format `'(x,y,z)'` to a `Vector3`.
498
+ */
499
+ public static parseVector(value: ParameterValue): Vector3 {
500
+ if (!isString(value)) {
501
+ throw new Error(`Unable to convert "${value}" into a Vector: not a string.`);
502
+ }
503
+ let cleanedValue = value.split(' ').join('');
504
+ if (cleanedValue.startsWith('(') && cleanedValue.endsWith(')')) {
505
+ cleanedValue = cleanedValue.substring(1, cleanedValue.length - 1);
506
+ const [x, y, z] = cleanedValue.split(',').map(value => parseFloat(value));
507
+ return new Vector3(x, y, z);
508
+ } else {
509
+ throw new Error(`Unable to parse "${value}" to a Vector: expected "(x,y,z)".`);
510
+ }
511
+ }
512
+
513
+ /**
514
+ * Stringifies a Vector3 to format `(x,y,z)`.
515
+ */
516
+ public static stringifyVector(value: Vector3): string {
517
+ return `(${value.x},${value.y},${value.z})`;
518
+ }
519
+
520
+ /**
521
+ * Parses a string of format `'(x,y,z)'` with angular degrees to a `Vector3` with rotation information in radians.
522
+ */
523
+ public static parseRotation(value: ParameterValue): Vector3 {
524
+ const rotation = Parameter.parseVector(value);
525
+ const deg2rad = function (deg: number) {
526
+ return (deg * Math.PI) / 180;
527
+ };
528
+ return rotation.set(deg2rad(rotation.x), deg2rad(rotation.y), deg2rad(rotation.z));
529
+ }
530
+
531
+ /**
532
+ * Parses a string of format `'(x,y,z)'` or a simple `number` to a `Vector3` for scaling.
533
+ */
534
+ public static parseScaling(value: ParameterValue): Vector3 {
535
+ let scaling = new Vector3();
536
+ if (isString(value)) {
537
+ const cleanedFactor = value.split(' ').join('');
538
+ if (cleanedFactor.startsWith('(') && cleanedFactor.endsWith(')')) {
539
+ scaling = Parameter.parseVector(cleanedFactor);
540
+ } else {
541
+ const _factor = parseFloat(value);
542
+ scaling = new Vector3(_factor, _factor, _factor);
543
+ }
544
+ } else if (isNumber(value)) {
545
+ scaling = new Vector3(value, value, value);
546
+ } else {
547
+ throw new Error(`Unable to convert "${value}" to a Vector: expected "(x,y,z)".`);
548
+ }
549
+ return scaling;
550
+ }
551
+
552
+ /**
553
+ * Parses a string of format `'#rrggbb'` or `'(r,g,b)'` to a `Color3`.
554
+ */
555
+ public static parseColor(value: ParameterValue): Color3 {
556
+ const cleanedValue = value.toString().split(' ').join('');
557
+ if (cleanedValue.startsWith('#')) {
558
+ return Color3.FromHexString(value.toString());
559
+ }
560
+ if (cleanedValue.startsWith('(') && cleanedValue.endsWith(')')) {
561
+ const rgb = cleanedValue.substring(1, cleanedValue.length - 1);
562
+ const [r, g, b] = rgb.split(',').map(value => parseFloat(value));
563
+ return Color3.FromInts(r, g, b);
564
+ }
565
+ const humanReadable = capitalize(cleanedValue);
566
+ // disable lint for this rule as there is no smoother option for checking static class functions (eg Color.Red())
567
+ // eslint-disable-next-line no-prototype-builtins
568
+ if (Color3.hasOwnProperty(humanReadable)) {
569
+ // @ts-ignore same as above
570
+ return Color3[humanReadable]();
571
+ }
572
+ throw new Error(
573
+ `Unable to parse "${value}" to a Color: expected "#rrggbb", "(r,g,b)" or any human readable property implemented in Color3.`
574
+ );
575
+ }
576
+
577
+ /**
578
+ * Parses a `boolean` string (`'true'`/`'1'` or `'false'`/`'0'`) or `number` (`1` or `0`) to a `boolean` or
579
+ * `undefined`.
580
+ */
581
+ public static parseBoolean(value: ParameterValue): boolean {
582
+ if (value.toString() === 'true' || value.toString() === '1') {
583
+ return true;
584
+ } else if (value.toString() === 'false' || value.toString() === '0') {
585
+ return false;
586
+ }
587
+ throw new Error(`Unable to parse "${value}" to a boolean.`);
588
+ }
589
+
590
+ /**
591
+ * Parses a number `string` to a `number`.
592
+ */
593
+ public static parseNumber(value: ParameterValue): number {
594
+ return parseFloat(value.toString());
595
+ }
596
+
597
+ /**
598
+ * Parses string defintion of a paintable into a paintable object.\
599
+ * Here are some examples:
600
+ * ```ts
601
+ * // Default definition as JSON object:
602
+ * '{ "src": "https://path.to/image.jpg", "uScale": 0.5 }'
603
+ *
604
+ * // Short hand definition, only contains source string:
605
+ * 'https://path.to/image.jpg'
606
+ *
607
+ * // Full content, paintable texture is flipped in both directions
608
+ * '{ "src": "https://path.to/image.jpg", "uScale": -1, "vScale": -1, "uOffset": 0, "vOffset": 0 }'
609
+ *
610
+ * // SVG content can be used directly:
611
+ * '<svg>...</svg>'
612
+ *
613
+ * // SVG in src property works as well:
614
+ * '{ "src": "<svg>...</svg>", "uScale": 0.5 }'
615
+ * ```
616
+ */
617
+ public static parsePaintable(value: ParameterValue): PaintableValue {
618
+ if (!isString(value)) {
619
+ throw new Error(`Not a string.`);
620
+ }
621
+
622
+ const paintableValue: PaintableValue = { src: '' };
623
+ let valObj: { [key: string]: any } | null = null;
624
+
625
+ try {
626
+ valObj = JSON.parse(value);
627
+ } catch {
628
+ // use string directly
629
+ paintableValue.src = value;
630
+
631
+ if (value.startsWith('{')) {
632
+ // seems like the user tried to use a JSON string, as the input starts with {
633
+ throw new Error(`Not a valid JSON string.`);
634
+ }
635
+ }
636
+
637
+ if (valObj) {
638
+ // input string is JSON, src attribute is required
639
+ if (!valObj.src) {
640
+ throw new Error(`Property "src" is missing.`);
641
+ }
642
+
643
+ if (!isString(valObj.src)) {
644
+ throw new Error(`Property "src" is not a string.`);
645
+ }
646
+
647
+ // split src and options
648
+ const { src, ...options } = valObj;
649
+ paintableValue.src = src;
650
+
651
+ // only forward valid paintable options
652
+ const validOptionKeys = ['uScale', 'vScale', 'uOffset', 'vOffset'];
653
+ const { validOptions, invalidKeys } = Object.entries(options).reduce(
654
+ (accRes, [curKey, curValue]) => {
655
+ const isValidKey = validOptionKeys.includes(curKey);
656
+ if (isValidKey) {
657
+ accRes.validOptions[curKey] = curValue;
658
+ } else {
659
+ accRes.invalidKeys.push(curKey);
660
+ }
661
+
662
+ return accRes;
663
+ },
664
+ { validOptions: {} as { [key: string]: any }, invalidKeys: [] as string[] }
665
+ );
666
+
667
+ if (invalidKeys.length) {
668
+ console.warn('parsePaintable: invalid paintable options provided: ' + invalidKeys.toString());
669
+ }
670
+
671
+ paintableValue.options = validOptions;
672
+ }
673
+
674
+ return paintableValue;
675
+ }
676
+
677
+ /**
678
+ * Parses a string with comma separated values to a list of strings.
679
+ */
680
+ public static parseCommaSeparatedList(value: ParameterValue): string[] {
681
+ return value
682
+ .toString()
683
+ .split(',')
684
+ .map(s => s.trim());
685
+ }
686
+ }