diff --git a/components/script/dom/webidls/XRHitTestResult.webidl b/components/script/dom/webidls/XRHitTestResult.webidl index 37be75d7ec6..08bf4bf5389 100644 --- a/components/script/dom/webidls/XRHitTestResult.webidl +++ b/components/script/dom/webidls/XRHitTestResult.webidl @@ -6,5 +6,5 @@ [SecureContext, Exposed=Window] interface XRHitTestResult { - // XRPose? getPose(XRSpace baseSpace); -}; \ No newline at end of file + XRPose? getPose(XRSpace baseSpace); +}; diff --git a/components/script/dom/xrframe.rs b/components/script/dom/xrframe.rs index 27a9085bded..fc1351383bd 100644 --- a/components/script/dom/xrframe.rs +++ b/components/script/dom/xrframe.rs @@ -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 { + 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); diff --git a/components/script/dom/xrhittestresult.rs b/components/script/dom/xrhittestresult.rs index 00b4cf5e5c1..be11f3dc7b6 100644 --- a/components/script/dom/xrhittestresult.rs +++ b/components/script/dom/xrhittestresult.rs @@ -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> { + let base = self.frame.get_pose(base)?; + let pose = base.inverse().pre_transform(&self.result.space); + Some(XRPose::new(&self.global(), pose.cast_unit())) + } +} diff --git a/components/script/dom/xrray.rs b/components/script/dom/xrray.rs index 75e973e621b..6f50820c6f0 100644 --- a/components/script/dom/xrray.rs +++ b/components/script/dom/xrray.rs @@ -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 })) }