Add XRHitTestResult::getPose

This commit is contained in:
Manish Goregaokar 2020-04-10 14:45:10 -07:00
parent c7b91523d0
commit 87bce8cde9
4 changed files with 31 additions and 9 deletions

View file

@ -6,5 +6,5 @@
[SecureContext, Exposed=Window]
interface XRHitTestResult {
// XRPose? getPose(XRSpace baseSpace);
};
XRPose? getPose(XRSpace baseSpace);
};

View file

@ -10,7 +10,7 @@ use crate::dom::bindings::root::{Dom, DomRoot};
use crate::dom::globalscope::GlobalScope;
use crate::dom::xrpose::XRPose;
use crate::dom::xrreferencespace::XRReferenceSpace;
use crate::dom::xrsession::XRSession;
use crate::dom::xrsession::{ApiPose, XRSession};
use crate::dom::xrspace::XRSpace;
use crate::dom::xrviewerpose::XRViewerPose;
use dom_struct::dom_struct;
@ -51,6 +51,10 @@ impl XRFrame {
pub fn set_animation_frame(&self, animation_frame: bool) {
self.animation_frame.set(animation_frame);
}
pub fn get_pose(&self, space: &XRSpace) -> Option<ApiPose> {
space.get_pose(&self.data)
}
}
impl XRFrameMethods for XRFrame {
@ -92,12 +96,12 @@ impl XRFrameMethods for XRFrame {
if !self.active.get() {
return Err(Error::InvalidState);
}
let space = if let Some(space) = space.get_pose(&self.data) {
let space = if let Some(space) = self.get_pose(space) {
space
} else {
return Ok(None);
};
let relative_to = if let Some(r) = relative_to.get_pose(&self.data) {
let relative_to = if let Some(r) = self.get_pose(relative_to) {
r
} else {
return Ok(None);

View file

@ -2,10 +2,13 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use crate::dom::bindings::reflector::{reflect_dom_object, Reflector};
use crate::dom::bindings::codegen::Bindings::XRHitTestResultBinding::XRHitTestResultMethods;
use crate::dom::bindings::reflector::{reflect_dom_object, DomObject, Reflector};
use crate::dom::bindings::root::{Dom, DomRoot};
use crate::dom::globalscope::GlobalScope;
use crate::dom::xrframe::XRFrame;
use crate::dom::xrpose::XRPose;
use crate::dom::xrspace::XRSpace;
use dom_struct::dom_struct;
use webxr_api::HitTestResult;
@ -37,3 +40,12 @@ impl XRHitTestResult {
)
}
}
impl XRHitTestResultMethods for XRHitTestResult {
// https://immersive-web.github.io/hit-test/#dom-xrhittestresult-getpose
fn GetPose(&self, base: &XRSpace) -> Option<DomRoot<XRPose>> {
let base = self.frame.get_pose(base)?;
let pose = base.inverse().pre_transform(&self.result.space);
Some(XRPose::new(&self.global(), pose.cast_unit()))
}
}

View file

@ -55,12 +55,18 @@ impl XRRay {
return Err(Error::Type("Direction w coordinate must be 0".into()));
}
if *direction.x == 0.0 && *direction.y == 0.0 && *direction.z == 0.0 {
return Err(Error::Type("Direction vector cannot have zero length".into()));
return Err(Error::Type(
"Direction vector cannot have zero length".into(),
));
}
let origin = Vector3D::new(origin.x as f32, origin.y as f32, origin.z as f32);
let direction =
Vector3D::new(*direction.x as f32, *direction.y as f32, *direction.z as f32).normalize();
let direction = Vector3D::new(
*direction.x as f32,
*direction.y as f32,
*direction.z as f32,
)
.normalize();
Ok(Self::new(&window.global(), Ray { origin, direction }))
}