Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hide labels based off distance from camera #187

Merged
merged 5 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/CameraControls/useCenterGraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ export const useCenterGraph = ({
disabled
}: CenterGraphInput): CenterGraphOutput => {
const nodes = useStore(state => state.nodes);
const isCentered = useStore(state => state.isCentered);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need to put this in the store based on the current useage, u could just return in state.

const setIsCentered = useStore(state => state.setIsCentered);
const invalidate = useThree(state => state.invalidate);
const { controls } = useCameraControls();
const camera = useThree(state => state.camera) as PerspectiveCamera;
Expand Down Expand Up @@ -72,6 +74,10 @@ export const useCenterGraph = ({
);
await controls.setTarget(x, y, z, animated);

if (!isCentered) {
setIsCentered(true);
}

invalidate();
}
},
Expand Down
13 changes: 8 additions & 5 deletions src/GraphScene.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ export const GraphScene: FC<GraphSceneProps & { ref?: Ref<GraphSceneRef> }> =
const nodes = useStore(state => state.nodes);
const edges = useStore(state => state.edges);
const clusters = useStore(state => [...state.clusters.values()]);
const isCentered = useStore(state => state.isCentered);

// Center the graph on the nodes
const { centerNodesById } = useCenterGraph({
Expand Down Expand Up @@ -429,11 +430,13 @@ export const GraphScene: FC<GraphSceneProps & { ref?: Ref<GraphSceneRef> }> =
);

return (
<Fragment>
{nodeComponents}
{edgeComponents}
{clusterComponents}
</Fragment>
isCentered && (
<Fragment>
{nodeComponents}
{edgeComponents}
{clusterComponents}
</Fragment>
)
Comment on lines +432 to +438
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could make more sense named mounted, but that's also not exactly what it is

isCentered is true when the camera re-orients from it's initial z position of 1000, which happens on big graphs

Without this the label visibility calculation is usually off on the initial render

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also gets rid of that laggy zoom out/jump on initial big graph renders which I think was previously being handled here with a mounted && ...

);
}
);
Expand Down
7 changes: 6 additions & 1 deletion src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export interface GraphState {
drags?: DragReferences;
panning?: boolean;
theme: Theme;
isCentered: boolean;
setTheme: (theme: Theme) => void;
setClusters: (clusters: Map<string, ClusterGroup>) => void;
setPanning: (panning: boolean) => void;
Expand All @@ -48,6 +49,7 @@ export interface GraphState {
setEdges: (edges: InternalGraphEdge[]) => void;
setNodePosition: (id: string, position: InternalGraphPosition) => void;
setCollapsedNodeIds: (nodeIds: string[]) => void;
setIsCentered: (isCentered: boolean) => void;
}

export const { Provider, useStore } = createContext<StoreApi<GraphState>>();
Expand Down Expand Up @@ -81,6 +83,7 @@ export const createStore = ({
selections,
drags: {},
graph: new Graph({ multi: true }),
isCentered: false,
setTheme: theme => set(state => ({ ...state, theme })),
setClusters: clusters => set(state => ({ ...state, clusters })),
setEdgeContextMenus: edgeContextMenus =>
Expand Down Expand Up @@ -133,5 +136,7 @@ export const createStore = ({
};
}),
setCollapsedNodeIds: (nodeIds = []) =>
set(state => ({ ...state, collapsedNodeIds: nodeIds }))
set(state => ({ ...state, collapsedNodeIds: nodeIds })),
setIsCentered: (isCentered: boolean) =>
set(state => ({ ...state, isCentered }))
}));
26 changes: 25 additions & 1 deletion src/useGraph.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { useRef, useCallback, useEffect, useMemo } from 'react';
import { useThree } from '@react-three/fiber';
import { PerspectiveCamera } from 'three';
import { SizingType } from './sizing';
import {
LayoutTypes,
layoutProvider,
LayoutStrategy,
LayoutOverrides
} from './layout';
import { LabelVisibilityType } from './utils/visibility';
import { LabelVisibilityType, calcLabelVisibility } from './utils/visibility';
import { tick } from './layout/layoutUtils';
import { GraphEdge, GraphNode } from './types';
import { buildGraph, transformGraph } from './utils/graph';
Expand Down Expand Up @@ -51,6 +53,7 @@ export const useGraph = ({
const setClusters = useStore(state => state.setClusters);
const stateCollapsedNodeIds = useStore(state => state.collapsedNodeIds);
const setEdges = useStore(state => state.setEdges);
const stateNodes = useStore(state => state.nodes);
const setNodes = useStore(state => state.setNodes);
const setSelections = useStore(state => state.setSelections);
const setActives = useStore(state => state.setActives);
Expand All @@ -59,6 +62,7 @@ export const useGraph = ({
const setCollapsedNodeIds = useStore(state => state.setCollapsedNodeIds);
const layoutMounted = useRef<boolean>(false);
const layout = useRef<LayoutStrategy | null>(null);
const camera = useThree(state => state.camera) as PerspectiveCamera;

const { visibleEdges, visibleNodes } = useMemo(
() =>
Expand Down Expand Up @@ -132,6 +136,26 @@ export const useGraph = ({
]
);

useEffect(() => {
const nodes = stateNodes.map(node => ({
...node,
labelVisible: calcLabelVisibility({
nodeCount: stateNodes?.length,
labelType,
camera,
nodePosition: node?.position
})('node', node?.size)
}));

const isVisibilityUpdated = nodes.some(
(node, i) => node.labelVisible !== stateNodes[i].labelVisible
);

if (isVisibilityUpdated) {
setNodes(nodes);
}
}, [camera, camera.zoom, camera.position.z, setNodes, stateNodes, labelType]);

useEffect(() => {
// Let's set the store selections so its easier to access
if (layoutMounted.current) {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export function transformGraph({
});

const nodeCount = graph.nodes.length;
const checkVisibility = calcLabelVisibility(nodeCount, labelType);
const checkVisibility = calcLabelVisibility({ nodeCount, labelType });

graph.forEachNode((id, node) => {
const position = layout.getNodePosition(id);
Expand Down
34 changes: 26 additions & 8 deletions src/utils/visibility.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,39 @@
import { PerspectiveCamera } from 'three';
import { EdgeLabelPosition } from '../symbols';

const NODE_THRESHOLD = 20;

export type LabelVisibilityType = 'all' | 'auto' | 'none' | 'nodes' | 'edges';

export function calcLabelVisibility(
nodeCount: number,
type: LabelVisibilityType
) {
interface CalcLabelVisibilityArgs {
nodeCount: number;
nodePosition?: { x: number; y: number; z: number };
labelType: LabelVisibilityType;
camera?: PerspectiveCamera;
}

export function calcLabelVisibility({
nodeCount,
nodePosition,
labelType,
camera
}: CalcLabelVisibilityArgs) {
return (shape: 'node' | 'edge', size: number) => {
if (type === 'all') {
if (
camera &&
nodePosition &&
camera?.position?.z / camera?.zoom - nodePosition?.z > 6000
) {
return false;
}

if (labelType === 'all') {
return true;
} else if (type === 'nodes' && shape === 'node') {
} else if (labelType === 'nodes' && shape === 'node') {
return true;
} else if (type === 'edges' && shape === 'edge') {
} else if (labelType === 'edges' && shape === 'edge') {
return true;
} else if (type === 'auto' && shape === 'node') {
} else if (labelType === 'auto' && shape === 'node') {
if (nodeCount <= NODE_THRESHOLD) {
return true;
} else {
Expand Down