@combeenation/3d-viewer 8.1.0-alpha1 → 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 (113) 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 -672
  8. package/dist/lib-cjs/api/classes/element.js.map +1 -1
  9. package/dist/lib-cjs/api/classes/event.d.ts +401 -401
  10. package/dist/lib-cjs/api/classes/event.js +424 -424
  11. package/dist/lib-cjs/api/classes/eventBroadcaster.d.ts +26 -26
  12. package/dist/lib-cjs/api/classes/eventBroadcaster.js +49 -49
  13. package/dist/lib-cjs/api/classes/fuzzyMap.d.ts +7 -7
  14. package/dist/lib-cjs/api/classes/fuzzyMap.js +21 -21
  15. package/dist/lib-cjs/api/classes/parameter.d.ts +410 -378
  16. package/dist/lib-cjs/api/classes/parameter.js +642 -551
  17. package/dist/lib-cjs/api/classes/parameter.js.map +1 -1
  18. package/dist/lib-cjs/api/classes/parameterObservable.d.ts +36 -36
  19. package/dist/lib-cjs/api/classes/parameterObservable.js +72 -72
  20. package/dist/lib-cjs/api/classes/parameterizable.d.ts +15 -15
  21. package/dist/lib-cjs/api/classes/parameterizable.js +102 -102
  22. package/dist/lib-cjs/api/classes/placementAnimation.d.ts +45 -45
  23. package/dist/lib-cjs/api/classes/placementAnimation.js +176 -176
  24. package/dist/lib-cjs/api/classes/variant.d.ts +261 -261
  25. package/dist/lib-cjs/api/classes/variant.js +870 -870
  26. package/dist/lib-cjs/api/classes/variant.js.map +1 -1
  27. package/dist/lib-cjs/api/classes/variantInstance.d.ts +53 -53
  28. package/dist/lib-cjs/api/classes/variantInstance.js +125 -125
  29. package/dist/lib-cjs/api/classes/variantParameterizable.d.ts +17 -17
  30. package/dist/lib-cjs/api/classes/variantParameterizable.js +86 -86
  31. package/dist/lib-cjs/api/classes/viewer.d.ts +213 -213
  32. package/dist/lib-cjs/api/classes/viewer.js +717 -717
  33. package/dist/lib-cjs/api/classes/viewerError.d.ts +43 -43
  34. package/dist/lib-cjs/api/classes/viewerError.js +55 -55
  35. package/dist/lib-cjs/api/classes/viewerLight.d.ts +66 -66
  36. package/dist/lib-cjs/api/classes/viewerLight.js +348 -348
  37. package/dist/lib-cjs/api/classes/viewerLight.js.map +1 -1
  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 -13
  41. package/dist/lib-cjs/api/internal/sceneSetup.js +226 -226
  42. package/dist/lib-cjs/api/manager/animationManager.d.ts +30 -30
  43. package/dist/lib-cjs/api/manager/animationManager.js +126 -126
  44. package/dist/lib-cjs/api/manager/gltfExportManager.d.ts +103 -103
  45. package/dist/lib-cjs/api/manager/gltfExportManager.js +325 -325
  46. package/dist/lib-cjs/api/manager/gltfExportManager.js.map +1 -1
  47. package/dist/lib-cjs/api/manager/sceneManager.d.ts +33 -33
  48. package/dist/lib-cjs/api/manager/sceneManager.js +128 -128
  49. package/dist/lib-cjs/api/manager/tagManager.d.ts +118 -118
  50. package/dist/lib-cjs/api/manager/tagManager.js +522 -508
  51. package/dist/lib-cjs/api/manager/tagManager.js.map +1 -1
  52. package/dist/lib-cjs/api/manager/textureLoadManager.d.ts +22 -22
  53. package/dist/lib-cjs/api/manager/textureLoadManager.js +97 -97
  54. package/dist/lib-cjs/api/manager/variantInstanceManager.d.ts +106 -106
  55. package/dist/lib-cjs/api/manager/variantInstanceManager.js +290 -290
  56. package/dist/lib-cjs/api/store/specStorage.d.ts +32 -32
  57. package/dist/lib-cjs/api/store/specStorage.js +65 -65
  58. package/dist/lib-cjs/api/util/babylonHelper.d.ts +237 -235
  59. package/dist/lib-cjs/api/util/babylonHelper.js +825 -753
  60. package/dist/lib-cjs/api/util/babylonHelper.js.map +1 -1
  61. package/dist/lib-cjs/api/util/globalTypes.d.ts +479 -469
  62. package/dist/lib-cjs/api/util/globalTypes.js +1 -1
  63. package/dist/lib-cjs/api/util/resourceHelper.d.ts +58 -58
  64. package/dist/lib-cjs/api/util/resourceHelper.js +211 -203
  65. package/dist/lib-cjs/api/util/resourceHelper.js.map +1 -1
  66. package/dist/lib-cjs/api/util/sceneLoaderHelper.d.ts +44 -44
  67. package/dist/lib-cjs/api/util/sceneLoaderHelper.js +175 -175
  68. package/dist/lib-cjs/api/util/sceneLoaderHelper.js.map +1 -1
  69. package/dist/lib-cjs/api/util/stringHelper.d.ts +13 -13
  70. package/dist/lib-cjs/api/util/stringHelper.js +32 -32
  71. package/dist/lib-cjs/api/util/structureHelper.d.ts +9 -9
  72. package/dist/lib-cjs/api/util/structureHelper.js +60 -60
  73. package/dist/lib-cjs/buildinfo.json +3 -3
  74. package/dist/lib-cjs/commonjs.tsconfig.tsbuildinfo +1 -1
  75. package/dist/lib-cjs/index.d.ts +54 -54
  76. package/dist/lib-cjs/index.js +117 -117
  77. package/package.json +82 -81
  78. package/src/api/classes/animationInterface.ts +10 -10
  79. package/src/api/classes/dottedPath.ts +181 -181
  80. package/src/api/classes/element.ts +723 -733
  81. package/src/api/classes/event.ts +457 -457
  82. package/src/api/classes/eventBroadcaster.ts +52 -52
  83. package/src/api/classes/fuzzyMap.ts +21 -21
  84. package/src/api/classes/parameter.ts +686 -591
  85. package/src/api/classes/parameterObservable.ts +73 -73
  86. package/src/api/classes/parameterizable.ts +87 -87
  87. package/src/api/classes/placementAnimation.ts +162 -162
  88. package/src/api/classes/variant.ts +963 -963
  89. package/src/api/classes/variantInstance.ts +123 -123
  90. package/src/api/classes/variantParameterizable.ts +83 -83
  91. package/src/api/classes/viewer.ts +770 -770
  92. package/src/api/classes/viewerError.ts +63 -63
  93. package/src/api/classes/viewerLight.ts +339 -339
  94. package/src/api/internal/debugViewer.ts +90 -90
  95. package/src/api/internal/lensRendering.ts +9 -9
  96. package/src/api/internal/sceneSetup.ts +205 -205
  97. package/src/api/manager/animationManager.ts +143 -143
  98. package/src/api/manager/gltfExportManager.ts +302 -302
  99. package/src/api/manager/sceneManager.ts +134 -134
  100. package/src/api/manager/tagManager.ts +564 -536
  101. package/src/api/manager/textureLoadManager.ts +95 -95
  102. package/src/api/manager/variantInstanceManager.ts +306 -306
  103. package/src/api/store/specStorage.ts +68 -68
  104. package/src/api/util/babylonHelper.ts +913 -823
  105. package/src/api/util/globalTypes.ts +555 -543
  106. package/src/api/util/resourceHelper.ts +198 -191
  107. package/src/api/util/sceneLoaderHelper.ts +170 -170
  108. package/src/api/util/stringHelper.ts +30 -30
  109. package/src/api/util/structureHelper.ts +60 -60
  110. package/src/buildinfo.json +3 -3
  111. package/src/dev.ts +62 -62
  112. package/src/index.ts +103 -103
  113. package/src/types.d.ts +38 -38
@@ -1,591 +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 metallness.
154
- *
155
- * Scopes:
156
- * * {@link Material} via {@link TagManager}
157
- *
158
- * @parser {@link parseNumber}
159
- */
160
- public static readonly METALLNESS = 'metallness';
161
-
162
- /**
163
- * Mutates the roughness.
164
- *
165
- * Scopes:
166
- * * {@link Material} via {@link TagManager}
167
- *
168
- * @parser {@link parseNumber}
169
- */
170
- public static readonly ROUGHNESS = 'roughness';
171
-
172
- /**
173
- * Mutates whether a shadow should be casted or not.
174
- *
175
- * Scopes:
176
- * * {@link Element}
177
- * * {@link Variant}
178
- *
179
- * @parser {@link parseBoolean}
180
- */
181
- public static readonly CAST_SHADOW = 'castShadow';
182
-
183
- /**
184
- * Mutates from which lights a shadow should be casted.
185
- *
186
- * Scopes:
187
- * * {@link Element}
188
- * * {@link Variant}
189
- *
190
- * @parser {@link parseCommaSeparatedList}
191
- */
192
- public static readonly CAST_SHADOW_FROM_LIGHTS = 'castShadow.fromLights';
193
-
194
- /**
195
- * Mutates whether to receive shadows or not.
196
- *
197
- * Scopes:
198
- * * {@link Element}
199
- * * {@link Variant}
200
- *
201
- * @parser {@link parseBoolean}
202
- */
203
- public static readonly RECEIVE_SHADOWS = 'receiveShadows';
204
-
205
- /**
206
- * Mutates the intensity of Lights.
207
- *
208
- * Scopes:
209
- * * {@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)
210
- *
211
- * @parser {@link parseNumber}
212
- */
213
- public static readonly INTENSITY = 'intensity';
214
-
215
- /**
216
- * Mutates the direction of Lights.
217
- *
218
- * Scopes:
219
- * * {@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)
220
- *
221
- * @parser {@link parseNumber}
222
- */
223
- public static readonly DIRECTION = 'direction';
224
-
225
- /**
226
- * Mutates the angle of Lights.
227
- *
228
- * Scopes:
229
- * * {@link ViewerLight} Available for [SpotLight](https://doc.babylonjs.com/typedoc/classes/babylon.spotlight#angle)
230
- *
231
- * @parser {@link parseNumber}
232
- */
233
- public static readonly ANGLE = 'angle';
234
-
235
- /**
236
- * Mutates the exponent of Lights.
237
- *
238
- * Scopes:
239
- * * {@link ViewerLight} Available for [SpotLight](https://doc.babylonjs.com/typedoc/classes/babylon.spotlight#exponent)
240
- *
241
- * @parser {@link parseNumber}
242
- */
243
- public static readonly EXPONENT = 'exponent';
244
-
245
- /**
246
- * Mutates the diffuse color of Lights.
247
- *
248
- * Scopes:
249
- * * {@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)
250
- *
251
- * @parser {@link parseNumber}
252
- */
253
- public static readonly DIFFUSE = 'diffuse';
254
-
255
- /**
256
- * Mutates the specular color of Lights.
257
- *
258
- * Scopes:
259
- * * {@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)
260
- *
261
- * @parser {@link parseNumber}
262
- */
263
- public static readonly SPECULAR = 'specular';
264
-
265
- /**
266
- * Sets the environment. See [BABYLON.Scene.environmentTexture](https://doc.babylonjs.com/typedoc/classes/BABYLON.Scene#environmentTexture).
267
- *
268
- * Usually the URL to an `*.env` file.
269
- *
270
- * See [environment page](./../pages/Documentation/Environment.html) for details on how to use this.
271
- *
272
- * Scopes:
273
- * * {@link SceneManager}
274
- *
275
- * @parser string
276
- */
277
- public static readonly ENVIRONMENT = 'environment';
278
-
279
- /**
280
- * Sets [BABYLON.Scene.clearColor](https://doc.babylonjs.com/typedoc/classes/BABYLON.Scene#clearColor)
281
- *
282
- * See [environment page](./../pages/Documentation/Environment.html) for details on how to use this.
283
- *
284
- * Scopes:
285
- * * {@link SceneManager}
286
- *
287
- * @parser {@link parseColor}
288
- */
289
- public static readonly ENVIRONMENT_COLOR = 'environment.color';
290
-
291
- /**
292
- * Rotates connected textures, i.e. IBL, reflection, background, ...
293
- * Rotation angle in degree.
294
- *
295
- * See [environment page](./../pages/Documentation/Environment.html) for details on how to use this.
296
- *
297
- * Scopes:
298
- * * {@link SceneManager}
299
- *
300
- * @parser {@link parseNumber}
301
- */
302
- public static readonly ENVIRONMENT_ROTATION = 'environment.rotation';
303
-
304
- /**
305
- * Intensity of IBL. Defaults to 1, usually between 0 & 2.
306
- *
307
- * See [environment page](./../pages/Documentation/Environment.html) for details on how to use this.
308
- *
309
- * Scopes:
310
- * * {@link SceneManager}
311
- *
312
- * @parser {@link parseNumber}
313
- */
314
- public static readonly ENVIRONMENT_INTENSITY = 'environment.intensity';
315
-
316
- /**
317
- * Background texture on a [PhotoDome](https://doc.babylonjs.com/divingDeeper/environment/360PhotoDome).
318
- * Just background, no IBL or reflection.
319
- *
320
- * Recommended format is `*.jpg`. HDR files are not supported as background but can be converted to `*.jpg` using
321
- * tools like Photoshop, Gimp or various free online converters.
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 string
329
- */
330
- public static readonly ENVIRONMENT_BACKGROUND = 'environment.background';
331
-
332
- /**
333
- * If `true`, sets a `scene.environmentTexture` and creates a *BackgroundHelper* Node (via
334
- * [scene.createDefaultEnvironment](https://doc.babylonjs.com/typedoc/classes/BABYLON.Scene#createDefaultEnvironment))
335
- * with two subnodes/meshes:
336
- * - BackgroundPlane
337
- * - BackgroundSkybox
338
- *
339
- * The value of this parameter is ignored if {@link ENVIRONMENT_BACKGROUND} is also set.
340
- *
341
- * See [environment page](./../pages/Documentation/Environment.html) for details on how to use this.
342
- *
343
- * Scopes:
344
- * * {@link SceneManager}
345
- *
346
- * @parser {@link parseBoolean}
347
- */
348
- public static readonly ENVIRONMENT_USEDEFAULT = 'environment.usedefault';
349
-
350
- /**
351
- * The {@link ParameterDeclarations} for all parameters in this class.
352
- */
353
- public static get declarations(): ParameterDeclarations {
354
- return {
355
- [Parameter.VISIBLE]: { type: 'boolean', parser: Parameter.parseBoolean },
356
- [Parameter.SCALING]: { type: 'number', parser: Parameter.parseNumber },
357
- [Parameter.POSITION]: { type: 'vector', parser: Parameter.parseVector },
358
- [Parameter.ROTATION]: { type: 'vector', parser: Parameter.parseVector },
359
- [Parameter.HIGHLIGHTED]: { type: 'boolean', parser: Parameter.parseBoolean },
360
- [Parameter.MATERIAL]: { type: 'string' },
361
- [Parameter.MATERIAL_COLOR]: { type: 'color', parser: Parameter.parseColor },
362
- [Parameter.MATERIAL_METALLNESS]: { type: 'number', parser: Parameter.parseNumber },
363
- [Parameter.MATERIAL_ROUGHNESS]: { type: 'number', parser: Parameter.parseNumber },
364
- [Parameter.HIGHLIGHT_ENABLED]: { type: 'boolean', parser: Parameter.parseBoolean },
365
- [Parameter.HIGHLIGHT_COLOR]: { type: 'color', parser: Parameter.parseColor },
366
- [Parameter.HIGHLIGHTED]: { type: 'boolean', parser: Parameter.parseBoolean },
367
- [Parameter.CAST_SHADOW]: { type: 'boolean', parser: Parameter.parseBoolean },
368
- [Parameter.CAST_SHADOW_FROM_LIGHTS]: { type: 'csl', parser: Parameter.parseCommaSeparatedList },
369
- [Parameter.RECEIVE_SHADOWS]: { type: 'boolean', parser: Parameter.parseBoolean },
370
- [Parameter.INTENSITY]: { type: 'number', parser: Parameter.parseNumber },
371
- [Parameter.DIRECTION]: { type: 'vector', parser: Parameter.parseVector },
372
- [Parameter.ANGLE]: { type: 'number', parser: Parameter.parseNumber },
373
- [Parameter.EXPONENT]: { type: 'number', parser: Parameter.parseNumber },
374
- [Parameter.DIFFUSE]: { type: 'color', parser: Parameter.parseColor },
375
- [Parameter.SPECULAR]: { type: 'color', parser: Parameter.parseColor },
376
- [Parameter.ENVIRONMENT]: { type: 'string' },
377
- [Parameter.ENVIRONMENT_COLOR]: { type: 'color', parser: Parameter.parseColor },
378
- [Parameter.ENVIRONMENT_ROTATION]: { type: 'number', parser: Parameter.parseNumber },
379
- [Parameter.ENVIRONMENT_INTENSITY]: { type: 'number', parser: Parameter.parseNumber },
380
- [Parameter.ENVIRONMENT_BACKGROUND]: { type: 'string' },
381
- [Parameter.ENVIRONMENT_USEDEFAULT]: { type: 'boolean', parser: Parameter.parseBoolean },
382
- };
383
- }
384
-
385
- /**
386
- * The default {@link ParameterValue}s returned as {@link ParameterBag} for a set of parameters in this class.
387
- */
388
- public static get defaultValues(): ParameterBag {
389
- return {
390
- [Parameter.SCALING]: '(1, 1, 1)',
391
- [Parameter.POSITION]: '(0, 0, 0)',
392
- [Parameter.ROTATION]: '(0, 0, 0)',
393
- };
394
- }
395
-
396
- /**
397
- * Gets the {@link ParameterDeclaration} for a given {@link Parameter} declared in this class.
398
- */
399
- public static getDeclaration(parameter: string): ParameterDeclaration {
400
- return Parameter.declarations[parameter];
401
- }
402
-
403
- /**
404
- * Gets the default {@link ParameterValue} for a given {@link Parameter} declared in this class.
405
- */
406
- public static getDefaultValue(parameter: string): ParameterValue {
407
- return Parameter.defaultValues[parameter];
408
- }
409
-
410
- /**
411
- * All parameters defined in this class.
412
- */
413
- public static get all() {
414
- const all: string[] = [];
415
- for (const parameter in Parameter) {
416
- const property = Object.getOwnPropertyDescriptor(Parameter, parameter);
417
- if (parameter.toUpperCase() === parameter && property && isString(property.value)) {
418
- all.push(property.value);
419
- }
420
- }
421
- return all;
422
- }
423
-
424
- /**
425
- * Asserts whether given parameter is declared and valid.
426
- */
427
- public static assertParameter(parameterDeclaration: ParameterDeclarations, parameter: string, value: ParameterValue) {
428
- if (!(parameter in parameterDeclaration)) {
429
- // This is a valid case since we are not able to check parameters that are not declared.
430
- // We just ignore parameters that are not declared.
431
- return;
432
- }
433
- const declaration = parameterDeclaration[parameter];
434
- const genericError = `"${value}" is not a valid value for parameter "${parameter}" of type "${declaration.type}".`;
435
- if (declaration.parser) {
436
- try {
437
- declaration.parser(value);
438
- } catch (e: any) {
439
- throw new ViewerError({
440
- id: ViewerErrorIds.InvalidParameterValue,
441
- message: `${genericError} ${e.message}`,
442
- });
443
- }
444
- }
445
- switch (declaration.type) {
446
- case 'select':
447
- if (!declaration.options) {
448
- throw new ViewerError({
449
- id: ViewerErrorIds.UnknownParameterOption,
450
- message: `No options defined for parameter declaration "${parameter}" of type "${declaration.type}".`,
451
- });
452
- }
453
- if (declaration.options.indexOf(value) === -1) {
454
- throw Error(genericError + ` Valid values are: "${declaration.options.join('", "')}".`);
455
- }
456
- break;
457
- }
458
- }
459
-
460
- /**
461
- * Parses given {@link ParameterBag} with given {@link ParameterDeclarations}.
462
- */
463
- public static parseFromDeclarations(
464
- declarations: ParameterDeclarations,
465
- parameterBag: ParameterBag
466
- ): ParsedParameterBag {
467
- const parameters: ParsedParameterBag = {};
468
- for (const parameter in parameterBag) {
469
- const value = get(parameterBag, parameter);
470
- parameters[parameter] = value;
471
- const declaration: ParameterDeclaration = declarations[parameter];
472
- if (declaration) {
473
- if (!isEmpty(value) && declaration.parser) {
474
- parameters[parameter] = declaration.parser(value);
475
- }
476
- }
477
- }
478
- return parameters;
479
- }
480
-
481
- /**
482
- * Parses a string of format `'(x,y,z)'` to a `Vector3`.
483
- */
484
- public static parseVector(value: ParameterValue): Vector3 {
485
- if (!isString(value)) {
486
- throw new Error(`Unable to convert "${value}" into a Vector: not a string.`);
487
- }
488
- let cleanedValue = value.split(' ').join('');
489
- if (cleanedValue.startsWith('(') && cleanedValue.endsWith(')')) {
490
- cleanedValue = cleanedValue.substring(1, cleanedValue.length - 1);
491
- const [x, y, z] = cleanedValue.split(',').map(value => parseFloat(value));
492
- return new Vector3(x, y, z);
493
- } else {
494
- throw new Error(`Unable to parse "${value}" to a Vector: expected "(x,y,z)".`);
495
- }
496
- }
497
-
498
- /**
499
- * Stringifies a Vector3 to format `(x,y,z)`.
500
- */
501
- public static stringifyVector(value: Vector3): string {
502
- return `(${value.x},${value.y},${value.z})`;
503
- }
504
-
505
- /**
506
- * Parses a string of format `'(x,y,z)'` with angular degrees to a `Vector3` with rotation information in radians.
507
- */
508
- public static parseRotation(value: ParameterValue): Vector3 {
509
- const rotation = Parameter.parseVector(value);
510
- const deg2rad = function (deg: number) {
511
- return (deg * Math.PI) / 180;
512
- };
513
- return rotation.set(deg2rad(rotation.x), deg2rad(rotation.y), deg2rad(rotation.z));
514
- }
515
-
516
- /**
517
- * Parses a string of format `'(x,y,z)'` or a simple `number` to a `Vector3` for scaling.
518
- */
519
- public static parseScaling(value: ParameterValue): Vector3 {
520
- let scaling = new Vector3();
521
- if (isString(value)) {
522
- const cleanedFactor = value.split(' ').join('');
523
- if (cleanedFactor.startsWith('(') && cleanedFactor.endsWith(')')) {
524
- scaling = Parameter.parseVector(cleanedFactor);
525
- } else {
526
- const _factor = parseFloat(value);
527
- scaling = new Vector3(_factor, _factor, _factor);
528
- }
529
- } else if (isNumber(value)) {
530
- scaling = new Vector3(value, value, value);
531
- } else {
532
- throw new Error(`Unable to convert "${value}" to a Vector: expected "(x,y,z)".`);
533
- }
534
- return scaling;
535
- }
536
-
537
- /**
538
- * Parses a string of format `'#rrggbb'` or `'(r,g,b)'` to a `Color3`.
539
- */
540
- public static parseColor(value: ParameterValue): Color3 {
541
- const cleanedValue = value.toString().split(' ').join('');
542
- if (cleanedValue.startsWith('#')) {
543
- return Color3.FromHexString(value.toString());
544
- }
545
- if (cleanedValue.startsWith('(') && cleanedValue.endsWith(')')) {
546
- const rgb = cleanedValue.substring(1, cleanedValue.length - 1);
547
- const [r, g, b] = rgb.split(',').map(value => parseFloat(value));
548
- return Color3.FromInts(r, g, b);
549
- }
550
- const humanReadable = capitalize(cleanedValue);
551
- // disable lint for this rule as there is no smoother option for checking static class functions (eg Color.Red())
552
- // eslint-disable-next-line no-prototype-builtins
553
- if (Color3.hasOwnProperty(humanReadable)) {
554
- // @ts-ignore same as above
555
- return Color3[humanReadable]();
556
- }
557
- throw new Error(
558
- `Unable to parse "${value}" to a Color: expected "#rrggbb", "(r,g,b)" or any human readable property implemented in Color3.`
559
- );
560
- }
561
-
562
- /**
563
- * Parses a `boolean` string (`'true'`/`'1'` or `'false'`/`'0'`) or `number` (`1` or `0`) to a `boolean` or
564
- * `undefined`.
565
- */
566
- public static parseBoolean(value: ParameterValue): boolean {
567
- if (value.toString() === 'true' || value.toString() === '1') {
568
- return true;
569
- } else if (value.toString() === 'false' || value.toString() === '0') {
570
- return false;
571
- }
572
- throw new Error(`Unable to parse "${value}" to a boolean.`);
573
- }
574
-
575
- /**
576
- * Parses a number `string` to a `number`.
577
- */
578
- public static parseNumber(value: ParameterValue): number {
579
- return parseFloat(value.toString());
580
- }
581
-
582
- /**
583
- * Parses a string with comma separated values to a list of strings.
584
- */
585
- public static parseCommaSeparatedList(value: ParameterValue): string[] {
586
- return value
587
- .toString()
588
- .split(',')
589
- .map(s => s.trim());
590
- }
591
- }
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
+ }