@combeenation/3d-viewer 13.0.1 → 14.0.1-rc1

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 (72) hide show
  1. package/README.md +9 -9
  2. package/dist/lib-cjs/buildinfo.json +3 -3
  3. package/dist/lib-cjs/commonjs.tsconfig.tsbuildinfo +1 -1
  4. package/dist/lib-cjs/index.d.ts +62 -63
  5. package/dist/lib-cjs/index.js +94 -95
  6. package/dist/lib-cjs/index.js.map +1 -1
  7. package/dist/lib-cjs/internal/cbn-custom-babylon-loader-plugin.d.ts +10 -10
  8. package/dist/lib-cjs/internal/cbn-custom-babylon-loader-plugin.js +131 -131
  9. package/dist/lib-cjs/internal/cloning-helper.d.ts +19 -19
  10. package/dist/lib-cjs/internal/cloning-helper.js +163 -163
  11. package/dist/lib-cjs/internal/device-helper.d.ts +9 -9
  12. package/dist/lib-cjs/internal/device-helper.js +24 -24
  13. package/dist/lib-cjs/internal/geometry-helper.d.ts +21 -21
  14. package/dist/lib-cjs/internal/geometry-helper.js +145 -145
  15. package/dist/lib-cjs/internal/metadata-helper.d.ts +26 -26
  16. package/dist/lib-cjs/internal/metadata-helper.js +50 -50
  17. package/dist/lib-cjs/internal/paintable-helper.d.ts +40 -40
  18. package/dist/lib-cjs/internal/paintable-helper.js +286 -286
  19. package/dist/lib-cjs/internal/tags-helper.d.ts +12 -12
  20. package/dist/lib-cjs/internal/tags-helper.js +37 -37
  21. package/dist/lib-cjs/manager/camera-manager.d.ts +110 -51
  22. package/dist/lib-cjs/manager/camera-manager.js +206 -153
  23. package/dist/lib-cjs/manager/camera-manager.js.map +1 -1
  24. package/dist/lib-cjs/manager/debug-manager.d.ts +60 -60
  25. package/dist/lib-cjs/manager/debug-manager.js +217 -217
  26. package/dist/lib-cjs/manager/event-manager.d.ts +52 -52
  27. package/dist/lib-cjs/manager/event-manager.js +71 -71
  28. package/dist/lib-cjs/manager/gltf-export-manager.d.ts +84 -75
  29. package/dist/lib-cjs/manager/gltf-export-manager.js +290 -278
  30. package/dist/lib-cjs/manager/gltf-export-manager.js.map +1 -1
  31. package/dist/lib-cjs/manager/material-manager.d.ts +35 -35
  32. package/dist/lib-cjs/manager/material-manager.js +125 -125
  33. package/dist/lib-cjs/manager/model-manager.d.ts +145 -145
  34. package/dist/lib-cjs/manager/model-manager.js +382 -382
  35. package/dist/lib-cjs/manager/parameter-manager.d.ts +210 -210
  36. package/dist/lib-cjs/manager/parameter-manager.js +514 -514
  37. package/dist/lib-cjs/manager/scene-manager.d.ts +45 -45
  38. package/dist/lib-cjs/manager/scene-manager.js +64 -64
  39. package/dist/lib-cjs/manager/texture-manager.d.ts +12 -12
  40. package/dist/lib-cjs/manager/texture-manager.js +43 -43
  41. package/dist/lib-cjs/viewer-error.d.ts +48 -48
  42. package/dist/lib-cjs/viewer-error.js +60 -60
  43. package/dist/lib-cjs/viewer.d.ts +115 -117
  44. package/dist/lib-cjs/viewer.js +217 -221
  45. package/dist/lib-cjs/viewer.js.map +1 -1
  46. package/package.json +91 -91
  47. package/src/buildinfo.json +3 -3
  48. package/src/dev.ts +47 -47
  49. package/src/global-types.d.ts +39 -39
  50. package/src/index.ts +81 -82
  51. package/src/internal/cbn-custom-babylon-loader-plugin.ts +159 -159
  52. package/src/internal/cloning-helper.ts +225 -225
  53. package/src/internal/device-helper.ts +25 -25
  54. package/src/internal/geometry-helper.ts +181 -181
  55. package/src/internal/metadata-helper.ts +63 -63
  56. package/src/internal/paintable-helper.ts +310 -310
  57. package/src/internal/tags-helper.ts +41 -41
  58. package/src/manager/camera-manager.ts +365 -236
  59. package/src/manager/debug-manager.ts +245 -245
  60. package/src/manager/event-manager.ts +72 -72
  61. package/src/manager/gltf-export-manager.ts +357 -341
  62. package/src/manager/material-manager.ts +135 -135
  63. package/src/manager/model-manager.ts +458 -458
  64. package/src/manager/parameter-manager.ts +652 -652
  65. package/src/manager/scene-manager.ts +101 -101
  66. package/src/manager/texture-manager.ts +32 -32
  67. package/src/viewer-error.ts +68 -68
  68. package/src/viewer.ts +290 -296
  69. package/dist/lib-cjs/manager/screenshot-manager.d.ts +0 -36
  70. package/dist/lib-cjs/manager/screenshot-manager.js +0 -40
  71. package/dist/lib-cjs/manager/screenshot-manager.js.map +0 -1
  72. package/src/manager/screenshot-manager.ts +0 -59
@@ -1,236 +1,365 @@
1
- import {
2
- Animation,
3
- AnimationGroup,
4
- ArcRotateCamera,
5
- ExcludedGeometryList,
6
- Vector3,
7
- Viewer,
8
- ViewerEvent,
9
- } from '../index';
10
-
11
- export type CameraPosition = {
12
- alpha: number;
13
- beta: number;
14
- radius: number;
15
- target: Vector3;
16
- };
17
-
18
- export type AutofocusSettings = {
19
- /** Can be used to customize the margins shown around the 3d model. Defaults to 1. */
20
- radiusFactor?: number;
21
- adjustWheelPrecision?: boolean;
22
- adjustPanningSensibility?: boolean;
23
- adjustPinchPrecision?: boolean;
24
- /** Desired horizontal camera angle, this won't be overwritten by the autofocus function */
25
- alpha?: number;
26
- /** Desired vertical camera angle, this won't be overwritten by the autofocus function */
27
- beta?: number;
28
- /** Optional list of geometry to be excluded from consideration */
29
- exclude?: ExcludedGeometryList;
30
- durationMs?: number;
31
- };
32
-
33
- /**
34
- * Manager for camera related tasks
35
- */
36
- export class CameraManager {
37
- public static readonly CAMERA_ANIMATION_NAME = '__cameraAnimation__';
38
-
39
- /** @internal */
40
- public static readonly DEFAULT_CAMERA_POSITION: CameraPosition = {
41
- alpha: (45 * Math.PI) / 180,
42
- beta: (75 * Math.PI) / 180,
43
- radius: 1,
44
- target: new Vector3(0, 0, 0),
45
- };
46
-
47
- protected static readonly _AUTOFOCUS_CONSTANTS = {
48
- minZ: 100,
49
- wheelPrecision: 100,
50
- panningSensibility: 2500,
51
- pinchPrecision: 200,
52
- };
53
-
54
- protected static readonly _DEFAULT_AUTOFOCUS_RADIUS_FACTOR = 1;
55
- protected static readonly _DEFAULT_CAM_SPEED_MS = 250;
56
-
57
- /** @internal */
58
- public constructor(protected viewer: Viewer) {}
59
-
60
- /**
61
- * Focuses the camera to see every visible mesh in scene and tries to optimize wheel precision and panning
62
- */
63
- public async autofocusActiveCamera(settings?: AutofocusSettings): Promise<void> {
64
- const activeCamera = this.viewer.scene.activeCamera;
65
-
66
- if (!(activeCamera instanceof ArcRotateCamera)) {
67
- throw new Error(`Camera type "${activeCamera?.getClassName()}" is not implemented for "autofocusActiveCamera".`);
68
- }
69
-
70
- // get bounding box of all visible meshes, this is the base for the autofocus algorithm
71
- const boundingInfo = this.viewer.calculateBoundingInfo(settings?.exclude);
72
- // optionally show bounding sphere for debugging purpose
73
- this.viewer.eventManager.fireEvent(ViewerEvent.AutofocusStart, boundingInfo.boundingSphere);
74
-
75
- const radius = boundingInfo.boundingSphere.radius;
76
- const center = boundingInfo.boundingSphere.center;
77
-
78
- // set lower radius limit on edge of bounding sphere to make sure that we can't dive into the meshes
79
- activeCamera.lowerRadiusLimit = radius;
80
-
81
- // additional settings
82
- // constants for division are taken directly from Babylon.js repository
83
- activeCamera.minZ = Math.min(radius / CameraManager._AUTOFOCUS_CONSTANTS.minZ, 1);
84
- if (settings?.adjustWheelPrecision !== false) {
85
- activeCamera.wheelPrecision = CameraManager._AUTOFOCUS_CONSTANTS.wheelPrecision / radius;
86
- }
87
- if (settings?.adjustPanningSensibility !== false) {
88
- activeCamera.panningSensibility = CameraManager._AUTOFOCUS_CONSTANTS.panningSensibility / radius;
89
- }
90
- if (settings?.adjustPinchPrecision !== false) {
91
- activeCamera.pinchPrecision = CameraManager._AUTOFOCUS_CONSTANTS.pinchPrecision / radius;
92
- }
93
-
94
- const radiusFactor = settings?.radiusFactor ?? CameraManager._DEFAULT_AUTOFOCUS_RADIUS_FACTOR;
95
- const alpha = settings?.alpha ?? CameraManager.DEFAULT_CAMERA_POSITION.alpha;
96
- const beta = settings?.beta ?? CameraManager.DEFAULT_CAMERA_POSITION.beta;
97
-
98
- // calculation is taken from Babylon.js repository, which on their side took it from this SO post:
99
- // http://stackoverflow.com/questions/2866350/move-camera-to-fit-3d-scene
100
- // there's a good sketch which explains the calculation quite a bit
101
- const aspectRatio = this.viewer.engine.getAspectRatio(activeCamera);
102
- const frustumSlopeY = Math.tan(activeCamera.fov / 2);
103
- const frustumSlopeX = frustumSlopeY * aspectRatio;
104
-
105
- const distanceForHorizontalFrustum = radius * Math.sqrt(1 + 1 / (frustumSlopeX * frustumSlopeX));
106
- const distanceForVerticalFrustum = radius * Math.sqrt(1 + 1 / (frustumSlopeY * frustumSlopeY));
107
- const distance = Math.max(distanceForHorizontalFrustum, distanceForVerticalFrustum);
108
-
109
- const newCameraPosition: CameraPosition = {
110
- alpha: alpha,
111
- beta: beta,
112
- radius: distance * radiusFactor,
113
- target: center,
114
- };
115
-
116
- await this.moveActiveCameraTo(newCameraPosition, settings?.durationMs);
117
- }
118
-
119
- /**
120
- * Moves the active camera smoothly to certain position
121
- *
122
- * @param durationMs defaults to 250ms if unset, use 0 for instant movement
123
- */
124
- public async moveActiveCameraTo(
125
- position: CameraPosition,
126
- durationMs = CameraManager._DEFAULT_CAM_SPEED_MS
127
- ): Promise<void> {
128
- const activeCamera = this.viewer.scene.activeCamera;
129
-
130
- if (!(activeCamera instanceof ArcRotateCamera)) {
131
- throw new Error(`Camera type "${activeCamera?.getClassName()}" is not implemented yet for "moveActiveCameraTo".`);
132
- }
133
-
134
- if (durationMs === 0) {
135
- // instant movement without animation
136
- activeCamera.alpha = position.alpha ?? activeCamera.alpha;
137
- activeCamera.beta = position.beta ?? activeCamera.beta;
138
- activeCamera.radius = position.radius ?? activeCamera.radius;
139
- activeCamera.target = position.target ?? activeCamera.target;
140
- }
141
-
142
- const framesPerSec = 100;
143
- const animationGroup = new AnimationGroup(CameraManager.CAMERA_ANIMATION_NAME, this.viewer.scene);
144
-
145
- if (position.alpha !== undefined) {
146
- // transform the target's alpha value into the same turn as the current camera position to avoid movements
147
- // > 360 degrees
148
- const alphaGap = activeCamera.alpha - position.alpha;
149
- const cntTurns = Math.round(alphaGap / (2 * Math.PI));
150
- const targetAlpha = position.alpha + 2 * Math.PI * cntTurns;
151
-
152
- CameraManager._addCameraAnimationToGroup(
153
- 'alpha',
154
- activeCamera.alpha,
155
- targetAlpha,
156
- framesPerSec,
157
- Animation.ANIMATIONTYPE_FLOAT,
158
- animationGroup,
159
- activeCamera
160
- );
161
- }
162
- if (position.beta !== undefined) {
163
- CameraManager._addCameraAnimationToGroup(
164
- 'beta',
165
- activeCamera.beta,
166
- position.beta,
167
- framesPerSec,
168
- Animation.ANIMATIONTYPE_FLOAT,
169
- animationGroup,
170
- activeCamera
171
- );
172
- }
173
- if (position.radius !== undefined) {
174
- CameraManager._addCameraAnimationToGroup(
175
- 'radius',
176
- activeCamera.radius,
177
- position.radius,
178
- framesPerSec,
179
- Animation.ANIMATIONTYPE_FLOAT,
180
- animationGroup,
181
- activeCamera
182
- );
183
- }
184
- if (position.target !== undefined) {
185
- CameraManager._addCameraAnimationToGroup(
186
- 'target',
187
- activeCamera.target,
188
- position.target,
189
- framesPerSec,
190
- Animation.ANIMATIONTYPE_VECTOR3,
191
- animationGroup,
192
- activeCamera
193
- );
194
- }
195
-
196
- // animation is created for 1 second (100 frames), adjust speed ratio according to desired movement time
197
- const speedRatio = 1000 / durationMs;
198
-
199
- return new Promise<void>(resolve => {
200
- animationGroup.onAnimationGroupEndObservable.addOnce(() => {
201
- animationGroup.dispose();
202
- resolve();
203
- });
204
- animationGroup.start(false, speedRatio, 0, 100);
205
- });
206
- }
207
-
208
- protected static _addCameraAnimationToGroup(
209
- targetProperty: string,
210
- from: number | Vector3,
211
- to: number | Vector3,
212
- framesPerSec: number,
213
- dataType: number,
214
- animationGroup: AnimationGroup,
215
- activeCamera: ArcRotateCamera
216
- ): void {
217
- const alphaAnimation = new Animation(
218
- targetProperty,
219
- targetProperty,
220
- framesPerSec,
221
- dataType,
222
- Animation.ANIMATIONLOOPMODE_CONSTANT
223
- );
224
- alphaAnimation.setKeys([
225
- {
226
- frame: 0,
227
- value: from,
228
- },
229
- {
230
- frame: 100,
231
- value: to,
232
- },
233
- ]);
234
- animationGroup.addTargetedAnimation(alphaAnimation, activeCamera);
235
- }
236
- }
1
+ import {
2
+ Animation,
3
+ AnimationGroup,
4
+ ArcRotateCamera,
5
+ BoundingSphere,
6
+ ExcludedGeometryList,
7
+ IScreenshotSize,
8
+ ScreenshotTools,
9
+ Vector3,
10
+ Viewer,
11
+ ViewerEvent,
12
+ } from '../index';
13
+ import { isNodeExcluded } from '../internal/geometry-helper';
14
+
15
+ export type CameraPosition = {
16
+ alpha: number;
17
+ beta: number;
18
+ radius: number;
19
+ target: Vector3;
20
+ };
21
+
22
+ export type AutofocusSettings = {
23
+ /** Can be used to customize the margins shown around the 3d model. Defaults to 1. */
24
+ radiusFactor?: number;
25
+ adjustWheelPrecision?: boolean;
26
+ adjustPanningSensibility?: boolean;
27
+ adjustPinchPrecision?: boolean;
28
+ /** Desired horizontal camera angle, this won't be overwritten by the autofocus function */
29
+ alpha?: number;
30
+ /** Desired vertical camera angle, this won't be overwritten by the autofocus function */
31
+ beta?: number;
32
+ /** Optional list of geometry to be excluded from consideration */
33
+ exclude?: ExcludedGeometryList;
34
+ durationMs?: number;
35
+ };
36
+
37
+ export type ScreenshotSettings = {
38
+ /** Width of the final screenshot image, defaults to canvas width */
39
+ width?: number;
40
+ /** Height of the final screenshot image, defaults to canvas height */
41
+ height?: number;
42
+ /** Camera alpha angle used for the screenshot, defaults to alpha angle of currently active camera */
43
+ alpha?: number;
44
+ /** Camera beta angle used for the screenshot, defaults to beta angle of currently active camera */
45
+ beta?: number;
46
+ /**
47
+ * Camera radius used for the screenshot, default value depends on `autofocusScene` flag:
48
+ * - `true`: radius is calculated automatically so that the whole current scene content is visible
49
+ * - `false`: radius of currently active camera is used
50
+ */
51
+ radius?: number;
52
+ /**
53
+ * Camera target used for the screenshot, default value depends on `autofocusScene` flag:
54
+ * - `true`: center of the current scene content is set as camera target
55
+ * - `false`: target of currently active camera is used
56
+ */
57
+ target?: Vector3;
58
+ /** Used to scale the calculated radius if `radius` property is not set and `autofocusScene` mode is active */
59
+ radiusFactor?: number;
60
+ /** Can be used to automatically calculate camera `radius` and `target`, if these 2 settings are not defined
61
+ * explicitely */
62
+ autofocusScene?: boolean;
63
+ /** Optional list of geometry to be excluded from consideration */
64
+ exclude?: ExcludedGeometryList;
65
+ /**
66
+ * "MIME type" of the returned screenshot image, defaults to `image/png`
67
+ *
68
+ * **Info regarding JPEG:** \
69
+ * Use mimeType `image/jpeg` (**not** `image/jpg`) when creating jpeg's. \
70
+ * Also ensure that the viewer scenes `clearColor` has an alpha value of `1` as jpeg's don't
71
+ * support transparency. Otherwise background will always be black for jpeg's.
72
+ */
73
+ mimeType?: string;
74
+ /** If file name is given, the screenshot image will be downloaded and the base64 string will NOT be returned! */
75
+ fileName?: string;
76
+ /** Expert settings to tweak the screenshot image, see [Babylon.js](https://doc.babylonjs.com/typedoc/functions/BABYLON.CreateScreenshotUsingRenderTarget) docs for further information */
77
+ samples?: number;
78
+ /** Expert settings to tweak the screenshot image, see [Babylon.js](https://doc.babylonjs.com/typedoc/functions/BABYLON.CreateScreenshotUsingRenderTarget) docs for further information */
79
+ antialiasing?: boolean;
80
+ /** Expert settings to tweak the screenshot image, see [Babylon.js](https://doc.babylonjs.com/typedoc/functions/BABYLON.CreateScreenshotUsingRenderTarget) docs for further information */
81
+ renderSprites?: boolean;
82
+ /** Expert settings to tweak the screenshot image, see [Babylon.js](https://doc.babylonjs.com/typedoc/functions/BABYLON.CreateScreenshotUsingRenderTarget) docs for further information */
83
+ enableStencilBuffer?: boolean;
84
+ /** Expert settings to tweak the screenshot image, see [Babylon.js](https://doc.babylonjs.com/typedoc/functions/BABYLON.CreateScreenshotUsingRenderTarget) docs for further information */
85
+ useLayerMask?: boolean;
86
+ /** Expert settings to tweak the screenshot image, see [Babylon.js](https://doc.babylonjs.com/typedoc/functions/BABYLON.CreateScreenshotUsingRenderTarget) docs for further information */
87
+ quality?: number;
88
+ };
89
+
90
+ /**
91
+ * Manager for camera related tasks
92
+ */
93
+ export class CameraManager {
94
+ public static readonly CAMERA_ANIMATION_NAME = '__cameraAnimation__';
95
+
96
+ /** @internal */
97
+ public static readonly DEFAULT_CAMERA_POSITION: CameraPosition = {
98
+ alpha: (45 * Math.PI) / 180,
99
+ beta: (75 * Math.PI) / 180,
100
+ radius: 1,
101
+ target: new Vector3(0, 0, 0),
102
+ };
103
+
104
+ protected static readonly _AUTOFOCUS_CONSTANTS = {
105
+ minZ: 100,
106
+ wheelPrecision: 100,
107
+ panningSensibility: 2500,
108
+ pinchPrecision: 200,
109
+ };
110
+
111
+ protected static readonly _DEFAULT_AUTOFOCUS_RADIUS_FACTOR = 1;
112
+ protected static readonly _DEFAULT_CAM_SPEED_MS = 250;
113
+ protected static readonly _SCREENSHOT_CAMERA_NAME = '__screenshotCamera__';
114
+
115
+ /** @internal */
116
+ public constructor(protected viewer: Viewer) {}
117
+
118
+ /**
119
+ * Focuses the camera to see every visible mesh in scene and tries to optimize wheel precision and panning
120
+ */
121
+ public async autofocusActiveCamera(settings?: AutofocusSettings): Promise<void> {
122
+ const activeCamera = this.viewer.scene.activeCamera;
123
+
124
+ if (!(activeCamera instanceof ArcRotateCamera)) {
125
+ throw new Error(`Camera type "${activeCamera?.getClassName()}" is not implemented for "autofocusActiveCamera".`);
126
+ }
127
+
128
+ // get bounding box of all visible meshes, this is the base for the autofocus algorithm
129
+ const boundingInfo = this.viewer.calculateBoundingInfo(settings?.exclude);
130
+ // optionally show bounding sphere for debugging purpose
131
+ this.viewer.eventManager.fireEvent(ViewerEvent.AutofocusStart, boundingInfo.boundingSphere);
132
+
133
+ const distance = this._getAutofocusZoomingDistance(boundingInfo.boundingSphere, activeCamera);
134
+ const radius = boundingInfo.boundingSphere.radius;
135
+ const center = boundingInfo.boundingSphere.center;
136
+
137
+ // set lower radius limit on edge of bounding sphere to make sure that we can't dive into the meshes
138
+ activeCamera.lowerRadiusLimit = radius;
139
+
140
+ // additional settings
141
+ // constants for division are taken directly from Babylon.js repository
142
+ activeCamera.minZ = Math.min(radius / CameraManager._AUTOFOCUS_CONSTANTS.minZ, 1);
143
+ if (settings?.adjustWheelPrecision !== false) {
144
+ activeCamera.wheelPrecision = CameraManager._AUTOFOCUS_CONSTANTS.wheelPrecision / radius;
145
+ }
146
+ if (settings?.adjustPanningSensibility !== false) {
147
+ activeCamera.panningSensibility = CameraManager._AUTOFOCUS_CONSTANTS.panningSensibility / radius;
148
+ }
149
+ if (settings?.adjustPinchPrecision !== false) {
150
+ activeCamera.pinchPrecision = CameraManager._AUTOFOCUS_CONSTANTS.pinchPrecision / radius;
151
+ }
152
+
153
+ const radiusFactor = settings?.radiusFactor ?? CameraManager._DEFAULT_AUTOFOCUS_RADIUS_FACTOR;
154
+ const alpha = settings?.alpha ?? CameraManager.DEFAULT_CAMERA_POSITION.alpha;
155
+ const beta = settings?.beta ?? CameraManager.DEFAULT_CAMERA_POSITION.beta;
156
+
157
+ const newCameraPosition: CameraPosition = {
158
+ alpha: alpha,
159
+ beta: beta,
160
+ radius: distance * radiusFactor,
161
+ target: center,
162
+ };
163
+
164
+ await this.moveActiveCameraTo(newCameraPosition, settings?.durationMs);
165
+ }
166
+
167
+ /**
168
+ * Moves the active camera smoothly to certain position
169
+ *
170
+ * @param durationMs defaults to 250ms if unset, use 0 for instant movement
171
+ */
172
+ public async moveActiveCameraTo(
173
+ position: CameraPosition,
174
+ durationMs = CameraManager._DEFAULT_CAM_SPEED_MS
175
+ ): Promise<void> {
176
+ const activeCamera = this.viewer.scene.activeCamera;
177
+
178
+ if (!(activeCamera instanceof ArcRotateCamera)) {
179
+ throw new Error(`Camera type "${activeCamera?.getClassName()}" is not implemented yet for "moveActiveCameraTo".`);
180
+ }
181
+
182
+ if (durationMs === 0) {
183
+ // instant movement without animation
184
+ activeCamera.alpha = position.alpha ?? activeCamera.alpha;
185
+ activeCamera.beta = position.beta ?? activeCamera.beta;
186
+ activeCamera.radius = position.radius ?? activeCamera.radius;
187
+ activeCamera.target = position.target ?? activeCamera.target;
188
+ }
189
+
190
+ const framesPerSec = 100;
191
+ const animationGroup = new AnimationGroup(CameraManager.CAMERA_ANIMATION_NAME, this.viewer.scene);
192
+
193
+ if (position.alpha !== undefined) {
194
+ // transform the target's alpha value into the same turn as the current camera position to avoid movements
195
+ // > 360 degrees
196
+ const alphaGap = activeCamera.alpha - position.alpha;
197
+ const cntTurns = Math.round(alphaGap / (2 * Math.PI));
198
+ const targetAlpha = position.alpha + 2 * Math.PI * cntTurns;
199
+
200
+ CameraManager._addCameraAnimationToGroup(
201
+ 'alpha',
202
+ activeCamera.alpha,
203
+ targetAlpha,
204
+ framesPerSec,
205
+ Animation.ANIMATIONTYPE_FLOAT,
206
+ animationGroup,
207
+ activeCamera
208
+ );
209
+ }
210
+ if (position.beta !== undefined) {
211
+ CameraManager._addCameraAnimationToGroup(
212
+ 'beta',
213
+ activeCamera.beta,
214
+ position.beta,
215
+ framesPerSec,
216
+ Animation.ANIMATIONTYPE_FLOAT,
217
+ animationGroup,
218
+ activeCamera
219
+ );
220
+ }
221
+ if (position.radius !== undefined) {
222
+ CameraManager._addCameraAnimationToGroup(
223
+ 'radius',
224
+ activeCamera.radius,
225
+ position.radius,
226
+ framesPerSec,
227
+ Animation.ANIMATIONTYPE_FLOAT,
228
+ animationGroup,
229
+ activeCamera
230
+ );
231
+ }
232
+ if (position.target !== undefined) {
233
+ CameraManager._addCameraAnimationToGroup(
234
+ 'target',
235
+ activeCamera.target,
236
+ position.target,
237
+ framesPerSec,
238
+ Animation.ANIMATIONTYPE_VECTOR3,
239
+ animationGroup,
240
+ activeCamera
241
+ );
242
+ }
243
+
244
+ // animation is created for 1 second (100 frames), adjust speed ratio according to desired movement time
245
+ const speedRatio = 1000 / durationMs;
246
+
247
+ return new Promise<void>(resolve => {
248
+ animationGroup.onAnimationGroupEndObservable.addOnce(() => {
249
+ animationGroup.dispose();
250
+ resolve();
251
+ });
252
+ animationGroup.start(false, speedRatio, 0, 100);
253
+ });
254
+ }
255
+
256
+ /**
257
+ * Takes a sceenshot the the current scene.\
258
+ * The result is a string containing a base64 encoded image.
259
+ */
260
+ public async createScreenshot(settings?: ScreenshotSettings): Promise<string> {
261
+ const screenshotCam = this.viewer.scene.activeCamera?.clone(
262
+ CameraManager._SCREENSHOT_CAMERA_NAME
263
+ ) as ArcRotateCamera;
264
+ const boundingInfo = settings?.autofocusScene ? this.viewer.calculateBoundingInfo(settings.exclude) : undefined;
265
+
266
+ if (settings?.alpha !== undefined) {
267
+ screenshotCam.alpha = settings.alpha;
268
+ }
269
+ if (settings?.beta !== undefined) {
270
+ screenshotCam.beta = settings.beta;
271
+ }
272
+ if (settings?.radius !== undefined) {
273
+ screenshotCam.radius = settings.radius;
274
+ } else if (boundingInfo) {
275
+ // zoom out to have full scene in view if requested by `autofocusScene` flag
276
+ const distance = this._getAutofocusZoomingDistance(boundingInfo.boundingSphere, screenshotCam);
277
+ const radiusFactor = settings?.radiusFactor ?? 1;
278
+ screenshotCam.radius = distance * radiusFactor;
279
+ }
280
+ // `cloneAlphaBetaRadius` has to be set to true, otherwise these values get adapted by setting the new target
281
+ // this would also be the case when setting `screenshotCam.target` directly
282
+ if (settings?.target !== undefined) {
283
+ screenshotCam.setTarget(settings.target, undefined, undefined, true);
284
+ } else if (boundingInfo) {
285
+ screenshotCam.setTarget(boundingInfo.boundingSphere.center, undefined, undefined, true);
286
+ }
287
+
288
+ // don't expose internal props of `IScreenshotSize` in the API (yet), as it gets rather confusing and it shouldn't
289
+ // be necessary in the first place
290
+ // also let Babylon.js do all the fallback handling for missing properties
291
+ const size: IScreenshotSize = { width: settings?.width, height: settings?.height };
292
+
293
+ // for some reason the `customizeTexture` property is not exposed in the async version of this function, therefore
294
+ // we create the promise here manually
295
+ const imageStr = await new Promise<string>(resolve => {
296
+ ScreenshotTools.CreateScreenshotUsingRenderTarget(
297
+ this.viewer.engine,
298
+ screenshotCam,
299
+ size,
300
+ (data: string) => resolve(data),
301
+ settings?.mimeType,
302
+ settings?.samples,
303
+ settings?.antialiasing,
304
+ settings?.fileName,
305
+ settings?.renderSprites,
306
+ settings?.enableStencilBuffer,
307
+ settings?.useLayerMask,
308
+ settings?.quality,
309
+ texture => {
310
+ texture.renderList = this.viewer.scene.meshes.filter(
311
+ mesh => !settings?.exclude || !isNodeExcluded(mesh, settings.exclude)
312
+ );
313
+ }
314
+ );
315
+ });
316
+
317
+ screenshotCam.dispose();
318
+
319
+ return imageStr;
320
+ }
321
+
322
+ protected static _addCameraAnimationToGroup(
323
+ targetProperty: string,
324
+ from: number | Vector3,
325
+ to: number | Vector3,
326
+ framesPerSec: number,
327
+ dataType: number,
328
+ animationGroup: AnimationGroup,
329
+ activeCamera: ArcRotateCamera
330
+ ): void {
331
+ const alphaAnimation = new Animation(
332
+ targetProperty,
333
+ targetProperty,
334
+ framesPerSec,
335
+ dataType,
336
+ Animation.ANIMATIONLOOPMODE_CONSTANT
337
+ );
338
+ alphaAnimation.setKeys([
339
+ {
340
+ frame: 0,
341
+ value: from,
342
+ },
343
+ {
344
+ frame: 100,
345
+ value: to,
346
+ },
347
+ ]);
348
+ animationGroup.addTargetedAnimation(alphaAnimation, activeCamera);
349
+ }
350
+
351
+ protected _getAutofocusZoomingDistance(boundingSphere: BoundingSphere, camera: ArcRotateCamera): number {
352
+ // calculation is taken from Babylon.js repository, which on their side took it from this SO post:
353
+ // http://stackoverflow.com/questions/2866350/move-camera-to-fit-3d-scene
354
+ // there's a good sketch which explains the calculation quite a bit
355
+ const aspectRatio = this.viewer.engine.getAspectRatio(camera);
356
+ const frustumSlopeY = Math.tan(camera.fov / 2);
357
+ const frustumSlopeX = frustumSlopeY * aspectRatio;
358
+
359
+ const distanceForHorizontalFrustum = boundingSphere.radius * Math.sqrt(1 + 1 / (frustumSlopeX * frustumSlopeX));
360
+ const distanceForVerticalFrustum = boundingSphere.radius * Math.sqrt(1 + 1 / (frustumSlopeY * frustumSlopeY));
361
+ const distance = Math.max(distanceForHorizontalFrustum, distanceForVerticalFrustum);
362
+
363
+ return distance;
364
+ }
365
+ }