Camera Ray Casting (Pixel Picking)
Code#
Cast a ray from a window pixel:
if (loop.Input.KeyboardAndMouse.NewMouseClicks.Count > 0) {
var clickLocation = loop.Input.KeyboardAndMouse.NewMouseClicks[0].Location; // (1)!
var ray = renderer.CastRayFromRenderSurface(clickLocation); // (2)!
}
-
This example converts the location of a mouse click on a
Windowto a ray starting at the camera's near plane.You don't need to use a mouse click however;
CastRayFromRenderSurface()simply takes anXYPair<int>indicating which pixel on theWindowthe resultantRayshould eminate from. -
The
clickLocationparameter indicates which pixel on the render output surface (e.g. window) the ray should be cast from.The
CameraandWindow(orRenderOutputBuffer) that thisrendererwas built with will be used to create the resultantRay.
Cast a ray from a camera's near plane:
// With a camera instance
var ray = camera.CastRayFromNearPlane(); // (1)!
var ray = camera.CastRayFromNearPlane(new XYPair<float>(0f, 0f)); // (2)!
// Without a camera instance
var ray = CameraUtils.CreateRayFromPerspectiveCameraParameters( // (3)!
modelMatrix,
projectionMatrix,
rayCoord // (4)!
);
var ray = CameraUtils.CreateRayFromPerspectiveCameraParameters(
cameraPosition,
cameraViewDirection,
cameraUpDirection,
nearPlaneDistance,
farPlaneDistance,
verticalFov,
aspectRatio,
rayCoord
);
-
This overload casts a ray from the very centre of the camera's near plane.
-
This overload takes an
XYPair<float>specifying where on the near plane the ray should originate from.This coordinate pair is expected as a normalized device coordinate, e.g. X and Y both in the range
-1to1(where(-1, -1)is the bottom left corner).(0, 0)indicates the ray should emanate from the very centre of the camera's view (identical to the overload on the line above). -
CreateRayFromPerspectiveCameraParametersworks assuming a perspective projection. UseCreateRayFromOrthographicCameraParametersfor an orthographic projection.The default for most cameras is a perspective projection.
-
This argument specifies where on the near plane the ray should originate from; and is expected as a normalized device coordinate, e.g. X and Y both in the range
-1to1(where(-1, -1)is the bottom left corner).
Explanation#
The code above demonstrates how to "pixel pick" in TinyFFR; this is where you convert a mouse click on a window to a ray cast out from the camera in to the 3D world.
The resultant Ray will have the following properties:
-
StartPoint -
This
Locationwill indicate the point on the camera's near plane that the ray originates from. -
Direction -
This
Directionindicates where the ray points out from theStartPointand is guaranteed to point within the camera's frustum.
A Ray has infinite length. If you want to limit its length to the camera's far plane, you can create a BoundedRay like so: var boundedRay = ray.ToBoundedRay(camera.FarPlaneDistance - camera.NearPlaneDistance);