Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@ __pycache__/

# Zola build output of the user documentation (site/)
/site/public/

# Local editor settings and CMake build trees (e.g. from the oracle scripts)
/.vscode/
/build/
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ ocio = "0.2.0"
# families `media`, `physics`, `proc`, `semantics`, `skel`, `ui` (`geom`, `lux`,
# `render`, `shade`, `vol` are always on). Bump order: Moho, then crust-render,
# then Mantle.
moho = { git = "https://github.com/doubleailes/Moho", rev = "8163843d896f161d167a761dcae9b14a8bd21f7b" }
moho = { git = "https://github.com/doubleailes/Moho", rev = "4d63d411c368883ea27c5c0542876d805a7d60b6" }

# The hot loops are spread across crates: the integrator in `crust-core`
# calls the kernel in `crust-rt`, which calls `glam`'s SIMD vector ops.
Expand Down
50 changes: 31 additions & 19 deletions crates/crust-core/src/scene/hydra/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,22 +149,24 @@ pub(in crate::scene) fn load_ptex(
loaded
}

/// An asset value as a path: openusd's `resolved_path` when it has one, else
/// the authored string, a relative one anchored against the directory of the
/// layer that authors it (`layer_dir`, the host's
/// `HostFacts::authoring_layer_dir`), the root layer's when that is unknown.
/// An asset value as a path, from the index alone: the value's
/// `resolved_path` when it has one, else the authored string, an absolute
/// one as it is and a relative one against the directory of `stage_path`.
///
/// openusd's value resolution anchors an `asset` value on its authoring layer
/// and records where it resolves even when no file is there (a `<UDIM>` set,
/// a missing texture), and the scene index carries that value, so an asset
/// authored in a sublayer or a reference (ALab's look layers, five directories
/// below `entry.usda`) reaches the host from its own layer's directory. A
/// path authored as a `string` carries no resolution: `layer_dir` anchors it
/// (`tests/asset_anchoring.rs`).
/// The index anchors an `asset` value on the layer that authors it and
/// records where it resolves even when no file is there (a `<UDIM>` set, a
/// missing texture; Moho anchors what openusd leaves unresolved), so an asset
/// authored in a sublayer or a reference (ALab's look layers, five
/// directories below `entry.usda`) reaches the host from its own layer's
/// directory. A path authored as a `string` is text, which USD does not
/// anchor: a relative one resolves against the stage's directory and raises
/// `asset.string_path`, naming `attribute`, the property that authors it
/// (`tests/asset_anchoring.rs`). No answer about the host's stage is asked:
/// the image depends on the index alone.
pub(in crate::scene) fn asset_value_path(
value: &sdf::Value,
stage_path: &Path,
layer_dir: &dyn Fn() -> Option<PathBuf>,
attribute: &dyn Fn() -> Option<sdf::Path>,
) -> Option<PathBuf> {
let (authored, resolved) = match value {
sdf::Value::AssetPath(p) => (p.as_str().to_string(), p.resolved_path()),
Expand All @@ -183,11 +185,21 @@ pub(in crate::scene) fn asset_value_path(
if candidate.is_absolute() {
return Some(candidate.to_path_buf());
}
let dir = layer_dir().unwrap_or_else(|| {
stage_path
.parent()
.unwrap_or_else(|| Path::new("."))
.to_path_buf()
});
Some(dir.join(candidate))
let dir = stage_path.parent().unwrap_or_else(|| Path::new("."));
let resolved = dir.join(candidate);
if matches!(value, sdf::Value::String(_)) {
let attribute = attribute();
let prim = attribute
.as_ref()
.map_or_else(String::new, |a| a.prim_path().to_string());
let named = attribute.map_or_else(|| "an attribute".to_owned(), |a| a.to_string());
crate::warning!(
AssetStringPath,
at = prim,
"{named} authors the relative path \"{authored}\" as a string, which USD does not \
anchor on its layer; it resolves against the stage's directory, to {}",
resolved.display()
);
}
Some(resolved)
}
38 changes: 19 additions & 19 deletions crates/crust-core/src/scene/hydra/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -415,15 +415,14 @@ impl RenderCamera {
/// camera on `stage`.
pub(in crate::scene) fn build_camera(
scene: &dyn SceneIndex,
host: &dyn super::index::host::HostFacts,
path: &sdf::Path,
settings: &RenderSettings,
framing: Framing,
) -> Option<RenderCamera> {
let cam = camera_at(scene, path)?;
let world = super::xform::world(&cam);
let frame = CameraFrame::read(&cam, world, settings, framing);
let (camera, refused_meters_per_unit) = lens_camera(&cam, host, &frame, settings);
let (camera, refused_meters_per_unit) = lens_camera(&cam, scene, &frame, settings);
let clip = clipping(&cam);
let camera = camera.with_clipping(clip.near, clip.far, &clip.planes);
let read = lens(&cam, &frame);
Expand Down Expand Up @@ -570,10 +569,18 @@ const FALLBACK_METERS_PER_UNIT: f64 = 0.01;

/// The stage's authored `metersPerUnit` (C++ `UsdGeomGetStageMetersPerUnit`,
/// which reads only the root and session layers), `None` when no layer
/// authors it: a host fact, read off the stage (design D4: Hydra carries no
/// unit). Unchecked: [`lens_diameter`] refuses a value that is not a length.
fn authored_meters_per_unit(host: &dyn super::index::host::HostFacts) -> Option<f64> {
host.meters_per_unit()
/// authors it: Moho's `__usdMetersPerUnit/metersPerUnit` on the index's
/// absolute root, there only when authored (Hydra itself carries no unit).
/// Unchecked: [`lens_diameter`] refuses a value that is not a length.
fn authored_meters_per_unit(scene: &dyn SceneIndex) -> Option<f64> {
use moho::tokens::usd_meters_per_unit as t;
match PrimData::at(scene, &sdf::Path::abs_root())
.locate([t::USD_METERS_PER_UNIT, t::METERS_PER_UNIT])?
.value()?
{
sdf::Value::Double(m) => Some(m),
_ => None,
}
}

/// The lens diameter in scene units: `focalLength / fStop` millimetres,
Expand All @@ -584,7 +591,7 @@ fn authored_meters_per_unit(host: &dyn super::index::host::HostFacts) -> Option<
/// that is not finite, not positive, or so small that the diameter overflows
/// `f32` is refused and reads as the fallback.
fn lens_diameter(
host: &dyn super::index::host::HostFacts,
scene: &dyn SceneIndex,
focal_length: f32,
f_stop: f32,
depth_of_field: bool,
Expand All @@ -595,7 +602,7 @@ fn lens_diameter(
let diameter = |meters_per_unit: f64| {
(f64::from(focal_length) / f64::from(f_stop) * 0.001 / meters_per_unit) as f32
};
match authored_meters_per_unit(host) {
match authored_meters_per_unit(scene) {
// A unit so small that the diameter overflows `f32` is no length
// either: the lens would offset every ray to infinity.
Some(m) if m.is_finite() && m > 0.0 && diameter(m).is_finite() => (diameter(m), None),
Expand All @@ -608,7 +615,7 @@ fn lens_diameter(
/// the stage unit its lens refused.
fn lens_camera(
cam: &PrimData,
host: &dyn super::index::host::HostFacts,
scene: &dyn SceneIndex,
frame: &CameraFrame,
settings: &RenderSettings,
) -> (Camera, Option<f64>) {
Expand Down Expand Up @@ -641,7 +648,7 @@ fn lens_camera(

let vfov_deg = 2.0 * frame.tan_half_vfov().atan().to_degrees();
let (aperture, refused_meters_per_unit) =
lens_diameter(host, frame.focal_length, f_stop, settings.depth_of_field());
lens_diameter(scene, frame.focal_length, f_stop, settings.depth_of_field());
// A pinhole has no focus plane, and USD's `focusDistance = 0` (what
// GfCamera writes, and a DCC camera with focus off) would put the image
// plane on the eye: every ray would have no direction. A pinhole's rays
Expand Down Expand Up @@ -797,7 +804,6 @@ mod tests {
use crate::scene::hydra::settings::CameraChoice;
use crate::scene::usd_import::prim_at;
use crate::scene::usd_import::stage_builder;
use crate::scene::usd_import::stage_facts::StageFacts;
use moho::openusd::usd::InitialLoadSet;
use moho::openusd::usd::{Prim, Stage};

Expand All @@ -814,13 +820,7 @@ mod tests {
framing: Framing,
) -> Option<RenderCamera> {
let scene = scene_of(stage);
super::build_camera(
scene.chain.as_ref(),
&StageFacts::of(stage),
prim.path(),
settings,
framing,
)
super::build_camera(scene.chain.as_ref(), prim.path(), settings, framing)
}

fn camera_lens(
Expand All @@ -844,7 +844,7 @@ mod tests {
}

fn authored_meters_per_unit(stage: &Stage) -> Option<f64> {
super::authored_meters_per_unit(&StageFacts::of(stage))
super::authored_meters_per_unit(scene_of(stage).chain.as_ref())
}

/// A stage on a `.usda` holding `metadata` and nothing else.
Expand Down
2 changes: 1 addition & 1 deletion crates/crust-core/src/scene/hydra/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ fn visit_camera(
// named.
let is_first = ctx.first_camera_path.as_ref() == Some(path);
if ctx.camera.is_none() && (is_named || is_first) {
match build_camera(index, ctx.caches.host, path, &ctx.settings, ctx.framing) {
match build_camera(index, path, &ctx.settings, ctx.framing) {
Some(c) if is_named || named.is_none() => {
debug!("Imported USD camera at {}", path);
ctx.camera = Some((c, path.clone()));
Expand Down
11 changes: 0 additions & 11 deletions crates/crust-core/src/scene/hydra/index/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ pub trait HostFacts {
fn layer_stack(&self) -> Vec<PathBuf>;
/// How many prims the strongest layer hides with an `over`.
fn invisible_overrides(&self) -> Option<u32>;
/// The stage's `metersPerUnit`, when a layer authors it.
fn meters_per_unit(&self) -> Option<f64>;
/// Why the prim at `pass` is no `RenderPass`, for the host's error.
fn not_a_pass(&self, pass: &sdf::Path) -> String;
/// The `.mtlx` document (and the material in it) the material at
Expand All @@ -49,9 +47,6 @@ pub trait HostFacts {
/// Whether the prim at `path` is an instance whose prototype the stage
/// does not compose.
fn instance_without_prototype(&self, path: &sdf::Path) -> bool;
/// The directory of the layer authoring the value of the attribute at
/// `attr`, where a relative asset path in it is anchored.
fn authoring_layer_dir(&self, attr: &sdf::Path) -> Option<PathBuf>;
}

/// A host with no stage to ask: every fact absent.
Expand All @@ -70,9 +65,6 @@ impl HostFacts for NoHostFacts {
fn invisible_overrides(&self) -> Option<u32> {
None
}
fn meters_per_unit(&self) -> Option<f64> {
None
}
fn not_a_pass(&self, _: &sdf::Path) -> String {
"the host has no stage to say why".to_string()
}
Expand All @@ -91,7 +83,4 @@ impl HostFacts for NoHostFacts {
fn instance_without_prototype(&self, _: &sdf::Path) -> bool {
false
}
fn authoring_layer_dir(&self, _: &sdf::Path) -> Option<PathBuf> {
None
}
}
9 changes: 5 additions & 4 deletions crates/crust-core/src/scene/hydra/lights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ fn lux_shaping(

let ies_file = lp(prim).value("shaping:ies:file").and_then(|v| {
asset_value_path(&v, caches.stage_path, &|| {
caches.layer_dir(prim.path(), "inputs:shaping:ies:file")
prim.path().append_property("inputs:shaping:ies:file").ok()
})
});
if let Some(path) = ies_file {
Expand Down Expand Up @@ -505,7 +505,7 @@ fn rect_light_texture(
let file = "inputs:texture:file";
let path = lp(prim).value("texture:file").and_then(|v| {
asset_value_path(&v, caches.stage_path, &|| {
caches.layer_dir(prim.path(), file)
prim.path().append_property(file).ok()
})
})?;
let space = texture_color_space(prim, caches.working);
Expand Down Expand Up @@ -796,11 +796,12 @@ pub(in crate::scene) fn emit_dome_light(
) {
// The authoring layer anchors the path, not the root layer: the Moana
// island's lights author `../textures/islandsun.exr` relative to
// `usd/island.usda` (see `asset_value_path`).
// `usd/island.usda`, an `asset` the index resolves on that layer (see
// `asset_value_path`).
let file = "inputs:texture:file";
let texture_path = lp(prim).value("texture:file").and_then(|v| {
asset_value_path(&v, ctx.stage_path, &|| {
ctx.caches.layer_dir(prim.path(), file)
prim.path().append_property(file).ok()
})
});
let (lights, assets) = (&mut ctx.lights, ctx.assets);
Expand Down
57 changes: 6 additions & 51 deletions crates/crust-core/src/scene/hydra/materials.rs
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ fn pxr_displacement(
.and_then(|v| {
asset_value_path(v, caches.stage_path, &|| {
node.as_ref()
.and_then(|n| src.layer_dir(n, "inputs:filename"))
.and_then(|n| n.append_property("inputs:filename").ok())
})
})?;
maps.push(crate::PtexRef(load_ptex(
Expand Down Expand Up @@ -788,7 +788,6 @@ fn inline_mtlx_material(
let mat_path = &src.path;
let net = super::mtlx_network::translate(
caches.stage_path,
&|p, name| src.layer_dir(p, name),
&caches.materials.library,
surface,
volume,
Expand Down Expand Up @@ -822,27 +821,18 @@ fn inline_mtlx_material(
}

/// A material as the index readers read it: its prim and its networks
/// ([`super::index::network`]) in the scene index the import reads, and the
/// nodes' prims, for what a shader input authors itself.
pub(in crate::scene) struct MatSource<'h> {
/// ([`super::index::network`]) in the scene index the import reads.
pub(in crate::scene) struct MatSource {
/// The material's path, in the namespace it is bound in.
pub(in crate::scene) path: sdf::Path,
pub(in crate::scene) prim: super::index::data::PrimData,
pub(in crate::scene) networks:
std::collections::BTreeMap<String, super::index::network::Network>,
index: moho::scene_index::SceneIndexHandle,
/// For a material inside a re-rooted prototype, `(the path the
/// instancing chain holds it at, its name)`: its prims are read there
/// and named here ([`super::index::data::PrimData::rebased`]).
rebase: Option<(sdf::Path, sdf::Path)>,
/// The host, for what only it knows: the layer an asset value is
/// authored in ([`MatSource::layer_dir`]).
host: &'h dyn super::index::host::HostFacts,
}

impl<'h> MatSource<'h> {
impl MatSource {
/// The material at `mat_path`.
pub(in crate::scene) fn new(mat_path: &sdf::Path, caches: &ImportCaches<'h>) -> Self {
pub(in crate::scene) fn new(mat_path: &sdf::Path, caches: &ImportCaches<'_>) -> Self {
use super::index::data::PrimData;
// A material bound inside a re-rooted prototype: read in the
// instancing chain, at the path the binding names (Moho's M11).
Expand All @@ -855,9 +845,6 @@ impl<'h> MatSource<'h> {
path: mat_path.clone(),
networks: super::index::network::networks(&prim),
prim,
index: inst.clone(),
rebase: Some((data.clone(), name.clone())),
host: caches.host,
};
}
// Read at the top of the import's chain, after the material-node
Expand All @@ -870,38 +857,6 @@ impl<'h> MatSource<'h> {
path: mat_path.clone(),
networks: super::index::network::networks(&prim),
prim,
index,
rebase: None,
host: caches.host,
}
}

/// The directory of the layer authoring the value of attribute `name` of
/// the prim at `path` (read as the material is, so named by its USD
/// path): where a relative asset path in it is anchored.
pub(in crate::scene) fn layer_dir(
&self,
path: &sdf::Path,
name: &str,
) -> Option<std::path::PathBuf> {
let attr = self.at(path).path().append_property(name).ok()?;
self.host.authoring_layer_dir(&attr)
}

/// The prim at `path`, read as the material is.
pub(in crate::scene) fn at(&self, path: &sdf::Path) -> super::index::data::PrimData {
use super::index::data::PrimData;
match &self.rebase {
Some((data, name)) => {
let at = path
.as_str()
.strip_prefix(name.as_str())
.filter(|rest| rest.is_empty() || rest.starts_with('/'))
.and_then(|rest| sdf::Path::new(format!("{data}{rest}").as_str()).ok())
.unwrap_or_else(|| path.clone());
PrimData::rebased(&self.index, &at, data, name)
}
None => PrimData::in_index(&self.index, path),
}
}

Expand Down Expand Up @@ -1203,7 +1158,7 @@ pub(in crate::scene) fn material_ptex(
let node_path = network.path(name)?;
let value = network.nodes.get(name)?.value(file)?;
let path = asset_value_path(&value, caches.stage_path, &|| {
src.layer_dir(&node_path, &format!("inputs:{file}"))
node_path.append_property(format!("inputs:{file}")).ok()
})?;

let space = crate::ColorSpace::new(Space::G22_REC709, caches.working);
Expand Down
Loading
Loading