The requestHitTestSource()
method of the XRSession
interface returns a Promise
that resolves with an XRHitTestSource
object that can be passed to XRFrame.getHitTestResults()
.
requestHitTestSource(options)
A Promise
that resolves with an XRHitTestSource
object.
Rather than throwing true exceptions, requestHitTestSource()
rejects the returned promise with a DOMException
, specifically, one of the following:
-
NotSupportedError
DOMException
-
Thrown if hit-test
is not an enabled feature in XRSystem.requestSession()
.
-
InvalidStateError
DOMException
-
Thrown if the session has already ended.
-
NotAllowedError
DOMException
-
Thrown if there is an unreasonable amount of requests. Some user agents might limit usage for privacy reasons.
To request a hit test source, start an XRSession
with the hit-test
session feature enabled. Next, configure the hit test source and store it for later use in the frame loop and call XRFrame.getHitTestResults()
to obtain the result.
const xrSession = navigator.xr.requestSession("immersive-ar", {
requiredFeatures: ["local", "hit-test"],
});
let hitTestSource = null;
xrSession
.requestHitTestSource({
space: viewerSpace,
offsetRay: new XRRay({ y: 0.5 }),
})
.then((viewerHitTestSource) => {
hitTestSource = viewerHitTestSource;
});
function onXRFrame(time, xrFrame) {
let hitTestResults = xrFrame.getHitTestResults(hitTestSource);
}