Skip to content

Commit

Permalink
chore(web): add flyToBBox (#865)
Browse files Browse the repository at this point in the history
  • Loading branch information
keiya01 authored Jan 10, 2024
1 parent a16870e commit 76ada0e
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 0 deletions.
4 changes: 4 additions & 0 deletions web/src/beta/lib/core/Crust/Plugins/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,7 @@ export function commonReearth({
isPositionVisible,
setView,
toWindowPosition,
flyToBBox,
enableScreenSpaceCameraController,
lookHorizontal,
lookVertical,
Expand Down Expand Up @@ -489,6 +490,7 @@ export function commonReearth({
isPositionVisible: GlobalThis["reearth"]["scene"]["isPositionVisible"];
setView: GlobalThis["reearth"]["camera"]["setView"];
toWindowPosition: GlobalThis["reearth"]["scene"]["toWindowPosition"];
flyToBBox: GlobalThis["reearth"]["camera"]["flyToBBox"];
inEditor: () => GlobalThis["reearth"]["scene"]["inEditor"];
built: () => GlobalThis["reearth"]["scene"]["built"];
enableScreenSpaceCameraController: GlobalThis["reearth"]["camera"]["enableScreenSpaceController"];
Expand Down Expand Up @@ -537,6 +539,7 @@ export function commonReearth({
moveOverTerrain,
flyToGround,
setView,
flyToBBox,
},
get property() {
return sceneProperty?.();
Expand Down Expand Up @@ -601,6 +604,7 @@ export function commonReearth({
moveOverTerrain,
flyToGround,
setView,
flyToBBox,
},
layers: {
get layersInViewport() {
Expand Down
12 changes: 12 additions & 0 deletions web/src/beta/lib/core/Crust/Plugins/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,16 @@ export default function ({
[engineRef],
);

const flyToBBox = useCallback(
(
bbox: [number, number, number, number],
options?: CameraOptions & { heading?: number; pitch?: number; range?: number },
) => {
return engineRef?.flyToBBox(bbox, options);
},
[engineRef],
);

const enableScreenSpaceCameraController = useCallback(
(enabled: boolean) => engineRef?.enableScreenSpaceCameraController(enabled),
[engineRef],
Expand Down Expand Up @@ -466,6 +476,7 @@ export default function ({
overrideSceneProperty: overrideScenePropertyCommon,
layersInViewport,
flyTo,
flyToBBox,
lookAt,
zoomIn,
zoomOut,
Expand Down Expand Up @@ -529,6 +540,7 @@ export default function ({
overrideScenePropertyCommon,
layersInViewport,
flyTo,
flyToBBox,
lookAt,
zoomIn,
zoomOut,
Expand Down
4 changes: 4 additions & 0 deletions web/src/beta/lib/core/Crust/Plugins/plugin_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,10 @@ export type Camera = {
readonly zoomOut: (amount: number, options?: CameraOptions) => void;
/** Moves the camera position to the specified destination. */
readonly flyTo: (destination: string | FlyToDestination, options?: CameraOptions) => void;
readonly flyToBBox: (
bbox: [number, number, number, number],
options?: CameraOptions & { heading?: number; pitch?: number; range?: number },
) => void;
/** Moves the camera position to look at the specified destination. */
readonly lookAt: (destination: LookAtDestination, options?: CameraOptions) => void;
/** Rotate the camera around the center of earth. */
Expand Down
1 change: 1 addition & 0 deletions web/src/beta/lib/core/Crust/Plugins/storybook.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ export const context: Context = {
getFovInfo: act("getFovInfo"),
enableScreenSpaceController: act("enableScreenSpaceController"),
flyTo: act("flyTo"),
flyToBBox: act("flyToBBox"),
lookAt: act("lookAt"),
zoomIn: act("zoomIn"),
zoomOut: act("zoomOut"),
Expand Down
1 change: 1 addition & 0 deletions web/src/beta/lib/core/Map/ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const engineRefKeys: FunctionKeys<EngineRef> = {
setView: 1,
toWindowPosition: 1,
getViewport: 1,
flyToBBox: 1,
lookAt: 1,
lookAtLayer: 1,
lookHorizontal: 1,
Expand Down
4 changes: 4 additions & 0 deletions web/src/beta/lib/core/Map/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ export type EngineRef = {
position: [x: number, y: number, z: number],
) => [x: number, y: number] | undefined;
flyTo: FlyTo;
flyToBBox: (
bbox: [number, number, number, number],
options?: CameraOptions & { heading?: number; pitch?: number; range?: number },
) => void;
lookAt: (destination: LookAtDestination, options?: CameraOptions) => void;
lookAtLayer: (layerId: string) => void;
zoomIn: (amount: number, options?: CameraOptions) => void;
Expand Down
27 changes: 27 additions & 0 deletions web/src/beta/lib/core/engines/Cesium/useEngineRef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,33 @@ export default function useEngineRef(
}
}
},
flyToBBox: (bbox, options) => {
const viewer = cesium.current?.cesiumElement;
if (!viewer || viewer.isDestroyed()) return;

cancelCameraFlight.current?.();

const boundingSphere = Cesium.BoundingSphere.fromRectangle3D(
Cesium.Rectangle.fromDegrees(...bbox),
);

const camera = viewer.scene.camera;

viewer.camera.flyToBoundingSphere(boundingSphere, {
offset: {
heading: options?.heading
? CesiumMath.toDegrees(options.heading)
: viewer.scene.camera.heading,
pitch: options?.pitch ? CesiumMath.toDegrees(options.pitch) : camera.pitch,
range: options?.range ?? 0,
},
duration: options?.duration,
});

cancelCameraFlight.current = () => {
camera?.cancelFlight();
};
},
lookAt: (camera, options) => {
const viewer = cesium.current?.cesiumElement;
if (!viewer || viewer.isDestroyed()) return;
Expand Down

0 comments on commit 76ada0e

Please sign in to comment.