Skip to content

Commit

Permalink
chore(web): add some functionalities for pedestrian (#862)
Browse files Browse the repository at this point in the history
  • Loading branch information
keiya01 committed Dec 18, 2023
1 parent 02aac2b commit 293335b
Show file tree
Hide file tree
Showing 12 changed files with 329 additions and 0 deletions.
13 changes: 13 additions & 0 deletions web/src/beta/lib/core/Crust/Plugins/api.ts
Expand Up @@ -431,6 +431,10 @@ export function commonReearth({
computeGlobeHeight,
toXYZ,
toLngLatHeight,
convertScreenToPositionOffset,
isPositionVisible,
setView,
toWindowPosition,
enableScreenSpaceCameraController,
lookHorizontal,
lookVertical,
Expand Down Expand Up @@ -481,6 +485,10 @@ export function commonReearth({
computeGlobeHeight: GlobalThis["reearth"]["scene"]["computeGlobeHeight"];
toXYZ: GlobalThis["reearth"]["scene"]["toXYZ"];
toLngLatHeight: GlobalThis["reearth"]["scene"]["toLngLatHeight"];
convertScreenToPositionOffset: GlobalThis["reearth"]["scene"]["convertScreenToPositionOffset"];
isPositionVisible: GlobalThis["reearth"]["scene"]["isPositionVisible"];
setView: GlobalThis["reearth"]["camera"]["setView"];
toWindowPosition: GlobalThis["reearth"]["scene"]["toWindowPosition"];
inEditor: () => GlobalThis["reearth"]["scene"]["inEditor"];
built: () => GlobalThis["reearth"]["scene"]["built"];
enableScreenSpaceCameraController: GlobalThis["reearth"]["camera"]["enableScreenSpaceController"];
Expand Down Expand Up @@ -528,6 +536,7 @@ export function commonReearth({
moveRight,
moveOverTerrain,
flyToGround,
setView,
},
get property() {
return sceneProperty?.();
Expand Down Expand Up @@ -557,6 +566,9 @@ export function commonReearth({
computeGlobeHeight,
toXYZ,
toLngLatHeight,
convertScreenToPositionOffset,
isPositionVisible,
toWindowPosition,
pickManyFromViewport,
},
get viewport() {
Expand Down Expand Up @@ -588,6 +600,7 @@ export function commonReearth({
moveRight,
moveOverTerrain,
flyToGround,
setView,
},
layers: {
get layersInViewport() {
Expand Down
36 changes: 36 additions & 0 deletions web/src/beta/lib/core/Crust/Plugins/hooks.ts
Expand Up @@ -267,6 +267,34 @@ export default function ({
[engineRef],
);

const convertScreenToPositionOffset = useCallback(
(rawPosition: [x: number, y: number, z: number], screenOffset: [x: number, y: number]) => {
return engineRef?.convertScreenToPositionOffset(rawPosition, screenOffset);
},
[engineRef],
);

const isPositionVisible = useCallback(
(position: [x: number, y: number, z: number]) => {
return !!engineRef?.isPositionVisible(position);
},
[engineRef],
);

const setView = useCallback(
(camera: CameraPosition) => {
return engineRef?.setView(camera);
},
[engineRef],
);

const toWindowPosition = useCallback(
(position: [x: number, y: number, z: number]) => {
return engineRef?.toWindowPosition(position);
},
[engineRef],
);

const enableScreenSpaceCameraController = useCallback(
(enabled: boolean) => engineRef?.enableScreenSpaceCameraController(enabled),
[engineRef],
Expand Down Expand Up @@ -446,6 +474,10 @@ export default function ({
computeGlobeHeight,
toXYZ,
toLngLatHeight,
convertScreenToPositionOffset,
isPositionVisible,
setView,
toWindowPosition,
rotateRight,
orbit,
captureScreen,
Expand Down Expand Up @@ -511,6 +543,10 @@ export default function ({
computeGlobeHeight,
toXYZ,
toLngLatHeight,
convertScreenToPositionOffset,
isPositionVisible,
setView,
toWindowPosition,
lookHorizontal,
lookVertical,
moveForward,
Expand Down
9 changes: 9 additions & 0 deletions web/src/beta/lib/core/Crust/Plugins/plugin_types.ts
Expand Up @@ -141,6 +141,14 @@ export type Scene = {
z: number,
options?: { useGlobeEllipsoid?: boolean },
) => [lng: number, lat: number, height: number] | undefined;
readonly convertScreenToPositionOffset: (
rawPosition: [x: number, y: number, z: number],
screenOffset: [x: number, y: number],
) => [x: number, y: number, z: number] | undefined;
readonly isPositionVisible: (position: [x: number, y: number, z: number]) => boolean;
readonly toWindowPosition: (
position: [x: number, y: number, z: number],
) => [x: number, y: number] | undefined;
readonly pickManyFromViewport: (
windowPosition: [x: number, y: number],
windowWidth: number,
Expand Down Expand Up @@ -185,6 +193,7 @@ export type Camera = {
options?: CameraOptions,
offset?: number,
) => void;
readonly setView: (camera: CameraPosition) => void;
};

export type Clock = {
Expand Down
4 changes: 4 additions & 0 deletions web/src/beta/lib/core/Crust/Plugins/storybook.tsx
Expand Up @@ -97,6 +97,9 @@ export const context: Context = {
computeGlobeHeight: act("computeGlobeHeight"),
toXYZ: act("toXYZ"),
toLngLatHeight: act("toLngLatHeight"),
convertScreenToPositionOffset: act("convertScreenToPositionOffset"),
isPositionVisible: act("isPositionVisible"),
toWindowPosition: act("toWindowPosition"),
pickManyFromViewport: act("pickManyFromViewport"),
},
layers: {
Expand Down Expand Up @@ -148,6 +151,7 @@ export const context: Context = {
moveRight: act("moveRight"),
moveOverTerrain: act("moveOverTerrain"),
flyToGround: act("flyToGround"),
setView: act("setView"),
},
clock: {
startTime: new Date("2022-06-01"),
Expand Down
4 changes: 4 additions & 0 deletions web/src/beta/lib/core/Map/ref.ts
Expand Up @@ -26,6 +26,10 @@ const engineRefKeys: FunctionKeys<EngineRef> = {
computeGlobeHeight: 1,
toXYZ: 1,
toLngLatHeight: 1,
convertScreenToPositionOffset: 1,
isPositionVisible: 1,
setView: 1,
toWindowPosition: 1,
getViewport: 1,
lookAt: 1,
lookAtLayer: 1,
Expand Down
10 changes: 10 additions & 0 deletions web/src/beta/lib/core/Map/types/index.ts
Expand Up @@ -18,6 +18,7 @@ import type {
SelectedFeatureInfo,
Feature,
ComputedFeature,
CameraPosition,
} from "../../mantle";
import type { CameraOptions, FlyTo, FlyToDestination, LookAtDestination } from "../../types";
import type {
Expand Down Expand Up @@ -86,6 +87,15 @@ export type EngineRef = {
z: number,
options?: { useGlobeEllipsoid?: boolean },
) => [lng: number, lat: number, height: number] | undefined;
convertScreenToPositionOffset: (
rawPosition: [x: number, y: number, z: number],
screenOffset: [x: number, y: number],
) => [x: number, y: number, z: number] | undefined;
isPositionVisible: (position: [x: number, y: number, z: number]) => boolean;
setView: (camera: CameraPosition) => void;
toWindowPosition: (
position: [x: number, y: number, z: number],
) => [x: number, y: number] | undefined;
flyTo: FlyTo;
lookAt: (destination: LookAtDestination, options?: CameraOptions) => void;
lookAtLayer: (layerId: string) => void;
Expand Down
@@ -0,0 +1,48 @@
import { Meta, Story } from "@storybook/react";

import { engine } from "../..";
import Component, { Props } from "../../../../Map";

export default {
component: Component,
parameters: { actions: { argTypesRegex: "^on.*" } },
} as Meta;

const Template: Story<Props> = args => <Component {...args} />;

export const Default = Template.bind([]);
Default.args = {
engine: "cesium",
engines: {
cesium: engine,
},
ready: true,
layers: [
{
id: "l",
type: "simple",
data: {
type: "geojson",
value: {
type: "Feature",
geometry: {
type: "Point",
coordinates: [0, 0, 1000],
},
},
},
ellipse: {
radius: 1000,
fillColor: "#FF0000",
},
},
],
property: {
tiles: [
{
id: "default",
tile_type: "default",
},
],
},
};
94 changes: 94 additions & 0 deletions web/src/beta/lib/core/engines/Cesium/Feature/Ellipse/index.tsx
@@ -0,0 +1,94 @@
/* eslint-disable react-hooks/exhaustive-deps */
import { CallbackProperty, Cartesian3, PositionProperty } from "cesium";
import { useMemo } from "react";
import { EllipseGraphics } from "resium";

import { toColor } from "@reearth/beta/utils/value";

import type { EllipseAppearance } from "../../..";
import { classificationType, heightReference, shadowMode } from "../../common";
import {
EntityExt,
toDistanceDisplayCondition,
toTimeInterval,
type FeatureComponentConfig,
type FeatureProps,
} from "../utils";

export type Props = FeatureProps<Property>;

export type Property = EllipseAppearance;

export default function Ellipse({ id, isVisible, property, geometry, layer, feature }: Props) {
const { show = true } = property ?? {};
const coordinates = useMemo(
() => (geometry?.type === "Point" ? geometry.coordinates : undefined),
[geometry?.coordinates, geometry?.type],
);

const {
heightReference: hr,
shadows,
radius = 1000,
fillColor,
fill,
classificationType: ct,
} = property ?? {};
const { useTransition, translate } = layer?.transition ?? {};

console.log(translate, radius);

const pos = useMemo(
() =>
coordinates
? Cartesian3.fromDegrees(coordinates[0], coordinates[1], coordinates[2])
: undefined,
[coordinates],
);
const translateCallbackProperty = useMemo(
() =>
useTransition && translate
? new CallbackProperty(() => Cartesian3.fromDegrees(...translate), false)
: undefined,
[useTransition, translate],
);

const semiAxisProperty = useMemo(
() => (useTransition ? new CallbackProperty(() => radius, false) : radius),
[radius],
);

const material = useMemo(() => toColor(fillColor), [fillColor]);
const availability = useMemo(() => toTimeInterval(feature?.interval), [feature?.interval]);
const distanceDisplayCondition = useMemo(
() => toDistanceDisplayCondition(property?.near, property?.far),
[property?.near, property?.far],
);

return !isVisible || !pos || !show ? null : (
<EntityExt
id={id}
position={(translateCallbackProperty as unknown as PositionProperty) || pos}
layerId={layer?.id}
featureId={feature?.id}
draggable
availability={availability}
properties={feature?.properties}>
<EllipseGraphics
// Support drawing circle, not an ellipse.
semiMajorAxis={semiAxisProperty}
semiMinorAxis={semiAxisProperty}
material={material}
fill={fill}
heightReference={heightReference(hr)}
classificationType={classificationType(ct)}
shadows={shadowMode(shadows)}
distanceDisplayCondition={distanceDisplayCondition}
/>
</EntityExt>
);
}

export const config: FeatureComponentConfig = {
noLayer: true,
};
2 changes: 2 additions & 0 deletions web/src/beta/lib/core/engines/Cesium/Feature/index.tsx
Expand Up @@ -6,6 +6,7 @@ import { ComputedFeature, DataType, guessType } from "@reearth/beta/lib/core/man
import type { AppearanceTypes, FeatureComponentProps, ComputedLayer } from "../..";

import Box, { config as boxConfig } from "./Box";
import Ellipse, { config as ellipseConfig } from "./Ellipse";
import Ellipsoid, { config as ellipsoidConfig } from "./Ellipsoid";
import Frustum, { config as frustumConfig } from "./Frustum";
import Marker, { config as markerConfig } from "./Marker";
Expand Down Expand Up @@ -41,6 +42,7 @@ const components: Record<
polyline: [Polyline, polylineConfig],
polygon: [Polygon, polygonConfig],
ellipsoid: [Ellipsoid, ellipsoidConfig],
ellipse: [Ellipse, ellipseConfig],
model: [Model, modelConfig],
"3dtiles": [Tileset, tilesetConfig],
box: [Box, boxConfig],
Expand Down

0 comments on commit 293335b

Please sign in to comment.