From 7dd2a71d6de36bd5e1d87998e70454a0d87c6b5a Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Wed, 8 Apr 2020 16:51:29 -0700 Subject: [PATCH] Add XRRay.matrix --- components/script/dom/webidls/XRRay.webidl | 2 +- components/script/dom/xrray.rs | 43 ++++++++++++++++++- .../hit-test/xrRay_constructor.https.html.ini | 4 -- .../hit-test/xrRay_matrix.https.html.ini | 4 -- 4 files changed, 43 insertions(+), 10 deletions(-) delete mode 100644 tests/wpt/metadata/webxr/hit-test/xrRay_constructor.https.html.ini delete mode 100644 tests/wpt/metadata/webxr/hit-test/xrRay_matrix.https.html.ini diff --git a/components/script/dom/webidls/XRRay.webidl b/components/script/dom/webidls/XRRay.webidl index b67d8a511a5..7a6558e55e1 100644 --- a/components/script/dom/webidls/XRRay.webidl +++ b/components/script/dom/webidls/XRRay.webidl @@ -10,5 +10,5 @@ interface XRRay { constructor(XRRigidTransform transform); [SameObject] readonly attribute DOMPointReadOnly origin; [SameObject] readonly attribute DOMPointReadOnly direction; - // [SameObject] readonly attribute Float32Array matrix; + [SameObject] readonly attribute Float32Array matrix; }; diff --git a/components/script/dom/xrray.rs b/components/script/dom/xrray.rs index df53ab4ecc9..dd3e6cfb95c 100644 --- a/components/script/dom/xrray.rs +++ b/components/script/dom/xrray.rs @@ -6,12 +6,16 @@ use crate::dom::bindings::codegen::Bindings::DOMPointBinding::DOMPointInit; use crate::dom::bindings::codegen::Bindings::XRRayBinding::XRRayMethods; use crate::dom::bindings::reflector::{reflect_dom_object, DomObject, Reflector}; use crate::dom::bindings::root::DomRoot; +use crate::dom::bindings::utils::create_typed_array; use crate::dom::dompointreadonly::DOMPointReadOnly; use crate::dom::globalscope::GlobalScope; use crate::dom::window::Window; use crate::dom::xrrigidtransform::XRRigidTransform; +use crate::script_runtime::JSContext; use dom_struct::dom_struct; -use euclid::Vector3D; +use euclid::{Angle, RigidTransform3D, Rotation3D, Vector3D}; +use js::jsapi::{Heap, JSObject}; +use std::ptr::NonNull; use webxr_api::{ApiSpace, Ray}; #[dom_struct] @@ -19,6 +23,8 @@ pub struct XRRay { reflector_: Reflector, #[ignore_malloc_size_of = "defined in webxr"] ray: Ray, + #[ignore_malloc_size_of = "defined in mozjs"] + matrix: Heap<*mut JSObject>, } impl XRRay { @@ -26,6 +32,7 @@ impl XRRay { XRRay { reflector_: Reflector::new(), ray, + matrix: Heap::default(), } } @@ -82,4 +89,38 @@ impl XRRayMethods for XRRay { 0., ) } + + /// https://immersive-web.github.io/hit-test/#dom-xrray-matrix + fn Matrix(&self, _cx: JSContext) -> NonNull { + // https://immersive-web.github.io/hit-test/#xrray-obtain-the-matrix + // Step 1 + if self.matrix.get().is_null() { + let cx = self.global().get_cx(); + // Step 2 + let z = Vector3D::new(0., 0., -1.); + // Step 3 + let axis = z.cross(self.ray.direction); + // Step 4 + let cos_angle = z.dot(self.ray.direction); + // Step 5 + let rotation = if cos_angle > -1. && cos_angle < 1. { + Rotation3D::around_axis(axis, Angle::radians(cos_angle.acos())) + } else if cos_angle == -1. { + let axis = Vector3D::new(1., 0., 0.); + Rotation3D::around_axis(axis, Angle::radians(cos_angle.acos())) + } else { + Rotation3D::identity() + }; + // Step 6 + let translation = self.ray.origin; + // Step 7 + // According to the spec all matrices are column-major, + // however euclid uses row vectors so we use .to_row_major_array() + let arr = RigidTransform3D::new(rotation, translation) + .to_transform() + .to_row_major_array(); + create_typed_array(cx, &arr, &self.matrix); + } + NonNull::new(self.matrix.get()).unwrap() + } } diff --git a/tests/wpt/metadata/webxr/hit-test/xrRay_constructor.https.html.ini b/tests/wpt/metadata/webxr/hit-test/xrRay_constructor.https.html.ini deleted file mode 100644 index 5381acd7bbf..00000000000 --- a/tests/wpt/metadata/webxr/hit-test/xrRay_constructor.https.html.ini +++ /dev/null @@ -1,4 +0,0 @@ -[xrRay_constructor.https.html] - [XRRay constructors work] - expected: FAIL - diff --git a/tests/wpt/metadata/webxr/hit-test/xrRay_matrix.https.html.ini b/tests/wpt/metadata/webxr/hit-test/xrRay_matrix.https.html.ini deleted file mode 100644 index fd045eaa394..00000000000 --- a/tests/wpt/metadata/webxr/hit-test/xrRay_matrix.https.html.ini +++ /dev/null @@ -1,4 +0,0 @@ -[xrRay_matrix.https.html] - [XRRay matrix works] - expected: FAIL -