Skip to content

Commit

Permalink
feat: add sampleTerrainHeight on reearht/core (#466)
Browse files Browse the repository at this point in the history
feat: add sampleTerrainHeight api on reeath/core
  • Loading branch information
keiya01 authored Feb 16, 2023
1 parent 4c89aa2 commit 55334ec
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/core/Crust/Plugins/PluginFrame/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ export type Ref = {
arena: () => Arena | undefined;
};

const AsyncFunction = (async () => {}).constructor;

// restrict any classes
export const defaultIsMarshalable = (obj: any): boolean => {
return (
Expand All @@ -53,7 +55,8 @@ export const defaultIsMarshalable = (obj: any): boolean => {
Object.getPrototypeOf(obj) === Function.prototype ||
Object.getPrototypeOf(obj) === Object.prototype ||
obj instanceof Date ||
obj instanceof Promise
obj instanceof Promise ||
obj instanceof AsyncFunction
);
};

Expand Down
3 changes: 3 additions & 0 deletions src/core/Crust/Plugins/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ export function commonReearth({
rotateRight,
captureScreen,
getLocationFromScreen,
sampleTerrainHeight,
enableScreenSpaceCameraController,
lookHorizontal,
lookVertical,
Expand Down Expand Up @@ -350,6 +351,7 @@ export function commonReearth({
cameraViewport?: () => GlobalThis["reearth"]["camera"]["viewport"];
captureScreen: GlobalThis["reearth"]["scene"]["captureScreen"];
getLocationFromScreen: GlobalThis["reearth"]["scene"]["getLocationFromScreen"];
sampleTerrainHeight: GlobalThis["reearth"]["scene"]["sampleTerrainHeight"];
inEditor: () => GlobalThis["reearth"]["scene"]["inEditor"];
enableScreenSpaceCameraController: GlobalThis["reearth"]["camera"]["enableScreenSpaceController"];
lookHorizontal: GlobalThis["reearth"]["camera"]["lookHorizontal"];
Expand Down Expand Up @@ -411,6 +413,7 @@ export function commonReearth({
overrideProperty: overrideSceneProperty,
captureScreen,
getLocationFromScreen,
sampleTerrainHeight,
},
get viewport() {
return viewport?.();
Expand Down
9 changes: 9 additions & 0 deletions src/core/Crust/Plugins/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,13 @@ export default function ({
[engineRef],
);

const sampleTerrainHeight = useCallback(
async (lng: number, lat: number) => {
return await engineRef?.sampleTerrainHeight(lng, lat);
},
[engineRef],
);

const enableScreenSpaceCameraController = useCallback(
(enabled: boolean) => engineRef?.enableScreenSpaceCameraController(enabled),
[engineRef],
Expand Down Expand Up @@ -298,6 +305,7 @@ export default function ({
orbit,
captureScreen,
getLocationFromScreen,
sampleTerrainHeight,
enableScreenSpaceCameraController,
lookHorizontal,
lookVertical,
Expand Down Expand Up @@ -341,6 +349,7 @@ export default function ({
flyTo,
flyToGround,
getLocationFromScreen,
sampleTerrainHeight,
hideLayer,
lookHorizontal,
lookVertical,
Expand Down
1 change: 1 addition & 0 deletions src/core/Crust/Plugins/plugin_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ export type Scene = {
y: number,
withTerrain?: boolean,
) => LatLngHeight | undefined;
readonly sampleTerrainHeight: (lng: number, lat: number) => Promise<number | undefined>;
};

export type Camera = {
Expand Down
1 change: 1 addition & 0 deletions src/core/Crust/Plugins/storybook.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ export const context: Context = {
overrideProperty: act("overrideSceneProperty"),
captureScreen: act("captureScreen"),
getLocationFromScreen: act("getLocationFromScreen"),
sampleTerrainHeight: act("sampleTerrainHeight"),
},
layers: {
hide: act("layers.hide"),
Expand Down
1 change: 1 addition & 0 deletions src/core/Map/ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const engineRefKeys: FunctionKeys<EngineRef> = {
getCamera: 1,
getClock: 1,
getLocationFromScreen: 1,
sampleTerrainHeight: 1,
getViewport: 1,
lookAt: 1,
lookAtLayer: 1,
Expand Down
1 change: 1 addition & 0 deletions src/core/Map/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export type EngineRef = {
getViewport: () => Rect | undefined;
getCamera: () => Camera | undefined;
getLocationFromScreen: (x: number, y: number, withTerrain?: boolean) => LatLngHeight | undefined;
sampleTerrainHeight: (lng: number, lat: number) => Promise<number | undefined>;
flyTo: (target: string | FlyToDestination, options?: CameraOptions) => void;
lookAt: (destination: LookAtDestination, options?: CameraOptions) => void;
lookAtLayer: (layerId: string) => void;
Expand Down
6 changes: 6 additions & 0 deletions src/core/engines/Cesium/useEngineRef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
getCenterCamera,
zoom,
lookAtWithoutAnimation,
sampleTerrainHeight,
} from "./common";
import { findEntity } from "./utils";

Expand Down Expand Up @@ -69,6 +70,11 @@ export default function useEngineRef(
if (!viewer || viewer.isDestroyed()) return;
return getLocationFromScreen(viewer.scene, x, y, withTerrain);
},
sampleTerrainHeight: async (lng, lat) => {
const viewer = cesium.current?.cesiumElement;
if (!viewer || viewer.isDestroyed()) return;
return await sampleTerrainHeight(viewer.scene, lng, lat);
},
flyTo: (target, options) => {
if (target && typeof target === "object") {
const viewer = cesium.current?.cesiumElement;
Expand Down

0 comments on commit 55334ec

Please sign in to comment.