@combeenation/3d-viewer 4.0.0-rc1 → 4.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (103) hide show
  1. package/README.md +114 -114
  2. package/dist/lib-cjs/api/classes/animationInterface.d.ts +8 -8
  3. package/dist/lib-cjs/api/classes/animationInterface.js +1 -1
  4. package/dist/lib-cjs/api/classes/dottedPath.d.ts +79 -79
  5. package/dist/lib-cjs/api/classes/dottedPath.js +190 -190
  6. package/dist/lib-cjs/api/classes/element.d.ts +130 -130
  7. package/dist/lib-cjs/api/classes/element.js +746 -743
  8. package/dist/lib-cjs/api/classes/element.js.map +1 -1
  9. package/dist/lib-cjs/api/classes/elementParameterizable.d.ts +14 -14
  10. package/dist/lib-cjs/api/classes/elementParameterizable.js +134 -134
  11. package/dist/lib-cjs/api/classes/event.d.ts +326 -326
  12. package/dist/lib-cjs/api/classes/event.js +371 -371
  13. package/dist/lib-cjs/api/classes/eventBroadcaster.d.ts +26 -26
  14. package/dist/lib-cjs/api/classes/eventBroadcaster.js +53 -53
  15. package/dist/lib-cjs/api/classes/parameter.d.ts +259 -259
  16. package/dist/lib-cjs/api/classes/parameter.js +387 -387
  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 +101 -101
  20. package/dist/lib-cjs/api/classes/parameterizable.d.ts +15 -15
  21. package/dist/lib-cjs/api/classes/parameterizable.js +149 -149
  22. package/dist/lib-cjs/api/classes/placementAnimation.d.ts +43 -38
  23. package/dist/lib-cjs/api/classes/placementAnimation.js +160 -149
  24. package/dist/lib-cjs/api/classes/placementAnimation.js.map +1 -1
  25. package/dist/lib-cjs/api/classes/variant.d.ts +234 -234
  26. package/dist/lib-cjs/api/classes/variant.js +1147 -1147
  27. package/dist/lib-cjs/api/classes/variantInstance.d.ts +45 -45
  28. package/dist/lib-cjs/api/classes/variantInstance.js +108 -108
  29. package/dist/lib-cjs/api/classes/variantParameterizable.d.ts +17 -17
  30. package/dist/lib-cjs/api/classes/variantParameterizable.js +99 -92
  31. package/dist/lib-cjs/api/classes/variantParameterizable.js.map +1 -1
  32. package/dist/lib-cjs/api/classes/viewer.d.ts +131 -131
  33. package/dist/lib-cjs/api/classes/viewer.js +567 -567
  34. package/dist/lib-cjs/api/classes/viewer.js.map +1 -1
  35. package/dist/lib-cjs/api/classes/viewerLight.d.ts +66 -66
  36. package/dist/lib-cjs/api/classes/viewerLight.js +392 -389
  37. package/dist/lib-cjs/api/classes/viewerLight.js.map +1 -1
  38. package/dist/lib-cjs/api/internal/debugViewer.d.ts +13 -13
  39. package/dist/lib-cjs/api/internal/debugViewer.js +87 -87
  40. package/dist/lib-cjs/api/internal/lensRendering.d.ts +8 -8
  41. package/dist/lib-cjs/api/internal/lensRendering.js +11 -11
  42. package/dist/lib-cjs/api/internal/sceneSetup.d.ts +10 -10
  43. package/dist/lib-cjs/api/internal/sceneSetup.js +231 -231
  44. package/dist/lib-cjs/api/manager/animationManager.d.ts +29 -29
  45. package/dist/lib-cjs/api/manager/animationManager.js +121 -121
  46. package/dist/lib-cjs/api/manager/gltfExportManager.d.ts +35 -0
  47. package/dist/lib-cjs/api/manager/gltfExportManager.js +108 -0
  48. package/dist/lib-cjs/api/manager/gltfExportManager.js.map +1 -0
  49. package/dist/lib-cjs/api/manager/sceneManager.d.ts +32 -32
  50. package/dist/lib-cjs/api/manager/sceneManager.js +132 -132
  51. package/dist/lib-cjs/api/manager/variantInstanceManager.d.ts +90 -90
  52. package/dist/lib-cjs/api/manager/variantInstanceManager.js +321 -321
  53. package/dist/lib-cjs/api/store/specStorage.d.ts +24 -24
  54. package/dist/lib-cjs/api/store/specStorage.js +51 -51
  55. package/dist/lib-cjs/api/util/babylonHelper.d.ts +166 -175
  56. package/dist/lib-cjs/api/util/babylonHelper.js +500 -520
  57. package/dist/lib-cjs/api/util/babylonHelper.js.map +1 -1
  58. package/dist/lib-cjs/api/util/globalTypes.d.ts +338 -333
  59. package/dist/lib-cjs/api/util/globalTypes.js +1 -1
  60. package/dist/lib-cjs/api/util/resourceHelper.d.ts +53 -53
  61. package/dist/lib-cjs/api/util/resourceHelper.js +240 -240
  62. package/dist/lib-cjs/api/util/stringHelper.d.ts +9 -9
  63. package/dist/lib-cjs/api/util/stringHelper.js +25 -25
  64. package/dist/lib-cjs/buildinfo.json +3 -3
  65. package/dist/lib-cjs/index.d.ts +48 -48
  66. package/dist/lib-cjs/index.js +86 -86
  67. package/package.json +83 -83
  68. package/src/api/classes/animationInterface.ts +11 -11
  69. package/src/api/classes/dottedPath.ts +189 -189
  70. package/src/api/classes/element.ts +648 -644
  71. package/src/api/classes/event.ts +370 -370
  72. package/src/api/classes/eventBroadcaster.ts +54 -54
  73. package/src/api/classes/parameter.ts +408 -408
  74. package/src/api/classes/parameterObservable.ts +99 -99
  75. package/src/api/classes/parameterizable.ts +89 -89
  76. package/src/api/classes/placementAnimation.ts +157 -143
  77. package/src/api/classes/variant.ts +817 -817
  78. package/src/api/classes/variantInstance.ts +88 -88
  79. package/src/api/classes/variantParameterizable.ts +80 -73
  80. package/src/api/classes/viewer.ts +487 -487
  81. package/src/api/classes/viewerLight.ts +334 -330
  82. package/src/api/internal/debugViewer.ts +81 -81
  83. package/src/api/internal/lensRendering.ts +10 -10
  84. package/src/api/internal/sceneSetup.ts +193 -193
  85. package/src/api/manager/animationManager.ts +116 -116
  86. package/src/api/manager/sceneManager.ts +105 -105
  87. package/src/api/manager/variantInstanceManager.ts +236 -236
  88. package/src/api/store/specStorage.ts +53 -53
  89. package/src/api/util/babylonHelper.ts +534 -555
  90. package/src/api/util/globalTypes.ts +385 -379
  91. package/src/api/util/resourceHelper.ts +184 -184
  92. package/src/api/util/stringHelper.ts +25 -25
  93. package/src/buildinfo.json +2 -2
  94. package/src/commonjs.tsconfig.json +13 -13
  95. package/src/declaration.tsconfig.json +10 -10
  96. package/src/dev.ts +42 -42
  97. package/src/es6.tsconfig.json +13 -13
  98. package/src/index.ts +91 -91
  99. package/src/pagesconfig.json +65 -61
  100. package/src/tsconfig.json +43 -43
  101. package/src/tsconfig.types.json +9 -9
  102. package/src/types.d.ts +4 -4
  103. package/dist/webpack-stats.json +0 -0
@@ -1,408 +1,408 @@
1
- import { Color3 } from '@babylonjs/core/Maths/math.color';
2
- import { Vector3 } from '@babylonjs/core/Maths/math.vector';
3
- import { get, isEmpty, isNumber, isString } from 'lodash-es';
4
-
5
-
6
- /**
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
- /**
14
- * Constructor.
15
- */
16
- private constructor() {}
17
-
18
- /**
19
- * Mutates the visibility. Helper methods are {@link VariantParameterizable.show} and
20
- * {@link VariantParameterizable.hide}. Getter and setter is {@link VariantParameterizable.visible}.
21
- *
22
- * Scopes:
23
- * * {@link Element}
24
- * * {@link Variant}
25
- * * {@link ViewerLight} Available for all light types
26
- *
27
- * @parser {@link parseBoolean}
28
- */
29
- public static readonly VISIBLE = 'visible';
30
-
31
- /**
32
- * Stretches or compresses some meshes. Getter and setter is {@link VariantParameterizable.scaling}.
33
- *
34
- * Scopes:
35
- * * {@link Element}
36
- * * {@link Variant}
37
- *
38
- * @parser {@link parseScaling}
39
- */
40
- public static readonly SCALING = 'scaling';
41
-
42
- /**
43
- * Mutates the material. Getter and setter is {@link VariantParameterizable.material}. The value of this parameter
44
- * represents the ID of a baked-in glTF or custom created material in the {@link Variant} tree.
45
- *
46
- * Scopes:
47
- * * {@link Element}
48
- * * {@link Variant}
49
- *
50
- * @parser string
51
- */
52
- public static readonly MATERIAL = 'material';
53
-
54
- /**
55
- * Mutates the material color.
56
- *
57
- * Scopes:
58
- * * {@link Element}
59
- * * {@link Variant}
60
- *
61
- * @parser {@link parseColor}
62
- */
63
- public static readonly MATERIAL_COLOR = 'material.color';
64
-
65
- /**
66
- * Mutates the material metallness.
67
- *
68
- * Scopes:
69
- * * {@link Element}
70
- * * {@link Variant}
71
- *
72
- * @parser {@link parseNumber}
73
- */
74
- public static readonly MATERIAL_METALLNESS = 'material.metallness';
75
-
76
- /**
77
- * Mutates the material roughness.
78
- *
79
- * Scopes:
80
- * * {@link Element}
81
- * * {@link Variant}
82
- *
83
- * @parser {@link parseNumber}
84
- */
85
- public static readonly MATERIAL_ROUGHNESS = 'material.roughness';
86
-
87
- /**
88
- * Mutates whether a highlight layer should be enabled.
89
- *
90
- * Scopes:
91
- * * {@link Element}
92
- * * {@link Variant}
93
- *
94
- * @parser {@link parseBoolean}
95
- */
96
- public static readonly HIGHLIGHT_ENABLED = 'highlight.enabled';
97
-
98
- /**
99
- * Changes the highlight color.
100
- *
101
- * Scopes:
102
- * * {@link Element}
103
- * * {@link Variant}
104
- *
105
- * @parser {@link parseColor}
106
- */
107
- public static readonly HIGHLIGHT_COLOR = 'highlight.color';
108
-
109
- /**
110
- * Mutates the highlighted state. Getter and setter is {@link VariantParameterizable.highlighted}.
111
- *
112
- * Scopes:
113
- * * {@link Element}
114
- * * {@link Variant}
115
- *
116
- * @parser {@link parseBoolean}
117
- */
118
- public static readonly HIGHLIGHTED = 'highlighted';
119
-
120
- /**
121
- * Mutates the position relative to its origin position. Getter and setter is {@link VariantParameterizable.position}.
122
- *
123
- * Scopes:
124
- * * {@link Element}
125
- * * {@link Variant}
126
- * * {@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)
127
- *
128
- * @parser {@link parseVector}
129
- */
130
- public static readonly POSITION = 'position';
131
-
132
- /**
133
- * Mutates the rotation relative to its origin rotation. Getter and setter is {@link VariantParameterizable.rotation}.
134
- *
135
- * Scopes:
136
- * * {@link Element}
137
- * * {@link Variant}
138
- *
139
- * @parser {@link parseRotation}
140
- */
141
- public static readonly ROTATION = 'rotation';
142
-
143
- /**
144
- * Mutates whether a shadow should be casted or not.
145
- *
146
- * Scopes:
147
- * * {@link Element}
148
- * * {@link Variant}
149
- *
150
- * @parser {@link parseBoolean}
151
- */
152
- public static readonly CAST_SHADOW = 'castShadow';
153
-
154
- /**
155
- * Mutates from which lights a shadow should be casted.
156
- *
157
- * Scopes:
158
- * * {@link Element}
159
- * * {@link Variant}
160
- *
161
- * @parser {@link parseCommaSeparatedList}
162
- */
163
- public static readonly CAST_SHADOW_FROM_LIGHTS = 'castShadow.fromLights';
164
-
165
- /**
166
- * Mutates whether to receive shadows or not.
167
- *
168
- * Scopes:
169
- * * {@link Element}
170
- * * {@link Variant}
171
- *
172
- * @parser {@link parseBoolean}
173
- */
174
- public static readonly RECEIVE_SHADOWS = 'receiveShadows';
175
-
176
- /**
177
- * Mutates the intensity of Lights.
178
- *
179
- * Scopes:
180
- * * {@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)
181
- *
182
- * @parser {@link parseNumber}
183
- */
184
- public static readonly INTENSITY = 'intensity';
185
-
186
- /**
187
- * Mutates the direction of Lights.
188
- *
189
- * Scopes:
190
- * * {@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)
191
- *
192
- * @parser {@link parseNumber}
193
- */
194
- public static readonly DIRECTION = 'direction';
195
-
196
- /**
197
- * Mutates the angle of Lights.
198
- *
199
- * Scopes:
200
- * * {@link ViewerLight} Available for [SpotLight](https://doc.babylonjs.com/typedoc/classes/babylon.spotlight#angle)
201
- *
202
- * @parser {@link parseNumber}
203
- */
204
- public static readonly ANGLE = 'angle';
205
-
206
- /**
207
- * Mutates the exponent of Lights.
208
- *
209
- * Scopes:
210
- * * {@link ViewerLight} Available for [SpotLight](https://doc.babylonjs.com/typedoc/classes/babylon.spotlight#exponent)
211
- *
212
- * @parser {@link parseNumber}
213
- */
214
- public static readonly EXPONENT = 'exponent';
215
-
216
- /**
217
- * Mutates the diffuse color of Lights.
218
- *
219
- * Scopes:
220
- * * {@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)
221
- *
222
- * @parser {@link parseNumber}
223
- */
224
- public static readonly DIFFUSE = 'diffuse';
225
-
226
- /**
227
- * Mutates the specular color of Lights.
228
- *
229
- * Scopes:
230
- * * {@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)
231
- *
232
- * @parser {@link parseNumber}
233
- */
234
- public static readonly SPECULAR = 'specular';
235
-
236
- /**
237
- * The {@link ParameterDeclarations} for all parameters in this class.
238
- */
239
- public static get declarations(): ParameterDeclarations {
240
- const declarations: ParameterDeclarations = {};
241
- declarations[Parameter.VISIBLE] = { type: 'boolean', parser: Parameter.parseBoolean };
242
- declarations[Parameter.SCALING] = { type: 'number', parser: Parameter.parseNumber };
243
- declarations[Parameter.HIGHLIGHTED] = { type: 'boolean', parser: Parameter.parseBoolean };
244
- declarations[Parameter.MATERIAL] = { type: 'string' };
245
- declarations[Parameter.MATERIAL_COLOR] = { type: 'color', parser: Parameter.parseColor };
246
- declarations[Parameter.MATERIAL_METALLNESS] = { type: 'number', parser: Parameter.parseNumber };
247
- declarations[Parameter.MATERIAL_ROUGHNESS] = { type: 'number', parser: Parameter.parseNumber };
248
- declarations[Parameter.HIGHLIGHT_ENABLED] = { type: 'boolean', parser: Parameter.parseBoolean };
249
- declarations[Parameter.HIGHLIGHT_COLOR] = { type: 'color', parser: Parameter.parseColor };
250
- declarations[Parameter.HIGHLIGHTED] = { type: 'boolean', parser: Parameter.parseBoolean };
251
- declarations[Parameter.POSITION] = { type: 'vector', parser: Parameter.parseVector };
252
- declarations[Parameter.ROTATION] = { type: 'vector', parser: Parameter.parseVector };
253
- declarations[Parameter.CAST_SHADOW] = { type: 'boolean', parser: Parameter.parseBoolean };
254
- declarations[Parameter.CAST_SHADOW_FROM_LIGHTS] = { type: 'csl', parser: Parameter.parseCommaSeparatedList };
255
- declarations[Parameter.RECEIVE_SHADOWS] = { type: 'boolean', parser: Parameter.parseBoolean };
256
- declarations[Parameter.INTENSITY] = { type: 'number', parser: Parameter.parseNumber };
257
- declarations[Parameter.DIRECTION] = { type: 'vector', parser: Parameter.parseVector };
258
- declarations[Parameter.ANGLE] = { type: 'number', parser: Parameter.parseNumber };
259
- declarations[Parameter.EXPONENT] = { type: 'number', parser: Parameter.parseNumber };
260
- declarations[Parameter.DIFFUSE] = { type: 'color', parser: Parameter.parseColor };
261
- declarations[Parameter.SPECULAR] = { type: 'color', parser: Parameter.parseColor };
262
- return declarations;
263
- }
264
-
265
- /**
266
- * Gets the {@link ParameterDeclaration} for a given {@link Parameter} declared in this class.
267
- */
268
- public static getDeclaration(parameter: string): ParameterDeclaration {
269
- return Parameter.declarations[parameter];
270
- }
271
-
272
- /**
273
- * All parameters defined in this class.
274
- */
275
- public static get all() {
276
- const all: string[] = [];
277
- for( const parameter in Parameter ) {
278
- const property = Object.getOwnPropertyDescriptor(Parameter, parameter);
279
- if( parameter.toUpperCase() === parameter && property && isString( property.value ) ) {
280
- all.push( property.value );
281
- }
282
- }
283
- return all;
284
- }
285
-
286
- /**
287
- * Parses given {@link ParameterBag} with given {@link ParameterDeclarations}.
288
- */
289
- public static parseFromDeclarations( declarations: ParameterDeclarations, parameterBag: ParameterBag ): ParsedParameterBag {
290
- const parameters: ParsedParameterBag = {};
291
- for( const parameter in parameterBag ) {
292
- const value = get( parameterBag, parameter );
293
- parameters[parameter] = value;
294
- const declaration: ParameterDeclaration = declarations[parameter];
295
- if( declaration ) {
296
- if( !isEmpty( value ) && declaration.parser ) {
297
- parameters[parameter] = declaration.parser( value );
298
- }
299
- }
300
- }
301
- return parameters;
302
- }
303
-
304
- /**
305
- * Parses a string of format `'(x,y,z)'` to a `Vector3`.
306
- */
307
- public static parseVector( value: ParameterValue ): Vector3 {
308
- if( !isString( value ) ) {
309
- throw new Error( `Unable to convert "${value}" into a Vector: not a string.` );
310
- }
311
- let cleanedValue = value.split( ' ' ).join( '' );
312
- if( cleanedValue.startsWith( '(' ) && cleanedValue.endsWith( ')' ) ) {
313
- cleanedValue = cleanedValue.substring( 1, cleanedValue.length - 1 );
314
- let [x, y, z] = cleanedValue.split( ',' ).map( value => parseFloat( value ) );
315
- return new Vector3( x, y, z );
316
- } else {
317
- throw new Error( `Unable to parse "${value}" to a Vector: expected "(x,y,z)".` );
318
- }
319
- }
320
-
321
- /**
322
- * Stringifies a Vector3 to format `(x,y,z)`.
323
- */
324
- public static stringifyVector( value: Vector3 ): string {
325
- return `(${value.x},${value.y},${value.z})`;
326
- }
327
-
328
- /**
329
- * Parses a string of format `'(x,y,z)'` with angular degrees to a `Vector3` with rotation information in radians.
330
- */
331
- public static parseRotation( value: ParameterValue ): Vector3 {
332
- const rotation = Parameter.parseVector( value );
333
- const deg2rad = function( deg: number ) {
334
- return deg * Math.PI / 180;
335
- };
336
- return rotation.set(
337
- deg2rad( rotation.x ),
338
- deg2rad( rotation.y ),
339
- deg2rad( rotation.z )
340
- );
341
- }
342
-
343
- /**
344
- * Parses a string of format `'(x,y,z)'` or a simple `number` to a `Vector3` for scaling.
345
- */
346
- public static parseScaling( value: ParameterValue ): Vector3 {
347
- let scaling = new Vector3();
348
- if( isString( value ) ) {
349
- const cleanedFactor = value.split( ' ' ).join( '' );
350
- if( cleanedFactor.startsWith( '(' ) && cleanedFactor.endsWith( ')' ) ) {
351
- scaling = Parameter.parseVector( cleanedFactor );
352
- } else {
353
- const _factor = parseFloat( value );
354
- scaling = new Vector3( _factor, _factor, _factor );
355
- }
356
- } else if( isNumber( value ) ) {
357
- scaling = new Vector3( value, value, value );
358
- } else {
359
- throw new Error( `Unable to convert "${value}" to a Vector: expected "(x,y,z)".` );
360
- }
361
- return scaling;
362
- }
363
-
364
- /**
365
- * Parses a string of format `'#rrggbb'` or `'(r,g,b)'` to a `Color3`.
366
- */
367
- public static parseColor( value: ParameterValue ): Color3 {
368
- let cleanedValue = value.toString().split( ' ' ).join( '' );
369
- if( cleanedValue.startsWith( '#' ) ) {
370
- return Color3.FromHexString( value.toString() );
371
- }
372
- if( cleanedValue.startsWith( '(' ) && cleanedValue.endsWith( ')' ) ) {
373
- cleanedValue = cleanedValue.substring( 1, cleanedValue.length - 1 );
374
- let [r, g, b] = cleanedValue.split( ',' ).map( value => parseFloat( value ) );
375
- return Color3.FromInts( r, g, b );
376
- }
377
- throw new Error( `Unable to parse "${value}" to a Color: expected "#rrggbb" or "(r,g,b)".` );
378
- }
379
-
380
- /**
381
- * Parses a `boolean` string (`'true'`/`'1'` or `'false'`/`'0'`) or `number` (`1` or `0`) to a `boolean` or `undefined`.
382
- */
383
- public static parseBoolean( value: ParameterValue ): boolean {
384
- if( value.toString() === 'true' || value.toString() === '1' ) {
385
- return true;
386
- } else if( value.toString() === 'false' || value.toString() === '0' ) {
387
- return false;
388
- }
389
- throw new Error( `Unable to parse "${value}" to a boolean.` );
390
- }
391
-
392
- /**
393
- * Parses a number `string` to a `number`.
394
- */
395
- public static parseNumber( value: ParameterValue ): number {
396
- return parseFloat( value.toString() );
397
- }
398
-
399
- /**
400
- * Parses a string with comma separated values to a list of strings.
401
- */
402
- public static parseCommaSeparatedList( value: ParameterValue ): string[] {
403
- return value.toString()
404
- .split( ',' )
405
- .map( s => s.trim() );
406
- }
407
-
408
- }
1
+ import { Color3 } from '@babylonjs/core/Maths/math.color';
2
+ import { Vector3 } from '@babylonjs/core/Maths/math.vector';
3
+ import { get, isEmpty, isNumber, isString } from 'lodash-es';
4
+
5
+
6
+ /**
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
+ /**
14
+ * Constructor.
15
+ */
16
+ private constructor() {}
17
+
18
+ /**
19
+ * Mutates the visibility. Helper methods are {@link VariantParameterizable.show} and
20
+ * {@link VariantParameterizable.hide}. Getter and setter is {@link VariantParameterizable.visible}.
21
+ *
22
+ * Scopes:
23
+ * * {@link Element}
24
+ * * {@link Variant}
25
+ * * {@link ViewerLight} Available for all light types
26
+ *
27
+ * @parser {@link parseBoolean}
28
+ */
29
+ public static readonly VISIBLE = 'visible';
30
+
31
+ /**
32
+ * Stretches or compresses some meshes. Getter and setter is {@link VariantParameterizable.scaling}.
33
+ *
34
+ * Scopes:
35
+ * * {@link Element}
36
+ * * {@link Variant}
37
+ *
38
+ * @parser {@link parseScaling}
39
+ */
40
+ public static readonly SCALING = 'scaling';
41
+
42
+ /**
43
+ * Mutates the material. Getter and setter is {@link VariantParameterizable.material}. The value of this parameter
44
+ * represents the ID of a baked-in glTF or custom created material in the {@link Variant} tree.
45
+ *
46
+ * Scopes:
47
+ * * {@link Element}
48
+ * * {@link Variant}
49
+ *
50
+ * @parser string
51
+ */
52
+ public static readonly MATERIAL = 'material';
53
+
54
+ /**
55
+ * Mutates the material color.
56
+ *
57
+ * Scopes:
58
+ * * {@link Element}
59
+ * * {@link Variant}
60
+ *
61
+ * @parser {@link parseColor}
62
+ */
63
+ public static readonly MATERIAL_COLOR = 'material.color';
64
+
65
+ /**
66
+ * Mutates the material metallness.
67
+ *
68
+ * Scopes:
69
+ * * {@link Element}
70
+ * * {@link Variant}
71
+ *
72
+ * @parser {@link parseNumber}
73
+ */
74
+ public static readonly MATERIAL_METALLNESS = 'material.metallness';
75
+
76
+ /**
77
+ * Mutates the material roughness.
78
+ *
79
+ * Scopes:
80
+ * * {@link Element}
81
+ * * {@link Variant}
82
+ *
83
+ * @parser {@link parseNumber}
84
+ */
85
+ public static readonly MATERIAL_ROUGHNESS = 'material.roughness';
86
+
87
+ /**
88
+ * Mutates whether a highlight layer should be enabled.
89
+ *
90
+ * Scopes:
91
+ * * {@link Element}
92
+ * * {@link Variant}
93
+ *
94
+ * @parser {@link parseBoolean}
95
+ */
96
+ public static readonly HIGHLIGHT_ENABLED = 'highlight.enabled';
97
+
98
+ /**
99
+ * Changes the highlight color.
100
+ *
101
+ * Scopes:
102
+ * * {@link Element}
103
+ * * {@link Variant}
104
+ *
105
+ * @parser {@link parseColor}
106
+ */
107
+ public static readonly HIGHLIGHT_COLOR = 'highlight.color';
108
+
109
+ /**
110
+ * Mutates the highlighted state. Getter and setter is {@link VariantParameterizable.highlighted}.
111
+ *
112
+ * Scopes:
113
+ * * {@link Element}
114
+ * * {@link Variant}
115
+ *
116
+ * @parser {@link parseBoolean}
117
+ */
118
+ public static readonly HIGHLIGHTED = 'highlighted';
119
+
120
+ /**
121
+ * Mutates the position relative to its origin position. Getter and setter is {@link VariantParameterizable.position}.
122
+ *
123
+ * Scopes:
124
+ * * {@link Element}
125
+ * * {@link Variant}
126
+ * * {@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)
127
+ *
128
+ * @parser {@link parseVector}
129
+ */
130
+ public static readonly POSITION = 'position';
131
+
132
+ /**
133
+ * Mutates the rotation relative to its origin rotation. Getter and setter is {@link VariantParameterizable.rotation}.
134
+ *
135
+ * Scopes:
136
+ * * {@link Element}
137
+ * * {@link Variant}
138
+ *
139
+ * @parser {@link parseRotation}
140
+ */
141
+ public static readonly ROTATION = 'rotation';
142
+
143
+ /**
144
+ * Mutates whether a shadow should be casted or not.
145
+ *
146
+ * Scopes:
147
+ * * {@link Element}
148
+ * * {@link Variant}
149
+ *
150
+ * @parser {@link parseBoolean}
151
+ */
152
+ public static readonly CAST_SHADOW = 'castShadow';
153
+
154
+ /**
155
+ * Mutates from which lights a shadow should be casted.
156
+ *
157
+ * Scopes:
158
+ * * {@link Element}
159
+ * * {@link Variant}
160
+ *
161
+ * @parser {@link parseCommaSeparatedList}
162
+ */
163
+ public static readonly CAST_SHADOW_FROM_LIGHTS = 'castShadow.fromLights';
164
+
165
+ /**
166
+ * Mutates whether to receive shadows or not.
167
+ *
168
+ * Scopes:
169
+ * * {@link Element}
170
+ * * {@link Variant}
171
+ *
172
+ * @parser {@link parseBoolean}
173
+ */
174
+ public static readonly RECEIVE_SHADOWS = 'receiveShadows';
175
+
176
+ /**
177
+ * Mutates the intensity of Lights.
178
+ *
179
+ * Scopes:
180
+ * * {@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)
181
+ *
182
+ * @parser {@link parseNumber}
183
+ */
184
+ public static readonly INTENSITY = 'intensity';
185
+
186
+ /**
187
+ * Mutates the direction of Lights.
188
+ *
189
+ * Scopes:
190
+ * * {@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)
191
+ *
192
+ * @parser {@link parseNumber}
193
+ */
194
+ public static readonly DIRECTION = 'direction';
195
+
196
+ /**
197
+ * Mutates the angle of Lights.
198
+ *
199
+ * Scopes:
200
+ * * {@link ViewerLight} Available for [SpotLight](https://doc.babylonjs.com/typedoc/classes/babylon.spotlight#angle)
201
+ *
202
+ * @parser {@link parseNumber}
203
+ */
204
+ public static readonly ANGLE = 'angle';
205
+
206
+ /**
207
+ * Mutates the exponent of Lights.
208
+ *
209
+ * Scopes:
210
+ * * {@link ViewerLight} Available for [SpotLight](https://doc.babylonjs.com/typedoc/classes/babylon.spotlight#exponent)
211
+ *
212
+ * @parser {@link parseNumber}
213
+ */
214
+ public static readonly EXPONENT = 'exponent';
215
+
216
+ /**
217
+ * Mutates the diffuse color of Lights.
218
+ *
219
+ * Scopes:
220
+ * * {@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)
221
+ *
222
+ * @parser {@link parseNumber}
223
+ */
224
+ public static readonly DIFFUSE = 'diffuse';
225
+
226
+ /**
227
+ * Mutates the specular color of Lights.
228
+ *
229
+ * Scopes:
230
+ * * {@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)
231
+ *
232
+ * @parser {@link parseNumber}
233
+ */
234
+ public static readonly SPECULAR = 'specular';
235
+
236
+ /**
237
+ * The {@link ParameterDeclarations} for all parameters in this class.
238
+ */
239
+ public static get declarations(): ParameterDeclarations {
240
+ const declarations: ParameterDeclarations = {};
241
+ declarations[Parameter.VISIBLE] = { type: 'boolean', parser: Parameter.parseBoolean };
242
+ declarations[Parameter.SCALING] = { type: 'number', parser: Parameter.parseNumber };
243
+ declarations[Parameter.POSITION] = { type: 'vector', parser: Parameter.parseVector };
244
+ declarations[Parameter.ROTATION] = { type: 'vector', parser: Parameter.parseVector };
245
+ declarations[Parameter.HIGHLIGHTED] = { type: 'boolean', parser: Parameter.parseBoolean };
246
+ declarations[Parameter.MATERIAL] = { type: 'string' };
247
+ declarations[Parameter.MATERIAL_COLOR] = { type: 'color', parser: Parameter.parseColor };
248
+ declarations[Parameter.MATERIAL_METALLNESS] = { type: 'number', parser: Parameter.parseNumber };
249
+ declarations[Parameter.MATERIAL_ROUGHNESS] = { type: 'number', parser: Parameter.parseNumber };
250
+ declarations[Parameter.HIGHLIGHT_ENABLED] = { type: 'boolean', parser: Parameter.parseBoolean };
251
+ declarations[Parameter.HIGHLIGHT_COLOR] = { type: 'color', parser: Parameter.parseColor };
252
+ declarations[Parameter.HIGHLIGHTED] = { type: 'boolean', parser: Parameter.parseBoolean };
253
+ declarations[Parameter.CAST_SHADOW] = { type: 'boolean', parser: Parameter.parseBoolean };
254
+ declarations[Parameter.CAST_SHADOW_FROM_LIGHTS] = { type: 'csl', parser: Parameter.parseCommaSeparatedList };
255
+ declarations[Parameter.RECEIVE_SHADOWS] = { type: 'boolean', parser: Parameter.parseBoolean };
256
+ declarations[Parameter.INTENSITY] = { type: 'number', parser: Parameter.parseNumber };
257
+ declarations[Parameter.DIRECTION] = { type: 'vector', parser: Parameter.parseVector };
258
+ declarations[Parameter.ANGLE] = { type: 'number', parser: Parameter.parseNumber };
259
+ declarations[Parameter.EXPONENT] = { type: 'number', parser: Parameter.parseNumber };
260
+ declarations[Parameter.DIFFUSE] = { type: 'color', parser: Parameter.parseColor };
261
+ declarations[Parameter.SPECULAR] = { type: 'color', parser: Parameter.parseColor };
262
+ return declarations;
263
+ }
264
+
265
+ /**
266
+ * Gets the {@link ParameterDeclaration} for a given {@link Parameter} declared in this class.
267
+ */
268
+ public static getDeclaration(parameter: string): ParameterDeclaration {
269
+ return Parameter.declarations[parameter];
270
+ }
271
+
272
+ /**
273
+ * All parameters defined in this class.
274
+ */
275
+ public static get all() {
276
+ const all: string[] = [];
277
+ for( const parameter in Parameter ) {
278
+ const property = Object.getOwnPropertyDescriptor(Parameter, parameter);
279
+ if( parameter.toUpperCase() === parameter && property && isString( property.value ) ) {
280
+ all.push( property.value );
281
+ }
282
+ }
283
+ return all;
284
+ }
285
+
286
+ /**
287
+ * Parses given {@link ParameterBag} with given {@link ParameterDeclarations}.
288
+ */
289
+ public static parseFromDeclarations( declarations: ParameterDeclarations, parameterBag: ParameterBag ): ParsedParameterBag {
290
+ const parameters: ParsedParameterBag = {};
291
+ for( const parameter in parameterBag ) {
292
+ const value = get( parameterBag, parameter );
293
+ parameters[parameter] = value;
294
+ const declaration: ParameterDeclaration = declarations[parameter];
295
+ if( declaration ) {
296
+ if( !isEmpty( value ) && declaration.parser ) {
297
+ parameters[parameter] = declaration.parser( value );
298
+ }
299
+ }
300
+ }
301
+ return parameters;
302
+ }
303
+
304
+ /**
305
+ * Parses a string of format `'(x,y,z)'` to a `Vector3`.
306
+ */
307
+ public static parseVector( value: ParameterValue ): Vector3 {
308
+ if( !isString( value ) ) {
309
+ throw new Error( `Unable to convert "${value}" into a Vector: not a string.` );
310
+ }
311
+ let cleanedValue = value.split( ' ' ).join( '' );
312
+ if( cleanedValue.startsWith( '(' ) && cleanedValue.endsWith( ')' ) ) {
313
+ cleanedValue = cleanedValue.substring( 1, cleanedValue.length - 1 );
314
+ let [x, y, z] = cleanedValue.split( ',' ).map( value => parseFloat( value ) );
315
+ return new Vector3( x, y, z );
316
+ } else {
317
+ throw new Error( `Unable to parse "${value}" to a Vector: expected "(x,y,z)".` );
318
+ }
319
+ }
320
+
321
+ /**
322
+ * Stringifies a Vector3 to format `(x,y,z)`.
323
+ */
324
+ public static stringifyVector( value: Vector3 ): string {
325
+ return `(${value.x},${value.y},${value.z})`;
326
+ }
327
+
328
+ /**
329
+ * Parses a string of format `'(x,y,z)'` with angular degrees to a `Vector3` with rotation information in radians.
330
+ */
331
+ public static parseRotation( value: ParameterValue ): Vector3 {
332
+ const rotation = Parameter.parseVector( value );
333
+ const deg2rad = function( deg: number ) {
334
+ return deg * Math.PI / 180;
335
+ };
336
+ return rotation.set(
337
+ deg2rad( rotation.x ),
338
+ deg2rad( rotation.y ),
339
+ deg2rad( rotation.z )
340
+ );
341
+ }
342
+
343
+ /**
344
+ * Parses a string of format `'(x,y,z)'` or a simple `number` to a `Vector3` for scaling.
345
+ */
346
+ public static parseScaling( value: ParameterValue ): Vector3 {
347
+ let scaling = new Vector3();
348
+ if( isString( value ) ) {
349
+ const cleanedFactor = value.split( ' ' ).join( '' );
350
+ if( cleanedFactor.startsWith( '(' ) && cleanedFactor.endsWith( ')' ) ) {
351
+ scaling = Parameter.parseVector( cleanedFactor );
352
+ } else {
353
+ const _factor = parseFloat( value );
354
+ scaling = new Vector3( _factor, _factor, _factor );
355
+ }
356
+ } else if( isNumber( value ) ) {
357
+ scaling = new Vector3( value, value, value );
358
+ } else {
359
+ throw new Error( `Unable to convert "${value}" to a Vector: expected "(x,y,z)".` );
360
+ }
361
+ return scaling;
362
+ }
363
+
364
+ /**
365
+ * Parses a string of format `'#rrggbb'` or `'(r,g,b)'` to a `Color3`.
366
+ */
367
+ public static parseColor( value: ParameterValue ): Color3 {
368
+ let cleanedValue = value.toString().split( ' ' ).join( '' );
369
+ if( cleanedValue.startsWith( '#' ) ) {
370
+ return Color3.FromHexString( value.toString() );
371
+ }
372
+ if( cleanedValue.startsWith( '(' ) && cleanedValue.endsWith( ')' ) ) {
373
+ cleanedValue = cleanedValue.substring( 1, cleanedValue.length - 1 );
374
+ let [r, g, b] = cleanedValue.split( ',' ).map( value => parseFloat( value ) );
375
+ return Color3.FromInts( r, g, b );
376
+ }
377
+ throw new Error( `Unable to parse "${value}" to a Color: expected "#rrggbb" or "(r,g,b)".` );
378
+ }
379
+
380
+ /**
381
+ * Parses a `boolean` string (`'true'`/`'1'` or `'false'`/`'0'`) or `number` (`1` or `0`) to a `boolean` or `undefined`.
382
+ */
383
+ public static parseBoolean( value: ParameterValue ): boolean {
384
+ if( value.toString() === 'true' || value.toString() === '1' ) {
385
+ return true;
386
+ } else if( value.toString() === 'false' || value.toString() === '0' ) {
387
+ return false;
388
+ }
389
+ throw new Error( `Unable to parse "${value}" to a boolean.` );
390
+ }
391
+
392
+ /**
393
+ * Parses a number `string` to a `number`.
394
+ */
395
+ public static parseNumber( value: ParameterValue ): number {
396
+ return parseFloat( value.toString() );
397
+ }
398
+
399
+ /**
400
+ * Parses a string with comma separated values to a list of strings.
401
+ */
402
+ public static parseCommaSeparatedList( value: ParameterValue ): string[] {
403
+ return value.toString()
404
+ .split( ',' )
405
+ .map( s => s.trim() );
406
+ }
407
+
408
+ }