The requestHitTestSourceForTransientInput() method of the XRSession interface returns a Promise that resolves with an XRTransientInputHitTestSource object that can be passed to XRFrame.getHitTestResultsForTransientInput().
requestHitTestSourceForTransientInput(options)
Rather than throwing true exceptions, requestHitTestSourceForTransientInput() 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.getHitTestResultsForTransientInput() to obtain the result.
const xrSession = navigator.xr.requestSession("immersive-ar", {
requiredFeatures: ["local", "hit-test"],
});
let transientHitTestSource = null;
xrSession
.requestHitTestSourceForTransientInput({
profile: "generic-touchscreen",
offsetRay: new XRRay(),
})
.then((touchScreenHitTestSource) => {
transientHitTestSource = touchScreenHitTestSource;
});
function onXRFrame(time, xrFrame) {
let hitTestResults = xrFrame.getHitTestResultsForTransientInput(
transientHitTestSource,
);
}