Add material library and targeted surface drops
This commit is contained in:
1 parent
3931de1f0a
commit
02247f029a
24 files changed
+2794
-150
No files matched your search
@@ -79,6 +79,33 @@ impl MaterialAsset {
|
||||
}
|
||||
}
|
||||
|
||||
/// Loads a project Material or direct-base Material Instance into one effective descriptor.
|
||||
///
|
||||
/// The returned descriptor retains the selected asset path so primitive/brush authoring can locate
|
||||
/// the shared source while renderer slots continue to preserve their stable [`MaterialRef`].
|
||||
pub fn load_resolved_material_from_path(
|
||||
catalog_path: &str,
|
||||
) -> Result<(MaterialDesc, MaterialRenderState), String> {
|
||||
if let Ok(asset) = MaterialAsset::load_from_path(catalog_path) {
|
||||
let mut material = asset.material;
|
||||
material.material_asset_path = Some(catalog_path.to_string());
|
||||
return Ok((material, asset.render_state));
|
||||
}
|
||||
|
||||
let instance = MaterialInstanceAsset::load_from_path(catalog_path)?;
|
||||
let base_path = instance.base.0.source_path.as_deref().ok_or_else(|| {
|
||||
format!(
|
||||
"material instance {} has no loadable base path",
|
||||
instance.label
|
||||
)
|
||||
})?;
|
||||
let base = MaterialAsset::load_from_path(base_path)?;
|
||||
let mut material = base.material;
|
||||
instance.apply_to(&mut material);
|
||||
material.material_asset_path = Some(catalog_path.to_string());
|
||||
Ok((material, base.render_state))
|
||||
}
|
||||
|
||||
/// Explicit reusable overrides over one project Material asset.
|
||||
#[derive(Asset, TypePath, Debug, Clone, Serialize, Deserialize, PartialEq)]
|
||||
pub struct MaterialInstanceAsset {
|
||||
@@ -270,4 +297,66 @@ mod tests {
|
||||
.iter()
|
||||
.any(|value| value.name == "edge_width"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn resolved_material_instance_applies_sparse_overrides_and_keeps_instance_path() {
|
||||
let root = std::env::temp_dir().join(format!(
|
||||
"blacksite-resolved-material-instance-{}",
|
||||
std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.unwrap()
|
||||
.as_nanos()
|
||||
));
|
||||
let base_path = root.join("base.ron");
|
||||
let instance_path = root.join("instance.ron");
|
||||
std::fs::create_dir_all(&root).unwrap();
|
||||
let base = MaterialAsset {
|
||||
schema_version: MATERIAL_ASSET_SCHEMA_VERSION,
|
||||
label: "Base".into(),
|
||||
shader: None,
|
||||
shader_ref: None,
|
||||
render_state: MaterialRenderState {
|
||||
double_sided: true,
|
||||
..Default::default()
|
||||
},
|
||||
material: MaterialDesc {
|
||||
roughness: 0.8,
|
||||
..Default::default()
|
||||
},
|
||||
};
|
||||
std::fs::write(
|
||||
&base_path,
|
||||
ron::ser::to_string_pretty(&base, ron::ser::PrettyConfig::default()).unwrap(),
|
||||
)
|
||||
.unwrap();
|
||||
let instance = MaterialInstanceAsset {
|
||||
schema_version: MATERIAL_INSTANCE_SCHEMA_VERSION,
|
||||
label: "Instance".into(),
|
||||
base: MaterialRef::new(
|
||||
EditorAssetRef::new("base", "material:source", "Base")
|
||||
.with_source_path(base_path.to_string_lossy().into_owned()),
|
||||
),
|
||||
parameters: vec![MaterialParameter {
|
||||
name: "roughness".into(),
|
||||
value: crate::MaterialParameterValue::Float(0.25),
|
||||
}],
|
||||
textures: Vec::new(),
|
||||
};
|
||||
std::fs::write(
|
||||
&instance_path,
|
||||
ron::ser::to_string_pretty(&instance, ron::ser::PrettyConfig::default()).unwrap(),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let (resolved, state) =
|
||||
load_resolved_material_from_path(instance_path.to_str().unwrap()).unwrap();
|
||||
|
||||
assert_eq!(resolved.roughness, 0.25);
|
||||
assert_eq!(
|
||||
resolved.material_asset_path.as_deref(),
|
||||
instance_path.to_str()
|
||||
);
|
||||
assert!(state.double_sided);
|
||||
std::fs::remove_dir_all(root).unwrap();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user