Camera Extensions
Extension methods for CameraComponent. They fall into two halves: converting between screen and world space, which needs no physics engine, and raycasting into the scene, which does - so those live in the Bepu and Bullet packages.
Screen and world space
In the Stride.CommunityToolkit.Engine namespace. Screen positions here are normalized: (0,0) is the top-left of the viewport and (1,1) the bottom-right.
ScreenToWorldRay()- ARayfrom the camera through a screen point. The usual starting point for object picking.ScreenToWorldPointOnXYPlane()- Where that ray crosses theZ=0plane, as aVector2, ornullwhen the ray is parallel to the plane or crosses it outside the near and far planes. This is how you turn a mouse position into a 2D world position without any physics at all.ScreenToWorldRayEnds()- The near and far points of the ray asVector4world positions, already divided byw.GetOrthographicVisibleSize()- The width and height an orthographic camera shows, in world units, read from its projection.GetOrthographicVisibleBounds()- That rectangle of the XY plane centred on the camera, as its lower-left and upper-right corners: the edges of a 2D scene's view.ScreenToWorldRaySegment()- The ray as aRaySegmentfrom the near plane to the far plane, which is the form the Bepu and BulletSimulationraycast helpers take.ScreenToWorldPoint()- Converts a screen position with a depth to a world position.WorldToScreenPoint()- The inverse: where a world position lands on screen. Use this to hang a label on an object.IsInScreenRectangle()- Whether a world position lands inside a rectangle of the screen, given as two normalized corners in either order: the test a marquee selection makes for every candidate. A position behind the camera is never inside.E05_3D_LayerSelectiondrags a rectangle and selects with it.TryWorldToScreenPoint()- The same in pixels, andfalsefor a position behind the camera or off the screen, so a label is only drawn for what is in view. An overload gives the normalized position.WorldToClip()- Converts a world position to clip space, one step earlier thanWorldToScreenPoint().LogicDirectionToWorldDirection()- Turns a 2D input direction into a world direction relative to where the camera is facing, so "forward" on a gamepad stick means forward on screen. An overload takes an explicit up vector.
Note
Most of these have ref/out overloads that avoid copying vectors, and WorldToScreenPoint() has one
taking a GraphicsDevice when you want pixels rather than normalized coordinates. See the
API reference for the full set.
Raycasting - Bepu
In the Stride.CommunityToolkit.Bepu namespace.
Raycast()- Casts from the camera through a screen position and reports the first hit as aHitInfo.RaycastMouse()- The same, reading the mouse position for you.
Both cast in the Bepu simulation of the camera's entity, so the camera must already be in the scene.
Raycasting - Bullet
In the Stride.CommunityToolkit.Bullet namespace. Both take either a ScriptComponent or a Simulation and return a HitResult.
Raycast()- Casts from the camera through a screen position, filtered by collision group.RaycastMouse()- The same, reading the mouse position for you.