@geoblocks/cesium-sphere-camera 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,29 @@
1
+ BSD 3-Clause License
2
+
3
+ Copyright (c) 2020-present, camptocamp SA
4
+ All rights reserved.
5
+
6
+ Redistribution and use in source and binary forms, with or without
7
+ modification, are permitted provided that the following conditions are met:
8
+
9
+ 1. Redistributions of source code must retain the above copyright notice, this
10
+ list of conditions and the following disclaimer.
11
+
12
+ 2. Redistributions in binary form must reproduce the above copyright notice,
13
+ this list of conditions and the following disclaimer in the documentation
14
+ and/or other materials provided with the distribution.
15
+
16
+ 3. Neither the name of the copyright holder nor the names of its
17
+ contributors may be used to endorse or promote products derived from
18
+ this software without specific prior written permission.
19
+
20
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1,141 @@
1
+ import {
2
+ ScreenSpaceEventType,
3
+ Math as CesiumMath,
4
+ Cartesian3,
5
+ Cartesian2,
6
+ Ray,
7
+ } from "cesium";
8
+
9
+ const look3DStartPos = new Cartesian2();
10
+ const look3DEndPos = new Cartesian2();
11
+ const look3DStartRay = new Ray();
12
+ const look3DEndRay = new Ray();
13
+ const look3DNegativeRot = new Cartesian3();
14
+ const look3DTan = new Cartesian3();
15
+
16
+ export default class CesiumSphereCamera {
17
+ /**
18
+ * @param {import('cesium').Viewer} viewer
19
+ */
20
+ constructor(viewer) {
21
+ this.viewer = viewer;
22
+ this.dragging_ = false;
23
+ this.active_ = false;
24
+
25
+ this.handleDownEvent_ = this.handleDownEvent.bind(this);
26
+ this.handleMoveEvent_ = this.handleMoveEvent.bind(this);
27
+ this.handleUpEvent_ = this.handleUpEvent.bind(this);
28
+ }
29
+
30
+ get active() {
31
+ return this.active_;
32
+ }
33
+
34
+ set active(active) {
35
+ if (active === this.active_) {
36
+ return;
37
+ }
38
+ this.active_ = active;
39
+ this.viewer.scene.screenSpaceCameraController.enableInputs = !active;
40
+ if (active) {
41
+ this.viewer.screenSpaceEventHandler.setInputAction(this.handleDownEvent_, ScreenSpaceEventType.LEFT_DOWN);
42
+ this.viewer.screenSpaceEventHandler.setInputAction(this.handleMoveEvent_, ScreenSpaceEventType.MOUSE_MOVE);
43
+ this.viewer.screenSpaceEventHandler.setInputAction(this.handleUpEvent_, ScreenSpaceEventType.LEFT_UP);
44
+ } else {
45
+ this.viewer.screenSpaceEventHandler.removeInputAction(ScreenSpaceEventType.LEFT_DOWN);
46
+ this.viewer.screenSpaceEventHandler.removeInputAction(ScreenSpaceEventType.MOUSE_MOVE);
47
+ this.viewer.screenSpaceEventHandler.removeInputAction(ScreenSpaceEventType.LEFT_UP);
48
+ }
49
+ }
50
+
51
+ handleDownEvent() {
52
+ this.dragging_ = true;
53
+ }
54
+
55
+ /**
56
+ * Code from look3D function in ScreenSpaceCameraController
57
+ * @param {import('cesium').ScreenSpaceEventHandler.MotionEvent} movement
58
+ */
59
+ handleMoveEvent(movement) {
60
+ if (!this.dragging_) {
61
+ return;
62
+ }
63
+ const camera = this.viewer.camera;
64
+
65
+ // x axis
66
+
67
+ let angle = 0.0;
68
+ look3DStartPos.x = movement.startPosition.x;
69
+ look3DStartPos.y = 0.0;
70
+ look3DEndPos.x = movement.endPosition.x;
71
+ look3DEndPos.y = 0.0;
72
+
73
+ let startDirection = camera.getPickRay(look3DStartPos, look3DStartRay).direction;
74
+ let endDirection = camera.getPickRay(look3DEndPos, look3DEndRay).direction;
75
+
76
+ let dot = Cartesian3.dot(startDirection, endDirection);
77
+ if (dot < 1.0) {
78
+ // dot is in [0, 1]
79
+ angle = Math.acos(dot);
80
+ }
81
+ angle = movement.startPosition.x > movement.endPosition.x ? -angle : angle;
82
+ const rotationAxis =
83
+ this.viewer.scene.globe.ellipsoid.geodeticSurfaceNormal(camera.position);
84
+ camera.look(rotationAxis, -angle);
85
+
86
+ // y axis
87
+
88
+ look3DStartPos.x = 0.0;
89
+ look3DStartPos.y = movement.startPosition.y;
90
+ look3DEndPos.x = 0.0;
91
+ look3DEndPos.y = movement.endPosition.y;
92
+
93
+ startDirection = camera.getPickRay(look3DStartPos, look3DStartRay).direction;
94
+ endDirection = camera.getPickRay(look3DEndPos, look3DEndRay).direction;
95
+ angle = 0.0;
96
+
97
+ dot = Cartesian3.dot(startDirection, endDirection);
98
+ if (dot < 1.0) {
99
+ // dot is in [0, 1]
100
+ angle = Math.acos(dot);
101
+ }
102
+ angle = movement.startPosition.y > movement.endPosition.y ? -angle : angle;
103
+ const direction = camera.direction;
104
+ const negativeRotationAxis = Cartesian3.negate(
105
+ rotationAxis,
106
+ look3DNegativeRot
107
+ );
108
+ const northParallel = Cartesian3.equalsEpsilon(
109
+ direction,
110
+ rotationAxis,
111
+ CesiumMath.EPSILON2
112
+ );
113
+ const southParallel = Cartesian3.equalsEpsilon(
114
+ direction,
115
+ negativeRotationAxis,
116
+ CesiumMath.EPSILON2
117
+ );
118
+ if (!northParallel && !southParallel) {
119
+ dot = Cartesian3.dot(direction, rotationAxis);
120
+ let angleToAxis = CesiumMath.acosClamped(dot);
121
+ if (angle > 0 && angle > angleToAxis) {
122
+ angle = angleToAxis - CesiumMath.EPSILON4;
123
+ }
124
+
125
+ dot = Cartesian3.dot(direction, negativeRotationAxis);
126
+ angleToAxis = CesiumMath.acosClamped(dot);
127
+ if (angle < 0 && -angle > angleToAxis) {
128
+ angle = -angleToAxis + CesiumMath.EPSILON4;
129
+ }
130
+
131
+ const tangent = Cartesian3.cross(rotationAxis, direction, look3DTan);
132
+ camera.look(tangent, angle);
133
+ } else if ((northParallel && angle < 0) || (southParallel && angle > 0)) {
134
+ camera.look(camera.right, -angle);
135
+ }
136
+ }
137
+
138
+ handleUpEvent() {
139
+ this.dragging_ = false;
140
+ }
141
+ }
package/package.json ADDED
@@ -0,0 +1,19 @@
1
+ {
2
+ "name": "@geoblocks/cesium-sphere-camera",
3
+ "version": "0.0.1",
4
+ "license": "BSD-3-Clause",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "https://github.com/geoblocks/cesium-helpers/",
8
+ "directory": "packages/cesium-sphere-camera"
9
+ },
10
+ "main": "cesium-sphere-camera.js",
11
+ "module": "cesium-sphere-camera.js",
12
+ "publishConfig": {
13
+ "access": "public"
14
+ },
15
+ "peerDependencies": {
16
+ "cesium": "1.x"
17
+ },
18
+ "gitHead": "c08927b0e94ebe8dc39c1e2cf685e7d481ebdfb8"
19
+ }