Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebXR fix Hit Test providing multiple results #5778

Merged
merged 4 commits into from Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 29 additions & 1 deletion examples/src/examples/xr/ar-hit-test.mjs
Expand Up @@ -61,7 +61,7 @@ async function example({ canvas }) {
target.addComponent("render", {
type: "cylinder"
});
target.setLocalScale(0.5, 0.01, 0.5);
target.setLocalScale(0.1, 0.01, 0.1);
app.root.addChild(target);

if (app.xr.supported) {
Expand Down Expand Up @@ -140,6 +140,34 @@ async function example({ canvas }) {
}
});

if (app.xr.hitTest.supported) {
app.xr.input.on('add', (inputSource) => {
inputSource.hitTestStart({
entityTypes: [pc.XRTRACKABLE_POINT, pc.XRTRACKABLE_PLANE],
callback: (err, hitTestSource) => {
if (err) return;

let target = new pc.Entity();
target.addComponent("render", {
type: "cylinder"
});
target.setLocalScale(0.1, 0.01, 0.1);
app.root.addChild(target);

hitTestSource.on('result', (position, rotation) => {
target.setPosition(position);
target.setRotation(rotation);
});

hitTestSource.once('remove', () => {
target.destroy();
target = null;
});
}
});
});
}

if (!app.xr.isAvailable(pc.XRTYPE_AR)) {
message("Immersive AR is not available");
} else if (!app.xr.hitTest.supported) {
Expand Down
37 changes: 27 additions & 10 deletions src/framework/xr/xr-hit-test-source.js
Expand Up @@ -132,23 +132,40 @@ class XrHitTestSource extends EventHandler {
* @private
*/
updateHitResults(results, inputSource) {
if (inputSource && !inputSource.hitTestSourcesSet.has(this))
return;

const origin = poolVec3.pop() ?? new Vec3();

if (inputSource) {
origin.copy(inputSource.getOrigin());
} else {
origin.copy(this.manager.camera.getPosition());
}

let candidateDistance = Infinity;

const position = poolVec3.pop() ?? new Vec3();
const rotation = poolQuat.pop() ?? new Quat();

for (let i = 0; i < results.length; i++) {
const pose = results[i].getPose(this.manager._referenceSpace);

let position = poolVec3.pop();
if (!position) position = new Vec3();
position.copy(pose.transform.position);
const distance = origin.distance(pose.transform.position);
if (distance >= candidateDistance)
continue;

let rotation = poolQuat.pop();
if (!rotation) rotation = new Quat();
candidateDistance = distance;
position.copy(pose.transform.position);
rotation.copy(pose.transform.orientation);
}

this.fire('result', position, rotation, inputSource);
this.manager.hitTest.fire('result', this, position, rotation, inputSource);
this.fire('result', position, rotation, inputSource);
this.manager.hitTest.fire('result', this, position, rotation, inputSource);

poolVec3.push(position);
poolQuat.push(rotation);
}
poolVec3.push(origin);
poolVec3.push(position);
poolQuat.push(rotation);
}
}

Expand Down
8 changes: 8 additions & 0 deletions src/framework/xr/xr-input-source.js
Expand Up @@ -139,6 +139,12 @@ class XrInputSource extends EventHandler {
*/
_hitTestSources = [];

/**
* @type {Set<import('./xr-hit-test-source.js').XrHitTestSource>}
* @ignore
*/
hitTestSourcesSet = new Set();

/**
* Create a new XrInputSource instance.
*
Expand Down Expand Up @@ -610,6 +616,7 @@ class XrInputSource extends EventHandler {
* @private
*/
onHitTestSourceAdd(hitTestSource) {
this.hitTestSourcesSet.add(hitTestSource);
this._hitTestSources.push(hitTestSource);

this.fire('hittest:add', hitTestSource);
Expand All @@ -632,6 +639,7 @@ class XrInputSource extends EventHandler {
* @private
*/
onHitTestSourceRemove(hitTestSource) {
this.hitTestSourcesSet.delete(hitTestSource);
const ind = this._hitTestSources.indexOf(hitTestSource);
if (ind !== -1) this._hitTestSources.splice(ind, 1);
}
Expand Down