Skip to content

Commit

Permalink
ar-hit-test: uses 1st tracked controller instead of headset orientati…
Browse files Browse the repository at this point in the history
…on (fix #5315) (#5308)

* ar-hit-test: uses first tracked controller instead of headset orientation

* Removes headset HitTest when controller HitTest created

* ar-hit-test: saves controller HitTest to cache

* ar-hit-test: fixes bug #5315: when selectend happens before the reticle is placed anywhere

* ar-hit-test: catches errors while updating anchor poses

* ar-hit-test: fixes bug: previous anchors now actually saved

---------

Co-authored-by: Diego Marcos <[email protected]>
  • Loading branch information
DougReeder and dmarcos authored Nov 22, 2024
1 parent 291a950 commit 3eadfd3
Showing 1 changed file with 36 additions and 15 deletions.
51 changes: 36 additions & 15 deletions src/components/scene/ar-hit-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ HitTest.prototype.sessionStart = function sessionStart (hitTestSourceDetails) {
};

/**
* Turns the last hit test into an anchor, the provided Object3D will have it's
* Turns the last hit test into an anchor, the provided Object3D will have its
* position update to track the anchor.
*
* @param {Object3D} object3D object to track
Expand Down Expand Up @@ -190,18 +190,18 @@ HitTest.updateAnchorPoses = function (frame, refSpace) {
try {
// Query most recent pose of the anchor relative to some reference space:
anchorPose = frame.getPose(anchor.anchorSpace, refSpace);
if (anchorPose) {
object3DOptions = HitTest.prototype.anchorToObject3D.get(anchor);
if (!object3DOptions) { return; }
offset = object3DOptions.offset;
object3D = object3DOptions.object3D;
applyPose(anchorPose, object3D, offset);
}
} catch (e) {
// This will fail if the anchor has been deleted that frame
}

if (anchorPose) {
object3DOptions = HitTest.prototype.anchorToObject3D.get(anchor);
if (!object3DOptions) { return; }
offset = object3DOptions.offset;
object3D = object3DOptions.object3D;
applyPose(anchorPose, object3D, offset);
console.error('while updating anchor poses:', e);
}
});
HitTest.prototype.previousFrameAnchors = trackedAnchors;
};

var hitTestCache;
Expand Down Expand Up @@ -285,15 +285,36 @@ module.exports.Component = register('ar-hit-test', {
// Default to selecting through the face
session.requestReferenceSpace('viewer')
.then(function (viewerSpace) {
this.hitTest = new HitTest(renderer, {
this.viewerHitTest = this.hitTest = new HitTest(renderer, {
space: viewerSpace
});

hitTestCache.set(viewerSpace, this.hitTest);

this.el.emit('ar-hit-test-start');
}.bind(this));

// If a tracked controller is available, selects via that instead of the headset
var arHitTestComp = this;
this.el.sceneEl.addEventListener('controllersupdated', function () {
var sceneEl = this;
var inputSources = sceneEl.xrSession && sceneEl.xrSession.inputSources;
if (!inputSources) { return; }
for (var i = 0; i < inputSources.length; ++i) {
if (inputSources[i].targetRayMode === 'tracked-pointer') {
arHitTestComp.hitTest = new HitTest(renderer, {
space: inputSources[i].targetRaySpace
});
hitTestCache.set(inputSources[i], arHitTestComp.hitTest);

if (arHitTestComp.viewerHitTest && typeof arHitTestComp.viewerHitTest.cancel === 'function') {
arHitTestComp.viewerHitTest.cancel();
arHitTestComp.viewerHitTest = null;
}

break; // only uses first tracked controller
}
}
});

// These are transient inputs so need to be handled separately
var profileToSupport = 'generic-touchscreen';
var transientHitTest = new HitTest(renderer, {
Expand Down Expand Up @@ -357,9 +378,9 @@ module.exports.Component = register('ar-hit-test', {
position: this.bboxMesh.position,
orientation: this.bboxMesh.quaternion
});
}

this.hitTest = null;
this.hitTest = null;
}
}.bind(this));
}.bind(this));

Expand Down

0 comments on commit 3eadfd3

Please sign in to comment.