@openglobus/og 0.23.0 → 0.24.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 (84) hide show
  1. package/lib/@openglobus/js/Globe.d.ts +0 -1
  2. package/lib/@openglobus/js/camera/Camera.d.ts +48 -15
  3. package/lib/@openglobus/js/camera/PlanetCamera.d.ts +13 -0
  4. package/lib/@openglobus/js/control/MouseNavigation.d.ts +62 -51
  5. package/lib/@openglobus/js/control/OldMouseNavigation.d.ts +64 -0
  6. package/lib/@openglobus/js/control/index.d.ts +1 -2
  7. package/lib/@openglobus/js/entity/Entity.d.ts +24 -20
  8. package/lib/@openglobus/js/entity/GeoObject.d.ts +0 -9
  9. package/lib/@openglobus/js/input/MouseHandler.d.ts +1 -0
  10. package/lib/@openglobus/js/math/Mat3.d.ts +1 -1
  11. package/lib/@openglobus/js/math/Mat4.d.ts +10 -1
  12. package/lib/@openglobus/js/math/Plane.d.ts +3 -1
  13. package/lib/@openglobus/js/math/Quat.d.ts +1 -0
  14. package/lib/@openglobus/js/math/Ray.d.ts +6 -16
  15. package/lib/@openglobus/js/math/Vec2.d.ts +22 -0
  16. package/lib/@openglobus/js/renderer/RendererEvents.d.ts +1 -0
  17. package/lib/@openglobus/js/scene/Planet.d.ts +4 -0
  18. package/lib/@openglobus/js/webgl/Handler.d.ts +21 -12
  19. package/lib/@openglobus/og.esm.js +1 -1
  20. package/lib/@openglobus/og.esm.js.map +1 -1
  21. package/lib/@openglobus/og.umd.js +1 -1
  22. package/lib/@openglobus/og.umd.js.map +1 -1
  23. package/lib/js/Globe.d.ts +0 -1
  24. package/lib/js/Globe.js +4 -2
  25. package/lib/js/Popup.js +1 -1
  26. package/lib/js/camera/Camera.d.ts +48 -15
  27. package/lib/js/camera/Camera.js +82 -28
  28. package/lib/js/camera/PlanetCamera.d.ts +13 -0
  29. package/lib/js/camera/PlanetCamera.js +52 -5
  30. package/lib/js/control/CameraLock.js +1 -1
  31. package/lib/js/control/DebugInfo.js +30 -10
  32. package/lib/js/control/KeyboardNavigation.js +8 -8
  33. package/lib/js/control/MouseNavigation.d.ts +62 -51
  34. package/lib/js/control/MouseNavigation.js +361 -305
  35. package/lib/js/control/MouseWheelZoomControl.d.ts +0 -44
  36. package/lib/js/control/MouseWheelZoomControl.js +270 -181
  37. package/lib/js/control/OldMouseNavigation.d.ts +64 -0
  38. package/lib/js/control/OldMouseNavigation.js +368 -0
  39. package/lib/js/control/ScaleControl.js +2 -2
  40. package/lib/js/control/SimpleNavigation.js +8 -7
  41. package/lib/js/control/Sun.js +5 -5
  42. package/lib/js/control/TouchNavigation.js +11 -8
  43. package/lib/js/control/elevationProfile/ElevationProfileScene.js +2 -1
  44. package/lib/js/control/geoObjectEditor/GeoObjectEditorDialog.js +19 -33
  45. package/lib/js/control/geoObjectEditor/GeoObjectEditorScene.js +18 -18
  46. package/lib/js/control/geoObjectEditor/MoveAxisEntity.js +3 -2
  47. package/lib/js/control/geoObjectEditor/MovePlaneEntity.js +1 -4
  48. package/lib/js/control/geoObjectEditor/RotateEntity.js +1 -1
  49. package/lib/js/control/index.d.ts +1 -2
  50. package/lib/js/control/index.js +1 -2
  51. package/lib/js/control/visibleExtent/Segment.js +4 -4
  52. package/lib/js/entity/Entity.d.ts +24 -20
  53. package/lib/js/entity/Entity.js +62 -47
  54. package/lib/js/entity/GeoObject.d.ts +0 -9
  55. package/lib/js/entity/GeoObject.js +0 -3
  56. package/lib/js/entity/InstanceData.js +6 -3
  57. package/lib/js/input/MouseHandler.d.ts +1 -0
  58. package/lib/js/input/MouseHandler.js +12 -1
  59. package/lib/js/math/Mat3.d.ts +1 -1
  60. package/lib/js/math/Mat3.js +1 -1
  61. package/lib/js/math/Mat4.d.ts +10 -1
  62. package/lib/js/math/Mat4.js +17 -1
  63. package/lib/js/math/Plane.d.ts +3 -1
  64. package/lib/js/math/Plane.js +11 -1
  65. package/lib/js/math/Quat.d.ts +1 -0
  66. package/lib/js/math/Quat.js +1 -0
  67. package/lib/js/math/Ray.d.ts +6 -16
  68. package/lib/js/math/Ray.js +53 -46
  69. package/lib/js/math/Vec2.d.ts +22 -0
  70. package/lib/js/math/Vec2.js +32 -0
  71. package/lib/js/renderer/Renderer.js +10 -6
  72. package/lib/js/renderer/RendererEvents.d.ts +1 -0
  73. package/lib/js/renderer/RendererEvents.js +3 -1
  74. package/lib/js/scene/Planet.d.ts +4 -0
  75. package/lib/js/scene/Planet.js +24 -3
  76. package/lib/js/segment/Segment.js +4 -4
  77. package/lib/js/terrain/GlobusTerrain.js +2 -2
  78. package/lib/js/webgl/Handler.d.ts +21 -12
  79. package/lib/js/webgl/Handler.js +42 -33
  80. package/package.json +2 -2
  81. package/lib/@openglobus/js/control/EarthNavigation.d.ts +0 -50
  82. package/lib/@openglobus/js/control/MouseWheelZoomControl.d.ts +0 -44
  83. package/lib/js/control/EarthNavigation.d.ts +0 -50
  84. package/lib/js/control/EarthNavigation.js +0 -202
@@ -1,358 +1,414 @@
1
- import * as math from "../math";
2
1
  import { Control } from "./Control";
3
- import { input } from "../input/input";
4
- import { Key } from "../Lock";
5
- import { LonLat } from "../LonLat";
6
- import { Mat4 } from "../math/Mat4";
7
2
  import { Quat } from "../math/Quat";
8
3
  import { Ray } from "../math/Ray";
9
4
  import { Sphere } from "../bv/Sphere";
5
+ import { Vec2 } from "../math/Vec2";
10
6
  import { Vec3 } from "../math/Vec3";
11
- /**
12
- * Mouse planet camera dragging control.
13
- */
7
+ import { input } from "../input/input";
8
+ import { Plane } from "../math/Plane";
9
+ const DEFAULT_VELINERTIA = 0.96;
14
10
  export class MouseNavigation extends Control {
15
11
  constructor(options = {}) {
16
- super(options);
17
- this._deactivate = false;
12
+ super({
13
+ name: "mouseNavigation",
14
+ autoActivate: true,
15
+ ...options
16
+ });
18
17
  this._shiftBusy = false;
19
- this._name = "mouseNavigation";
20
- this.grabbedPoint = new Vec3();
21
- this._eye0 = new Vec3();
22
- this.pointOnEarth = new Vec3();
23
- this.earthUp = new Vec3();
24
- this.inertia = 0.007;
25
- this.grabbedSpheroid = new Sphere();
26
- this.qRot = new Quat();
27
- this.scaleRot = 0.0;
28
- this.distDiff = 0.3;
29
- this.stepsCount = 8;
30
- this.stepsForward = null;
31
- this.stepIndex = 0;
32
- this._lmbDoubleClickActive = true;
33
- this.minSlope = options.minSlope || 0.1;
34
- this._wheelDirection = +1;
35
- this._keyLock = new Key();
36
- }
37
- static getMovePointsFromPixelTerrain(cam, planet, stepsCount, delta, point, forward, dir) {
38
- const steps = [];
39
- let eye = cam.eye.clone(), n = cam._b.clone(), u = cam._r.clone(), v = cam._u.clone();
40
- let a = planet.getCartesianFromPixelTerrain(point);
41
- if (!a) {
42
- a = planet.getCartesianFromPixelTerrain(planet.renderer.handler.getCenter());
43
- }
44
- if (a) {
45
- if (!dir) {
46
- dir = Vec3.sub(a, cam.eye).normalize();
18
+ this._hold = false;
19
+ this._prevVel = new Vec3();
20
+ this._screenPosIsChanged = true;
21
+ this._onShiftFree = () => {
22
+ this._shiftBusy = false;
23
+ };
24
+ this._onMouseMove = (e) => {
25
+ if (this._active && this.renderer.events.isKeyPressed(input.KEY_ALT)) {
26
+ if (!this._shiftBusy) {
27
+ this._shiftBusy = true;
28
+ this._onRHold(e);
29
+ }
30
+ this._onRDown(e);
31
+ }
32
+ };
33
+ this._onMouseEnter = (e) => {
34
+ const renderEvents = this.renderer.events;
35
+ if (renderEvents.isKeyPressed(input.KEY_ALT)) {
36
+ renderEvents.releaseKeys();
47
37
  }
48
- let d = (delta * cam.eye.distance(a)) / stepsCount;
49
- if (forward) {
50
- d = -1.25 * d;
38
+ renderEvents.updateButtonsStates(e.sys.buttons);
39
+ if (renderEvents.mouseState.leftButtonDown) {
40
+ this.renderer.handler.canvas.classList.add("ogGrabbingPoiner");
51
41
  }
52
42
  else {
53
- d *= 2;
43
+ this.renderer.handler.canvas.classList.remove("ogGrabbingPoiner");
54
44
  }
55
- const scaled_n = n.scaleTo(d);
56
- const slope = dir.dot(cam.eye.normal().negate());
57
- if (slope >= 0.1) {
58
- const grabbedSpheroid = new Sphere();
59
- grabbedSpheroid.radius = a.length();
60
- let rotArr = [], eyeArr = [];
61
- let breaked = false;
62
- for (let i = 0; i < stepsCount; i++) {
63
- eye.addA(scaled_n);
64
- const b = new Ray(eye, dir).hitSphere(grabbedSpheroid);
65
- eyeArr[i] = eye.clone();
66
- if (b) {
67
- rotArr[i] = new Mat4().rotateBetweenVectors(a.normal(), b.normal());
68
- }
69
- else {
70
- breaked = true;
71
- break;
72
- }
45
+ };
46
+ this._onMouseLeave = () => {
47
+ if (this.renderer.events.mouseState.leftButtonDown) {
48
+ this.vel.scale(0);
49
+ }
50
+ this.renderer.handler.canvas.classList.remove("ogGrabbingPoiner");
51
+ };
52
+ this._onRHold = (e) => {
53
+ if (this._targetRotationPoint) {
54
+ let _noRotationInertia = false;
55
+ this._velInertia = 0.8;
56
+ this.force_h = 0.2 * (e.x - e.prev_x);
57
+ this.force_v = 0.2 * (e.y - e.prev_y);
58
+ }
59
+ };
60
+ this._onRDown = (e) => {
61
+ if (this.planet) {
62
+ this.planet.stopFlying();
63
+ this._targetRotationPoint = this._getTargetPoint(e.pos);
64
+ if (this._targetRotationPoint) {
65
+ this._targetZoomPoint = null;
66
+ this._targetDragPoint = null;
67
+ this.vel.set(0, 0, 0);
68
+ this._tUp = this._targetRotationPoint.getNormal();
69
+ this._tRad = this.planet.camera.eye.distance(this._targetRotationPoint);
73
70
  }
74
- if (!breaked) {
75
- for (let i = 0; i < stepsCount; i++) {
76
- let rot = rotArr[i];
77
- steps[i] = {
78
- eye: rot.mulVec3(eyeArr[i]),
79
- v: rot.mulVec3(v),
80
- u: rot.mulVec3(u),
81
- n: rot.mulVec3(n)
82
- };
83
- }
71
+ }
72
+ };
73
+ this._onMouseWheel = (e) => {
74
+ if (this.planet) {
75
+ this._targetRotationPoint = null;
76
+ this._targetDragPoint = null;
77
+ let _targetZoomPoint = this._getTargetPoint(e.pos);
78
+ if (!_targetZoomPoint)
79
+ return;
80
+ this._targetZoomPoint = _targetZoomPoint;
81
+ this._grabbedSphere.radius = this._targetZoomPoint.length();
82
+ this._curPitch = this.planet.camera.getPitch();
83
+ this._curYaw = this.planet.camera.getYaw();
84
+ this._curRoll = this.planet.camera.getRoll();
85
+ if (Math.sign(e.wheelDelta) !== this._wheelDirection) {
86
+ this.vel.scale(0.3);
87
+ this._currScreenPos.set(e.x, e.y);
88
+ this._wheelDirection = Math.sign(e.wheelDelta);
89
+ return;
90
+ }
91
+ //let dd = this.targetPoint!.distance(this.planet.camera.eye);
92
+ // let brk = 1;
93
+ // if (this._wheelDirection > 0 && dd < 5000) {
94
+ //this.vel.scale(0.3);
95
+ // brk = dist / 5000;
96
+ // }
97
+ this._currScreenPos.set(e.x, e.y);
98
+ this._wheelDirection = Math.sign(e.wheelDelta);
99
+ let scale = 2;
100
+ this._isTouchPad = e.isTouchPad;
101
+ if (e.isTouchPad) {
102
+ this._velInertia = 0.88;
103
+ scale = 0.5;
84
104
  }
85
105
  else {
86
- eye = cam.eye.clone();
87
- for (let i = 0; i < stepsCount; i++) {
88
- steps[i] = {
89
- eye: eye.addA(scaled_n).clone(),
90
- v: v,
91
- u: u,
92
- n: n,
93
- };
94
- }
106
+ this._velInertia = DEFAULT_VELINERTIA;
95
107
  }
108
+ let dist = this.planet.camera.eye.distance(this._targetZoomPoint) * scale;
109
+ this.force = (e.direction.scale(Math.sign(this._wheelDirection))).normalize().scale(dist);
110
+ this.force_roll = this._curRoll;
96
111
  }
97
- else {
98
- for (let i = 0; i < stepsCount; i++) {
99
- steps[i] = {
100
- eye: eye.addA(dir.scaleTo(-d)).clone(),
101
- v: v,
102
- u: u,
103
- n: n,
104
- };
105
- }
112
+ };
113
+ this._onLDown = (e) => {
114
+ this.stop();
115
+ this._targetRotationPoint = null;
116
+ this._targetZoomPoint = null;
117
+ if (!this.planet)
118
+ return;
119
+ this.planet.stopFlying();
120
+ this._grabbedPoint = this._getTargetPoint(e.pos);
121
+ if (!this._grabbedPoint)
122
+ return;
123
+ this.renderer.handler.canvas.classList.add("ogGrabbingPoiner");
124
+ this._grabbedSphere.radius = this._grabbedPoint.length();
125
+ this._eye0 = this.planet.camera.eye.clone();
126
+ this._grabbedCameraHeight = this._eye0.length();
127
+ this._curPitch = this.planet.camera.getPitch();
128
+ this._curYaw = this.planet.camera.getYaw();
129
+ this._curRoll = this.planet.camera.getRoll();
130
+ this._currScreenPos.copy(e.pos);
131
+ if (this.planet.camera.getUp().dot(new Vec3(0, 0, 1)) > 0.3) {
132
+ this.fixedUp = true;
106
133
  }
107
- return steps;
108
- }
109
- }
110
- onactivate() {
111
- if (this.renderer) {
112
- this.renderer.events.on("mousewheel", this.onMouseWheel, this);
113
- this.renderer.events.on("lhold", this.onMouseLeftButtonDown, this);
114
- this.renderer.events.on("rhold", this.onMouseRightButtonDown, this);
115
- this.renderer.events.on("ldown", this.onMouseLeftButtonClick, this);
116
- this.renderer.events.on("lup", this.onMouseLeftButtonUp, this);
117
- this.renderer.events.on("rdown", this.onMouseRightButtonClick, this);
118
- this.renderer.events.on("draw", this.onDraw, this, -1000);
119
- this.renderer.events.on("mousemove", this.onMouseMove, this);
120
- this.renderer.events.on("mouseleave", this.onMouseLeave, this);
121
- this.renderer.events.on("mouseenter", this.onMouseEnter, this);
122
- if (this._lmbDoubleClickActive) {
123
- this.renderer.events.on("ldblclick", this.onMouseLeftButtonDoubleClick, this);
134
+ };
135
+ this._onLHold = (e) => {
136
+ if (this._grabbedPoint && this.planet) {
137
+ let cam = this.planet.camera;
138
+ if (cam.slope > 0.2) {
139
+ let _targetDragPoint = new Ray(cam.eye, e.direction).hitSphere(this._grabbedSphere);
140
+ if (!_targetDragPoint) {
141
+ return;
142
+ }
143
+ this._targetDragPoint = _targetDragPoint;
144
+ let newEye = new Vec3();
145
+ let rot = Quat.getRotationBetweenVectors(this._targetDragPoint.getNormal(), this._grabbedPoint.getNormal());
146
+ newEye.copy(rot.mulVec3(cam.eye));
147
+ this.force = newEye.sub(cam.eye).scale(70);
148
+ }
149
+ else {
150
+ let p0 = this._grabbedPoint, p1 = Vec3.add(p0, cam.getRight()), p2 = Vec3.add(p0, p0.getNormal());
151
+ let px = new Vec3();
152
+ new Ray(cam.eye, e.direction).hitPlaneRes(Plane.fromPoints(p0, p1, p2), px);
153
+ let newEye = cam.eye.add(px.subA(p0).negate());
154
+ this.force = newEye.sub(cam.eye).scale(70);
155
+ this._targetDragPoint = px;
156
+ }
157
+ this.vel.set(0.0, 0.0, 0.0);
158
+ if (!this._currScreenPos.equal(e.pos)) {
159
+ this._screenPosIsChanged = true;
160
+ this._currScreenPos.copy(e.pos);
161
+ }
162
+ this._hold = true;
124
163
  }
125
- }
164
+ };
165
+ this._onLUp = (e) => {
166
+ this._hold = false;
167
+ this.renderer.handler.canvas.classList.remove("ogGrabbingPoiner");
168
+ };
169
+ this.speed = options.speed || 1.0; // m/s
170
+ this.force = new Vec3();
171
+ this.force_h = 0;
172
+ this.force_v = 0;
173
+ this.vel = new Vec3();
174
+ this.vel_h = 0;
175
+ this.vel_v = 0;
176
+ this.vel_roll = 0;
177
+ this.force_roll = 0;
178
+ this.mass = 1;
179
+ this._velInertia = DEFAULT_VELINERTIA;
180
+ this._lookPos = undefined;
181
+ this._grabbedPoint = null;
182
+ this._targetZoomPoint = null;
183
+ this._targetDragPoint = null;
184
+ this._targetRotationPoint = null;
185
+ this._tUp = new Vec3();
186
+ this._tRad = 0;
187
+ this._rotHDir = 0;
188
+ this._rotVDir = 0;
189
+ this._wheelDirection = 1;
190
+ this._currScreenPos = new Vec2();
191
+ this._grabbedSphere = new Sphere();
192
+ this.fixedUp = options.fixedUp != undefined ? options.fixedUp : true;
193
+ this._rot = new Quat();
194
+ this._curPitch = 0;
195
+ this._curYaw = 0;
196
+ this._curRoll = 0;
197
+ this._eye0 = new Vec3();
198
+ this._newEye = new Vec3();
199
+ this._grabbedCameraHeight = 0;
200
+ this._isTouchPad = false;
126
201
  }
127
- ondeactivate() {
202
+ oninit() {
128
203
  if (this.renderer) {
129
- this.renderer.events.off("mousewheel", this.onMouseWheel);
130
- this.renderer.events.off("lhold", this.onMouseLeftButtonDown);
131
- this.renderer.events.off("rhold", this.onMouseRightButtonDown);
132
- this.renderer.events.off("ldown", this.onMouseLeftButtonClick);
133
- this.renderer.events.off("lup", this.onMouseLeftButtonUp);
134
- this.renderer.events.off("rdown", this.onMouseRightButtonClick);
135
- this.renderer.events.off("draw", this.onDraw);
136
- this.renderer.events.off("ldblclick", this.onMouseLeftButtonDoubleClick);
137
- this.renderer.events.off("mouseleave", this.onMouseLeave);
138
- this.renderer.events.off("mouseenter", this.onMouseEnter);
139
- }
140
- }
141
- activateDoubleClickZoom() {
142
- if (!this._lmbDoubleClickActive) {
143
- this._lmbDoubleClickActive = true;
144
- this.renderer && this.renderer.events.on("ldblclick", this.onMouseLeftButtonDoubleClick, this);
145
- }
146
- }
147
- deactivateDoubleClickZoom() {
148
- if (this._lmbDoubleClickActive) {
149
- this._lmbDoubleClickActive = false;
150
- this.renderer && this.renderer.events.off("ldblclick", this.onMouseLeftButtonDoubleClick);
204
+ this.renderer.events.on("keyfree", input.KEY_ALT, this._onShiftFree);
205
+ this.renderer.events.on("keyfree", input.KEY_PRINTSCREEN, this._onShiftFree);
151
206
  }
152
207
  }
153
- onMouseEnter(e) {
154
- const renderEvents = this.renderer.events;
155
- if (renderEvents.isKeyPressed(input.KEY_ALT)) {
156
- renderEvents.releaseKeys();
157
- }
158
- renderEvents.updateButtonsStates(e.sys.buttons);
159
- if (renderEvents.mouseState.leftButtonDown) {
160
- this.renderer.handler.canvas.classList.add("ogGrabbingPoiner");
161
- }
162
- else {
163
- this.renderer.handler.canvas.classList.remove("ogGrabbingPoiner");
164
- }
208
+ onactivate() {
209
+ super.onactivate();
210
+ let r = this.renderer;
211
+ r.events.on("mousewheel", this._onMouseWheel);
212
+ r.events.on("rhold", this._onRHold);
213
+ r.events.on("rdown", this._onRDown);
214
+ r.events.on("lhold", this._onLHold);
215
+ r.events.on("ldown", this._onLDown);
216
+ r.events.on("lup", this._onLUp);
217
+ r.events.on("draw", this.onDraw, this, -1000);
218
+ r.events.on("mousemove", this._onMouseMove);
219
+ r.events.on("mouseleave", this._onMouseLeave);
220
+ r.events.on("mouseenter", this._onMouseEnter);
165
221
  }
166
- onMouseLeave() {
167
- if (this.renderer.events.mouseState.leftButtonDown) {
168
- this.scaleRot = 0;
169
- }
170
- this.renderer.handler.canvas.classList.remove("ogGrabbingPoiner");
222
+ ondeactivate() {
223
+ super.ondeactivate();
224
+ let r = this.renderer;
225
+ r.events.off("mousewheel", this._onMouseWheel);
226
+ r.events.off("rhold", this._onRHold);
227
+ r.events.off("rdown", this._onRDown);
228
+ r.events.off("lhold", this._onLHold);
229
+ r.events.off("ldown", this._onLDown);
230
+ r.events.off("lup", this._onLUp);
231
+ r.events.off("draw", this.onDraw);
232
+ r.events.off("mousemove", this._onMouseMove);
233
+ r.events.off("mouseleave", this._onMouseLeave);
234
+ r.events.off("mouseenter", this._onMouseEnter);
171
235
  }
172
- onMouseWheel(e) {
173
- if (this.stepIndex) {
174
- return;
175
- }
176
- this.planet.stopFlying();
177
- this.stopRotation();
178
- this._deactivate = true;
179
- this.lockPlanet(true);
180
- this.stepsForward = MouseNavigation.getMovePointsFromPixelTerrain(this.planet.camera, this.planet, this.stepsCount, this.distDiff, e.pos, e.wheelDelta > 0, e.direction) || null;
181
- this._wheelDirection = e.wheelDelta;
182
- if (this.stepsForward) {
183
- this.stepIndex = this.stepsCount;
184
- }
236
+ onDraw() {
237
+ this._updateVel();
238
+ this._handleZoom();
239
+ this._handleDrag();
240
+ this._handleRotation();
185
241
  }
186
- oninit() {
187
- this.activate();
188
- if (this.renderer) {
189
- this.renderer.events.on("keyfree", input.KEY_ALT, this.onShiftFree, this);
190
- this.renderer.events.on("keyfree", input.KEY_PRINTSCREEN, this.onShiftFree, this);
242
+ _handleRotation() {
243
+ if (this.planet && this._targetRotationPoint) {
244
+ let cam = this.planet.camera;
245
+ // let l = (0.3 / this._tRad) * math.RADIANS;
246
+ // if (l > 0.007) {
247
+ // l = 0.007;
248
+ // } else if (l < 0.003) {
249
+ // l = 0.003;
250
+ // }
251
+ let d_v_h = this.vel_h * this.dt;
252
+ let d_v_v = this.vel_v * this.dt;
253
+ cam.rotateHorizontal(d_v_h, false, this._targetRotationPoint, this._tUp);
254
+ cam.rotateVertical(d_v_v, this._targetRotationPoint, 0.1);
255
+ this._curPitch = cam.getPitch();
256
+ this._curYaw = cam.getYaw();
257
+ this._curRoll = cam.getRoll();
258
+ this._velInertia = DEFAULT_VELINERTIA;
191
259
  }
192
260
  }
193
- onMouseLeftButtonDoubleClick(e) {
194
- this.planet.stopFlying();
195
- this.stopRotation();
196
- const p = this.planet.getCartesianFromPixelTerrain(e.pos);
197
- if (p) {
198
- const cam = this.planet.camera;
199
- let maxAlt = cam.maxAltitude + this.planet.ellipsoid.polarSize;
200
- let minAlt = cam.minAltitude + this.planet.ellipsoid.polarSize;
201
- const camAlt = cam.eye.length();
202
- const g = this.planet.ellipsoid.cartesianToLonLat(p);
203
- if (camAlt > maxAlt || camAlt < minAlt) {
204
- this.planet.flyLonLat(new LonLat(g.lon, g.lat));
205
- return;
206
- }
207
- if (this.renderer.events.isKeyPressed(input.KEY_ALT)) {
208
- this.planet.flyLonLat(new LonLat(g.lon, g.lat, cam.eye.distance(p) * 2.0));
209
- }
210
- else {
211
- this.planet.flyLonLat(new LonLat(g.lon, g.lat, cam.eye.distance(p) * 0.57));
261
+ _getTargetPoint(p) {
262
+ if (this.planet) {
263
+ if (this.planet.camera.getAltitude() > 10000) {
264
+ return this.planet.getCartesianFromPixelEllipsoid(p) || null;
212
265
  }
266
+ return this.planet.getCartesianFromPixelTerrain(p) || null;
213
267
  }
268
+ return null;
214
269
  }
215
- onMouseLeftButtonClick() {
216
- if (this._active) {
217
- this.renderer.handler.canvas.classList.add("ogGrabbingPoiner");
218
- this.grabbedPoint = this.planet.getCartesianFromMouseTerrain();
219
- if (this.grabbedPoint) {
220
- this._eye0.copy(this.planet.camera.eye);
221
- this.grabbedSpheroid.radius = this.grabbedPoint.length();
222
- this.stopRotation();
270
+ _handleDrag() {
271
+ if (this.planet && this._targetDragPoint && this._grabbedPoint && this.vel.length() > 0.0) {
272
+ this._velInertia = DEFAULT_VELINERTIA;
273
+ let cam = this.planet.camera;
274
+ if (Math.abs(cam.eyeNorm.dot(Vec3.NORTH)) > 0.9) {
275
+ this.fixedUp = false;
223
276
  }
224
- }
225
- }
226
- stopRotation() {
227
- this.qRot.clear();
228
- this.freePlanet();
229
- }
230
- onMouseLeftButtonUp(e) {
231
- this.renderer.handler.canvas.classList.remove("ogGrabbingPoiner");
232
- if (e.x === e.prev_x && e.y === e.prev_y) {
233
- this.scaleRot = 0.0;
234
- }
235
- }
236
- onMouseLeftButtonDown(e) {
237
- if (this._active) {
238
- if (!this.grabbedPoint) {
239
- return;
277
+ if (!this._screenPosIsChanged) {
278
+ if (this.vel.length() > this._prevVel.length()) {
279
+ this.fixedUp = false;
280
+ }
240
281
  }
241
- this.planet.stopFlying();
242
- if (e.moving) {
243
- let cam = this.planet.camera;
244
- if (cam.slope > 0.2) {
245
- const targetPoint = new Ray(cam.eye, e.direction).hitSphere(this.grabbedSpheroid);
246
- if (targetPoint) {
247
- this.scaleRot = 1.0;
248
- this.qRot = Quat.getRotationBetweenVectors(targetPoint.normal(), this.grabbedPoint.normal());
249
- let rot = this.qRot;
250
- cam.eye = rot.mulVec3(cam.eye);
251
- cam._u = rot.mulVec3(cam._u);
252
- cam._r = rot.mulVec3(cam._r);
253
- cam._b = rot.mulVec3(cam._b);
254
- }
282
+ this._screenPosIsChanged = false;
283
+ this._prevVel.copy(this.vel);
284
+ if (cam.slope > 0.2) {
285
+ let d_v = this.vel.scaleTo(this.dt);
286
+ // let d_s = d_v;
287
+ // let newEye = cam.eye.add(d_s).normalize().scale(this._grabbedCameraHeight);
288
+ let d_s = Vec3.proj_b_to_plane(d_v, cam.eyeNorm);
289
+ let newEye = cam.eye.add(d_s).normalize().scale(this._grabbedCameraHeight);
290
+ if (this.fixedUp) {
291
+ cam.eye.copy(newEye);
292
+ this._corrRoll();
293
+ cam.setPitchYawRoll(this._curPitch, this._curYaw, this._curRoll);
255
294
  }
256
295
  else {
257
- let p0 = this.grabbedPoint, p1 = Vec3.add(p0, cam._r), p2 = Vec3.add(p0, p0.normal());
258
- let px = new Vec3();
259
- if (new Ray(cam.eye, e.direction).hitPlane(p0, p1, p2, px) === Ray.INSIDE) {
260
- cam.eye = this._eye0.addA(px.subA(p0).negate());
261
- }
296
+ let rot = Quat.getRotationBetweenVectors(cam.eye.getNormal(), newEye.getNormal());
297
+ cam.rotate(rot);
298
+ cam.eye.copy(newEye);
262
299
  }
263
300
  }
264
- }
265
- }
266
- onMouseRightButtonClick(e) {
267
- this.stopRotation();
268
- this.planet.stopFlying();
269
- this.pointOnEarth = this.planet.getCartesianFromPixelTerrain(e.pos);
270
- if (this.pointOnEarth) {
271
- this.earthUp = this.pointOnEarth.normal();
272
- }
273
- }
274
- onMouseRightButtonDown(e) {
275
- const cam = this.planet.camera;
276
- if (this.pointOnEarth && e.moving) {
277
- this.renderer.controlsBag.scaleRot = 1.0;
278
- let l = (0.5 / cam.eye.distance(this.pointOnEarth)) * cam._lonLat.height * math.RADIANS;
279
- if (l > 0.007) {
280
- l = 0.007;
281
- }
282
- else if (l < 0.003) {
283
- l = 0.003;
301
+ else {
302
+ let d_v = this.vel.scaleTo(this.dt);
303
+ let newEye = cam.eye.add(d_v);
304
+ cam.eye.copy(newEye);
305
+ cam.checkTerrainCollision();
284
306
  }
285
- cam.rotateHorizontal(l * (e.x - e.prev_x), false, this.pointOnEarth, this.earthUp);
286
- cam.rotateVertical(l * (e.y - e.prev_y), this.pointOnEarth, this.minSlope);
287
307
  }
288
308
  }
289
- onShiftFree() {
290
- this._shiftBusy = false;
291
- }
292
- onMouseMove(e) {
293
- if (this._active && this.renderer.events.isKeyPressed(input.KEY_ALT)) {
294
- if (!this._shiftBusy) {
295
- this._shiftBusy = true;
296
- this.onMouseRightButtonClick(e);
309
+ _corrRoll() {
310
+ if (this.planet.camera.slope < 0.5) {
311
+ this._curRoll -= this.vel_roll * this.dt;
312
+ if (this._curRoll < 0.01 * Math.PI / 180) {
313
+ this._curRoll = 0.01 * Math.PI / 180;
297
314
  }
298
- this.onMouseRightButtonDown(e);
299
315
  }
300
316
  }
301
- onDraw() {
302
- if (this._active) {
303
- const r = this.renderer;
304
- const cam = this.planet.camera;
305
- let prevEye = cam.eye.clone();
306
- if (this.stepIndex) {
307
- r.controlsBag.scaleRot = 1.0;
308
- const sf = this.stepsForward[this.stepsCount - this.stepIndex--];
309
- cam.eye = sf.eye;
310
- cam._u = sf.v;
311
- cam._r = sf.u;
312
- cam._b = sf.n;
313
- }
314
- else {
315
- if (this._deactivate) {
316
- this._deactivate = false;
317
- this.freePlanet();
318
- }
317
+ _handleZoom() {
318
+ if (this._targetZoomPoint && this.vel.length() > 0.0) {
319
+ // Common
320
+ let cam = this.planet.camera;
321
+ let a = this._targetZoomPoint;
322
+ let dir = a.sub(cam.eye).normalize();
323
+ let eye = cam.eye.clone();
324
+ let velDir = Math.sign(this.vel.getNormal().dot(cam.getForward()));
325
+ let d_v = this.vel.scaleTo(this.dt);
326
+ let d_s = d_v.projToVec(cam.getForward().scale(velDir));
327
+ //let d_s = cam.getForward().scaleTo(velDir * d_v.length());
328
+ // Braking tweak
329
+ let destDist = cam.eye.distance(a);
330
+ if (d_s.length() * 10 > destDist) {
331
+ let temp = d_s.length();
332
+ d_s.normalize().scale(temp * 0.5);
333
+ this.vel.scale(0.5);
319
334
  }
320
- if (r.events.mouseState.leftButtonDown || !this.scaleRot) {
335
+ eye.addA(d_s);
336
+ // Check max camera distance
337
+ let maxAlt = cam.maxAltitude + this.planet.ellipsoid.getEquatorialSize();
338
+ if (eye.length() > maxAlt) {
339
+ eye.copy(eye.getNormal().scale(maxAlt));
321
340
  return;
322
341
  }
323
- this.scaleRot -= this.inertia;
324
- if (this.scaleRot <= 0.0) {
325
- this.scaleRot = 0.0;
326
- }
327
- else {
328
- r.controlsBag.scaleRot = this.scaleRot;
329
- let rot = this.qRot
330
- .slerp(Quat.IDENTITY, 1.0 - this.scaleRot * this.scaleRot * this.scaleRot)
331
- .normalize();
332
- if (!(rot.x || rot.y || rot.z)) {
333
- this.scaleRot = 0.0;
334
- }
335
- cam.eye = rot.mulVec3(cam.eye);
336
- cam._u = rot.mulVec3(cam._u);
337
- cam._r = rot.mulVec3(cam._r);
338
- cam._b = rot.mulVec3(cam._b);
339
- }
340
- if (cam.eye.distance(prevEye) / cam.getAltitude() > 0.01) {
341
- this.lockPlanet();
342
+ let b = new Ray(eye, dir).hitSphere(this._grabbedSphere);
343
+ if (!b) {
344
+ this.vel.set(0, 0, 0);
345
+ return;
342
346
  }
343
- else {
344
- this.freePlanet();
347
+ let rot = Quat.getRotationBetweenVectors(b.getNormal(), a.getNormal());
348
+ cam.eye = rot.mulVec3(eye);
349
+ cam.rotate(rot);
350
+ if (this.fixedUp) {
351
+ this._corrRoll();
352
+ // restore camera direction
353
+ cam.setPitchYawRoll(this._curPitch, this._curYaw, this._curRoll);
354
+ cam.update();
355
+ let dirCurr = cam.unproject2v(this._currScreenPos);
356
+ let dirNew = a.sub(cam.eye).normalize();
357
+ let px0 = new Vec3();
358
+ let px1 = new Vec3();
359
+ let pl = Plane.fromPoints(a, a.add(cam.getUp()), a.add(cam.getRight()));
360
+ new Ray(cam.eye, dirCurr).hitPlaneRes(pl, px0);
361
+ new Ray(cam.eye, dirNew).hitPlaneRes(pl, px1);
362
+ let dp = px1.sub(px0);
363
+ cam.eye = cam.eye.add(dp);
364
+ // ver.2
365
+ // let px0 = new Ray(cam.eye, dirCurr).hitSphere(this._grabbedSphere)!;
366
+ // let px1 = new Ray(cam.eye, dirNew).hitSphere(this._grabbedSphere)!;
345
367
  }
368
+ cam.checkTerrainCollision();
369
+ }
370
+ }
371
+ _updateVel() {
372
+ let acc = this.force.scale(1.0 / this.mass);
373
+ this.vel.addA(acc);
374
+ this.vel.scale(this._velInertia);
375
+ if (this.vel.length() < 0.001) {
376
+ this.vel.set(0, 0, 0);
346
377
  }
378
+ this.force.set(0, 0, 0);
379
+ this._updateVel_h();
380
+ this._updateVel_v();
381
+ this._updateVel_roll();
382
+ }
383
+ _updateVel_h() {
384
+ let acc = this.force_h / this.mass;
385
+ this.vel_h += acc;
386
+ this.vel_h *= this._velInertia;
387
+ this.force_h = 0;
388
+ }
389
+ _updateVel_v() {
390
+ let acc = this.force_v / this.mass;
391
+ this.vel_v += acc;
392
+ this.vel_v *= this._velInertia;
393
+ this.force_v = 0;
394
+ }
395
+ _updateVel_roll() {
396
+ let acc = this.force_roll / this.mass;
397
+ this.vel_roll += acc;
398
+ this.vel_roll *= this._velInertia;
399
+ this.force_roll = 0;
347
400
  }
348
- lockPlanet(skipTerrain) {
349
- this.planet.layerLock.lock(this._keyLock);
350
- !skipTerrain && this.planet.terrainLock.lock(this._keyLock);
351
- this.planet._normalMapCreator.lock(this._keyLock);
401
+ get dt() {
402
+ return 0.001 * this.renderer.handler.deltaTime;
352
403
  }
353
- freePlanet() {
354
- this.planet.layerLock.free(this._keyLock);
355
- this.planet.terrainLock.free(this._keyLock);
356
- this.planet._normalMapCreator.free(this._keyLock);
404
+ stop() {
405
+ this.vel.set(0, 0, 0);
406
+ this.vel_h = 0;
407
+ this.vel_v = 0;
408
+ this._velInertia = DEFAULT_VELINERTIA;
409
+ this._targetZoomPoint = null;
410
+ this._grabbedPoint = null;
411
+ this._targetRotationPoint = null;
412
+ this._targetDragPoint = null;
357
413
  }
358
414
  }