Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement hit testing API #26171

Merged
merged 18 commits into from Apr 20, 2020
Merged
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

Add XRRay.matrix

  • Loading branch information
Manishearth committed Apr 20, 2020
commit 7dd2a71d6de36bd5e1d87998e70454a0d87c6b5a
@@ -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;
};
@@ -6,26 +6,33 @@ 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]
pub struct XRRay {
reflector_: Reflector,
#[ignore_malloc_size_of = "defined in webxr"]
ray: Ray<ApiSpace>,
#[ignore_malloc_size_of = "defined in mozjs"]
matrix: Heap<*mut JSObject>,
}

impl XRRay {
fn new_inherited(ray: Ray<ApiSpace>) -> 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<JSObject> {
// 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()
}
}

This file was deleted.

This file was deleted.

ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.