Skip to content

Commit

Permalink
perf: improve 3dtiles features calculation on reearth/core (#489)
Browse files Browse the repository at this point in the history
  • Loading branch information
keiya01 committed Feb 23, 2023
1 parent 324e284 commit 1204a65
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions src/core/engines/Cesium/Feature/Tileset/hooks.ts
Expand Up @@ -176,16 +176,38 @@ const useFeature = ({
const tileAppearance = useMemo(() => extractSimpleLayer(layer)?.["3dtiles"], [layer]);
const tileAppearanceShow = tileAppearance?.show;
const tileAppearanceColor = tileAppearance?.color;

// If styles are updated while features are calculating,
// we stop calculating features, and reassign styles.
const shouldSkipComputing = useRef(false);
useEffect(() => {
cachedFeaturesRef.current.map(f => {
shouldSkipComputing.current = true;
}, [tileAppearanceShow, tileAppearanceColor]);

const computeFeatures = useCallback(() => {
for (const f of cachedFeaturesRef.current) {
if (shouldSkipComputing.current) {
break;
}

const properties = f.feature.properties;
if (properties.show !== tileAppearanceShow || properties.color !== tileAppearanceColor) {
f.feature.properties.color = tileAppearanceColor;
f.feature.properties.show = tileAppearanceShow;
attachComputedFeature(f);
}
});
}
}, [tileAppearanceShow, tileAppearanceColor, attachComputedFeature]);

useEffect(() => {
computeFeatures();

// Computation is stopped, start re-calculating.
if (shouldSkipComputing.current) {
shouldSkipComputing.current = false;
computeFeatures();
}
}, [computeFeatures]);
};

export const useHooks = ({
Expand Down

0 comments on commit 1204a65

Please sign in to comment.