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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (93) 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 +743 -743
  8. package/dist/lib-cjs/api/classes/elementParameterizable.d.ts +14 -14
  9. package/dist/lib-cjs/api/classes/elementParameterizable.js +134 -134
  10. package/dist/lib-cjs/api/classes/event.d.ts +326 -326
  11. package/dist/lib-cjs/api/classes/event.js +371 -371
  12. package/dist/lib-cjs/api/classes/eventBroadcaster.d.ts +26 -26
  13. package/dist/lib-cjs/api/classes/eventBroadcaster.js +53 -53
  14. package/dist/lib-cjs/api/classes/parameter.d.ts +259 -259
  15. package/dist/lib-cjs/api/classes/parameter.js +387 -387
  16. package/dist/lib-cjs/api/classes/parameterObservable.d.ts +36 -36
  17. package/dist/lib-cjs/api/classes/parameterObservable.js +101 -101
  18. package/dist/lib-cjs/api/classes/parameterizable.d.ts +15 -15
  19. package/dist/lib-cjs/api/classes/parameterizable.js +149 -149
  20. package/dist/lib-cjs/api/classes/placementAnimation.d.ts +38 -38
  21. package/dist/lib-cjs/api/classes/placementAnimation.js +149 -149
  22. package/dist/lib-cjs/api/classes/variant.d.ts +234 -234
  23. package/dist/lib-cjs/api/classes/variant.js +1147 -1147
  24. package/dist/lib-cjs/api/classes/variantInstance.d.ts +45 -45
  25. package/dist/lib-cjs/api/classes/variantInstance.js +108 -108
  26. package/dist/lib-cjs/api/classes/variantParameterizable.d.ts +17 -17
  27. package/dist/lib-cjs/api/classes/variantParameterizable.js +92 -92
  28. package/dist/lib-cjs/api/classes/viewer.d.ts +131 -131
  29. package/dist/lib-cjs/api/classes/viewer.js +567 -567
  30. package/dist/lib-cjs/api/classes/viewerLight.d.ts +66 -66
  31. package/dist/lib-cjs/api/classes/viewerLight.js +389 -389
  32. package/dist/lib-cjs/api/internal/debugViewer.d.ts +13 -13
  33. package/dist/lib-cjs/api/internal/debugViewer.js +87 -87
  34. package/dist/lib-cjs/api/internal/lensRendering.d.ts +8 -8
  35. package/dist/lib-cjs/api/internal/lensRendering.js +11 -11
  36. package/dist/lib-cjs/api/internal/sceneSetup.d.ts +10 -10
  37. package/dist/lib-cjs/api/internal/sceneSetup.js +231 -231
  38. package/dist/lib-cjs/api/manager/animationManager.d.ts +29 -29
  39. package/dist/lib-cjs/api/manager/animationManager.js +121 -121
  40. package/dist/lib-cjs/api/manager/sceneManager.d.ts +32 -32
  41. package/dist/lib-cjs/api/manager/sceneManager.js +132 -132
  42. package/dist/lib-cjs/api/manager/variantInstanceManager.d.ts +90 -90
  43. package/dist/lib-cjs/api/manager/variantInstanceManager.js +321 -321
  44. package/dist/lib-cjs/api/store/specStorage.d.ts +24 -24
  45. package/dist/lib-cjs/api/store/specStorage.js +51 -51
  46. package/dist/lib-cjs/api/util/babylonHelper.d.ts +175 -175
  47. package/dist/lib-cjs/api/util/babylonHelper.js +520 -520
  48. package/dist/lib-cjs/api/util/globalTypes.d.ts +333 -333
  49. package/dist/lib-cjs/api/util/globalTypes.js +1 -1
  50. package/dist/lib-cjs/api/util/resourceHelper.d.ts +53 -53
  51. package/dist/lib-cjs/api/util/resourceHelper.js +240 -240
  52. package/dist/lib-cjs/api/util/stringHelper.d.ts +9 -9
  53. package/dist/lib-cjs/api/util/stringHelper.js +25 -25
  54. package/dist/lib-cjs/buildinfo.json +3 -3
  55. package/dist/lib-cjs/index.d.ts +48 -48
  56. package/dist/lib-cjs/index.js +86 -86
  57. package/package.json +83 -83
  58. package/src/api/classes/animationInterface.ts +11 -11
  59. package/src/api/classes/dottedPath.ts +189 -189
  60. package/src/api/classes/element.ts +644 -644
  61. package/src/api/classes/event.ts +370 -370
  62. package/src/api/classes/eventBroadcaster.ts +54 -54
  63. package/src/api/classes/parameter.ts +408 -408
  64. package/src/api/classes/parameterObservable.ts +99 -99
  65. package/src/api/classes/parameterizable.ts +89 -89
  66. package/src/api/classes/placementAnimation.ts +143 -143
  67. package/src/api/classes/variant.ts +817 -817
  68. package/src/api/classes/variantInstance.ts +88 -88
  69. package/src/api/classes/variantParameterizable.ts +73 -73
  70. package/src/api/classes/viewer.ts +487 -487
  71. package/src/api/classes/viewerLight.ts +330 -330
  72. package/src/api/internal/debugViewer.ts +81 -81
  73. package/src/api/internal/lensRendering.ts +10 -10
  74. package/src/api/internal/sceneSetup.ts +193 -193
  75. package/src/api/manager/animationManager.ts +116 -116
  76. package/src/api/manager/sceneManager.ts +105 -105
  77. package/src/api/manager/variantInstanceManager.ts +236 -236
  78. package/src/api/store/specStorage.ts +53 -53
  79. package/src/api/util/babylonHelper.ts +555 -555
  80. package/src/api/util/globalTypes.ts +379 -379
  81. package/src/api/util/resourceHelper.ts +184 -184
  82. package/src/api/util/stringHelper.ts +25 -25
  83. package/src/buildinfo.json +2 -2
  84. package/src/commonjs.tsconfig.json +13 -13
  85. package/src/declaration.tsconfig.json +10 -10
  86. package/src/dev.ts +42 -42
  87. package/src/es6.tsconfig.json +13 -13
  88. package/src/index.ts +91 -91
  89. package/src/pagesconfig.json +61 -61
  90. package/src/tsconfig.json +43 -43
  91. package/src/tsconfig.types.json +9 -9
  92. package/src/types.d.ts +4 -4
  93. package/dist/webpack-stats.json +0 -0
@@ -1,330 +1,330 @@
1
- import { PointLight } from '@babylonjs/core';
2
- import { HemisphericLight } from '@babylonjs/core/Lights/hemisphericLight';
3
- import { Light } from '@babylonjs/core/Lights/light';
4
- import { ShadowLight } from '@babylonjs/core/Lights/shadowLight';
5
- import { ShadowGenerator } from '@babylonjs/core/Lights/Shadows/shadowGenerator';
6
- import '@babylonjs/core/Lights/Shadows/shadowGeneratorSceneComponent';
7
- import { Vector3 } from '@babylonjs/core/Maths/math.vector';
8
- import { TransformNode } from '@babylonjs/core/Meshes/transformNode';
9
- import '@babylonjs/loaders/glTF/2.0/Extensions/KHR_lights_punctual';
10
- import { get, isEmpty, isString, set } from 'lodash-es';
11
- import {
12
- cloneNodeWithParents,
13
- disableNodeWithParents,
14
- enableNodeWithParents,
15
- getRootNode,
16
- injectNodeMetadata,
17
- moveTransformNode,
18
- rotateTransformNode
19
- } from './../util/babylonHelper';
20
- import { DottedPath } from './dottedPath';
21
- import { Parameter } from './parameter';
22
- import { Variant } from './variant';
23
- import { VariantParameterizable } from './variantParameterizable';
24
-
25
- /**
26
- * A {@link ViewerLight} of a {@link Variant}. Acts as a container for BabylonJS lights. Lives only in the context of a
27
- * {@link Variant}.
28
- */
29
- export class ViewerLight extends VariantParameterizable {
30
-
31
- protected _light: Light | undefined;
32
-
33
- /**
34
- * Constructor.
35
- */
36
- protected constructor( public readonly variant: Variant,
37
- public readonly name: string ) {
38
- super( variant, name );
39
- this.addParameterObservers();
40
- }
41
-
42
- /**
43
- * Creates a {@link ViewerLight} with given name.
44
- */
45
- public static async create( variant: Variant, name: string ): Promise<ViewerLight> {
46
- const viewerLight = new ViewerLight( variant, name );
47
- viewerLight._light = await viewerLight.createBabylonLightFromDefinition( viewerLight.definition );
48
- return viewerLight;
49
- }
50
-
51
- /**
52
- * The wrapped Light.
53
- */
54
- get light(): Light {
55
- if( !this._light ) {
56
- throw new Error( `Light for ViewerLight "${this.name}" has not been properly initialized.` );
57
- }
58
- return this._light!;
59
- }
60
-
61
- /**
62
- * The {@link DottedPath} in the built tree of {@link ViewerLight}s.
63
- * E.g. "_.top-1.sub-2.sub-sub-3.el-1"
64
- */
65
- get dottedPath(): DottedPath {
66
- return DottedPath.create( this.variant.dottedPath ).addPart( this.name );
67
- }
68
-
69
- /**
70
- * The id representing a {@link DottedPath}.
71
- */
72
- get id(): string {
73
- const dottedPath = DottedPath.create( this.dottedPath );
74
- dottedPath.shiftPart(); // remove root
75
- return dottedPath.path;
76
- }
77
-
78
- /**
79
- * The {@link LightDefinition} of the {@link ViewerLight}.
80
- */
81
- get definition(): LightDefinition {
82
- const definition = this.variant.structureJson.lights![this.name];
83
- if( isString( definition ) ) {
84
- return {
85
- type: 'baked',
86
- path: definition
87
- } as LightDefinition;
88
- }
89
- return definition as LightDefinition;
90
- }
91
-
92
- /**
93
- * The type of the {@link ViewerLight}'s light.
94
- */
95
- get type(): string {
96
- return this.light.constructor.name.replace( /light/i, '' ).toLowerCase();
97
- }
98
-
99
- /**
100
- * @see {@link VariantParameterizable.commitParameters}
101
- * @emit {@link Event.VIEWER_LIGHT_PARAMETER_COMMITTED}
102
- */
103
- public async commitParameters( parameters?: ParameterBag ): Promise<VariantParameterizable> {
104
- return super.commitParameters( parameters );
105
- }
106
-
107
- /**
108
- * Adds the default {@link ParameterObserver}s which are called every time {@link commitParameters} is called.
109
- */
110
- protected addParameterObservers(): ViewerLight {
111
- this._parameterObservers.set( Parameter.VISIBLE, [
112
- async ( light: ViewerLight, oldValue: ParameterValue, newValue: ParameterValue ) => {
113
- let visible;
114
- try {
115
- visible = Parameter.parseBoolean( newValue );
116
- } catch( e ) {
117
- return;
118
- }
119
- if( visible === true ) {
120
- enableNodeWithParents( light.light );
121
- } else if( visible === false ) {
122
- disableNodeWithParents( light.light );
123
- }
124
- }
125
- ] );
126
- this._parameterObservers.set( Parameter.INTENSITY, [
127
- async ( light: ViewerLight, oldValue: ParameterValue, newValue: ParameterValue ) => {
128
- set( light.light, 'intensity', Parameter.parseNumber( newValue ) );
129
- }
130
- ] );
131
- this._parameterObservers.set( Parameter.POSITION, [
132
- async ( light: ViewerLight, oldValue: ParameterValue, newValue: ParameterValue ) => {
133
- // we have to deal just with root nodes here due to relative impacts in a node tree
134
- const rootNode = getRootNode( light.light );
135
- if( rootNode instanceof TransformNode ) {
136
- moveTransformNode( rootNode, Parameter.parseVector( newValue ) );
137
- }
138
- }
139
- ] );
140
- this._parameterObservers.set( Parameter.ROTATION, [
141
- async ( light: ViewerLight, oldValue: ParameterValue, newValue: ParameterValue ) => {
142
- // The current implementation (rotating around coordinates 0,0,0) implicitly mutates the position of a node.
143
- // Since a user expects the rotation after the positioning, we have to manually fire the position observers.
144
- // Without calling these observers, the pivot and the position of a node is initially the same before rotating,
145
- // so there is no rotation happening at all.
146
- if( Parameter.POSITION in light.inheritedParameters ) {
147
- await light.commitParameter( Parameter.POSITION, light.inheritedParameters[Parameter.POSITION] );
148
- }
149
- // we have to deal just with root nodes here due to relative impacts in a node tree
150
- const rootNode = getRootNode( light.light );
151
- if( rootNode instanceof TransformNode ) {
152
- rotateTransformNode( rootNode, Parameter.parseRotation( newValue ) );
153
- }
154
- }
155
- ] );
156
- this._parameterObservers.set( Parameter.SCALING, [
157
- async ( light: ViewerLight, oldValue: ParameterValue, newValue: ParameterValue ) => {
158
- // we have to deal just with root nodes here due to relative impacts in a node tree
159
- const rootNode = getRootNode( light.light );
160
- if( rootNode instanceof TransformNode ) {
161
- rootNode.scaling = Parameter.parseScaling( newValue );
162
- }
163
- }
164
- ] );
165
- this._parameterObservers.set( Parameter.DIRECTION, [
166
- async ( light: ViewerLight, oldValue: ParameterValue, newValue: ParameterValue ) => {
167
- if(
168
- (light.light instanceof ShadowLight && !(light.light instanceof PointLight))
169
- || light.light instanceof HemisphericLight
170
- ) {
171
- set( light.light, 'direction', Parameter.parseVector( newValue ) );
172
- }
173
- }
174
- ] );
175
- this._parameterObservers.set( Parameter.ANGLE, [
176
- async ( light: ViewerLight, oldValue: ParameterValue, newValue: ParameterValue ) => {
177
- if( light.light.getClassName() === 'SpotLight' ) {
178
- set( light.light, 'angle', Parameter.parseNumber( newValue ) );
179
- }
180
- }
181
- ] );
182
- this._parameterObservers.set( Parameter.EXPONENT, [
183
- async ( light: ViewerLight, oldValue: ParameterValue, newValue: ParameterValue ) => {
184
- if( light.light.getClassName() === 'SpotLight' ) {
185
- set( light.light, 'exponent', Parameter.parseNumber( newValue ) );
186
- }
187
- }
188
- ] );
189
- this._parameterObservers.set( Parameter.DIFFUSE, [
190
- async ( light: ViewerLight, oldValue: ParameterValue, newValue: ParameterValue ) => {
191
- set( light.light, 'diffuse', Parameter.parseColor( newValue ) );
192
- }
193
- ] );
194
- this._parameterObservers.set( Parameter.SPECULAR, [
195
- async ( light: ViewerLight, oldValue: ParameterValue, newValue: ParameterValue ) => {
196
- set( light.light, 'specular', Parameter.parseColor( newValue ) );
197
- }
198
- ] );
199
- return this;
200
- }
201
-
202
- /**
203
- * @param definition
204
- * @protected
205
- */
206
- protected async createBabylonLightFromDefinition( definition: LightDefinition ): Promise<Light> {
207
- const parameters = Parameter.parseFromDeclarations( Parameter.declarations, this.inheritedParameters );
208
- const scene = this.variant.viewer.scene;
209
- let lightId = this.id;
210
- let babylonLight;
211
- switch( definition.type ) {
212
- case 'baked':
213
- if( !definition['path'] ) {
214
- throw new Error( `The light "${lightId}" of type "${definition.type}" needs a "path".` );
215
- }
216
- const bakedLight = this.variant.inheritedLights.find( l => l.metadata.dottedPath.path === definition['path'] );
217
- if( bakedLight ) {
218
- lightId = bakedLight.metadata.dottedPath.clone().unshiftPart( this.id ).path;
219
- babylonLight = cloneNodeWithParents( bakedLight ) as Light;
220
- babylonLight!.name = lightId;
221
- babylonLight!.id = lightId;
222
- } else {
223
- throw new Error( `No light found for path "${definition['path']}" in ViewerLight "${lightId}".` );
224
- }
225
- break;
226
- case 'hemispheric':
227
- if( !parameters['direction'] ) {
228
- throw new Error( `The ViewerLight "${lightId}" of type "${definition.type}" needs a "direction".` );
229
- }
230
- // @ts-ignore
231
- const hemisphericLightModule = await import(/* webpackChunkName: "hemispheric-light" */ '@babylonjs/core/Lights/hemisphericLight');
232
- babylonLight = new hemisphericLightModule.HemisphericLight(
233
- lightId,
234
- parameters['direction'],
235
- scene
236
- );
237
- break;
238
- case 'point':
239
- // @ts-ignore
240
- const pointLightModule = await import(/* webpackChunkName: "point-light" */ '@babylonjs/core/Lights/pointLight');
241
- babylonLight = new pointLightModule.PointLight(
242
- lightId,
243
- Vector3.Zero(), // position is set via parent TransformNode
244
- scene
245
- );
246
- break;
247
- case 'directional':
248
- if( !parameters['direction'] ) {
249
- throw new Error( `The ViewerLight "${lightId}" of type "${definition.type}" needs a "direction".` );
250
- }
251
- // @ts-ignore
252
- const directionalLightModule = await import(/* webpackChunkName: "directional-light" */ '@babylonjs/core/Lights/directionalLight');
253
- babylonLight = new directionalLightModule.DirectionalLight(
254
- lightId,
255
- parameters['direction'],
256
- scene
257
- );
258
- break;
259
- case 'spot':
260
- if( !parameters['direction'] ) {
261
- throw new Error( `The ViewerLight "${lightId}" of type "${definition.type}" needs a "direction".` );
262
- }
263
- if( !parameters['angle'] ) {
264
- throw new Error( `A ViewerLight of type "${definition.type}" needs an "angle".` );
265
- }
266
- if( !parameters['exponent'] ) {
267
- throw new Error( `The ViewerLight "${lightId}" of type "${definition.type}" needs an "exponent".` );
268
- }
269
- // @ts-ignore
270
- const spotLightModule = await import(/* webpackChunkName: "spot-light" */ '@babylonjs/core/Lights/spotLight');
271
- babylonLight = new spotLightModule.SpotLight(
272
- lightId,
273
- Vector3.Zero(), // position is set via parent TransformNode
274
- parameters['direction'],
275
- parameters['angle'],
276
- parameters['exponent'],
277
- scene
278
- );
279
- break;
280
- default:
281
- throw new Error( `The type "${definition.type}" for ViewerLight "${lightId}" is not implemented (yet).` );
282
- }
283
- if( !babylonLight ) {
284
- throw new Error( `The Light for ViewerLight "${lightId}" of type "${definition.type}" could no be created ` +
285
- `or found.` );
286
- }
287
- if( !babylonLight.parent ) {
288
- // Create pseudo parent since lights do not implement mutations like "rotation" by itself.
289
- babylonLight.parent = new TransformNode( '__light__', this.variant.viewer.scene, true );
290
- }
291
- injectNodeMetadata( babylonLight.parent, {
292
- variant: this.variant,
293
- variantParameterizable: this
294
- } );
295
- // disable/hide by default
296
- disableNodeWithParents( babylonLight );
297
- // process shadow generator
298
- const shadowGeneratorDefinition = get( definition, 'shadowGenerator' ) as ShadowGeneratorDefinition;
299
- if( !isEmpty( shadowGeneratorDefinition ) ) {
300
- if( !(babylonLight instanceof ShadowLight) ) {
301
- throw new Error( `Using a ShadowGenerator with light type "${definition.type}" is not supported for ` +
302
- `"${lightId}". Use lights deriving from ShadowLight.` );
303
- }
304
- await this.processShadowGenerator( babylonLight, shadowGeneratorDefinition );
305
- }
306
- return babylonLight;
307
- };
308
-
309
- /**
310
- * @param babylonLight
311
- * @param definition
312
- * @protected
313
- */
314
- protected async processShadowGenerator( babylonLight: ShadowLight,
315
- definition: ShadowGeneratorDefinition ): Promise<ShadowGenerator> {
316
- const parameterDeclarations: ParameterDeclarations = {};
317
- // define declarations here if needed in future
318
- const parameterBag = definition as {} as ParameterBag;
319
- const parameters = Parameter.parseFromDeclarations( parameterDeclarations, parameterBag );
320
- const shadowGenerator = new ShadowGenerator( parameters['mapSize'], babylonLight );
321
- for( const parameter in parameters ) {
322
- if( parameter === 'mapSize' ) {
323
- continue;
324
- }
325
- set( shadowGenerator, parameter, get( parameters, parameter ) );
326
- }
327
- return shadowGenerator;
328
- };
329
-
330
- }
1
+ import { PointLight } from '@babylonjs/core';
2
+ import { HemisphericLight } from '@babylonjs/core/Lights/hemisphericLight';
3
+ import { Light } from '@babylonjs/core/Lights/light';
4
+ import { ShadowLight } from '@babylonjs/core/Lights/shadowLight';
5
+ import { ShadowGenerator } from '@babylonjs/core/Lights/Shadows/shadowGenerator';
6
+ import '@babylonjs/core/Lights/Shadows/shadowGeneratorSceneComponent';
7
+ import { Vector3 } from '@babylonjs/core/Maths/math.vector';
8
+ import { TransformNode } from '@babylonjs/core/Meshes/transformNode';
9
+ import '@babylonjs/loaders/glTF/2.0/Extensions/KHR_lights_punctual';
10
+ import { get, isEmpty, isString, set } from 'lodash-es';
11
+ import {
12
+ cloneNodeWithParents,
13
+ disableNodeWithParents,
14
+ enableNodeWithParents,
15
+ getRootNode,
16
+ injectNodeMetadata,
17
+ moveTransformNode,
18
+ rotateTransformNode
19
+ } from './../util/babylonHelper';
20
+ import { DottedPath } from './dottedPath';
21
+ import { Parameter } from './parameter';
22
+ import { Variant } from './variant';
23
+ import { VariantParameterizable } from './variantParameterizable';
24
+
25
+ /**
26
+ * A {@link ViewerLight} of a {@link Variant}. Acts as a container for BabylonJS lights. Lives only in the context of a
27
+ * {@link Variant}.
28
+ */
29
+ export class ViewerLight extends VariantParameterizable {
30
+
31
+ protected _light: Light | undefined;
32
+
33
+ /**
34
+ * Constructor.
35
+ */
36
+ protected constructor( public readonly variant: Variant,
37
+ public readonly name: string ) {
38
+ super( variant, name );
39
+ this.addParameterObservers();
40
+ }
41
+
42
+ /**
43
+ * Creates a {@link ViewerLight} with given name.
44
+ */
45
+ public static async create( variant: Variant, name: string ): Promise<ViewerLight> {
46
+ const viewerLight = new ViewerLight( variant, name );
47
+ viewerLight._light = await viewerLight.createBabylonLightFromDefinition( viewerLight.definition );
48
+ return viewerLight;
49
+ }
50
+
51
+ /**
52
+ * The wrapped Light.
53
+ */
54
+ get light(): Light {
55
+ if( !this._light ) {
56
+ throw new Error( `Light for ViewerLight "${this.name}" has not been properly initialized.` );
57
+ }
58
+ return this._light!;
59
+ }
60
+
61
+ /**
62
+ * The {@link DottedPath} in the built tree of {@link ViewerLight}s.
63
+ * E.g. "_.top-1.sub-2.sub-sub-3.el-1"
64
+ */
65
+ get dottedPath(): DottedPath {
66
+ return DottedPath.create( this.variant.dottedPath ).addPart( this.name );
67
+ }
68
+
69
+ /**
70
+ * The id representing a {@link DottedPath}.
71
+ */
72
+ get id(): string {
73
+ const dottedPath = DottedPath.create( this.dottedPath );
74
+ dottedPath.shiftPart(); // remove root
75
+ return dottedPath.path;
76
+ }
77
+
78
+ /**
79
+ * The {@link LightDefinition} of the {@link ViewerLight}.
80
+ */
81
+ get definition(): LightDefinition {
82
+ const definition = this.variant.structureJson.lights![this.name];
83
+ if( isString( definition ) ) {
84
+ return {
85
+ type: 'baked',
86
+ path: definition
87
+ } as LightDefinition;
88
+ }
89
+ return definition as LightDefinition;
90
+ }
91
+
92
+ /**
93
+ * The type of the {@link ViewerLight}'s light.
94
+ */
95
+ get type(): string {
96
+ return this.light.constructor.name.replace( /light/i, '' ).toLowerCase();
97
+ }
98
+
99
+ /**
100
+ * @see {@link VariantParameterizable.commitParameters}
101
+ * @emit {@link Event.VIEWER_LIGHT_PARAMETER_COMMITTED}
102
+ */
103
+ public async commitParameters( parameters?: ParameterBag ): Promise<VariantParameterizable> {
104
+ return super.commitParameters( parameters );
105
+ }
106
+
107
+ /**
108
+ * Adds the default {@link ParameterObserver}s which are called every time {@link commitParameters} is called.
109
+ */
110
+ protected addParameterObservers(): ViewerLight {
111
+ this._parameterObservers.set( Parameter.VISIBLE, [
112
+ async ( light: ViewerLight, oldValue: ParameterValue, newValue: ParameterValue ) => {
113
+ let visible;
114
+ try {
115
+ visible = Parameter.parseBoolean( newValue );
116
+ } catch( e ) {
117
+ return;
118
+ }
119
+ if( visible === true ) {
120
+ enableNodeWithParents( light.light );
121
+ } else if( visible === false ) {
122
+ disableNodeWithParents( light.light );
123
+ }
124
+ }
125
+ ] );
126
+ this._parameterObservers.set( Parameter.INTENSITY, [
127
+ async ( light: ViewerLight, oldValue: ParameterValue, newValue: ParameterValue ) => {
128
+ set( light.light, 'intensity', Parameter.parseNumber( newValue ) );
129
+ }
130
+ ] );
131
+ this._parameterObservers.set( Parameter.POSITION, [
132
+ async ( light: ViewerLight, oldValue: ParameterValue, newValue: ParameterValue ) => {
133
+ // we have to deal just with root nodes here due to relative impacts in a node tree
134
+ const rootNode = getRootNode( light.light );
135
+ if( rootNode instanceof TransformNode ) {
136
+ moveTransformNode( rootNode, Parameter.parseVector( newValue ) );
137
+ }
138
+ }
139
+ ] );
140
+ this._parameterObservers.set( Parameter.ROTATION, [
141
+ async ( light: ViewerLight, oldValue: ParameterValue, newValue: ParameterValue ) => {
142
+ // The current implementation (rotating around coordinates 0,0,0) implicitly mutates the position of a node.
143
+ // Since a user expects the rotation after the positioning, we have to manually fire the position observers.
144
+ // Without calling these observers, the pivot and the position of a node is initially the same before rotating,
145
+ // so there is no rotation happening at all.
146
+ if( Parameter.POSITION in light.inheritedParameters ) {
147
+ await light.commitParameter( Parameter.POSITION, light.inheritedParameters[Parameter.POSITION] );
148
+ }
149
+ // we have to deal just with root nodes here due to relative impacts in a node tree
150
+ const rootNode = getRootNode( light.light );
151
+ if( rootNode instanceof TransformNode ) {
152
+ rotateTransformNode( rootNode, Parameter.parseRotation( newValue ) );
153
+ }
154
+ }
155
+ ] );
156
+ this._parameterObservers.set( Parameter.SCALING, [
157
+ async ( light: ViewerLight, oldValue: ParameterValue, newValue: ParameterValue ) => {
158
+ // we have to deal just with root nodes here due to relative impacts in a node tree
159
+ const rootNode = getRootNode( light.light );
160
+ if( rootNode instanceof TransformNode ) {
161
+ rootNode.scaling = Parameter.parseScaling( newValue );
162
+ }
163
+ }
164
+ ] );
165
+ this._parameterObservers.set( Parameter.DIRECTION, [
166
+ async ( light: ViewerLight, oldValue: ParameterValue, newValue: ParameterValue ) => {
167
+ if(
168
+ (light.light instanceof ShadowLight && !(light.light instanceof PointLight))
169
+ || light.light instanceof HemisphericLight
170
+ ) {
171
+ set( light.light, 'direction', Parameter.parseVector( newValue ) );
172
+ }
173
+ }
174
+ ] );
175
+ this._parameterObservers.set( Parameter.ANGLE, [
176
+ async ( light: ViewerLight, oldValue: ParameterValue, newValue: ParameterValue ) => {
177
+ if( light.light.getClassName() === 'SpotLight' ) {
178
+ set( light.light, 'angle', Parameter.parseNumber( newValue ) );
179
+ }
180
+ }
181
+ ] );
182
+ this._parameterObservers.set( Parameter.EXPONENT, [
183
+ async ( light: ViewerLight, oldValue: ParameterValue, newValue: ParameterValue ) => {
184
+ if( light.light.getClassName() === 'SpotLight' ) {
185
+ set( light.light, 'exponent', Parameter.parseNumber( newValue ) );
186
+ }
187
+ }
188
+ ] );
189
+ this._parameterObservers.set( Parameter.DIFFUSE, [
190
+ async ( light: ViewerLight, oldValue: ParameterValue, newValue: ParameterValue ) => {
191
+ set( light.light, 'diffuse', Parameter.parseColor( newValue ) );
192
+ }
193
+ ] );
194
+ this._parameterObservers.set( Parameter.SPECULAR, [
195
+ async ( light: ViewerLight, oldValue: ParameterValue, newValue: ParameterValue ) => {
196
+ set( light.light, 'specular', Parameter.parseColor( newValue ) );
197
+ }
198
+ ] );
199
+ return this;
200
+ }
201
+
202
+ /**
203
+ * @param definition
204
+ * @protected
205
+ */
206
+ protected async createBabylonLightFromDefinition( definition: LightDefinition ): Promise<Light> {
207
+ const parameters = Parameter.parseFromDeclarations( Parameter.declarations, this.inheritedParameters );
208
+ const scene = this.variant.viewer.scene;
209
+ let lightId = this.id;
210
+ let babylonLight;
211
+ switch( definition.type ) {
212
+ case 'baked':
213
+ if( !definition['path'] ) {
214
+ throw new Error( `The light "${lightId}" of type "${definition.type}" needs a "path".` );
215
+ }
216
+ const bakedLight = this.variant.inheritedLights.find( l => l.metadata.dottedPath.path === definition['path'] );
217
+ if( bakedLight ) {
218
+ lightId = bakedLight.metadata.dottedPath.clone().unshiftPart( this.id ).path;
219
+ babylonLight = cloneNodeWithParents( bakedLight ) as Light;
220
+ babylonLight!.name = lightId;
221
+ babylonLight!.id = lightId;
222
+ } else {
223
+ throw new Error( `No light found for path "${definition['path']}" in ViewerLight "${lightId}".` );
224
+ }
225
+ break;
226
+ case 'hemispheric':
227
+ if( !parameters['direction'] ) {
228
+ throw new Error( `The ViewerLight "${lightId}" of type "${definition.type}" needs a "direction".` );
229
+ }
230
+ // @ts-ignore
231
+ const hemisphericLightModule = await import(/* webpackChunkName: "hemispheric-light" */ '@babylonjs/core/Lights/hemisphericLight');
232
+ babylonLight = new hemisphericLightModule.HemisphericLight(
233
+ lightId,
234
+ parameters['direction'],
235
+ scene
236
+ );
237
+ break;
238
+ case 'point':
239
+ // @ts-ignore
240
+ const pointLightModule = await import(/* webpackChunkName: "point-light" */ '@babylonjs/core/Lights/pointLight');
241
+ babylonLight = new pointLightModule.PointLight(
242
+ lightId,
243
+ Vector3.Zero(), // position is set via parent TransformNode
244
+ scene
245
+ );
246
+ break;
247
+ case 'directional':
248
+ if( !parameters['direction'] ) {
249
+ throw new Error( `The ViewerLight "${lightId}" of type "${definition.type}" needs a "direction".` );
250
+ }
251
+ // @ts-ignore
252
+ const directionalLightModule = await import(/* webpackChunkName: "directional-light" */ '@babylonjs/core/Lights/directionalLight');
253
+ babylonLight = new directionalLightModule.DirectionalLight(
254
+ lightId,
255
+ parameters['direction'],
256
+ scene
257
+ );
258
+ break;
259
+ case 'spot':
260
+ if( !parameters['direction'] ) {
261
+ throw new Error( `The ViewerLight "${lightId}" of type "${definition.type}" needs a "direction".` );
262
+ }
263
+ if( !parameters['angle'] ) {
264
+ throw new Error( `A ViewerLight of type "${definition.type}" needs an "angle".` );
265
+ }
266
+ if( !parameters['exponent'] ) {
267
+ throw new Error( `The ViewerLight "${lightId}" of type "${definition.type}" needs an "exponent".` );
268
+ }
269
+ // @ts-ignore
270
+ const spotLightModule = await import(/* webpackChunkName: "spot-light" */ '@babylonjs/core/Lights/spotLight');
271
+ babylonLight = new spotLightModule.SpotLight(
272
+ lightId,
273
+ Vector3.Zero(), // position is set via parent TransformNode
274
+ parameters['direction'],
275
+ parameters['angle'],
276
+ parameters['exponent'],
277
+ scene
278
+ );
279
+ break;
280
+ default:
281
+ throw new Error( `The type "${definition.type}" for ViewerLight "${lightId}" is not implemented (yet).` );
282
+ }
283
+ if( !babylonLight ) {
284
+ throw new Error( `The Light for ViewerLight "${lightId}" of type "${definition.type}" could no be created ` +
285
+ `or found.` );
286
+ }
287
+ if( !babylonLight.parent ) {
288
+ // Create pseudo parent since lights do not implement mutations like "rotation" by itself.
289
+ babylonLight.parent = new TransformNode( '__light__', this.variant.viewer.scene, true );
290
+ }
291
+ injectNodeMetadata( babylonLight.parent, {
292
+ variant: this.variant,
293
+ variantParameterizable: this
294
+ } );
295
+ // disable/hide by default
296
+ disableNodeWithParents( babylonLight );
297
+ // process shadow generator
298
+ const shadowGeneratorDefinition = get( definition, 'shadowGenerator' ) as ShadowGeneratorDefinition;
299
+ if( !isEmpty( shadowGeneratorDefinition ) ) {
300
+ if( !(babylonLight instanceof ShadowLight) ) {
301
+ throw new Error( `Using a ShadowGenerator with light type "${definition.type}" is not supported for ` +
302
+ `"${lightId}". Use lights deriving from ShadowLight.` );
303
+ }
304
+ await this.processShadowGenerator( babylonLight, shadowGeneratorDefinition );
305
+ }
306
+ return babylonLight;
307
+ };
308
+
309
+ /**
310
+ * @param babylonLight
311
+ * @param definition
312
+ * @protected
313
+ */
314
+ protected async processShadowGenerator( babylonLight: ShadowLight,
315
+ definition: ShadowGeneratorDefinition ): Promise<ShadowGenerator> {
316
+ const parameterDeclarations: ParameterDeclarations = {};
317
+ // define declarations here if needed in future
318
+ const parameterBag = definition as {} as ParameterBag;
319
+ const parameters = Parameter.parseFromDeclarations( parameterDeclarations, parameterBag );
320
+ const shadowGenerator = new ShadowGenerator( parameters['mapSize'], babylonLight );
321
+ for( const parameter in parameters ) {
322
+ if( parameter === 'mapSize' ) {
323
+ continue;
324
+ }
325
+ set( shadowGenerator, parameter, get( parameters, parameter ) );
326
+ }
327
+ return shadowGenerator;
328
+ };
329
+
330
+ }