mirror of
https://github.com/exogen/t2-mapper.git
synced 2026-02-23 16:43:58 +00:00
22 lines
628 B
TypeScript
22 lines
628 B
TypeScript
import { useFrame, useThree } from "@react-three/fiber";
|
|
import { RefObject, useRef } from "react";
|
|
import { Object3D } from "three";
|
|
import { useWorldPosition } from "./useWorldPosition";
|
|
|
|
export function useDistanceFromCamera<T extends Object3D>(
|
|
ref: RefObject<T>,
|
|
): RefObject<number> {
|
|
const { camera } = useThree();
|
|
const distanceRef = useRef<number>(null);
|
|
const worldPosRef = useWorldPosition(ref);
|
|
|
|
useFrame(() => {
|
|
if (!worldPosRef.current) {
|
|
distanceRef.current = null;
|
|
} else {
|
|
distanceRef.current = camera.position.distanceTo(worldPosRef.current);
|
|
}
|
|
});
|
|
|
|
return distanceRef;
|
|
}
|