Skip to content

Commit

Permalink
fix: handle featureId for 3dtiles and compat select plugin api on ree…
Browse files Browse the repository at this point in the history
…arth/core (#417)
  • Loading branch information
keiya01 committed Feb 8, 2023
1 parent 3f51f28 commit 9144adb
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
8 changes: 5 additions & 3 deletions src/core/Crust/Plugins/api.ts
@@ -1,5 +1,5 @@
import type { Tag } from "@reearth/core/mantle/compat";
import type { Events, Layer, NaiveLayer } from "@reearth/core/Map";
import type { Events, Layer, LayerSelectionReason, LayersRef, NaiveLayer } from "@reearth/core/Map";
import { merge } from "@reearth/util/object";

import type { Block } from "../Infobox";
Expand Down Expand Up @@ -332,7 +332,7 @@ export function commonReearth({
selectedLayer: () => GlobalThis["reearth"]["layers"]["selected"];
layerSelectionReason: () => GlobalThis["reearth"]["layers"]["selectionReason"];
layerOverriddenProperties?: () => GlobalThis["reearth"]["layers"]["overriddenProperties"];
selectLayer: GlobalThis["reearth"]["layers"]["select"];
selectLayer: LayersRef["select"];
layersInViewport: GlobalThis["reearth"]["layers"]["layersInViewport"];
showLayer: GlobalThis["reearth"]["layers"]["show"];
hideLayer: GlobalThis["reearth"]["layers"]["hide"];
Expand Down Expand Up @@ -444,7 +444,9 @@ export function commonReearth({
return layersInViewport;
},
get select() {
return selectLayer;
// For compat
return (layerId: string | undefined, reason?: LayerSelectionReason | undefined) =>
selectLayer?.(layerId, undefined, reason);
},
get show() {
return showLayer;
Expand Down
5 changes: 5 additions & 0 deletions src/core/Crust/Plugins/plugin_types.ts
Expand Up @@ -63,6 +63,7 @@ export type Reearth = {
| "overrideProperties"
| "override"
| "add"
| "select"
| "addAll"
| "replace"
| "deleteLayer"
Expand All @@ -73,6 +74,10 @@ export type Reearth = {
readonly overriddenProperties?: OverriddenLayer[];
readonly overrideProperty?: WrappedRef<LayersRef>["overrideProperties"];
readonly add?: (layer: NaiveLayer) => string | undefined;
readonly select?: (
layerId: string | undefined,
reason?: LayerSelectionReason | undefined,
) => void;
selectionReason?: LayerSelectionReason;
overriddenInfobox?: LayerSelectionReason["overriddenInfobox"];
tags?: Tag[];
Expand Down
17 changes: 7 additions & 10 deletions src/core/engines/Cesium/Feature/Tileset/hooks.ts
Expand Up @@ -127,23 +127,19 @@ const useFeature = ({
return;
}

const currentTiles: Map<Cesium3DTile, number> = new Map();
const currentTiles: Map<Cesium3DTile, string> = new Map();

tileset.current?.tileLoad.addEventListener((t: Cesium3DTile) => {
if (currentTiles.has(t)) {
return;
}

const tiles = Array.from(currentTiles.values());
const prevFeatureId = tiles[tiles.length - 1] || 0;
const nextFeatureId = t.content.featuresLength + prevFeatureId;

currentTiles.set(t, nextFeatureId);

const tempFeatures: Feature[] = [];
const tempComputedFeatures: ComputedFeature[] = [];
lookupFeatures(t.content, (tileFeature, content) => {
const coordinates = content.tile.boundingSphere.center;
const nextFeatureId = String(tileFeature.featureId);
currentTiles.set(t, nextFeatureId);
const id = String(nextFeatureId + tileFeature.featureId);
const feature = (() => {
const normalFeature = makeFeatureFrom3DTile(id, tileFeature, [
Expand Down Expand Up @@ -172,11 +168,12 @@ const useFeature = ({
});

tileset.current?.tileUnload.addEventListener((t: Cesium3DTile) => {
const nextFeatureId = currentTiles.get(t) || 0;
currentTiles.delete(t);
const featureIds: string[] = [];
lookupFeatures(t.content, tileFeature => {
const id = String(nextFeatureId + tileFeature.featureId);
lookupFeatures(t.content, (tileFeature, content) => {
const coordinates = content.tile.boundingSphere.center;
const id =
`${tileFeature.featureId}` ?? `${coordinates.x}-${coordinates.y}-${coordinates.z}`;
featureIds.push(id);
});
onFeatureDelete?.(featureIds);
Expand Down

0 comments on commit 9144adb

Please sign in to comment.