use crate::cameras::{PerspectiveCamera, ViewOverride};
mod input;
pub use input::*;
mod app;
pub use app::*;
mod panel;
pub use panel::*;
mod panel_pass;
pub use panel_pass::*;
mod stereo;
pub mod phone;
#[cfg(target_arch = "wasm32")]
pub mod web;
#[cfg(all(target_os = "visionos", feature = "xr-visionos"))]
pub mod visionos;
use crate::math::{Matrix4, Vector3};
pub fn depth_zero_to_one(gl: [f32; 16]) -> [f32; 16] {
let mut m = gl;
for c in 0..4 {
m[c * 4 + 2] = 0.5 * (gl[c * 4 + 2] + gl[c * 4 + 3]);
}
m
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct XrEye {
pub projection: [f32; 16],
pub world: Matrix4,
pub viewport: [i32; 4],
}
impl XrEye {
pub fn new(
gl_projection: [f32; 16],
origin: &Matrix4,
pose: &Matrix4,
viewport: [i32; 4],
) -> Self {
XrEye {
projection: depth_zero_to_one(gl_projection),
world: origin.multiply(pose),
viewport,
}
}
pub fn fov(&self) -> f32 {
let p = &self.projection;
((1.0 + p[9]) / p[5]).atan() + ((1.0 - p[9]) / p[5]).atan()
}
pub fn camera(&self, lens: &PerspectiveCamera) -> PerspectiveCamera {
let mut c = lens.clone();
let (position, target, up, view) =
pose_from_world(&self.world, (lens.target - lens.position).length());
(c.position, c.target, c.up, c.view_override) = (position, target, up, view);
c.projection_override = Some(self.projection);
c.fov = self.fov();
c.aspect = self.viewport[2].max(1) as f32 / self.viewport[3].max(1) as f32;
c.shift_x = 0.0;
c.shift_y = 0.0;
c
}
}
pub fn pose_from_world(
m: &Matrix4,
distance: f32,
) -> (Vector3, Vector3, Vector3, Option<ViewOverride>) {
let e = &m.elements;
let position = Vector3::new(e[12], e[13], e[14]);
let forward = Vector3::new(-e[8], -e[9], -e[10]).normalize();
let up = Vector3::new(e[4], e[5], e[6]).normalize();
let d = if distance.is_finite() && distance > 1e-6 {
distance
} else {
1.0
};
let target = position + forward * d;
let (x, y, z) = (
Vector3::new(e[0], e[1], e[2]),
Vector3::new(e[4], e[5], e[6]),
Vector3::new(e[8], e[9], e[10]),
);
const EPS: f32 = 1e-5;
let rigid = [x, y, z].iter().all(|v| (v.length() - 1.0).abs() < EPS)
&& x.dot(y).abs() < EPS
&& y.dot(z).abs() < EPS
&& z.dot(x).abs() < EPS;
let view = (!rigid).then(|| ViewOverride {
view: m.invert(),
position,
target,
up,
});
(position, target, up, view)
}
pub const HAND_JOINTS: [&str; 25] = [
"wrist",
"thumb-metacarpal",
"thumb-phalanx-proximal",
"thumb-phalanx-distal",
"thumb-tip",
"index-finger-metacarpal",
"index-finger-phalanx-proximal",
"index-finger-phalanx-intermediate",
"index-finger-phalanx-distal",
"index-finger-tip",
"middle-finger-metacarpal",
"middle-finger-phalanx-proximal",
"middle-finger-phalanx-intermediate",
"middle-finger-phalanx-distal",
"middle-finger-tip",
"ring-finger-metacarpal",
"ring-finger-phalanx-proximal",
"ring-finger-phalanx-intermediate",
"ring-finger-phalanx-distal",
"ring-finger-tip",
"pinky-finger-metacarpal",
"pinky-finger-phalanx-proximal",
"pinky-finger-phalanx-intermediate",
"pinky-finger-phalanx-distal",
"pinky-finger-tip",
];
#[derive(Debug, Clone, Default)]
pub struct InputSlots<T> {
slots: Vec<Option<T>>,
}
impl<T: PartialEq> InputSlots<T> {
pub fn new() -> Self {
InputSlots { slots: vec![] }
}
pub fn connect(&mut self, source: T) -> usize {
if let Some(i) = self.slot_of(&source) {
return i;
}
match self.slots.iter().position(Option::is_none) {
Some(i) => {
self.slots[i] = Some(source);
i
}
None => {
self.slots.push(Some(source));
self.slots.len() - 1
}
}
}
pub fn disconnect(&mut self, source: &T) -> Option<usize> {
let i = self.slot_of(source)?;
self.slots[i] = None;
Some(i)
}
pub fn slot_of(&self, source: &T) -> Option<usize> {
self.slots.iter().position(|s| s.as_ref() == Some(source))
}
pub fn iter(&self) -> impl Iterator<Item = (usize, &T)> {
self.slots
.iter()
.enumerate()
.filter_map(|(i, s)| s.as_ref().map(|s| (i, s)))
}
pub fn clear(&mut self) {
self.slots.clear();
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::cameras::Camera;
fn gl_frustum(l: f32, r: f32, b: f32, t: f32, n: f32, f: f32) -> [f32; 16] {
[
2.0 * n / (r - l),
0.0,
0.0,
0.0,
0.0,
2.0 * n / (t - b),
0.0,
0.0,
(r + l) / (r - l),
(t + b) / (t - b),
-(f + n) / (f - n),
-1.0,
0.0,
0.0,
-2.0 * f * n / (f - n),
0.0,
]
}
fn clip(m: &[f32; 16], p: [f32; 3]) -> [f32; 4] {
let mut o = [0.0; 4];
for (r, v) in o.iter_mut().enumerate() {
*v = m[r] * p[0] + m[4 + r] * p[1] + m[8 + r] * p[2] + m[12 + r];
}
o
}
#[test]
fn near_and_far_land_on_zero_and_one() {
let (n, f) = (0.1, 50.0);
let p = depth_zero_to_one(gl_frustum(-0.12, 0.08, -0.1, 0.1, n, f));
for (z, want) in [(-n, 0.0), (-f, 1.0)] {
let c = clip(&p, [0.0, 0.0, z]);
assert!((c[2] / c[3] - want).abs() < 1e-5, "z {z}: {}", c[2] / c[3]);
}
let (g, c) = (
clip(&gl_frustum(-0.12, 0.08, -0.1, 0.1, n, f), [0.3, -0.2, -2.0]),
clip(&p, [0.3, -0.2, -2.0]),
);
assert_eq!((g[0], g[1], g[3]), (c[0], c[1], c[3]));
}
#[test]
fn a_symmetric_projection_matches_the_renderers_own() {
let (n, f) = (0.1f32, 100.0f32);
let t = n * (30f32.to_radians()).tan();
let eye = XrEye::new(
gl_frustum(-t, t, -t, t, n, f),
&Matrix4::identity(),
&Matrix4::identity(),
[0, 0, 8, 8],
);
let ours = Matrix4::perspective(60f32.to_radians(), 1.0, n, f);
for (a, b) in eye.projection.iter().zip(ours.elements) {
assert!(
(a - b).abs() < 1e-4,
"{:?} vs {:?}",
eye.projection,
ours.elements
);
}
assert!((eye.fov() - 60f32.to_radians()).abs() < 1e-5);
}
#[test]
fn the_eye_sits_under_the_origin_and_keeps_the_lens() {
let origin = Matrix4::translation(Vector3::new(10.0, 0.0, 0.0));
let pose = Matrix4::translation(Vector3::new(0.032, 1.6, 0.5));
let eye = XrEye::new(
gl_frustum(-0.1, 0.1, -0.1, 0.1, 0.1, 10.0),
&origin,
&pose,
[960, 0, 960, 1080],
);
let mut lens = PerspectiveCamera::new(50.0, 1.0, 0.1, 10.0);
lens.aperture = 0.02;
let c = eye.camera(&lens);
assert!((c.position - Vector3::new(10.032, 1.6, 0.5)).length() < 1e-5);
assert!(c.view_override.is_none(), "a rigid pose views by look_at");
assert_eq!(c.aperture, 0.02);
assert_eq!(c.projection_matrix().elements, eye.projection);
let v = c.view_matrix();
let at = clip(&v.elements, [10.032, 1.6, 0.5]);
assert!(at[..3].iter().all(|x| x.abs() < 1e-4), "{at:?}");
}
#[test]
fn a_scaled_rig_views_through_its_inverse() {
let origin = Matrix4::scale(Vector3::new(2.0, 2.0, 2.0));
let eye = XrEye::new(
gl_frustum(-0.1, 0.1, -0.1, 0.1, 0.1, 10.0),
&origin,
&Matrix4::identity(),
[0, 0, 8, 8],
);
let c = eye.camera(&PerspectiveCamera::new(50.0, 1.0, 0.1, 10.0));
let v = c.view_matrix();
let p = clip(&v.elements, [0.0, 0.0, -2.0]);
assert!(
(p[2] + 1.0).abs() < 1e-5,
"world -2 is one unit ahead of a doubled rig: {p:?}"
);
}
#[test]
fn slots_keep_their_sources() {
let mut s = InputSlots::new();
assert_eq!(s.connect("left"), 0);
assert_eq!(s.connect("right"), 1);
assert_eq!(s.disconnect(&"left"), Some(0));
assert_eq!(s.slot_of(&"right"), Some(1), "the right stays put");
assert_eq!(s.connect("hand"), 0, "a new source takes the free slot");
assert_eq!(s.connect("hand"), 0, "connecting twice is once");
assert_eq!(s.iter().map(|(i, _)| i).collect::<Vec<_>>(), [0, 1]);
}
}