Skip to content
This repository has been archived by the owner on Apr 25, 2023. It is now read-only.

Commit

Permalink
fix: correct flyToGround destination camera (#356)
Browse files Browse the repository at this point in the history
* fix: correct destination camera

* refactor: clean up

* refactor: clean up
  • Loading branch information
airslice committed Nov 18, 2022
1 parent ec0b81d commit 2257586
Showing 1 changed file with 12 additions and 17 deletions.
29 changes: 12 additions & 17 deletions src/components/molecules/Visualizer/Engine/Cesium/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import { tweenInterval } from "@reearth/util/raf";
import { Camera } from "@reearth/util/value";

import { CameraOptions, Clock } from "../../Plugin/types";
import { FlyToDestination } from "../ref";

export const layerIdField = `__reearth_layer_id`;

Expand Down Expand Up @@ -630,41 +631,34 @@ export function moveRight(scene: Scene, amount: number) {
}

export async function moveOverTerrain(viewer: Viewer, offset = 0) {
const camera = viewer.scene.camera;
const height = await sampleTerrainHeight(viewer.scene, camera.position);
const camera = getCamera(viewer);
if (camera?.lng === undefined || camera?.lat === undefined) return;
const height = await sampleTerrainHeight(viewer.scene, camera.lng, camera.lat);
if (height && height !== 0) {
const innerCamera = getCamera(viewer);
if (innerCamera && innerCamera?.height < height + offset) {
camera.moveUp(height + offset - innerCamera.height);
viewer.scene.camera.moveUp(height + offset - innerCamera.height);
}
}
}

export async function flyToGround(
viewer: Viewer,
cancelCameraFlight: MutableRefObject<(() => void) | undefined>,
camera?: {
lat?: number;
lng?: number;
height?: number;
heading?: number;
pitch?: number;
roll?: number;
fov?: number;
},
camera: FlyToDestination,
options?: {
duration?: number;
easing?: (time: number) => number;
},
offset = 0,
) {
const height = await sampleTerrainHeight(viewer.scene, viewer.scene.camera.position);
if (camera.lng === undefined || camera.lat === undefined) return;
const height = await sampleTerrainHeight(viewer.scene, camera.lng, camera.lat);
const tarHeight = height ? height + offset : offset;
const groundCamera = { ...camera, height: tarHeight };
cancelCameraFlight.current?.();
cancelCameraFlight.current = flyTo(
viewer.scene?.camera,
{ ...getCamera(viewer), ...groundCamera },
{ ...getCamera(viewer), ...camera, height: tarHeight },
options,
);
}
Expand All @@ -689,13 +683,14 @@ function rotateVectorAboutAxis(vector: Cartesian3, rotateAxis: Cartesian3, rotat

async function sampleTerrainHeight(
scene: Scene,
position: Cartesian3,
lng: number,
lat: number,
): Promise<number | undefined> {
const terrainProvider = scene.terrainProvider;
if (terrainProvider instanceof EllipsoidTerrainProvider) return 0;

const [sample] = await sampleTerrainMostDetailed(terrainProvider, [
Cartographic.fromCartesian(position, scene.globe.ellipsoid, new Cartographic()),
Cartographic.fromDegrees(lng, lat),
]);
return sample.height;
}

0 comments on commit 2257586

Please sign in to comment.