@combeenation/3d-viewer 7.1.0 → 7.1.2

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 (117) 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 +672 -670
  8. package/dist/lib-cjs/api/classes/element.js.map +1 -1
  9. package/dist/lib-cjs/api/classes/event.d.ts +401 -396
  10. package/dist/lib-cjs/api/classes/event.js +424 -419
  11. package/dist/lib-cjs/api/classes/event.js.map +1 -1
  12. package/dist/lib-cjs/api/classes/eventBroadcaster.d.ts +26 -26
  13. package/dist/lib-cjs/api/classes/eventBroadcaster.js +49 -49
  14. package/dist/lib-cjs/api/classes/fuzzyMap.d.ts +7 -7
  15. package/dist/lib-cjs/api/classes/fuzzyMap.js +21 -21
  16. package/dist/lib-cjs/api/classes/parameter.d.ts +351 -351
  17. package/dist/lib-cjs/api/classes/parameter.js +524 -517
  18. package/dist/lib-cjs/api/classes/parameter.js.map +1 -1
  19. package/dist/lib-cjs/api/classes/parameterObservable.d.ts +36 -36
  20. package/dist/lib-cjs/api/classes/parameterObservable.js +72 -72
  21. package/dist/lib-cjs/api/classes/parameterizable.d.ts +15 -15
  22. package/dist/lib-cjs/api/classes/parameterizable.js +102 -102
  23. package/dist/lib-cjs/api/classes/placementAnimation.d.ts +45 -45
  24. package/dist/lib-cjs/api/classes/placementAnimation.js +176 -176
  25. package/dist/lib-cjs/api/classes/variant.d.ts +253 -253
  26. package/dist/lib-cjs/api/classes/variant.js +858 -843
  27. package/dist/lib-cjs/api/classes/variant.js.map +1 -1
  28. package/dist/lib-cjs/api/classes/variantInstance.d.ts +53 -53
  29. package/dist/lib-cjs/api/classes/variantInstance.js +125 -125
  30. package/dist/lib-cjs/api/classes/variantParameterizable.d.ts +17 -17
  31. package/dist/lib-cjs/api/classes/variantParameterizable.js +86 -88
  32. package/dist/lib-cjs/api/classes/variantParameterizable.js.map +1 -1
  33. package/dist/lib-cjs/api/classes/viewer.d.ts +204 -199
  34. package/dist/lib-cjs/api/classes/viewer.js +682 -670
  35. package/dist/lib-cjs/api/classes/viewer.js.map +1 -1
  36. package/dist/lib-cjs/api/classes/viewerError.d.ts +43 -0
  37. package/dist/lib-cjs/api/classes/viewerError.js +56 -0
  38. package/dist/lib-cjs/api/classes/viewerError.js.map +1 -0
  39. package/dist/lib-cjs/api/classes/viewerLight.d.ts +66 -66
  40. package/dist/lib-cjs/api/classes/viewerLight.js +348 -348
  41. package/dist/lib-cjs/api/internal/lensRendering.d.ts +8 -8
  42. package/dist/lib-cjs/api/internal/lensRendering.js +11 -11
  43. package/dist/lib-cjs/api/internal/sceneSetup.d.ts +13 -13
  44. package/dist/lib-cjs/api/internal/sceneSetup.js +226 -226
  45. package/dist/lib-cjs/api/manager/animationManager.d.ts +30 -30
  46. package/dist/lib-cjs/api/manager/animationManager.js +126 -126
  47. package/dist/lib-cjs/api/manager/gltfExportManager.d.ts +79 -78
  48. package/dist/lib-cjs/api/manager/gltfExportManager.js +242 -241
  49. package/dist/lib-cjs/api/manager/gltfExportManager.js.map +1 -1
  50. package/dist/lib-cjs/api/manager/sceneManager.d.ts +33 -33
  51. package/dist/lib-cjs/api/manager/sceneManager.js +128 -130
  52. package/dist/lib-cjs/api/manager/sceneManager.js.map +1 -1
  53. package/dist/lib-cjs/api/manager/tagManager.d.ts +116 -108
  54. package/dist/lib-cjs/api/manager/tagManager.js +444 -419
  55. package/dist/lib-cjs/api/manager/tagManager.js.map +1 -1
  56. package/dist/lib-cjs/api/manager/textureLoadManager.d.ts +22 -22
  57. package/dist/lib-cjs/api/manager/textureLoadManager.js +97 -97
  58. package/dist/lib-cjs/api/manager/variantInstanceManager.d.ts +106 -102
  59. package/dist/lib-cjs/api/manager/variantInstanceManager.js +292 -284
  60. package/dist/lib-cjs/api/manager/variantInstanceManager.js.map +1 -1
  61. package/dist/lib-cjs/api/store/specStorage.d.ts +32 -32
  62. package/dist/lib-cjs/api/store/specStorage.js +65 -65
  63. package/dist/lib-cjs/api/util/babylonHelper.d.ts +235 -235
  64. package/dist/lib-cjs/api/util/babylonHelper.js +753 -753
  65. package/dist/lib-cjs/api/util/globalTypes.d.ts +441 -436
  66. package/dist/lib-cjs/api/util/globalTypes.js +1 -1
  67. package/dist/lib-cjs/api/util/resourceHelper.d.ts +58 -58
  68. package/dist/lib-cjs/api/util/resourceHelper.js +203 -203
  69. package/dist/lib-cjs/api/util/sceneLoaderHelper.d.ts +44 -44
  70. package/dist/lib-cjs/api/util/sceneLoaderHelper.js +175 -175
  71. package/dist/lib-cjs/api/util/stringHelper.d.ts +13 -13
  72. package/dist/lib-cjs/api/util/stringHelper.js +32 -32
  73. package/dist/lib-cjs/api/util/structureHelper.d.ts +9 -9
  74. package/dist/lib-cjs/api/util/structureHelper.js +57 -48
  75. package/dist/lib-cjs/api/util/structureHelper.js.map +1 -1
  76. package/dist/lib-cjs/buildinfo.json +3 -3
  77. package/dist/lib-cjs/commonjs.tsconfig.tsbuildinfo +1 -1
  78. package/dist/lib-cjs/index.d.ts +54 -53
  79. package/dist/lib-cjs/index.js +117 -114
  80. package/dist/lib-cjs/index.js.map +1 -1
  81. package/package.json +81 -81
  82. package/src/api/classes/animationInterface.ts +10 -10
  83. package/src/api/classes/dottedPath.ts +181 -181
  84. package/src/api/classes/element.ts +733 -731
  85. package/src/api/classes/event.ts +457 -452
  86. package/src/api/classes/eventBroadcaster.ts +52 -52
  87. package/src/api/classes/fuzzyMap.ts +21 -21
  88. package/src/api/classes/parameter.ts +561 -554
  89. package/src/api/classes/parameterObservable.ts +73 -73
  90. package/src/api/classes/parameterizable.ts +87 -87
  91. package/src/api/classes/placementAnimation.ts +162 -162
  92. package/src/api/classes/variant.ts +949 -933
  93. package/src/api/classes/variantInstance.ts +123 -123
  94. package/src/api/classes/variantParameterizable.ts +83 -85
  95. package/src/api/classes/viewer.ts +760 -743
  96. package/src/api/classes/viewerError.ts +63 -0
  97. package/src/api/classes/viewerLight.ts +339 -339
  98. package/src/api/internal/debugViewer.ts +90 -90
  99. package/src/api/internal/lensRendering.ts +9 -9
  100. package/src/api/internal/sceneSetup.ts +205 -205
  101. package/src/api/manager/animationManager.ts +143 -143
  102. package/src/api/manager/gltfExportManager.ts +237 -236
  103. package/src/api/manager/sceneManager.ts +134 -136
  104. package/src/api/manager/tagManager.ts +477 -451
  105. package/src/api/manager/textureLoadManager.ts +95 -95
  106. package/src/api/manager/variantInstanceManager.ts +309 -297
  107. package/src/api/store/specStorage.ts +68 -68
  108. package/src/api/util/babylonHelper.ts +823 -823
  109. package/src/api/util/globalTypes.ts +508 -503
  110. package/src/api/util/resourceHelper.ts +191 -191
  111. package/src/api/util/sceneLoaderHelper.ts +170 -170
  112. package/src/api/util/stringHelper.ts +30 -30
  113. package/src/api/util/structureHelper.ts +58 -49
  114. package/src/buildinfo.json +3 -3
  115. package/src/dev.ts +62 -62
  116. package/src/index.ts +103 -100
  117. package/src/types.d.ts +38 -38
@@ -1,73 +1,73 @@
1
- import { uuidv4 } from '../util/stringHelper';
2
- import { EventBroadcaster } from './eventBroadcaster';
3
- import { Parameter } from './parameter';
4
- import { merge } from 'lodash-es';
5
-
6
- export abstract class ParameterObservable extends EventBroadcaster {
7
- public readonly uuid: string;
8
-
9
- public readonly parameters: ParameterBag = {};
10
-
11
- protected readonly parameterDeclaration: ParameterDeclarations = {};
12
-
13
- protected parameterObservers: Map<string, ParameterObserver[]> = new Map();
14
-
15
- protected constructor(parameterDeclaration: ParameterDeclarations = {}, parameters: ParameterBag = {}) {
16
- super();
17
- this.uuid = uuidv4();
18
- this.parameterDeclaration = merge(this.parameterDeclaration, parameterDeclaration);
19
- this.parameters = merge(this.parameters, parameters);
20
- }
21
-
22
- /**
23
- * The uuid (if not overloaded).
24
- */
25
- get id(): string {
26
- return this.uuid;
27
- }
28
-
29
- /**
30
- * Adds an observer function for parameter changes.
31
- * The {@link ParameterObserver} gets 3 parameters: the {@link ParameterObservable}, the old {@link ParameterValue}
32
- * and the new {@link ParameterValue}.
33
- *
34
- * See [Parameters](https://3dviewer.docs.combeenation.com/pages/documentation/Parameters.html) for an example.
35
- */
36
- public addParameterObserver(parameter: string, observer: ParameterObserver): ParameterObservable {
37
- if (this.parameterObservers.has(parameter)) {
38
- this.parameterObservers.get(parameter)!.push(observer);
39
- return this;
40
- }
41
- this.parameterObservers.set(parameter, [observer]);
42
- return this;
43
- }
44
-
45
- /**
46
- * Removes all {@link ParameterObserver}s for given parameter.
47
- */
48
- public removeParameterObservers(parameter: string): ParameterObservable {
49
- this.parameterObservers.delete(parameter);
50
- return this;
51
- }
52
-
53
- /**
54
- * Invoke parameter observers for given parameter.
55
- */
56
- public async commitParameter(parameter: string, value: ParameterValue): Promise<ParameterObservable> {
57
- const parameters: ParameterBag = {};
58
- parameters[parameter] = value;
59
- return this.commitParameters(parameters);
60
- }
61
-
62
- /**
63
- * Invoke parameter observers for given parameters.
64
- */
65
- public abstract commitParameters(parameters?: ParameterBag): Promise<ParameterObservable>;
66
-
67
- /**
68
- * Asserts whether given parameter is declared and valid.
69
- */
70
- public assertParameter(parameterDeclaration: ParameterDeclarations, parameter: string, value: ParameterValue) {
71
- Parameter.assertParameter(parameterDeclaration, parameter, value);
72
- }
73
- }
1
+ import { uuidv4 } from '../util/stringHelper';
2
+ import { EventBroadcaster } from './eventBroadcaster';
3
+ import { Parameter } from './parameter';
4
+ import { merge } from 'lodash-es';
5
+
6
+ export abstract class ParameterObservable extends EventBroadcaster {
7
+ public readonly uuid: string;
8
+
9
+ public readonly parameters: ParameterBag = {};
10
+
11
+ protected readonly parameterDeclaration: ParameterDeclarations = {};
12
+
13
+ protected parameterObservers: Map<string, ParameterObserver[]> = new Map();
14
+
15
+ protected constructor(parameterDeclaration: ParameterDeclarations = {}, parameters: ParameterBag = {}) {
16
+ super();
17
+ this.uuid = uuidv4();
18
+ this.parameterDeclaration = merge(this.parameterDeclaration, parameterDeclaration);
19
+ this.parameters = merge(this.parameters, parameters);
20
+ }
21
+
22
+ /**
23
+ * The uuid (if not overloaded).
24
+ */
25
+ get id(): string {
26
+ return this.uuid;
27
+ }
28
+
29
+ /**
30
+ * Adds an observer function for parameter changes.
31
+ * The {@link ParameterObserver} gets 3 parameters: the {@link ParameterObservable}, the old {@link ParameterValue}
32
+ * and the new {@link ParameterValue}.
33
+ *
34
+ * See [Parameters](https://3dviewer.docs.combeenation.com/pages/documentation/Parameters.html) for an example.
35
+ */
36
+ public addParameterObserver(parameter: string, observer: ParameterObserver): ParameterObservable {
37
+ if (this.parameterObservers.has(parameter)) {
38
+ this.parameterObservers.get(parameter)!.push(observer);
39
+ return this;
40
+ }
41
+ this.parameterObservers.set(parameter, [observer]);
42
+ return this;
43
+ }
44
+
45
+ /**
46
+ * Removes all {@link ParameterObserver}s for given parameter.
47
+ */
48
+ public removeParameterObservers(parameter: string): ParameterObservable {
49
+ this.parameterObservers.delete(parameter);
50
+ return this;
51
+ }
52
+
53
+ /**
54
+ * Invoke parameter observers for given parameter.
55
+ */
56
+ public async commitParameter(parameter: string, value: ParameterValue): Promise<ParameterObservable> {
57
+ const parameters: ParameterBag = {};
58
+ parameters[parameter] = value;
59
+ return this.commitParameters(parameters);
60
+ }
61
+
62
+ /**
63
+ * Invoke parameter observers for given parameters.
64
+ */
65
+ public abstract commitParameters(parameters?: ParameterBag): Promise<ParameterObservable>;
66
+
67
+ /**
68
+ * Asserts whether given parameter is declared and valid.
69
+ */
70
+ public assertParameter(parameterDeclaration: ParameterDeclarations, parameter: string, value: ParameterValue) {
71
+ Parameter.assertParameter(parameterDeclaration, parameter, value);
72
+ }
73
+ }
@@ -1,87 +1,87 @@
1
- import { Parameter } from './parameter';
2
- import { ParameterObservable } from './parameterObservable';
3
-
4
- export abstract class Parameterizable extends ParameterObservable {
5
- protected parameterDeclaration: ParameterDeclarations = Parameter.declarations;
6
-
7
- get visible(): boolean | undefined {
8
- if (!(Parameter.VISIBLE in this.parameters)) {
9
- return undefined;
10
- }
11
- try {
12
- return Parameter.parseBoolean(this.parameters[Parameter.VISIBLE]);
13
- } catch (e) {
14
- return undefined;
15
- }
16
- }
17
-
18
- get highlighted(): boolean | undefined {
19
- if (!(Parameter.HIGHLIGHTED in this.parameters)) {
20
- return undefined;
21
- }
22
- try {
23
- return Parameter.parseBoolean(this.parameters[Parameter.HIGHLIGHTED]);
24
- } catch (e) {
25
- return undefined;
26
- }
27
- }
28
-
29
- get material(): string | undefined {
30
- if (!(Parameter.MATERIAL in this.parameters)) {
31
- return undefined;
32
- }
33
- return this.parameters[Parameter.MATERIAL].toString();
34
- }
35
-
36
- get scaling(): string | undefined {
37
- if (!(Parameter.SCALING in this.parameters)) {
38
- return undefined;
39
- }
40
- return this.parameters[Parameter.SCALING].toString();
41
- }
42
-
43
- get position(): string | undefined {
44
- if (!(Parameter.POSITION in this.parameters)) {
45
- return undefined;
46
- }
47
- return this.parameters[Parameter.POSITION].toString();
48
- }
49
-
50
- get rotation(): string | undefined {
51
- if (!(Parameter.ROTATION in this.parameters)) {
52
- return undefined;
53
- }
54
- return this.parameters[Parameter.ROTATION].toString();
55
- }
56
-
57
- get castShadow(): boolean | undefined {
58
- if (!(Parameter.CAST_SHADOW in this.parameters)) {
59
- return undefined;
60
- }
61
- try {
62
- return Parameter.parseBoolean(this.parameters[Parameter.CAST_SHADOW]);
63
- } catch (e) {
64
- return undefined;
65
- }
66
- }
67
-
68
- public async show(): Promise<ParameterObservable> {
69
- await this.commitParameter(Parameter.VISIBLE, true);
70
- return this;
71
- }
72
-
73
- public async hide(): Promise<ParameterObservable> {
74
- await this.commitParameter(Parameter.VISIBLE, false);
75
- return this;
76
- }
77
-
78
- public async toggle(): Promise<ParameterObservable> {
79
- await this.commitParameter(Parameter.VISIBLE, !this.visible);
80
- return this;
81
- }
82
-
83
- public async toggleHighlight(): Promise<ParameterObservable> {
84
- await this.commitParameter(Parameter.HIGHLIGHTED, !this.highlighted);
85
- return this;
86
- }
87
- }
1
+ import { Parameter } from './parameter';
2
+ import { ParameterObservable } from './parameterObservable';
3
+
4
+ export abstract class Parameterizable extends ParameterObservable {
5
+ protected parameterDeclaration: ParameterDeclarations = Parameter.declarations;
6
+
7
+ get visible(): boolean | undefined {
8
+ if (!(Parameter.VISIBLE in this.parameters)) {
9
+ return undefined;
10
+ }
11
+ try {
12
+ return Parameter.parseBoolean(this.parameters[Parameter.VISIBLE]);
13
+ } catch (e) {
14
+ return undefined;
15
+ }
16
+ }
17
+
18
+ get highlighted(): boolean | undefined {
19
+ if (!(Parameter.HIGHLIGHTED in this.parameters)) {
20
+ return undefined;
21
+ }
22
+ try {
23
+ return Parameter.parseBoolean(this.parameters[Parameter.HIGHLIGHTED]);
24
+ } catch (e) {
25
+ return undefined;
26
+ }
27
+ }
28
+
29
+ get material(): string | undefined {
30
+ if (!(Parameter.MATERIAL in this.parameters)) {
31
+ return undefined;
32
+ }
33
+ return this.parameters[Parameter.MATERIAL].toString();
34
+ }
35
+
36
+ get scaling(): string | undefined {
37
+ if (!(Parameter.SCALING in this.parameters)) {
38
+ return undefined;
39
+ }
40
+ return this.parameters[Parameter.SCALING].toString();
41
+ }
42
+
43
+ get position(): string | undefined {
44
+ if (!(Parameter.POSITION in this.parameters)) {
45
+ return undefined;
46
+ }
47
+ return this.parameters[Parameter.POSITION].toString();
48
+ }
49
+
50
+ get rotation(): string | undefined {
51
+ if (!(Parameter.ROTATION in this.parameters)) {
52
+ return undefined;
53
+ }
54
+ return this.parameters[Parameter.ROTATION].toString();
55
+ }
56
+
57
+ get castShadow(): boolean | undefined {
58
+ if (!(Parameter.CAST_SHADOW in this.parameters)) {
59
+ return undefined;
60
+ }
61
+ try {
62
+ return Parameter.parseBoolean(this.parameters[Parameter.CAST_SHADOW]);
63
+ } catch (e) {
64
+ return undefined;
65
+ }
66
+ }
67
+
68
+ public async show(): Promise<ParameterObservable> {
69
+ await this.commitParameter(Parameter.VISIBLE, true);
70
+ return this;
71
+ }
72
+
73
+ public async hide(): Promise<ParameterObservable> {
74
+ await this.commitParameter(Parameter.VISIBLE, false);
75
+ return this;
76
+ }
77
+
78
+ public async toggle(): Promise<ParameterObservable> {
79
+ await this.commitParameter(Parameter.VISIBLE, !this.visible);
80
+ return this;
81
+ }
82
+
83
+ public async toggleHighlight(): Promise<ParameterObservable> {
84
+ await this.commitParameter(Parameter.HIGHLIGHTED, !this.highlighted);
85
+ return this;
86
+ }
87
+ }
@@ -1,162 +1,162 @@
1
- import { Parameter } from '../classes/parameter';
2
- import { SpecStorage } from '../store/specStorage';
3
- import { AnimationInterface } from './animationInterface';
4
- import { ArcRotateCamera } from '@babylonjs/core/Cameras/arcRotateCamera';
5
- import { Vector3 } from '@babylonjs/core/Maths/math.vector';
6
- import { gsap } from 'gsap';
7
- import { cloneDeep, get, isString, merge, set } from 'lodash-es';
8
-
9
- /**
10
- * An {@link PlacementAnimation} handling `Tweens` of the `GSAP` API using configured {@link PlacementDefinition}s
11
- * and {@link AnimationDefinition}s. It mutates the attributes in the given {@link PlacementDefinition} with the given
12
- * {@link AnimationDefinition}. The latter can be anything the `GSAP` allows in its API. Please consider using the
13
- * API of the {@link AnimationManager} to handle instances of this class in your consuming project.
14
- */
15
- export class PlacementAnimation implements AnimationInterface {
16
- protected _placementDefinition: PlacementDefinition;
17
-
18
- protected _animationDefinition: AnimationDefinition;
19
-
20
- protected _timeline: GSAPTimeline;
21
-
22
- /**
23
- * Constructor.
24
- */
25
- public constructor(
26
- public readonly mutable: object,
27
- protected _placement: PlacementDefinition | string,
28
- protected _animation?: AnimationDefinition | string
29
- ) {
30
- if (isString(_placement)) {
31
- const placementDefinitions = SpecStorage.get<PlacementDefinitions>('scene.placements');
32
- if (!placementDefinitions) {
33
- throw new Error(`No PlacementDefinitions defined in JSON structure.`);
34
- }
35
- const placementDefinition = placementDefinitions[_placement];
36
- if (!placementDefinition) {
37
- throw new Error(`PlacementDefinition "${_placement}" not defined in JSON structure.`);
38
- }
39
- this._placementDefinition = cloneDeep(placementDefinition);
40
- } else {
41
- this._placementDefinition = cloneDeep(_placement) as PlacementDefinition;
42
- }
43
- if (isString(_animation)) {
44
- const animationDefinition = SpecStorage.get<AnimationDefinitions>('scene.animations')?.[_animation];
45
- if (!animationDefinition) {
46
- throw new Error(`AnimationDefinition "${_animation}" not defined in JSON structure.`);
47
- }
48
- this._animationDefinition = cloneDeep(animationDefinition);
49
- } else {
50
- this._animationDefinition = cloneDeep(_animation) as AnimationDefinition;
51
- }
52
-
53
- this.prepareAnimationDefinition();
54
- // create (shallow) copy for GSAP which doesn't contain `shortestWay`,
55
- // because that's not part of the original `TweenVars` input typing and therefore leads to warnings
56
- const { shortestWay, ...gsapTweenVars } = this._animationDefinition;
57
- this._timeline = gsap.timeline({
58
- defaults: gsapTweenVars,
59
- paused: true,
60
- });
61
-
62
- this.preparePlacementDefinition();
63
- this.createTweens();
64
- }
65
-
66
- /**
67
- * Plays all `Tweens` of this {@link PlacementAnimation}.
68
- */
69
- public async play(): Promise<AnimationInterface> {
70
- await this._timeline.play().then();
71
- return this;
72
- }
73
-
74
- /**
75
- * Pause all `Tweens` of this {@link PlacementAnimation}.
76
- */
77
- public pause(): AnimationInterface {
78
- this._timeline.pause();
79
- return this;
80
- }
81
-
82
- /**
83
- * Kills all `Tweens` of this {@link PlacementAnimation}.
84
- */
85
- public kill(): AnimationInterface {
86
- this._timeline.kill();
87
- return this;
88
- }
89
-
90
- /**
91
- * Creates a {@link AnimationDefinition} definition if not provided in the constructor and sets some defaults.
92
- * Only custom properties from {@link AnimationDefinition} are adjusted, all {@link GSAPTweenVars} remain untouched.
93
- */
94
- protected prepareAnimationDefinition() {
95
- // create an animation definition if not available yet
96
- this._animationDefinition = this._animationDefinition || {};
97
-
98
- // parse shortest way flag or set to `true` if not configured
99
- if (this._animationDefinition.shortestWay !== undefined) {
100
- this._animationDefinition.shortestWay = Parameter.parseBoolean(this._animationDefinition.shortestWay);
101
- } else {
102
- this._animationDefinition.shortestWay = true;
103
- }
104
- }
105
-
106
- /**
107
- * Prepares different types of {@link PlacementDefinition} and sets some defaults.
108
- */
109
- protected preparePlacementDefinition() {
110
- let position = this._placementDefinition.position;
111
- if (isString(position)) {
112
- position = Parameter.parseVector(position);
113
- this._placementDefinition.position = position;
114
- }
115
- if (this.mutable instanceof ArcRotateCamera) {
116
- // parse the target
117
- if (isString(this._placementDefinition.target)) {
118
- this._placementDefinition.target = Parameter.parseVector(this._placementDefinition.target);
119
- }
120
- if (position) {
121
- // parse the position and calculate alpha, beta and radius with a "ghost camera"
122
- // @see https://www.babylonjs-playground.com/#4IVMG#0
123
- const ghostCam = (this.mutable as ArcRotateCamera).clone('ghost') as ArcRotateCamera;
124
- ghostCam.setPosition(position);
125
- merge(this._placementDefinition, {
126
- alpha: ghostCam.alpha,
127
- beta: ghostCam.beta,
128
- radius: ghostCam.radius,
129
- });
130
- delete this._placementDefinition.position;
131
- ghostCam.dispose();
132
- } else if (this._animationDefinition.shortestWay && this._placementDefinition.alpha !== undefined) {
133
- // transform the target's alpha value into the same turn as the current camera position to avoid movements
134
- // > 360 degrees
135
- const alphaGap = this.mutable.alpha - this._placementDefinition.alpha;
136
- const cntTurns = Math.round(alphaGap / (2 * Math.PI));
137
- this._placementDefinition.alpha += 2 * Math.PI * cntTurns;
138
- }
139
- }
140
- }
141
-
142
- /**
143
- * Creates `Tweens` for the {@link PlacementDefinition} configured for this {@link PlacementAnimation}.
144
- */
145
- protected createTweens() {
146
- for (const property in this._placementDefinition) {
147
- const propertyValue = get(this._placementDefinition, property);
148
- const tweenVars: object = {};
149
- let _mutable = this.mutable;
150
- if (propertyValue instanceof Vector3) {
151
- // GSAP does not support mutation of complex objects, so we are forced
152
- // to use the properties on the object as mutable directly and mutate
153
- // the coordinates separately.
154
- _mutable = get(this.mutable, property);
155
- merge(tweenVars, { x: propertyValue.x, y: propertyValue.y, z: propertyValue.z });
156
- } else {
157
- set(tweenVars, property, propertyValue);
158
- }
159
- this._timeline.to(_mutable, tweenVars, 0); // Note: position 0 does the "parallel" stuff here
160
- }
161
- }
162
- }
1
+ import { Parameter } from '../classes/parameter';
2
+ import { SpecStorage } from '../store/specStorage';
3
+ import { AnimationInterface } from './animationInterface';
4
+ import { ArcRotateCamera } from '@babylonjs/core/Cameras/arcRotateCamera';
5
+ import { Vector3 } from '@babylonjs/core/Maths/math.vector';
6
+ import { gsap } from 'gsap';
7
+ import { cloneDeep, get, isString, merge, set } from 'lodash-es';
8
+
9
+ /**
10
+ * An {@link PlacementAnimation} handling `Tweens` of the `GSAP` API using configured {@link PlacementDefinition}s
11
+ * and {@link AnimationDefinition}s. It mutates the attributes in the given {@link PlacementDefinition} with the given
12
+ * {@link AnimationDefinition}. The latter can be anything the `GSAP` allows in its API. Please consider using the
13
+ * API of the {@link AnimationManager} to handle instances of this class in your consuming project.
14
+ */
15
+ export class PlacementAnimation implements AnimationInterface {
16
+ protected _placementDefinition: PlacementDefinition;
17
+
18
+ protected _animationDefinition: AnimationDefinition;
19
+
20
+ protected _timeline: GSAPTimeline;
21
+
22
+ /**
23
+ * Constructor.
24
+ */
25
+ public constructor(
26
+ public readonly mutable: object,
27
+ protected _placement: PlacementDefinition | string,
28
+ protected _animation?: AnimationDefinition | string
29
+ ) {
30
+ if (isString(_placement)) {
31
+ const placementDefinitions = SpecStorage.get<PlacementDefinitions>('scene.placements');
32
+ if (!placementDefinitions) {
33
+ throw new Error(`No PlacementDefinitions defined in JSON structure.`);
34
+ }
35
+ const placementDefinition = placementDefinitions[_placement];
36
+ if (!placementDefinition) {
37
+ throw new Error(`PlacementDefinition "${_placement}" not defined in JSON structure.`);
38
+ }
39
+ this._placementDefinition = cloneDeep(placementDefinition);
40
+ } else {
41
+ this._placementDefinition = cloneDeep(_placement) as PlacementDefinition;
42
+ }
43
+ if (isString(_animation)) {
44
+ const animationDefinition = SpecStorage.get<AnimationDefinitions>('scene.animations')?.[_animation];
45
+ if (!animationDefinition) {
46
+ throw new Error(`AnimationDefinition "${_animation}" not defined in JSON structure.`);
47
+ }
48
+ this._animationDefinition = cloneDeep(animationDefinition);
49
+ } else {
50
+ this._animationDefinition = cloneDeep(_animation) as AnimationDefinition;
51
+ }
52
+
53
+ this.prepareAnimationDefinition();
54
+ // create (shallow) copy for GSAP which doesn't contain `shortestWay`,
55
+ // because that's not part of the original `TweenVars` input typing and therefore leads to warnings
56
+ const { shortestWay, ...gsapTweenVars } = this._animationDefinition;
57
+ this._timeline = gsap.timeline({
58
+ defaults: gsapTweenVars,
59
+ paused: true,
60
+ });
61
+
62
+ this.preparePlacementDefinition();
63
+ this.createTweens();
64
+ }
65
+
66
+ /**
67
+ * Plays all `Tweens` of this {@link PlacementAnimation}.
68
+ */
69
+ public async play(): Promise<AnimationInterface> {
70
+ await this._timeline.play().then();
71
+ return this;
72
+ }
73
+
74
+ /**
75
+ * Pause all `Tweens` of this {@link PlacementAnimation}.
76
+ */
77
+ public pause(): AnimationInterface {
78
+ this._timeline.pause();
79
+ return this;
80
+ }
81
+
82
+ /**
83
+ * Kills all `Tweens` of this {@link PlacementAnimation}.
84
+ */
85
+ public kill(): AnimationInterface {
86
+ this._timeline.kill();
87
+ return this;
88
+ }
89
+
90
+ /**
91
+ * Creates a {@link AnimationDefinition} definition if not provided in the constructor and sets some defaults.
92
+ * Only custom properties from {@link AnimationDefinition} are adjusted, all {@link GSAPTweenVars} remain untouched.
93
+ */
94
+ protected prepareAnimationDefinition() {
95
+ // create an animation definition if not available yet
96
+ this._animationDefinition = this._animationDefinition || {};
97
+
98
+ // parse shortest way flag or set to `true` if not configured
99
+ if (this._animationDefinition.shortestWay !== undefined) {
100
+ this._animationDefinition.shortestWay = Parameter.parseBoolean(this._animationDefinition.shortestWay);
101
+ } else {
102
+ this._animationDefinition.shortestWay = true;
103
+ }
104
+ }
105
+
106
+ /**
107
+ * Prepares different types of {@link PlacementDefinition} and sets some defaults.
108
+ */
109
+ protected preparePlacementDefinition() {
110
+ let position = this._placementDefinition.position;
111
+ if (isString(position)) {
112
+ position = Parameter.parseVector(position);
113
+ this._placementDefinition.position = position;
114
+ }
115
+ if (this.mutable instanceof ArcRotateCamera) {
116
+ // parse the target
117
+ if (isString(this._placementDefinition.target)) {
118
+ this._placementDefinition.target = Parameter.parseVector(this._placementDefinition.target);
119
+ }
120
+ if (position) {
121
+ // parse the position and calculate alpha, beta and radius with a "ghost camera"
122
+ // @see https://www.babylonjs-playground.com/#4IVMG#0
123
+ const ghostCam = (this.mutable as ArcRotateCamera).clone('ghost') as ArcRotateCamera;
124
+ ghostCam.setPosition(position);
125
+ merge(this._placementDefinition, {
126
+ alpha: ghostCam.alpha,
127
+ beta: ghostCam.beta,
128
+ radius: ghostCam.radius,
129
+ });
130
+ delete this._placementDefinition.position;
131
+ ghostCam.dispose();
132
+ } else if (this._animationDefinition.shortestWay && this._placementDefinition.alpha !== undefined) {
133
+ // transform the target's alpha value into the same turn as the current camera position to avoid movements
134
+ // > 360 degrees
135
+ const alphaGap = this.mutable.alpha - this._placementDefinition.alpha;
136
+ const cntTurns = Math.round(alphaGap / (2 * Math.PI));
137
+ this._placementDefinition.alpha += 2 * Math.PI * cntTurns;
138
+ }
139
+ }
140
+ }
141
+
142
+ /**
143
+ * Creates `Tweens` for the {@link PlacementDefinition} configured for this {@link PlacementAnimation}.
144
+ */
145
+ protected createTweens() {
146
+ for (const property in this._placementDefinition) {
147
+ const propertyValue = get(this._placementDefinition, property);
148
+ const tweenVars: object = {};
149
+ let _mutable = this.mutable;
150
+ if (propertyValue instanceof Vector3) {
151
+ // GSAP does not support mutation of complex objects, so we are forced
152
+ // to use the properties on the object as mutable directly and mutate
153
+ // the coordinates separately.
154
+ _mutable = get(this.mutable, property);
155
+ merge(tweenVars, { x: propertyValue.x, y: propertyValue.y, z: propertyValue.z });
156
+ } else {
157
+ set(tweenVars, property, propertyValue);
158
+ }
159
+ this._timeline.to(_mutable, tweenVars, 0); // Note: position 0 does the "parallel" stuff here
160
+ }
161
+ }
162
+ }