Ray Casting in RealityKit
Ray casting is the new hit testing in ARKit 3
1 min readAug 18, 2019
This is part of my RealityKit Snippets collection of short code examples for getting things done in RealityKit.
About Ray Casting & Applications
- Convert from 2D screen space to 3D surfaces in world space
- Place content on a surface directly in front of the camera or where the user taps the screen
Simple Ray Cast Example
Make a ray cast from the center of the screen.
Tracked Ray Cast Example
ARKit will keep refining the ray cast as it learns more about detected surfaces.
Modifications
- Ray cast origin (
from: CGPoint
)
Change this parameter to any point on the screen. Combine ray casting with a gesture recognizer to allow users to place content on a selected surface - Plane type (
allowing: ARRaycastQuery.Target
)
Select which type of surfaces you want to search for. This parameter allows you to limit the search to existing planes or estimated planes. Check out Apple’s documentation for details about each target case. - Plane orientation (
alignment: ARRaycastQuery.TargetAlignment
)
Selecthorizontal
,vertical
, orany
planes.