diff options
author | Taym Haddadi <haddadi.taym@gmail.com> | 2024-01-17 11:38:01 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-01-17 10:38:01 +0000 |
commit | f76982e2e7f411e2e2fd8e6dbfe92a080acefc54 (patch) | |
tree | b29266963fbbe289fafbf19f702710dd32d37562 /components/script/dom/xrray.rs | |
parent | d43adb1a92c419680be86e7335f3de84b75fb2ef (diff) | |
download | servo-f76982e2e7f411e2e2fd8e6dbfe92a080acefc54.tar.gz servo-f76982e2e7f411e2e2fd8e6dbfe92a080acefc54.zip |
script: Use FLoat32Array in XRRay (#31087)
* use FLoat32Array in XRRay
Signed-off-by: Bentaimia Haddadi <haddadi.taym@gmail.com>
* Apply suggestions from code review
---------
Signed-off-by: Bentaimia Haddadi <haddadi.taym@gmail.com>
Co-authored-by: Martin Robinson <mrobinson@igalia.com>
Diffstat (limited to 'components/script/dom/xrray.rs')
-rw-r--r-- | components/script/dom/xrray.rs | 37 |
1 files changed, 19 insertions, 18 deletions
diff --git a/components/script/dom/xrray.rs b/components/script/dom/xrray.rs index 1bb925a03a1..6c6cfb0074f 100644 --- a/components/script/dom/xrray.rs +++ b/components/script/dom/xrray.rs @@ -2,12 +2,10 @@ * 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 std::ptr::NonNull; - use dom_struct::dom_struct; use euclid::{Angle, RigidTransform3D, Rotation3D, Vector3D}; -use js::jsapi::{Heap, JSObject}; use js::rust::HandleObject; +use js::typedarray::Float32Array; use webxr_api::{ApiSpace, Ray}; use crate::dom::bindings::codegen::Bindings::DOMPointBinding::DOMPointInit; @@ -15,7 +13,7 @@ use crate::dom::bindings::codegen::Bindings::XRRayBinding::{XRRayDirectionInit, use crate::dom::bindings::error::{Error, Fallible}; use crate::dom::bindings::reflector::{reflect_dom_object_with_proto, DomObject, Reflector}; use crate::dom::bindings::root::DomRoot; -use crate::dom::bindings::utils::create_typed_array; +use crate::dom::bindings::typedarrays::HeapFloat32Array; use crate::dom::dompointreadonly::DOMPointReadOnly; use crate::dom::globalscope::GlobalScope; use crate::dom::window::Window; @@ -29,7 +27,7 @@ pub struct XRRay { #[no_trace] ray: Ray<ApiSpace>, #[ignore_malloc_size_of = "defined in mozjs"] - matrix: Heap<*mut JSObject>, + matrix: HeapFloat32Array, } impl XRRay { @@ -37,7 +35,7 @@ impl XRRay { XRRay { reflector_: Reflector::new(), ray, - matrix: Heap::default(), + matrix: HeapFloat32Array::default(), } } @@ -133,18 +131,16 @@ impl XRRayMethods for XRRay { } /// https://immersive-web.github.io/hit-test/#dom-xrray-matrix - fn Matrix(&self, _cx: JSContext) -> NonNull<JSObject> { + fn Matrix(&self, _cx: JSContext) -> Float32Array { // https://immersive-web.github.io/hit-test/#xrray-obtain-the-matrix - // Step 1 - if self.matrix.get().is_null() { - let cx = GlobalScope::get_cx(); - // Step 2 + if !self.matrix.is_initialized() { + // Step 1 let z = Vector3D::new(0., 0., -1.); - // Step 3 + // Step 2 let axis = z.cross(self.ray.direction); - // Step 4 + // Step 3 let cos_angle = z.dot(self.ray.direction); - // Step 5 + // Step 4 let rotation = if cos_angle > -1. && cos_angle < 1. { Rotation3D::around_axis(axis, Angle::radians(cos_angle.acos())) } else if cos_angle == -1. { @@ -153,16 +149,21 @@ impl XRRayMethods for XRRay { } else { Rotation3D::identity() }; - // Step 6 + // Step 5 let translation = self.ray.origin; - // Step 7 + // Step 6 // According to the spec all matrices are column-major, // however euclid uses row vectors so we use .to_array() let arr = RigidTransform3D::new(rotation, translation) .to_transform() .to_array(); - create_typed_array(cx, &arr, &self.matrix); + self.matrix + .set_data(_cx, &arr) + .expect("Failed to set matrix data on XRRAy.") } - NonNull::new(self.matrix.get()).unwrap() + + self.matrix + .get_internal() + .expect("Failed to get matrix from XRRay.") } } |