Skip to content

Instantly share code, notes, and snippets.

View NachoToast's full-sized avatar

NachoToast NachoToast

View GitHub Profile
@NachoToast
NachoToast / cursorUtil.ts
Created September 24, 2023 23:04
NDC to XYZ Coordinates (ThreeJS)
/*
Translates normalised device coordinates (NCD) to world coordinates.
Calculate NCD using mouseMove/pointerMove event listeners:
mouseX = (e.clientX / window.innerWidth) * 2 - 1;
mouseY = -(e.clientY / window.innerHeight) * 2 + 1;
You can then test if it works by setting up a camera and a cursor mesh object in the scene,
copying its position from the return value every animation frame:
cursorMesh.position.copy(getCursorWorldCoordinates());