Skip to content

Commit

Permalink
Allow XR near interaction to interact with all meshes and GUI (#14787)
Browse files Browse the repository at this point in the history
* Allow XR near interaction to interact with all meshes and GUI

* update doc and function name
  • Loading branch information
RaananW committed Feb 19, 2024
1 parent dc765d4 commit 7954684
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
27 changes: 23 additions & 4 deletions packages/dev/core/src/Culling/ray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -557,11 +557,27 @@ export class Ray {
* @returns the new ray
*/
public static CreateNewFromTo(origin: Vector3, end: Vector3, world: DeepImmutable<Matrix> = Matrix.IdentityReadOnly): Ray {
const direction = end.subtract(origin);
const result = new Ray(new Vector3(0, 0, 0), new Vector3(0, 0, 0));
return Ray.CreateFromToToRef(origin, end, world, result);
}

/**
* Function will update a transformed ray starting from origin and ending at the end point. Ray's length will be set, and ray will be
* transformed to the given world matrix.
* @param origin The origin point
* @param end The end point
* @param world a matrix to transform the ray to. Default is the identity matrix.
* @param result the object to store the result
* @returns the ref ray
*/
public static CreateFromToToRef(origin: Vector3, end: Vector3, world: DeepImmutable<Matrix> = Matrix.IdentityReadOnly, result: Ray): Ray {
result.origin.copyFrom(origin);
const direction = end.subtractToRef(origin, result.direction);
const length = Math.sqrt(direction.x * direction.x + direction.y * direction.y + direction.z * direction.z);
direction.normalize();
result.length = length;
result.direction.normalize();

return Ray.Transform(new Ray(origin, direction, length), world);
return Ray.TransformToRef(result, world, result);
}

/**
Expand All @@ -582,8 +598,9 @@ export class Ray {
* @param ray ray to transform
* @param matrix matrix to apply
* @param result ray to store result in
* @returns the updated result ray
*/
public static TransformToRef(ray: DeepImmutable<Ray>, matrix: DeepImmutable<Matrix>, result: Ray): void {
public static TransformToRef(ray: DeepImmutable<Ray>, matrix: DeepImmutable<Matrix>, result: Ray): Ray {
Vector3.TransformCoordinatesToRef(ray.origin, matrix, result.origin);
Vector3.TransformNormalToRef(ray.direction, matrix, result.direction);
result.length = ray.length;
Expand All @@ -599,6 +616,8 @@ export class Ray {
dir.z *= num;
result.length *= len;
}

return result;
}

/**
Expand Down
19 changes: 18 additions & 1 deletion packages/dev/core/src/XR/features/WebXRNearInteraction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -931,6 +931,10 @@ export class WebXRNearInteraction extends WebXRAbstractFeature {
pickingInfo.gripTransform = controllerData.xrController.grip || null;
pickingInfo.originMesh = controllerData.touchCollisionMesh;
pickingInfo.distance = result.distance;
pickingInfo.bu = result.bu;
pickingInfo.bv = result.bv;
pickingInfo.faceId = result.faceId;
pickingInfo.subMeshId = result.subMeshId;
}
}
}
Expand Down Expand Up @@ -963,9 +967,10 @@ export class WebXRNearInteraction extends WebXRAbstractFeature {

const result = TmpVectors.Vector3[0];
const tmpVec = TmpVectors.Vector3[1];
const tmpRay = new Ray(Vector3.Zero(), Vector3.Zero(), 1);

let distance = +Infinity;
let tmp, tmpDistanceSphereToCenter, tmpDistanceSurfaceToCenter;
let tmp, tmpDistanceSphereToCenter, tmpDistanceSurfaceToCenter, intersectionInfo;
const center = TmpVectors.Vector3[2];
const worldToMesh = TmpVectors.Matrix[0];
worldToMesh.copyFrom(mesh.getWorldMatrix());
Expand All @@ -990,6 +995,12 @@ export class WebXRNearInteraction extends WebXRAbstractFeature {

if (tmp !== -1 && tmp < distance) {
distance = tmp;

// ray between the sphere center and the point on the mesh
Ray.CreateFromToToRef(sphere.center, tmpVec, undefined, tmpRay);
tmpRay.length = distance * 2;
intersectionInfo = tmpRay.intersectsMesh(mesh);

result.copyFrom(tmpVec);
}
}
Expand All @@ -999,6 +1010,12 @@ export class WebXRNearInteraction extends WebXRAbstractFeature {
pi.distance = distance;
pi.pickedMesh = mesh;
pi.pickedPoint = result.clone();
if (intersectionInfo && intersectionInfo.bu !== null && intersectionInfo.bv !== null) {
pi.faceId = intersectionInfo.faceId;
pi.subMeshId = intersectionInfo.subMeshId;
pi.bu = intersectionInfo.bu;
pi.bv = intersectionInfo.bv;
}
}

return pi;
Expand Down

0 comments on commit 7954684

Please sign in to comment.