Tutorial: Using getScreenPosition to Overlay UI at a World Position
import { ViewController } from '@hology/core/gameplay';
import { useRenderLateUpdate, useService } from '@hology/react';
import { useRef } from 'react';
import { Vector3 } from 'three';
function WorldPositionMarker() {
const markerRef = useRef<HTMLDivElement>(null);
const view = useService(ViewController);
useRenderLateUpdate(() => {
// Replace (0,0,0) with any world position you want to track
const pos = view.getScreenPosition(new Vector3(0, 0, 0));
if (pos && markerRef.current) {
markerRef.current.style.visibility = 'visible';
markerRef.current.style.left = pos.x + 'px';
markerRef.current.style.top = pos.y + 'px';
} else if (markerRef.current) {
markerRef.current.style.visibility = 'hidden';
}
});
return (
<div
ref={markerRef}
style={{
position: 'absolute',
width: '20px',
height: '20px',
background: 'red',
borderRadius: '50%',
pointerEvents: 'none',
transform: 'translate(-50%, -50%)',
}}
/>
);
}
How it Works
Customization
Last updated