use crate::cameras::{OrthographicCamera, PerspectiveCamera};
use crate::core::{BufferAttribute, BufferGeometry, InstancedMesh, LineSegments, Points};
use crate::geometries::{
BoxGeometry, CapsuleGeometry, ConeGeometry, CylinderGeometry, PlaneGeometry, SphereGeometry,
};
use crate::lights::{AmbientLight, DirectionalLight, Light, PointLight, RectAreaLight, SpotLight};
use crate::materials::{Material, PointsMaterial};
use crate::math::{Color, Matrix4, Quaternion, Vector3};
use super::parse::{UsdLayer, UsdPrim};
#[derive(Debug, Clone)]
pub struct UsdCamera {
pub object: crate::core::ObjectId,
pub name: String,
pub perspective: Option<PerspectiveCamera>,
pub orthographic: Option<OrthographicCamera>,
}
pub fn is_drawable(prim: &UsdPrim, show_guides: bool, show_proxies: bool) -> bool {
match prim.value("purpose").and_then(|v| v.as_str()) {
Some("guide") => show_guides,
Some("proxy") => show_proxies,
_ => true,
}
}
fn number(prim: &UsdPrim, name: &str, fallback: f32) -> f32 {
prim.value(name)
.and_then(|v| v.as_f64())
.map(|v| v as f32)
.unwrap_or(fallback)
}
fn colour(prim: &UsdPrim, fallback: Color) -> Color {
let value = prim
.value("inputs:color")
.or_else(|| prim.value("color"))
.map(|v| v.flat_f32())
.unwrap_or_default();
if value.len() >= 3 {
Color::new(value[0], value[1], value[2])
} else {
fallback
}
}
fn light_number(prim: &UsdPrim, name: &str, fallback: f32) -> f32 {
prim.value(&format!("inputs:{name}"))
.or_else(|| prim.value(name))
.and_then(|v| v.as_f64())
.map(|v| v as f32)
.unwrap_or(fallback)
}
pub fn light(prim: &UsdPrim) -> Option<Light> {
let colour = colour(prim, Color::WHITE);
let intensity =
light_number(prim, "intensity", 1.0) * light_number(prim, "exposure", 0.0).exp2();
Some(match prim.type_name.as_str() {
"DistantLight" => Light::Directional(DirectionalLight::new(colour, intensity)),
"SphereLight" | "DiskLight" | "CylinderLight" => {
match prim
.value("inputs:shaping:cone:angle")
.and_then(|v| v.as_f64())
{
Some(angle) => {
let mut spot = SpotLight::new(colour, intensity);
spot.angle = (angle as f32).to_radians();
spot.penumbra =
light_number(prim, "shaping:cone:softness", 0.0).clamp(0.0, 1.0);
Light::Spot(spot)
}
None => Light::Point(PointLight::new(colour, intensity)),
}
}
"RectLight" => Light::RectArea(RectAreaLight::new(
colour,
intensity,
light_number(prim, "width", 1.0),
light_number(prim, "height", 1.0),
)),
"DomeLight" => Light::Ambient(AmbientLight::new(colour, intensity)),
"GeometryLight" | "PortalLight" => return None,
_ => return None,
})
}
pub fn gprim(prim: &UsdPrim) -> Option<BufferGeometry> {
const SEGMENTS: usize = 32;
let geometry = match prim.type_name.as_str() {
"Cube" => {
let size = number(prim, "size", 2.0);
BoxGeometry::new(size, size, size)
}
"Sphere" => SphereGeometry::new(number(prim, "radius", 1.0), SEGMENTS, SEGMENTS / 2),
"Cylinder" => {
let radius = number(prim, "radius", 1.0);
CylinderGeometry::new(
radius,
radius,
number(prim, "height", 2.0),
SEGMENTS,
1,
false,
0.0,
std::f32::consts::TAU,
)
}
"Cone" => ConeGeometry::new(
number(prim, "radius", 1.0),
number(prim, "height", 2.0),
SEGMENTS,
1,
false,
0.0,
std::f32::consts::TAU,
),
"Capsule" => CapsuleGeometry::new(
number(prim, "radius", 0.5),
number(prim, "height", 1.0),
SEGMENTS / 4,
SEGMENTS,
),
"Plane" => PlaneGeometry::new(number(prim, "width", 1.0), number(prim, "length", 1.0)),
_ => return None,
};
Some(orient(geometry, prim))
}
fn orient(mut geometry: BufferGeometry, prim: &UsdPrim) -> BufferGeometry {
let axis = prim
.value("axis")
.and_then(|v| v.as_str())
.unwrap_or(match prim.type_name.as_str() {
"Plane" => "Z",
_ => "Z",
})
.to_string();
let quarter = std::f32::consts::FRAC_PI_2;
let (about, angle) = match (prim.type_name.as_str(), axis.as_str()) {
("Plane", "Y") => return geometry, ("Plane", "Z") => (Vector3::new(1.0, 0.0, 0.0), quarter),
("Plane", _) => (Vector3::new(0.0, 0.0, 1.0), quarter),
(_, "Y") => return geometry,
(_, "Z") => (Vector3::new(1.0, 0.0, 0.0), quarter),
(_, _) => (Vector3::new(0.0, 0.0, 1.0), -quarter),
};
rotate(&mut geometry, about, angle);
geometry
}
fn rotate(geometry: &mut BufferGeometry, axis: Vector3, angle: f32) {
let (s, c) = angle.sin_cos();
let turn = |v: [f32; 3]| -> [f32; 3] {
let v = Vector3::new(v[0], v[1], v[2]);
let dot = axis.x * v.x + axis.y * v.y + axis.z * v.z;
let cross = Vector3::new(
axis.y * v.z - axis.z * v.y,
axis.z * v.x - axis.x * v.z,
axis.x * v.y - axis.y * v.x,
);
[
v.x * c + cross.x * s + axis.x * dot * (1.0 - c),
v.y * c + cross.y * s + axis.y * dot * (1.0 - c),
v.z * c + cross.z * s + axis.z * dot * (1.0 - c),
]
};
for name in ["position", "normal"] {
let Some(attribute) = geometry.get_attribute(name) else {
continue;
};
let turned: Vec<f32> = attribute
.array
.as_chunks::<3>()
.0
.iter()
.flat_map(|c| turn([c[0], c[1], c[2]]))
.collect();
geometry.set_attribute(name, BufferAttribute::new(turned, 3));
}
}
pub fn points(prim: &UsdPrim) -> Option<Points> {
let positions = prim.value("points")?.flat_f32();
if positions.is_empty() {
return None;
}
let mut geometry = BufferGeometry::new();
geometry.set_attribute("position", BufferAttribute::new(positions.clone(), 3));
let colours = prim
.value("primvars:displayColor")
.map(|v| v.flat_f32())
.unwrap_or_default();
if colours.len() == positions.len() {
geometry.set_attribute("color", BufferAttribute::new(colours, 3));
}
let mut material = PointsMaterial::default();
if let Some(widths) = prim.value("widths").map(|v| v.flat_f32())
&& let Some(first) = widths.first()
{
material.size = *first;
}
Some(Points::new(geometry, Material::Points(material)))
}
pub fn curves(prim: &UsdPrim) -> Option<LineSegments> {
let positions = prim.value("points")?.flat_f32();
if positions.len() < 6 {
return None;
}
let counts: Vec<u32> = prim
.value("curveVertexCounts")
.map(|v| v.flat_u32())
.unwrap_or_else(|| vec![(positions.len() / 3) as u32]);
let mut line = Vec::new();
let mut at = 0usize;
for count in counts {
let count = count as usize;
for i in 0..count.saturating_sub(1) {
let a = (at + i) * 3;
let b = (at + i + 1) * 3;
if b + 2 >= positions.len() {
break;
}
line.extend_from_slice(&positions[a..a + 3]);
line.extend_from_slice(&positions[b..b + 3]);
}
at += count;
}
if line.is_empty() {
return None;
}
let mut geometry = BufferGeometry::new();
geometry.set_attribute("position", BufferAttribute::new(line, 3));
Some(LineSegments::new(
geometry,
Material::Line(crate::materials::LineBasicMaterial::new(Color::WHITE)),
))
}
pub fn camera(prim: &UsdPrim, object: crate::core::ObjectId) -> UsdCamera {
let horizontal = number(prim, "horizontalAperture", 20.955);
let vertical = number(prim, "verticalAperture", 15.2908);
let focal = number(prim, "focalLength", 50.0).max(1e-6);
let clip = prim
.value("clippingRange")
.map(|v| v.flat_f32())
.unwrap_or_default();
let near = clip.first().copied().unwrap_or(1.0);
let far = clip.get(1).copied().unwrap_or(1_000_000.0);
let orthographic = prim.value("projection").and_then(|v| v.as_str()) == Some("orthographic");
let mut out = UsdCamera {
object,
name: prim.name.clone(),
perspective: None,
orthographic: None,
};
if orthographic {
let (w, h) = (horizontal * 0.5, vertical * 0.5);
out.orthographic = Some(OrthographicCamera::new(-w, w, h, -h, near, far));
} else {
let fov = 2.0 * (vertical * 0.5 / focal).atan();
let aspect = if vertical > 0.0 {
horizontal / vertical
} else {
1.0
};
let mut camera = PerspectiveCamera::new(fov.to_degrees(), aspect, near, far);
camera.focus_distance = number(prim, "focusDistance", 0.0);
camera.f_stop = number(prim, "fStop", 0.0);
out.perspective = Some(camera);
}
out
}
pub fn point_instancer(
prim: &UsdPrim,
layer: &UsdLayer,
geometry_of: impl Fn(&UsdPrim) -> Option<(BufferGeometry, Material)>,
) -> Vec<(String, InstancedMesh)> {
let Some(positions) = prim.value("positions").map(|v| v.flat_f32()) else {
return Vec::new();
};
let count = positions.len() / 3;
if count == 0 {
return Vec::new();
}
let indices: Vec<u32> = prim
.value("protoIndices")
.map(|v| v.flat_u32())
.unwrap_or_else(|| vec![0; count]);
let targets = prim
.value("prototypes")
.map(|v| {
v.flat_tokens()
.into_iter()
.map(str::to_string)
.collect::<Vec<_>>()
})
.unwrap_or_default();
if targets.is_empty() {
return Vec::new();
}
let orientations = prim
.value("orientations")
.map(|v| v.flat_f32())
.unwrap_or_default();
let scales = prim
.value("scales")
.map(|v| v.flat_f32())
.unwrap_or_default();
let hidden: Vec<i64> = prim
.value("invisibleIds")
.map(|v| v.flat_f32().iter().map(|f| *f as i64).collect())
.unwrap_or_default();
let mut out: Vec<(String, InstancedMesh)> = Vec::new();
for (slot, path) in targets.iter().enumerate() {
let Some(prototype) = layer.prim_at(path) else {
continue;
};
let Some((geometry, material)) = geometry_of(prototype) else {
continue;
};
let transforms: Vec<Matrix4> = (0..count)
.filter(|i| indices.get(*i).copied().unwrap_or(0) as usize == slot)
.filter(|i| !hidden.contains(&(*i as i64)))
.map(|i| {
let position =
Vector3::new(positions[i * 3], positions[i * 3 + 1], positions[i * 3 + 2]);
let rotation = match orientations.get(i * 4..i * 4 + 4) {
Some(q) => Quaternion::new(q[1], q[2], q[3], q[0]),
None => Quaternion::identity(),
};
let scale = match scales.get(i * 3..i * 3 + 3) {
Some(s) => Vector3::new(s[0], s[1], s[2]),
None => Vector3::new(1.0, 1.0, 1.0),
};
Matrix4::compose(position, rotation, scale)
})
.collect();
if transforms.is_empty() {
continue;
}
let mut mesh = InstancedMesh::new(geometry, material, transforms.len());
mesh.transforms = transforms;
out.push((prototype.name.clone(), mesh));
}
out
}
pub fn is_light(type_name: &str) -> bool {
matches!(
type_name,
"DistantLight" | "SphereLight" | "DiskLight" | "CylinderLight" | "RectLight" | "DomeLight"
)
}
pub fn is_gprim(type_name: &str) -> bool {
matches!(
type_name,
"Cube" | "Sphere" | "Cylinder" | "Cone" | "Capsule" | "Plane"
)
}
#[cfg(test)]
mod tests {
use super::super::{parse::parse, scene::to_scene, scene::UsdScene};
use super::*;
use crate::core::{Object3D, ObjectId, ObjectKind};
fn stage() -> UsdScene {
to_scene(&parse(include_str!("testdata/schemas.usda")).expect("parses"))
}
fn find<'a>(scene: &'a UsdScene, name: &str) -> &'a Object3D {
fn walk<'a>(scene: &'a UsdScene, id: ObjectId, name: &str) -> Option<&'a Object3D> {
let node = scene.arena.get(id)?;
if node.name == name {
return Some(node);
}
node.children.iter().find_map(|c| walk(scene, *c, name))
}
scene
.roots
.iter()
.find_map(|r| walk(scene, *r, name))
.unwrap_or_else(|| panic!("no prim called {name}"))
}
fn names(scene: &UsdScene) -> Vec<String> {
fn walk(scene: &UsdScene, id: ObjectId, out: &mut Vec<String>) {
let Some(node) = scene.arena.get(id) else {
return;
};
out.push(node.name.clone());
for child in &node.children {
walk(scene, *child, out);
}
}
let mut out = Vec::new();
for root in &scene.roots {
walk(scene, *root, &mut out);
}
out
}
#[test]
fn lights_become_lights() {
let scene = stage();
let ObjectKind::Light(Light::Directional(sun)) = &find(&scene, "Sun").kind else {
panic!(
"DistantLight should be directional, got {:?}",
find(&scene, "Sun").kind
);
};
assert!((sun.intensity - 6.0).abs() < 1e-5, "{}", sun.intensity);
assert!((sun.color.r - 1.0).abs() < 1e-5 && (sun.color.b - 0.8).abs() < 1e-5);
assert!(matches!(
find(&scene, "Bulb").kind,
ObjectKind::Light(Light::Point(_))
));
assert!(matches!(
find(&scene, "Sky").kind,
ObjectKind::Light(Light::Ambient(_))
));
let ObjectKind::Light(Light::RectArea(panel)) = &find(&scene, "Panel").kind else {
panic!("RectLight should be an area light");
};
assert_eq!((panel.width, panel.height), (4.0, 2.0));
}
#[test]
fn a_shaped_light_is_a_spot() {
let scene = stage();
let ObjectKind::Light(Light::Spot(torch)) = &find(&scene, "Torch").kind else {
panic!(
"a ShapingAPI cone should make a spot, got {:?}",
find(&scene, "Torch").kind
);
};
assert!(
(torch.angle - 25f32.to_radians()).abs() < 1e-5,
"{}",
torch.angle
);
assert!((torch.penumbra - 0.4).abs() < 1e-5);
}
#[test]
fn the_quadrics_become_geometry() {
let scene = stage();
for (name, expected_extent) in [
("Box", 3.0f32),
("Ball", 4.0),
("Tube", 4.0),
("Spike", 2.0),
("Pill", 4.0),
("Ground", 20.0),
] {
let ObjectKind::Mesh(mesh) = &find(&scene, name).kind else {
panic!("{name} should be a mesh, got {:?}", find(&scene, name).kind);
};
let position = mesh.geometry.get_attribute("position").expect("positions");
assert!(position.count() > 3, "{name} has no vertices");
let mut lo = [f32::MAX; 3];
let mut hi = [f32::MIN; 3];
for v in position.array.as_chunks::<3>().0 {
for i in 0..3 {
lo[i] = lo[i].min(v[i]);
hi[i] = hi[i].max(v[i]);
}
}
let span = (0..3).map(|i| hi[i] - lo[i]).fold(0.0f32, f32::max);
assert!(
(span - expected_extent).abs() < 0.3,
"{name}: span {span}, expected about {expected_extent}"
);
}
}
#[test]
fn points_and_curves_become_their_own_node_kinds() {
let scene = stage();
let ObjectKind::Points(dust) = &find(&scene, "Dust").kind else {
panic!("Points should be a point cloud");
};
assert_eq!(dust.geometry.get_attribute("position").unwrap().count(), 3);
assert_eq!(dust.geometry.get_attribute("color").unwrap().count(), 3);
let ObjectKind::LineSegments(hair) = &find(&scene, "Hair").kind else {
panic!("BasisCurves should be line segments");
};
assert_eq!(hair.geometry.get_attribute("position").unwrap().count(), 6);
}
#[test]
fn cameras_come_back_with_the_stage() {
let scene = stage();
assert_eq!(scene.cameras.len(), 2, "both cameras");
let eye = scene.cameras.iter().find(|c| c.name == "Eye").expect("Eye");
let perspective = eye.perspective.as_ref().expect("perspective");
assert!(
(perspective.fov.to_degrees() - 37.85).abs() < 0.1,
"fov was {} degrees",
perspective.fov.to_degrees()
);
assert!((perspective.aspect - 1.5).abs() < 1e-5);
assert_eq!((perspective.near, perspective.far), (0.1, 500.0));
let ortho = scene
.cameras
.iter()
.find(|c| c.name == "Ortho")
.expect("Ortho");
let orthographic = ortho.orthographic.as_ref().expect("orthographic");
assert_eq!((orthographic.left, orthographic.right), (-50.0, 50.0));
assert!(eye.perspective.is_some() && ortho.perspective.is_none());
}
#[test]
fn purpose_and_visibility_are_honoured() {
let scene = stage();
assert!(
!find(&scene, "Hidden").visible,
"invisible should not be visible"
);
let names = names(&scene);
assert!(
names.contains(&"Hidden".to_string()),
"invisible still exists"
);
assert!(
!names.contains(&"GuideOnly".to_string()),
"guide geometry should not be in the scene: {names:?}"
);
}
#[test]
fn a_point_instancer_becomes_instanced_draws() {
let layer = parse(include_str!("testdata/instancer.usda")).expect("parses");
let scene = to_scene(&layer);
let scatter = find(&scene, "Scatter");
assert_eq!(scatter.children.len(), 2, "one draw per prototype");
let mut by_name: Vec<(String, usize)> = scatter
.children
.iter()
.map(|c| {
let node = scene.arena.get(*c).unwrap();
let ObjectKind::InstancedMesh(mesh) = &node.kind else {
panic!("{} should be instanced, got {:?}", node.name, node.kind);
};
(node.name.clone(), mesh.transforms.len())
})
.collect();
by_name.sort();
assert_eq!(by_name, vec![("Rock".into(), 1), ("Tree".into(), 2)]);
}
#[test]
fn instance_transforms_are_composed_from_all_three_arrays() {
let scene = to_scene(&parse(include_str!("testdata/instancer.usda")).unwrap());
let scatter = find(&scene, "Scatter");
let trees = scatter
.children
.iter()
.map(|c| scene.arena.get(*c).unwrap())
.find(|n| n.name == "Tree")
.expect("the tree draw");
let ObjectKind::InstancedMesh(mesh) = &trees.kind else {
panic!("instanced");
};
let first = mesh.transforms[0].elements;
assert_eq!((first[12], first[13], first[14]), (0.0, 0.0, 0.0));
assert!((first[0] - 1.0).abs() < 1e-5, "unit scale");
let third = mesh.transforms[1].elements;
assert_eq!(third[12], 10.0);
assert!(
(third[0] - 0.5).abs() < 1e-5,
"half scale, got {}",
third[0]
);
}
#[test]
fn hidden_instances_are_left_out() {
let scene = to_scene(&parse(include_str!("testdata/instancer.usda")).unwrap());
let total: usize = find(&scene, "Scatter")
.children
.iter()
.map(|c| match &scene.arena.get(*c).unwrap().kind {
ObjectKind::InstancedMesh(m) => m.transforms.len(),
_ => 0,
})
.sum();
assert_eq!(total, 3, "four instances less the one in invisibleIds");
}
#[test]
fn an_instanceable_reference_still_brings_its_content() {
use super::super::compose::{compose, ComposeOptions, MemoryResolver};
let layer = parse(include_str!("testdata/instancer.usda")).unwrap();
let composed = compose(
&layer,
"instancer.usda",
&MemoryResolver::new(),
&ComposeOptions::default(),
)
.unwrap();
assert!(
composed.prim_at("/World/Copy/Shape").is_some(),
"the instanced copy should carry the source's geometry"
);
}
#[test]
fn the_binary_form_gives_the_same_stage() {
let text = stage();
let binary = to_scene(
&super::super::UsdLoader::parse_layer(include_bytes!("testdata/schemas.usdc"))
.expect("the crate parses"),
);
assert_eq!(binary.cameras.len(), text.cameras.len());
assert_eq!(names(&binary), names(&text));
for name in [
"Sun", "Bulb", "Panel", "Torch", "Box", "Ball", "Dust", "Hair",
] {
let a = format!("{:?}", std::mem::discriminant(&find(&text, name).kind));
let b = format!("{:?}", std::mem::discriminant(&find(&binary, name).kind));
assert_eq!(a, b, "{name} came out as a different kind of node");
}
}
}