Skip to content

Commit 3eadfd3

Browse files
DougReederdmarcos
andauthored
ar-hit-test: uses 1st tracked controller instead of headset orientation (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 <diego.marcos@gmail.com>
1 parent 291a950 commit 3eadfd3

File tree

1 file changed

+36
-15
lines changed

1 file changed

+36
-15
lines changed

src/components/scene/ar-hit-test.js

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ HitTest.prototype.sessionStart = function sessionStart (hitTestSourceDetails) {
9696
};
9797

9898
/**
99-
* Turns the last hit test into an anchor, the provided Object3D will have it's
99+
* Turns the last hit test into an anchor, the provided Object3D will have its
100100
* position update to track the anchor.
101101
*
102102
* @param {Object3D} object3D object to track
@@ -190,18 +190,18 @@ HitTest.updateAnchorPoses = function (frame, refSpace) {
190190
try {
191191
// Query most recent pose of the anchor relative to some reference space:
192192
anchorPose = frame.getPose(anchor.anchorSpace, refSpace);
193+
if (anchorPose) {
194+
object3DOptions = HitTest.prototype.anchorToObject3D.get(anchor);
195+
if (!object3DOptions) { return; }
196+
offset = object3DOptions.offset;
197+
object3D = object3DOptions.object3D;
198+
applyPose(anchorPose, object3D, offset);
199+
}
193200
} catch (e) {
194-
// This will fail if the anchor has been deleted that frame
195-
}
196-
197-
if (anchorPose) {
198-
object3DOptions = HitTest.prototype.anchorToObject3D.get(anchor);
199-
if (!object3DOptions) { return; }
200-
offset = object3DOptions.offset;
201-
object3D = object3DOptions.object3D;
202-
applyPose(anchorPose, object3D, offset);
201+
console.error('while updating anchor poses:', e);
203202
}
204203
});
204+
HitTest.prototype.previousFrameAnchors = trackedAnchors;
205205
};
206206

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

292-
hitTestCache.set(viewerSpace, this.hitTest);
293-
294292
this.el.emit('ar-hit-test-start');
295293
}.bind(this));
296294

295+
// If a tracked controller is available, selects via that instead of the headset
296+
var arHitTestComp = this;
297+
this.el.sceneEl.addEventListener('controllersupdated', function () {
298+
var sceneEl = this;
299+
var inputSources = sceneEl.xrSession && sceneEl.xrSession.inputSources;
300+
if (!inputSources) { return; }
301+
for (var i = 0; i < inputSources.length; ++i) {
302+
if (inputSources[i].targetRayMode === 'tracked-pointer') {
303+
arHitTestComp.hitTest = new HitTest(renderer, {
304+
space: inputSources[i].targetRaySpace
305+
});
306+
hitTestCache.set(inputSources[i], arHitTestComp.hitTest);
307+
308+
if (arHitTestComp.viewerHitTest && typeof arHitTestComp.viewerHitTest.cancel === 'function') {
309+
arHitTestComp.viewerHitTest.cancel();
310+
arHitTestComp.viewerHitTest = null;
311+
}
312+
313+
break; // only uses first tracked controller
314+
}
315+
}
316+
});
317+
297318
// These are transient inputs so need to be handled separately
298319
var profileToSupport = 'generic-touchscreen';
299320
var transientHitTest = new HitTest(renderer, {
@@ -357,9 +378,9 @@ module.exports.Component = register('ar-hit-test', {
357378
position: this.bboxMesh.position,
358379
orientation: this.bboxMesh.quaternion
359380
});
360-
}
361381

362-
this.hitTest = null;
382+
this.hitTest = null;
383+
}
363384
}.bind(this));
364385
}.bind(this));
365386

0 commit comments

Comments
 (0)