Skip to content

Commit 83f92a3

Browse files
committed
Update XRRay semantics
Implements changes from immersive-web/hit-test#85
1 parent 7dd2a71 commit 83f92a3

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

components/script/dom/webidls/XRRay.webidl

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,17 @@
44

55
// https://immersive-web.github.io/hit-test/#xrray-interface
66

7+
dictionary XRRayDirectionInit {
8+
double x = 0;
9+
double y = 0;
10+
double z = -1;
11+
double w = 0;
12+
};
13+
714
[SecureContext, Exposed=Window, Pref="dom.webxr.enabled"]
815
interface XRRay {
9-
constructor(optional DOMPointInit origin = {}, optional DOMPointInit direction = {});
10-
constructor(XRRigidTransform transform);
16+
[Throws] constructor(optional DOMPointInit origin = {}, optional XRRayDirectionInit direction = {});
17+
[Throws] constructor(XRRigidTransform transform);
1118
[SameObject] readonly attribute DOMPointReadOnly origin;
1219
[SameObject] readonly attribute DOMPointReadOnly direction;
1320
[SameObject] readonly attribute Float32Array matrix;

components/script/dom/xrray.rs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
44

55
use crate::dom::bindings::codegen::Bindings::DOMPointBinding::DOMPointInit;
6-
use crate::dom::bindings::codegen::Bindings::XRRayBinding::XRRayMethods;
6+
use crate::dom::bindings::codegen::Bindings::XRRayBinding::{XRRayDirectionInit, XRRayMethods};
7+
use crate::dom::bindings::error::{Error, Fallible};
78
use crate::dom::bindings::reflector::{reflect_dom_object, DomObject, Reflector};
89
use crate::dom::bindings::root::DomRoot;
910
use crate::dom::bindings::utils::create_typed_array;
@@ -45,25 +46,35 @@ impl XRRay {
4546
pub fn Constructor(
4647
window: &Window,
4748
origin: &DOMPointInit,
48-
direction: &DOMPointInit,
49-
) -> DomRoot<Self> {
49+
direction: &XRRayDirectionInit,
50+
) -> Fallible<DomRoot<Self>> {
51+
if origin.w != 1.0 {
52+
return Err(Error::Type("Origin w coordinate must be 1".into()));
53+
}
54+
if *direction.w != 0.0 {
55+
return Err(Error::Type("Direction w coordinate must be 0".into()));
56+
}
57+
if *direction.x == 0.0 && *direction.y == 0.0 && *direction.z == 0.0 {
58+
return Err(Error::Type("Direction vector cannot have zero length".into()));
59+
}
60+
5061
let origin = Vector3D::new(origin.x as f32, origin.y as f32, origin.z as f32);
5162
let direction =
52-
Vector3D::new(direction.x as f32, direction.y as f32, direction.z as f32).normalize();
63+
Vector3D::new(*direction.x as f32, *direction.y as f32, *direction.z as f32).normalize();
5364

54-
Self::new(&window.global(), Ray { origin, direction })
65+
Ok(Self::new(&window.global(), Ray { origin, direction }))
5566
}
5667

5768
#[allow(non_snake_case)]
5869
/// https://immersive-web.github.io/hit-test/#dom-xrray-xrray-transform
59-
pub fn Constructor_(window: &Window, transform: &XRRigidTransform) -> DomRoot<Self> {
70+
pub fn Constructor_(window: &Window, transform: &XRRigidTransform) -> Fallible<DomRoot<Self>> {
6071
let transform = transform.transform();
6172
let origin = transform.translation;
6273
let direction = transform
6374
.rotation
6475
.transform_vector3d(Vector3D::new(0., 0., -1.));
6576

66-
Self::new(&window.global(), Ray { origin, direction })
77+
Ok(Self::new(&window.global(), Ray { origin, direction }))
6778
}
6879
}
6980

0 commit comments

Comments
 (0)