mirror of
https://github.com/servo/servo.git
synced 2025-07-23 23:33:43 +01:00
Add XRSpace::get_viewer_pose()
This commit is contained in:
parent
1e1f527c82
commit
d1d8e97c30
4 changed files with 103 additions and 0 deletions
|
@ -4,6 +4,7 @@
|
||||||
|
|
||||||
use crate::dom::bindings::codegen::Bindings::XRReferenceSpaceBinding;
|
use crate::dom::bindings::codegen::Bindings::XRReferenceSpaceBinding;
|
||||||
use crate::dom::bindings::codegen::Bindings::XRReferenceSpaceBinding::XRReferenceSpaceMethods;
|
use crate::dom::bindings::codegen::Bindings::XRReferenceSpaceBinding::XRReferenceSpaceMethods;
|
||||||
|
use crate::dom::bindings::inheritance::Castable;
|
||||||
use crate::dom::bindings::reflector::reflect_dom_object;
|
use crate::dom::bindings::reflector::reflect_dom_object;
|
||||||
use crate::dom::bindings::root::{DomRoot, MutDom};
|
use crate::dom::bindings::root::{DomRoot, MutDom};
|
||||||
use crate::dom::dompointreadonly::DOMPointReadOnly;
|
use crate::dom::dompointreadonly::DOMPointReadOnly;
|
||||||
|
@ -11,7 +12,10 @@ use crate::dom::window::Window;
|
||||||
use crate::dom::xrrigidtransform::XRRigidTransform;
|
use crate::dom::xrrigidtransform::XRRigidTransform;
|
||||||
use crate::dom::xrsession::XRSession;
|
use crate::dom::xrsession::XRSession;
|
||||||
use crate::dom::xrspace::XRSpace;
|
use crate::dom::xrspace::XRSpace;
|
||||||
|
use crate::dom::xrstationaryreferencespace::XRStationaryReferenceSpace;
|
||||||
use dom_struct::dom_struct;
|
use dom_struct::dom_struct;
|
||||||
|
use euclid::Transform3D;
|
||||||
|
use webvr_traits::WebVRFrameData;
|
||||||
|
|
||||||
#[dom_struct]
|
#[dom_struct]
|
||||||
pub struct XRReferenceSpace {
|
pub struct XRReferenceSpace {
|
||||||
|
@ -64,3 +68,33 @@ impl XRReferenceSpaceMethods for XRReferenceSpace {
|
||||||
self.transform.get()
|
self.transform.get()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl XRReferenceSpace {
|
||||||
|
/// Gets viewer pose represented by this space
|
||||||
|
pub fn get_viewer_pose(&self, base_pose: &WebVRFrameData) -> Transform3D<f64> {
|
||||||
|
let pose = self.get_pose(base_pose);
|
||||||
|
|
||||||
|
// This may change, see https://github.com/immersive-web/webxr/issues/567
|
||||||
|
let offset = self.transform.get().matrix();
|
||||||
|
// XXXManishearth we can directly compute the inverse from the transform parameters
|
||||||
|
// (and perhaps cache it)
|
||||||
|
// XXXManishearth we can also optimize for the unset/identity offset case
|
||||||
|
let inverse = offset
|
||||||
|
.inverse()
|
||||||
|
.expect("rigid transforms are always invertible");
|
||||||
|
inverse.pre_mul(&pose)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Gets pose represented by this space
|
||||||
|
///
|
||||||
|
/// Does not apply originOffset, use get_viewer_pose instead if you need it
|
||||||
|
pub fn get_pose(&self, base_pose: &WebVRFrameData) -> Transform3D<f64> {
|
||||||
|
if let Some(stationary) = self.downcast::<XRStationaryReferenceSpace>() {
|
||||||
|
stationary.get_pose(base_pose)
|
||||||
|
} else {
|
||||||
|
// non-subclassed XRReferenceSpaces exist, obtained via the "identity"
|
||||||
|
// type. The pose does not depend on the base pose.
|
||||||
|
Transform3D::identity()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
||||||
|
|
||||||
use crate::dom::bindings::codegen::Bindings::DOMPointBinding::DOMPointInit;
|
use crate::dom::bindings::codegen::Bindings::DOMPointBinding::DOMPointInit;
|
||||||
|
use crate::dom::bindings::codegen::Bindings::DOMPointReadOnlyBinding::DOMPointReadOnlyBinding::DOMPointReadOnlyMethods;
|
||||||
use crate::dom::bindings::codegen::Bindings::XRRigidTransformBinding;
|
use crate::dom::bindings::codegen::Bindings::XRRigidTransformBinding;
|
||||||
use crate::dom::bindings::codegen::Bindings::XRRigidTransformBinding::XRRigidTransformMethods;
|
use crate::dom::bindings::codegen::Bindings::XRRigidTransformBinding::XRRigidTransformMethods;
|
||||||
use crate::dom::bindings::error::Fallible;
|
use crate::dom::bindings::error::Fallible;
|
||||||
|
@ -12,6 +13,7 @@ use crate::dom::bindings::root::{Dom, DomRoot};
|
||||||
use crate::dom::dompointreadonly::DOMPointReadOnly;
|
use crate::dom::dompointreadonly::DOMPointReadOnly;
|
||||||
use crate::dom::window::Window;
|
use crate::dom::window::Window;
|
||||||
use dom_struct::dom_struct;
|
use dom_struct::dom_struct;
|
||||||
|
use euclid::{Rotation3D, Transform3D};
|
||||||
|
|
||||||
#[dom_struct]
|
#[dom_struct]
|
||||||
pub struct XRRigidTransform {
|
pub struct XRRigidTransform {
|
||||||
|
@ -65,6 +67,7 @@ impl XRRigidTransform {
|
||||||
) -> Fallible<DomRoot<Self>> {
|
) -> Fallible<DomRoot<Self>> {
|
||||||
let global = window.global();
|
let global = window.global();
|
||||||
let position = DOMPointReadOnly::new_from_init(&global, &position);
|
let position = DOMPointReadOnly::new_from_init(&global, &position);
|
||||||
|
// XXXManishearth normalize this
|
||||||
let orientation = DOMPointReadOnly::new_from_init(&global, &orientation);
|
let orientation = DOMPointReadOnly::new_from_init(&global, &orientation);
|
||||||
Ok(XRRigidTransform::new(window, &position, &orientation))
|
Ok(XRRigidTransform::new(window, &position, &orientation))
|
||||||
}
|
}
|
||||||
|
@ -80,3 +83,21 @@ impl XRRigidTransformMethods for XRRigidTransform {
|
||||||
DomRoot::from_ref(&self.orientation)
|
DomRoot::from_ref(&self.orientation)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl XRRigidTransform {
|
||||||
|
pub fn matrix(&self) -> Transform3D<f64> {
|
||||||
|
// XXXManishearth compute this during initialization
|
||||||
|
let translate = Transform3D::create_translation(
|
||||||
|
self.position.X(),
|
||||||
|
self.position.Y(),
|
||||||
|
self.position.Z(),
|
||||||
|
);
|
||||||
|
let rotation = Rotation3D::unit_quaternion(
|
||||||
|
self.orientation.X(),
|
||||||
|
self.orientation.Y(),
|
||||||
|
self.orientation.Z(),
|
||||||
|
self.orientation.W(),
|
||||||
|
);
|
||||||
|
translate.pre_mul(&rotation.to_transform())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -3,12 +3,16 @@
|
||||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
||||||
|
|
||||||
use crate::dom::bindings::codegen::Bindings::XRSpaceBinding;
|
use crate::dom::bindings::codegen::Bindings::XRSpaceBinding;
|
||||||
|
use crate::dom::bindings::inheritance::Castable;
|
||||||
use crate::dom::bindings::reflector::reflect_dom_object;
|
use crate::dom::bindings::reflector::reflect_dom_object;
|
||||||
use crate::dom::bindings::root::{Dom, DomRoot};
|
use crate::dom::bindings::root::{Dom, DomRoot};
|
||||||
use crate::dom::eventtarget::EventTarget;
|
use crate::dom::eventtarget::EventTarget;
|
||||||
use crate::dom::globalscope::GlobalScope;
|
use crate::dom::globalscope::GlobalScope;
|
||||||
|
use crate::dom::xrreferencespace::XRReferenceSpace;
|
||||||
use crate::dom::xrsession::XRSession;
|
use crate::dom::xrsession::XRSession;
|
||||||
use dom_struct::dom_struct;
|
use dom_struct::dom_struct;
|
||||||
|
use euclid::Transform3D;
|
||||||
|
use webvr_traits::WebVRFrameData;
|
||||||
|
|
||||||
#[dom_struct]
|
#[dom_struct]
|
||||||
pub struct XRSpace {
|
pub struct XRSpace {
|
||||||
|
@ -33,3 +37,25 @@ impl XRSpace {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl XRSpace {
|
||||||
|
/// Gets viewer pose represented by this space
|
||||||
|
pub fn get_viewer_pose(&self, base_pose: &WebVRFrameData) -> Transform3D<f64> {
|
||||||
|
if let Some(reference) = self.downcast::<XRReferenceSpace>() {
|
||||||
|
reference.get_viewer_pose(base_pose)
|
||||||
|
} else {
|
||||||
|
unreachable!()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Gets pose represented by this space
|
||||||
|
///
|
||||||
|
/// Does not apply originOffset, use get_viewer_pose instead if you need it
|
||||||
|
pub fn get_pose(&self, base_pose: &WebVRFrameData) -> Transform3D<f64> {
|
||||||
|
if let Some(reference) = self.downcast::<XRReferenceSpace>() {
|
||||||
|
reference.get_pose(base_pose)
|
||||||
|
} else {
|
||||||
|
unreachable!()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -11,6 +11,8 @@ use crate::dom::xrreferencespace::XRReferenceSpace;
|
||||||
use crate::dom::xrrigidtransform::XRRigidTransform;
|
use crate::dom::xrrigidtransform::XRRigidTransform;
|
||||||
use crate::dom::xrsession::XRSession;
|
use crate::dom::xrsession::XRSession;
|
||||||
use dom_struct::dom_struct;
|
use dom_struct::dom_struct;
|
||||||
|
use euclid::{Rotation3D, Transform3D};
|
||||||
|
use webvr_traits::WebVRFrameData;
|
||||||
|
|
||||||
#[dom_struct]
|
#[dom_struct]
|
||||||
pub struct XRStationaryReferenceSpace {
|
pub struct XRStationaryReferenceSpace {
|
||||||
|
@ -46,3 +48,23 @@ impl XRStationaryReferenceSpace {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl XRStationaryReferenceSpace {
|
||||||
|
/// Gets pose represented by this space
|
||||||
|
///
|
||||||
|
/// Does not apply originOffset, use get_viewer_pose instead
|
||||||
|
pub fn get_pose(&self, base_pose: &WebVRFrameData) -> Transform3D<f64> {
|
||||||
|
// XXXManishearth add floor-level transform for floor-level and disable position in position-disabled
|
||||||
|
let pos = base_pose.pose.position.unwrap_or([0., 0., 0.]);
|
||||||
|
let translation =
|
||||||
|
Transform3D::create_translation(pos[0] as f64, pos[1] as f64, pos[2] as f64);
|
||||||
|
let orient = base_pose.pose.orientation.unwrap_or([0., 0., 0., 0.]);
|
||||||
|
let rotation = Rotation3D::quaternion(
|
||||||
|
orient[0] as f64,
|
||||||
|
orient[1] as f64,
|
||||||
|
orient[2] as f64,
|
||||||
|
orient[3] as f64,
|
||||||
|
);
|
||||||
|
translation.pre_mul(&rotation.to_transform())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue