bruce-cesium 7.9.3 → 7.9.5

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.
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.FindGapSurfaces = exports.CameraControls = void 0;
3
+ exports.GrabPoint = exports.NearClearance = exports.FindGapSurfaces = exports.CameraControls = exports.ECameraMouseScheme = void 0;
4
4
  const Cesium = require("cesium");
5
5
  const drawing_utils_1 = require("../utils/drawing-utils");
6
6
  const CESIUM_CAMERA_CONTROLS_KEY = "_nextspace_camera_controls";
@@ -35,6 +35,11 @@ const PUSH_NOTCHES_DATA_BELOW = 3;
35
35
  const PUSH_NOTCHES_NOTHING_BELOW = 10;
36
36
  // Pushes further apart than this start counting again.
37
37
  const PUSH_WINDOW_MS = 1200;
38
+ // How long after a zoom newly loaded terrain may lift the camera, and how much longer while loading.
39
+ const SURFACE_HOLD_MS = 4000;
40
+ const SURFACE_LOADING_MS = 30000;
41
+ // No visible ground lies lower; a stand-in tileset below this is a coarse tile cutting under the curve.
42
+ const GROUND_FLOOR = -500;
38
43
  // How far below the ground something must reach to count as data underground, in metres.
39
44
  const UNDERGROUND_MARGIN = 3;
40
45
  // How long a scan of the scene for underground data and stand-in ground is trusted, in milliseconds.
@@ -48,6 +53,24 @@ const ZOOM_LOADING_SHARE = 0.7;
48
53
  const HIDDEN_GLOBE_MAX_HEIGHT = 10000000;
49
54
  // Marks a tileset that stands in for the globe, such as Google's photorealistic tiles.
50
55
  const GROUND_TILESET_KEY = "_nextspace_camera_ground";
56
+ // A viewer's own mouse scheme and wheel direction, kept on the viewer so a host may set them before Attach.
57
+ const MOUSE_KEY = "_nextspace_camera_mouse";
58
+ // Marks a wheel event these controls turned round, so it is not turned back again.
59
+ const MIRRORED_WHEEL_KEY = "_nextspace_mirrored";
60
+ /**
61
+ * Which mouse buttons pan, orbit and zoom: CesiumJS's own, or a familiar desktop 3D app's.
62
+ */
63
+ var ECameraMouseScheme;
64
+ (function (ECameraMouseScheme) {
65
+ // CesiumJS's own: left drag pans, middle or ctrl drag orbits, right drag zooms.
66
+ ECameraMouseScheme["Default"] = "default";
67
+ // Autodesk Fusion, Revit, Navisworks and AutoCAD: middle drag pans, shift+middle orbits, ctrl+shift+middle zooms.
68
+ ECameraMouseScheme["Autodesk"] = "autodesk";
69
+ // SketchUp: middle drag orbits, shift+middle pans.
70
+ ECameraMouseScheme["SketchUp"] = "sketchup";
71
+ })(ECameraMouseScheme = exports.ECameraMouseScheme || (exports.ECameraMouseScheme = {}));
72
+ // What a viewer without its own setting uses: the app user's preference, set once for every viewer the app makes.
73
+ const _mouseDefault = { scheme: ECameraMouseScheme.Default, wheelReversed: false };
51
74
  var EDrag;
52
75
  (function (EDrag) {
53
76
  EDrag["Pan"] = "pan";
@@ -63,7 +86,7 @@ var CameraControls;
63
86
  * Takes over mouse drag and wheel navigation close to the ground; CesiumJS keeps planet scale, pinch and look.
64
87
  */
65
88
  function Attach(viewer) {
66
- var _a, _b, _c;
89
+ var _a, _b, _c, _d;
67
90
  if (!viewer || viewer.isDestroyed() || viewer[CESIUM_CAMERA_CONTROLS_KEY]) {
68
91
  return;
69
92
  }
@@ -84,15 +107,18 @@ var CameraControls;
84
107
  push: { notches: 0, at: 0 },
85
108
  scan: null,
86
109
  undergroundOverride: null,
110
+ surface: { above: false, through: false, until: 0, at: null },
87
111
  saved: {
88
112
  rotateEventTypes: controller.rotateEventTypes,
89
113
  zoomEventTypes: controller.zoomEventTypes,
90
114
  tiltEventTypes: controller.tiltEventTypes,
115
+ translateEventTypes: controller.translateEventTypes,
91
116
  enableCollisionDetection: controller.enableCollisionDetection,
92
117
  backFaceAlpha: (_b = (_a = scene.globe) === null || _a === void 0 ? void 0 : _a.translucency) === null || _b === void 0 ? void 0 : _b.backFaceAlpha,
93
118
  showSkirts: (_c = scene.globe) === null || _c === void 0 ? void 0 : _c.showSkirts
94
119
  },
95
120
  ours: false,
121
+ cesiumScheme: null,
96
122
  removals: []
97
123
  };
98
124
  viewer[CESIUM_CAMERA_CONTROLS_KEY] = state;
@@ -108,7 +134,13 @@ var CameraControls;
108
134
  listen(window, "pointerup", (e) => onPointerUp(state, e));
109
135
  listen(window, "pointercancel", (e) => onPointerUp(state, e));
110
136
  listen(canvas, "wheel", (e) => onWheel(state, e), { passive: true });
111
- state.removals.push(scene.preUpdate.addEventListener(() => syncController(state)));
137
+ // Caught before the canvas sees it, so CesiumJS's controller and these controls both get the turned wheel.
138
+ const wheelHost = (_d = canvas.parentElement) !== null && _d !== void 0 ? _d : window;
139
+ listen(wheelHost, "wheel", (e) => mirrorWheel(state, e), { capture: true, passive: false });
140
+ state.removals.push(scene.preUpdate.addEventListener(() => {
141
+ syncController(state);
142
+ keepAboveGround(state);
143
+ }));
112
144
  syncController(state);
113
145
  scene.requestRender();
114
146
  }
@@ -140,6 +172,52 @@ var CameraControls;
140
172
  return Boolean(viewer === null || viewer === void 0 ? void 0 : viewer[CESIUM_CAMERA_CONTROLS_KEY]);
141
173
  }
142
174
  CameraControls.IsAttached = IsAttached;
175
+ /**
176
+ * Picks which mouse buttons pan, orbit and zoom, here and for CesiumJS's controller above the hand-over height.
177
+ */
178
+ function SetMouseScheme(viewer, scheme) {
179
+ if (!viewer || viewer.isDestroyed()) {
180
+ return;
181
+ }
182
+ viewer[MOUSE_KEY] = { ...viewer[MOUSE_KEY], scheme: knownScheme(scheme) };
183
+ const state = viewer[CESIUM_CAMERA_CONTROLS_KEY];
184
+ if (state && !state.ours) {
185
+ applyCesiumGestures(state);
186
+ }
187
+ }
188
+ CameraControls.SetMouseScheme = SetMouseScheme;
189
+ /**
190
+ * Sets the scheme for every viewer without its own, such as the app user's preference; viewers follow next frame.
191
+ */
192
+ function SetMouseSchemeDefault(scheme) {
193
+ _mouseDefault.scheme = knownScheme(scheme);
194
+ }
195
+ CameraControls.SetMouseSchemeDefault = SetMouseSchemeDefault;
196
+ function GetMouseScheme(viewer) {
197
+ return mouseOf(viewer).scheme;
198
+ }
199
+ CameraControls.GetMouseScheme = GetMouseScheme;
200
+ /**
201
+ * Makes wheel forward zoom out instead of in, for these controls and CesiumJS's controller alike.
202
+ */
203
+ function SetMouseWheelReversed(viewer, reversed) {
204
+ if (!viewer || viewer.isDestroyed()) {
205
+ return;
206
+ }
207
+ viewer[MOUSE_KEY] = { ...viewer[MOUSE_KEY], wheelReversed: Boolean(reversed) };
208
+ }
209
+ CameraControls.SetMouseWheelReversed = SetMouseWheelReversed;
210
+ /**
211
+ * Sets the wheel direction for every viewer without its own, such as the app user's preference.
212
+ */
213
+ function SetMouseWheelReversedDefault(reversed) {
214
+ _mouseDefault.wheelReversed = Boolean(reversed);
215
+ }
216
+ CameraControls.SetMouseWheelReversedDefault = SetMouseWheelReversedDefault;
217
+ function GetMouseWheelReversed(viewer) {
218
+ return mouseOf(viewer).wheelReversed;
219
+ }
220
+ CameraControls.GetMouseWheelReversed = GetMouseWheelReversed;
143
221
  /**
144
222
  * Marks a tileset as standing in for the globe (Google's photorealistic tiles), so the camera treats it as ground.
145
223
  */
@@ -197,11 +275,67 @@ var CameraControls;
197
275
  }
198
276
  CameraControls.SetModelSpace = SetModelSpace;
199
277
  })(CameraControls = exports.CameraControls || (exports.CameraControls = {}));
278
+ /*
279
+ * The scheme and wheel direction a viewer uses: its own where set, else the app default.
280
+ */
281
+ function mouseOf(viewer) {
282
+ var _a, _b;
283
+ const own = viewer === null || viewer === void 0 ? void 0 : viewer[MOUSE_KEY];
284
+ return {
285
+ scheme: (_a = own === null || own === void 0 ? void 0 : own.scheme) !== null && _a !== void 0 ? _a : _mouseDefault.scheme,
286
+ wheelReversed: (_b = own === null || own === void 0 ? void 0 : own.wheelReversed) !== null && _b !== void 0 ? _b : _mouseDefault.wheelReversed
287
+ };
288
+ }
289
+ /*
290
+ * A scheme this version knows, or Default for one saved by a newer app or mistyped.
291
+ */
292
+ function knownScheme(scheme) {
293
+ return Object.values(ECameraMouseScheme).includes(scheme) ? scheme : ECameraMouseScheme.Default;
294
+ }
200
295
  function restoreCesiumGestures(state) {
201
296
  const controller = state.viewer.scene.screenSpaceCameraController;
202
297
  controller.rotateEventTypes = state.saved.rotateEventTypes;
203
298
  controller.zoomEventTypes = state.saved.zoomEventTypes;
204
299
  controller.tiltEventTypes = state.saved.tiltEventTypes;
300
+ if (state.saved.translateEventTypes !== undefined) {
301
+ controller.translateEventTypes = state.saved.translateEventTypes;
302
+ }
303
+ }
304
+ /*
305
+ * Gives CesiumJS's controller its own gestures under the mouse scheme, so the buttons do not swap at the hand-over.
306
+ */
307
+ function applyCesiumGestures(state) {
308
+ restoreCesiumGestures(state);
309
+ const scheme = mouseOf(state.viewer).scheme;
310
+ state.cesiumScheme = scheme;
311
+ if (scheme === ECameraMouseScheme.Default) {
312
+ return;
313
+ }
314
+ const middle = Cesium.CameraEventType.MIDDLE_DRAG;
315
+ const shiftMiddle = { eventType: middle, modifier: Cesium.KeyboardEventModifier.SHIFT };
316
+ const tilt = asEventTypes(state.saved.tiltEventTypes);
317
+ // A host that moved CesiumJS's middle drag elsewhere has its own mapping, which is left alone.
318
+ if (!tilt.includes(middle)) {
319
+ return;
320
+ }
321
+ // CesiumJS orbits with tilt and pans with rotate in 3D, or translate in 2D and Columbus view.
322
+ const orbit = scheme === ECameraMouseScheme.Autodesk ? shiftMiddle : middle;
323
+ const pan = scheme === ECameraMouseScheme.Autodesk ? middle : shiftMiddle;
324
+ const controller = state.viewer.scene.screenSpaceCameraController;
325
+ controller.tiltEventTypes = tilt.map((type) => (type === middle ? orbit : type));
326
+ controller.rotateEventTypes = [...asEventTypes(state.saved.rotateEventTypes), pan];
327
+ if (state.saved.translateEventTypes !== undefined) {
328
+ controller.translateEventTypes = [...asEventTypes(state.saved.translateEventTypes), pan];
329
+ }
330
+ }
331
+ /*
332
+ * CesiumJS takes one event type or a list of them; this always gives a list.
333
+ */
334
+ function asEventTypes(types) {
335
+ if (types == null) {
336
+ return [];
337
+ }
338
+ return Array.isArray(types) ? types : [types];
205
339
  }
206
340
  /*
207
341
  * Decides each frame who has the mouse: these controls near the ground, CesiumJS for planets, orthographic and tracking.
@@ -213,6 +347,9 @@ function syncController(state) {
213
347
  }
214
348
  const ours = shouldControl(state);
215
349
  if (ours === state.ours) {
350
+ if (!ours && state.cesiumScheme !== mouseOf(viewer).scheme) {
351
+ applyCesiumGestures(state);
352
+ }
216
353
  return;
217
354
  }
218
355
  state.ours = ours;
@@ -223,7 +360,7 @@ function syncController(state) {
223
360
  controller.tiltEventTypes = [Cesium.CameraEventType.PINCH];
224
361
  }
225
362
  else {
226
- restoreCesiumGestures(state);
363
+ applyCesiumGestures(state);
227
364
  }
228
365
  }
229
366
  function shouldControl(state) {
@@ -260,6 +397,38 @@ function terrainHeightAt(state, carto) {
260
397
  const height = globe.getHeight(carto);
261
398
  return Number.isFinite(height) ? height : undefined;
262
399
  }
400
+ /*
401
+ * The ground's height at a place: the loaded terrain, or the tileset standing in for a hidden globe.
402
+ */
403
+ function groundHeightAt(state, carto) {
404
+ var _a;
405
+ if (globeShown(state)) {
406
+ return terrainHeightAt(state, carto);
407
+ }
408
+ const tileset = scanScene(state).ground;
409
+ if (!carto || !tileset || ((_a = tileset.isDestroyed) === null || _a === void 0 ? void 0 : _a.call(tileset)) || typeof tileset.getHeight !== "function") {
410
+ return undefined;
411
+ }
412
+ try {
413
+ // Only tiles drawn this frame answer, which near the camera are the finest loaded.
414
+ const height = tileset.getHeight(carto, state.viewer.scene);
415
+ return Number.isFinite(height) && height > GROUND_FLOOR ? height : undefined;
416
+ }
417
+ catch (e) {
418
+ return undefined;
419
+ }
420
+ }
421
+ /*
422
+ * Whether the ground is still loading: the globe's tiles, or the stand-in tileset's.
423
+ */
424
+ function groundLoading(state) {
425
+ var _a;
426
+ if (globeShown(state)) {
427
+ return state.viewer.scene.globe.tilesLoaded === false;
428
+ }
429
+ const tileset = scanScene(state).ground;
430
+ return Boolean(tileset && !((_a = tileset.isDestroyed) === null || _a === void 0 ? void 0 : _a.call(tileset)) && tileset.tilesLoaded === false);
431
+ }
263
432
  function ellipsoidOf(state) {
264
433
  var _a, _b;
265
434
  return (_b = (_a = state.viewer.scene.globe) === null || _a === void 0 ? void 0 : _a.ellipsoid) !== null && _b !== void 0 ? _b : Cesium.Ellipsoid.WGS84;
@@ -297,8 +466,8 @@ function groundPlane(state) {
297
466
  if (!carto) {
298
467
  return null;
299
468
  }
300
- // Level with the terrain under the camera, not sea level, so a guess over high ground is not far too deep.
301
- const surface = Cesium.Cartesian3.fromRadians(carto.longitude, carto.latitude, (_a = terrainHeightAt(state, carto)) !== null && _a !== void 0 ? _a : 0, ellipsoid);
469
+ // Level with the ground under the camera, not sea level, so a guess over high ground is not far too deep.
470
+ const surface = Cesium.Cartesian3.fromRadians(carto.longitude, carto.latitude, (_a = groundHeightAt(state, carto)) !== null && _a !== void 0 ? _a : 0, ellipsoid);
302
471
  return Cesium.Plane.fromPointNormal(surface, ellipsoid.geodeticSurfaceNormal(surface, new Cesium.Cartesian3()));
303
472
  }
304
473
  /*
@@ -322,6 +491,15 @@ const GAP_SLACK = 0.5;
322
491
  // Directions around the centre, and how many must hold nearer pixels for the centre to be surrounded.
323
492
  const GAP_SECTORS = 8;
324
493
  const GAP_SECTORS_NEAR = 6;
494
+ // How far around the cursor, in CSS pixels, nearer parts limit a zoom, and the most buffer pixels read.
495
+ const CLEARANCE_RADIUS_PX = 24;
496
+ const CLEARANCE_RADIUS_MAX = 64;
497
+ // The share of that window that must be nearer, so a stray pixel or a thin cable does not slow the zoom.
498
+ const CLEARANCE_SHARE = 0.05;
499
+ // A pixel at least this share of the level ground's distance on its ray is ground, and never limits.
500
+ const CLEARANCE_GROUND = 0.9;
501
+ // A pan or orbit grabbing this many times farther than the reach grabs at the reach instead.
502
+ const CLEARANCE_GRAB = 2;
325
503
  const PACKED_DEPTH = new Cesium.Cartesian4(1, 1 / 255, 1 / 65025, 1 / 16581375);
326
504
  /*
327
505
  * Distances along the view axis drawn around a window position, rows bottom-up, Infinity where nothing is drawn.
@@ -370,6 +548,21 @@ function readDepthDistances(scene, position, radius) {
370
548
  }
371
549
  return distances;
372
550
  }
551
+ /*
552
+ * The depth window around a position for the gap search and the reach, or null when unreadable.
553
+ */
554
+ function readWindow(scene, position) {
555
+ const scale = scene.drawingBufferWidth / scene.canvas.clientWidth;
556
+ const clearance = Math.min(Math.round(CLEARANCE_RADIUS_PX * (Number.isFinite(scale) ? scale : 1)), CLEARANCE_RADIUS_MAX);
557
+ const radius = Math.max(GAP_RADII[GAP_RADII.length - 1], clearance);
558
+ try {
559
+ const distances = readDepthDistances(scene, position, radius);
560
+ return distances ? { distances, radius } : null;
561
+ }
562
+ catch (e) {
563
+ return null;
564
+ }
565
+ }
373
566
  /*
374
567
  * Offsets of the surface pixel around the centre for each radius where clearly nearer pixels surround it.
375
568
  */
@@ -408,15 +601,7 @@ exports.FindGapSurfaces = FindGapSurfaces;
408
601
  /*
409
602
  * The point cloud surface around a pick that looked through a gap between its points, or null when it did not.
410
603
  */
411
- function fillGap(scene, position, picked) {
412
- const radius = GAP_RADII[GAP_RADII.length - 1];
413
- let distances = null;
414
- try {
415
- distances = readDepthDistances(scene, position, radius);
416
- }
417
- catch (e) {
418
- return null;
419
- }
604
+ function fillGap(scene, position, picked, distances, radius) {
420
605
  if (!distances) {
421
606
  return null;
422
607
  }
@@ -436,9 +621,81 @@ function fillGap(scene, position, picked) {
436
621
  return null;
437
622
  }
438
623
  /*
439
- * The point under a screen position: what is drawn there, else the ground plane, else a point at the last distance.
624
+ * The view-axis distance within which a share of the window around the cursor is drawn, ground aside.
625
+ */
626
+ function NearClearance(distances, radius, groundAt) {
627
+ const width = 2 * radius + 1;
628
+ const near = [];
629
+ for (let dy = -radius; dy <= radius; dy++) {
630
+ for (let dx = -radius; dx <= radius; dx++) {
631
+ const distance = distances[(radius + dy) * width + (radius + dx)];
632
+ if (!Number.isFinite(distance)) {
633
+ continue;
634
+ }
635
+ const ground = groundAt ? groundAt(dx, dy) : Infinity;
636
+ if (!(ground > 0 && distance >= ground * CLEARANCE_GROUND)) {
637
+ near.push(distance);
638
+ }
639
+ }
640
+ }
641
+ const needed = Math.max(1, Math.ceil(width * width * CLEARANCE_SHARE));
642
+ if (near.length < needed) {
643
+ return Infinity;
644
+ }
645
+ near.sort((a, b) => a - b);
646
+ return near[needed - 1];
647
+ }
648
+ exports.NearClearance = NearClearance;
649
+ /*
650
+ * The level ground's view-axis distance through each window pixel, or null when there is no ground.
651
+ */
652
+ function groundDistances(state, position) {
653
+ const scene = state.viewer.scene;
654
+ const camera = scene.camera;
655
+ const frustum = camera.frustum;
656
+ if (!(state.getModelSpaceFrame || hasGround(state)) ||
657
+ typeof (frustum === null || frustum === void 0 ? void 0 : frustum.getPixelDimensions) !== "function") {
658
+ return null;
659
+ }
660
+ const plane = groundPlane(state);
661
+ if (!plane) {
662
+ return null;
663
+ }
664
+ const width = scene.drawingBufferWidth;
665
+ const height = scene.drawingBufferHeight;
666
+ // The size of one buffer pixel one metre ahead, which turns a pixel offset into a ray direction.
667
+ const pixel = frustum.getPixelDimensions(width, height, 1, 1, new Cesium.Cartesian2());
668
+ const above = Cesium.Plane.getPointDistance(plane, camera.positionWC);
669
+ const normal = plane.normal;
670
+ const along = Cesium.Cartesian3.dot(camera.directionWC, normal);
671
+ const right = Cesium.Cartesian3.dot(camera.rightWC, normal);
672
+ const up = Cesium.Cartesian3.dot(camera.upWC, normal);
673
+ // The cursor's offset from the centre in buffer pixels, rows bottom-up as the window's are.
674
+ const x = Math.floor(position.x * width / scene.canvas.clientWidth) + 0.5 - width / 2;
675
+ const y = height / 2 - Math.floor(position.y * height / scene.canvas.clientHeight) - 0.5;
676
+ return (dx, dy) => {
677
+ const rate = along + (x + dx) * pixel.x * right + (y + dy) * pixel.y * up;
678
+ return rate < 0 ? -above / rate : Infinity;
679
+ };
680
+ }
681
+ /*
682
+ * The point under a screen position, and how far a zoom toward it may go before passing parts beside it.
440
683
  */
441
684
  function pickTarget(state, position) {
685
+ const scene = state.viewer.scene;
686
+ // Read before anything else picks, while the depth still holds the frame on screen.
687
+ const depths = scene.pickPositionSupported ? readWindow(scene, position) : null;
688
+ const target = pickPoint(state, position, depths);
689
+ if (target && depths) {
690
+ target.reach = NearClearance(depths.distances, depths.radius, groundDistances(state, position));
691
+ target.reachFrom = Cesium.Cartesian3.clone(state.viewer.camera.positionWC);
692
+ }
693
+ return target;
694
+ }
695
+ /*
696
+ * The point under a screen position: what is drawn there, else the ground plane, else a point at the last distance.
697
+ */
698
+ function pickPoint(state, position, depths) {
442
699
  const viewer = state.viewer;
443
700
  const scene = viewer.scene;
444
701
  const camera = viewer.camera;
@@ -463,12 +720,16 @@ function pickTarget(state, position) {
463
720
  }
464
721
  const plausible = plausibleDistance(state);
465
722
  // A click between point cloud points sees the background; turning or zooming about that throws the camera far off.
466
- const surface = scene.pickPositionSupported ? fillGap(scene, position, ahead !== null && ahead !== void 0 ? ahead : null) : null;
723
+ const surface = depths ? fillGap(scene, position, ahead !== null && ahead !== void 0 ? ahead : null, depths.distances, depths.radius) : null;
467
724
  if (isAhead(camera, surface) && Cesium.Cartesian3.distance(surface, camera.positionWC) < plausible) {
468
725
  return { point: surface, source: "pick", ground: false };
469
726
  }
470
727
  if (isAhead(camera, picked) && Cesium.Cartesian3.distance(picked, camera.positionWC) < plausible) {
471
- return { point: picked, source: "pick", ground: isGroundPick(state, position) };
728
+ const ground = isGroundPick(state, position);
729
+ // Right after a jump a stand-in tileset's coarse tiles cut far under the curve; use sea level.
730
+ if (!ground || globeShown(state) || heightOf(state, picked) > GROUND_FLOOR) {
731
+ return { point: picked, source: "pick", ground };
732
+ }
472
733
  }
473
734
  const ground = groundPlane(state);
474
735
  const onGround = ground ? Cesium.IntersectionTests.rayPlane(ray, ground) : null;
@@ -515,9 +776,18 @@ function isAhead(camera, point) {
515
776
  return Cesium.Cartesian3.dot(toPoint, camera.directionWC) > 0;
516
777
  }
517
778
  /*
518
- * Picks which gesture a button and its modifiers start, matching CesiumJS's own mapping.
779
+ * Picks which gesture a button and its modifiers start under the mouse scheme; Default matches CesiumJS's own mapping.
519
780
  */
520
- function dragKind(e) {
781
+ function dragKind(e, scheme) {
782
+ if (scheme === ECameraMouseScheme.Autodesk && e.button === 1) {
783
+ if (e.shiftKey && e.ctrlKey) {
784
+ return EDrag.Zoom;
785
+ }
786
+ return e.shiftKey ? EDrag.Orbit : EDrag.Pan;
787
+ }
788
+ if (scheme === ECameraMouseScheme.SketchUp && e.button === 1) {
789
+ return e.shiftKey ? EDrag.Pan : EDrag.Orbit;
790
+ }
521
791
  if (e.shiftKey) {
522
792
  // Shift drag looks around, which CesiumJS still handles.
523
793
  return null;
@@ -561,7 +831,7 @@ function onPointerDown(state, e) {
561
831
  return;
562
832
  }
563
833
  }
564
- const kind = dragKind(e);
834
+ const kind = dragKind(e, mouseOf(viewer).scheme);
565
835
  if (!kind || !allows(state, kind)) {
566
836
  return;
567
837
  }
@@ -570,8 +840,8 @@ function onPointerDown(state, e) {
570
840
  if (!picked) {
571
841
  return;
572
842
  }
573
- const target = picked.point;
574
843
  const camera = viewer.camera;
844
+ const target = kind === EDrag.Zoom ? picked.point : GrabPoint(camera, position, picked);
575
845
  const distance = Cesium.Cartesian3.distance(camera.positionWC, target);
576
846
  state.lastDistance = Math.max(distance, MIN_ZOOM_DISTANCE);
577
847
  const drag = { kind, pointerId: e.pointerId, last: position, target, up: upAt(state, target), distance, picked };
@@ -579,10 +849,29 @@ function onPointerDown(state, e) {
579
849
  drag.plane = panPlane(camera, target, drag.up);
580
850
  }
581
851
  else if (kind === EDrag.Zoom) {
852
+ holdSurface(state);
582
853
  drag.direction = Cesium.Cartesian3.normalize(Cesium.Cartesian3.subtract(target, camera.positionWC, new Cesium.Cartesian3()), new Cesium.Cartesian3());
583
854
  }
584
855
  state.drag = drag;
585
856
  }
857
+ /*
858
+ * Where a pan or orbit takes hold: the pick, or beside the cursor when it looked far past parts there.
859
+ */
860
+ function GrabPoint(camera, position, picked) {
861
+ const reach = picked.reach;
862
+ const ray = camera.getPickRay(position);
863
+ if (!ray || reach == null || !Number.isFinite(reach)) {
864
+ return picked.point;
865
+ }
866
+ const facing = Cesium.Cartesian3.dot(ray.direction, camera.directionWC);
867
+ const depth = Cesium.Cartesian3.dot(Cesium.Cartesian3.subtract(picked.point, camera.positionWC, new Cesium.Cartesian3()), camera.directionWC);
868
+ if (facing <= 0 || depth <= reach * CLEARANCE_GRAB) {
869
+ return picked.point;
870
+ }
871
+ // The reach is along the view axis, so the ray goes a little further to the same depth.
872
+ return Cesium.Ray.getPoint(ray, Math.max(reach, MIN_ZOOM_DISTANCE) / facing);
873
+ }
874
+ exports.GrabPoint = GrabPoint;
586
875
  /*
587
876
  * A level plane through the grabbed point, or one facing the camera when the level one is seen edge on.
588
877
  */
@@ -731,6 +1020,7 @@ function onWheel(state, e) {
731
1020
  if (!target || !ray) {
732
1021
  return;
733
1022
  }
1023
+ holdSurface(state);
734
1024
  const step = zoomStep(state, ray.direction, target, notches);
735
1025
  Cesium.Cartesian3.add(state.pendingZoom, step, state.pendingZoom);
736
1026
  state.zoomPosition = Cesium.Cartesian3.clone(camera.position);
@@ -738,6 +1028,37 @@ function onWheel(state, e) {
738
1028
  state.zoomFrame = requestAnimationFrame(() => easeZoom(state));
739
1029
  }
740
1030
  }
1031
+ /*
1032
+ * Swaps a wheel event over the scene for one turned round, so wheel forward zooms out when the user asked for that.
1033
+ */
1034
+ function mirrorWheel(state, e) {
1035
+ const viewer = state.viewer;
1036
+ const canvas = viewer.isDestroyed() ? null : viewer.scene.canvas;
1037
+ if (!canvas || e.target !== canvas || e[MIRRORED_WHEEL_KEY] || !mouseOf(viewer).wheelReversed) {
1038
+ return;
1039
+ }
1040
+ e.stopImmediatePropagation();
1041
+ e.preventDefault();
1042
+ const mirrored = new WheelEvent("wheel", {
1043
+ bubbles: true,
1044
+ cancelable: true,
1045
+ clientX: e.clientX,
1046
+ clientY: e.clientY,
1047
+ screenX: e.screenX,
1048
+ screenY: e.screenY,
1049
+ ctrlKey: e.ctrlKey,
1050
+ shiftKey: e.shiftKey,
1051
+ altKey: e.altKey,
1052
+ metaKey: e.metaKey,
1053
+ buttons: e.buttons,
1054
+ deltaMode: e.deltaMode,
1055
+ deltaX: e.deltaX,
1056
+ deltaY: -e.deltaY,
1057
+ deltaZ: e.deltaZ
1058
+ });
1059
+ mirrored[MIRRORED_WHEEL_KEY] = true;
1060
+ canvas.dispatchEvent(mirrored);
1061
+ }
741
1062
  /*
742
1063
  * Applies part of the outstanding wheel movement each frame, so a notch glides rather than jumps.
743
1064
  */
@@ -759,6 +1080,8 @@ function easeZoom(state) {
759
1080
  const step = Cesium.Cartesian3.multiplyByScalar(state.pendingZoom, share, new Cesium.Cartesian3());
760
1081
  Cesium.Cartesian3.subtract(state.pendingZoom, step, state.pendingZoom);
761
1082
  Cesium.Cartesian3.add(viewer.camera.position, step, viewer.camera.position);
1083
+ zoomMoved(state);
1084
+ keepAboveGround(state);
762
1085
  state.zoomPosition = Cesium.Cartesian3.clone(viewer.camera.position);
763
1086
  viewer.scene.requestRender();
764
1087
  if (share < 1) {
@@ -768,6 +1091,7 @@ function easeZoom(state) {
768
1091
  function moveAlong(state, direction, target, notches) {
769
1092
  const step = zoomStep(state, direction, target, notches);
770
1093
  Cesium.Cartesian3.add(state.viewer.camera.position, step, state.viewer.camera.position);
1094
+ zoomMoved(state);
771
1095
  }
772
1096
  /*
773
1097
  * The move for some notches toward a target along a direction, a share of the distance with a floor so it never stalls.
@@ -777,7 +1101,14 @@ function zoomStep(state, direction, picked, notches) {
777
1101
  const target = picked.point;
778
1102
  // Where the camera will be once the glide already under way ends, which this step continues from.
779
1103
  const from = Cesium.Cartesian3.add(camera.positionWC, state.pendingZoom, new Cesium.Cartesian3());
780
- const distance = Math.max(Cesium.Cartesian3.distance(from, target), MIN_ZOOM_DISTANCE);
1104
+ const unit = Cesium.Cartesian3.normalize(direction, new Cesium.Cartesian3());
1105
+ let distance = Cesium.Cartesian3.distance(from, target);
1106
+ // Between parts of a model, each step goes as far as it would on the nearest part around the cursor.
1107
+ if (picked.reach != null && picked.reachFrom) {
1108
+ const travelled = Cesium.Cartesian3.dot(Cesium.Cartesian3.subtract(from, picked.reachFrom, new Cesium.Cartesian3()), unit);
1109
+ distance = Math.min(distance, picked.reach - travelled);
1110
+ }
1111
+ distance = Math.max(distance, MIN_ZOOM_DISTANCE);
781
1112
  state.lastDistance = distance;
782
1113
  let length = distance * ZOOM_SHARE * notches * zoomSpeedShare(state, picked);
783
1114
  // A geospatial camera zooming out hands over to CesiumJS once it is high enough, so only model-space is capped.
@@ -789,7 +1120,6 @@ function zoomStep(state, direction, picked, notches) {
789
1120
  return new Cesium.Cartesian3();
790
1121
  }
791
1122
  }
792
- const unit = Cesium.Cartesian3.normalize(direction, new Cesium.Cartesian3());
793
1123
  // With the globe hidden there is no hand-over, so zooming out stops where the Earth is a dot.
794
1124
  if (length < 0 && !state.getModelSpaceFrame && !globeShown(state)) {
795
1125
  const rise = -Cesium.Cartesian3.dot(unit, upAt(state, from));
@@ -805,15 +1135,14 @@ function zoomStep(state, direction, picked, notches) {
805
1135
  return length > 0 ? resistDescent(state, from, step, picked, notches) : step;
806
1136
  }
807
1137
  /*
808
- * How fast to zoom toward a target: slower toward a guess, or while the terrain that would give a real one loads.
1138
+ * How fast to zoom toward a target: slower toward a guess, or while the ground that would give a real one loads.
809
1139
  */
810
1140
  function zoomSpeedShare(state, picked) {
811
1141
  let share = 1;
812
1142
  if (picked.source !== "pick") {
813
1143
  share *= ZOOM_GUESS_SHARE;
814
1144
  }
815
- const globe = state.viewer.scene.globe;
816
- if (!state.getModelSpaceFrame && globeShown(state) && globe.tilesLoaded === false) {
1145
+ if (!state.getModelSpaceFrame && groundLoading(state)) {
817
1146
  share *= ZOOM_LOADING_SHARE;
818
1147
  }
819
1148
  return share;
@@ -829,16 +1158,17 @@ function groundUnderMove(state, from, step, picked) {
829
1158
  if (picked === null || picked === void 0 ? void 0 : picked.ground) {
830
1159
  return { point: picked.point, up: upAt(state, picked.point) };
831
1160
  }
832
- if (!globeShown(state)) {
833
- return null;
834
- }
835
1161
  const ellipsoid = ellipsoidOf(state);
836
1162
  const end = Cesium.Cartesian3.add(from, step, new Cesium.Cartesian3());
837
1163
  const carto = ellipsoid.cartesianToCartographic(end);
838
1164
  if (!carto) {
839
1165
  return null;
840
1166
  }
841
- const height = (_b = (_a = terrainHeightAt(state, carto)) !== null && _a !== void 0 ? _a : terrainHeightAt(state, ellipsoid.cartesianToCartographic(from))) !== null && _b !== void 0 ? _b : 0;
1167
+ // Unloaded terrain counts as sea level; an unknown stand-in tileset counts as nothing.
1168
+ const height = (_b = (_a = groundHeightAt(state, carto)) !== null && _a !== void 0 ? _a : groundHeightAt(state, ellipsoid.cartesianToCartographic(from))) !== null && _b !== void 0 ? _b : (globeShown(state) ? 0 : null);
1169
+ if (height == null) {
1170
+ return null;
1171
+ }
842
1172
  const point = Cesium.Cartesian3.fromRadians(carto.longitude, carto.latitude, height, ellipsoid);
843
1173
  return { point, up: ellipsoid.geodeticSurfaceNormal(point, new Cesium.Cartesian3()) };
844
1174
  }
@@ -888,12 +1218,77 @@ function resistDescent(state, from, step, picked, notches) {
888
1218
  const needed = scanScene(state).undergroundData ? PUSH_NOTCHES_DATA_BELOW : PUSH_NOTCHES_NOTHING_BELOW;
889
1219
  if (state.push.notches >= needed) {
890
1220
  state.push.notches = 0;
1221
+ state.surface = { above: false, through: true, until: now + SURFACE_HOLD_MS, at: null };
891
1222
  // A shallow push would land far along the ground to get under it, so it goes no further than a usual step.
892
1223
  const through = Math.min((above + GROUND_BREAK_DEPTH) / sink, Math.max(length, (above + GROUND_BREAK_DEPTH) * 4));
893
1224
  return Cesium.Cartesian3.multiplyByScalar(unit, through, new Cesium.Cartesian3());
894
1225
  }
895
1226
  return Cesium.Cartesian3.multiplyByScalar(unit, toStandOff, new Cesium.Cartesian3());
896
1227
  }
1228
+ /*
1229
+ * The camera's height above the loaded ground under it, or undefined without ground or in model-space.
1230
+ */
1231
+ function heightAboveGround(state) {
1232
+ if (state.getModelSpaceFrame || !hasGround(state)) {
1233
+ return undefined;
1234
+ }
1235
+ const carto = ellipsoidOf(state).cartesianToCartographic(state.viewer.camera.positionWC);
1236
+ const ground = carto ? groundHeightAt(state, carto) : undefined;
1237
+ return ground == null ? undefined : carto.height - ground;
1238
+ }
1239
+ /*
1240
+ * Notes whether the camera is above the ground as a zoom begins, unless it is being pushed through it.
1241
+ */
1242
+ function holdSurface(state) {
1243
+ var _a;
1244
+ const now = Date.now();
1245
+ if (state.surface.through && now < state.surface.until) {
1246
+ return;
1247
+ }
1248
+ const at = Cesium.Cartesian3.clone(state.viewer.camera.positionWC);
1249
+ // Ground not loaded yet counts as sea level, as it does for a descent.
1250
+ const above = (_a = heightAboveGround(state)) !== null && _a !== void 0 ? _a : (hasGround(state) && !state.getModelSpaceFrame ? heightOf(state, at) : undefined);
1251
+ state.surface = { above: above != null && above > 0, through: false, until: now + SURFACE_HOLD_MS, at };
1252
+ }
1253
+ /*
1254
+ * Lifts a camera a zoom kept above the ground back over finer ground that loads in higher.
1255
+ */
1256
+ function keepAboveGround(state) {
1257
+ const surface = state.surface;
1258
+ const now = Date.now();
1259
+ if (!surface.above || !state.ours || now > surface.until + SURFACE_LOADING_MS) {
1260
+ return;
1261
+ }
1262
+ // Ground that has finished loading does not move under a still camera.
1263
+ if (now > surface.until && !groundLoading(state)) {
1264
+ return;
1265
+ }
1266
+ const camera = state.viewer.camera;
1267
+ // A bookmark, fly-to or pan moved it since, which may mean to go below the ground.
1268
+ if (!surface.at || !Cesium.Cartesian3.equalsEpsilon(camera.positionWC, surface.at, 0, 1e-3)) {
1269
+ surface.above = false;
1270
+ return;
1271
+ }
1272
+ const above = heightAboveGround(state);
1273
+ if (above == null || above >= GROUND_STANDOFF / 2) {
1274
+ return;
1275
+ }
1276
+ const ellipsoid = ellipsoidOf(state);
1277
+ const carto = ellipsoid.cartesianToCartographic(camera.positionWC);
1278
+ Cesium.Cartesian3.fromRadians(carto.longitude, carto.latitude, carto.height - above + GROUND_STANDOFF, ellipsoid, camera.position);
1279
+ // What is left of the glide would take it straight back down.
1280
+ state.pendingZoom = new Cesium.Cartesian3();
1281
+ surface.at = Cesium.Cartesian3.clone(camera.position);
1282
+ state.viewer.scene.requestRender();
1283
+ }
1284
+ /*
1285
+ * Notes where a zoom just put the camera, so the ground hold can tell its moves from others.
1286
+ */
1287
+ function zoomMoved(state) {
1288
+ if (state.surface.at) {
1289
+ Cesium.Cartesian3.clone(state.viewer.camera.positionWC, state.surface.at);
1290
+ }
1291
+ }
897
1292
  /*
898
1293
  * What the scene holds that the camera cares about, rescanned at most every couple of seconds.
899
1294
  */
@@ -904,7 +1299,7 @@ function scanScene(state) {
904
1299
  return applyOverride(state, state.scan);
905
1300
  }
906
1301
  const scene = state.viewer.scene;
907
- const found = { at: now, undergroundData: false, groundTileset: false };
1302
+ const found = { at: now, undergroundData: false, groundTileset: false, ground: null };
908
1303
  // Seeing through the ground is turned on to look at what is under it.
909
1304
  if ((_b = (_a = scene.globe) === null || _a === void 0 ? void 0 : _a.translucency) === null || _b === void 0 ? void 0 : _b.enabled) {
910
1305
  found.undergroundData = true;
@@ -921,6 +1316,7 @@ function scanScene(state) {
921
1316
  }
922
1317
  if (primitive[GROUND_TILESET_KEY]) {
923
1318
  found.groundTileset = true;
1319
+ found.ground = primitive;
924
1320
  return;
925
1321
  }
926
1322
  if (!found.undergroundData && reachesBelowGround(state, lowestPointOf(primitive))) {