What a ray meets among the models of a scene, nearest first: what is under the pointer, with no
physics. A ray from screenToRay and the scene (sceneOf(self)) answer "what was clicked"; a ray
down from a piece, "what is it standing on".
It looks at the models inside where (a box, or several), the boxes themselves and everything in
them, as they are drawn: what is hidden is not met, nor what is pinned to the screen, nor the side
of a triangle its material throws away (cull, the back unless it says otherwise), so what is met
is what is seen. Each instance of createMeshInstances is met where it is, and says which one.
Met as its shape is built and placed, which is how most models are drawn. Not so for a model that
bends around its bones, met by its corners as its file gives them, before the bones move them;
one whose material moves its corners in its shader (a flag, grass in the wind, vertexSnap), met
where they were; and under a camera that follows a 2D world (follow2d), which draws every model
stood upright, met by its placement as it is, not as it is drawn. What is inside a picture drawn
in the game is not met from the scene: look in the picture's own box for that. A game with physics casts its rays there instead
(usePhysicsWorld3d().raycast), against its colliders and their layers.
Each model is tried against the box it fits in before any of its triangles, so a scene of ordinary
size is quick enough to ask every time the pointer moves.
Example
declare const Chip: (props: { x: number; y: number; z: number }) => void;
const Table = () => {
const camera = useCamera3d({ projection: 'perspective', transform: { y: 4, z: 6, rotationX: -0.6 } });
const game = useGame();
const scene = useSelf();
createMesh({ geometry: useCubeGeometry({ width: 6, height: 0.2, depth: 4 }), tint: '#1d6b3a' });
usePointer().onDown((info) => {
const ray = screenToRay(camera, game.getWidth(), game.getHeight(), info.screenX, info.screenY);
const hit = castRay(ray, scene)[0];
if (hit !== undefined) {
// A chip put down where the table was clicked.
useSpawn(Chip)({ x: hit.point.x, y: hit.point.y, z: hit.point.z });
}
});
return createScene();
};