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 all 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
14 changes: 12 additions & 2 deletions src/CameraControls/useCenterGraph.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useThree } from '@react-three/fiber';
import { useCameraControls } from './useCameraControls';
import { useCallback, useLayoutEffect, useRef } from 'react';
import { useCallback, useLayoutEffect, useRef, useState } from 'react';
import { Vector3, Box3, PerspectiveCamera } from 'three';
import { useHotkeys } from 'reakeys';
import { getLayoutCenter } from '../utils/layout';
Expand Down Expand Up @@ -34,13 +34,19 @@ export interface CenterGraphOutput {
* If no ids are provided, the graph is centered on all nodes.
*/
centerNodesById: (ids?: string[]) => void;

/**
* Whether the graph is centered or not.
*/
isCentered?: boolean;
}

export const useCenterGraph = ({
animated,
disabled
}: CenterGraphInput): CenterGraphOutput => {
const nodes = useStore(state => state.nodes);
const [isCentered, setIsCentered] = useState<boolean>(false);
const invalidate = useThree(state => state.invalidate);
const { controls } = useCameraControls();
const camera = useThree(state => state.camera) as PerspectiveCamera;
Expand Down Expand Up @@ -72,6 +78,10 @@ export const useCenterGraph = ({
);
await controls.setTarget(x, y, z, animated);

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

invalidate();
}
},
Expand Down Expand Up @@ -126,5 +136,5 @@ export const useCenterGraph = ({
}
]);

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

// Center the graph on the nodes
const { centerNodesById } = useCenterGraph({
const { centerNodesById, isCentered } = useCenterGraph({
animated,
disabled
});
Expand Down Expand Up @@ -429,11 +429,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
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
Loading