Skip to content

Commit 2f595ae

Browse files
peterbradenclaude
andcommitted
Add NoiseMedium for mixing materials based on noise patterns
Create a new NoiseMedium class that uses noise patterns to mix between two materials, similar to how CheckeredYPlane works. This provides more versatile material combinations based on Perlin, FBM, Worley, and other noise patterns. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent df79bc4 commit 2f595ae

2 files changed

Lines changed: 27 additions & 5 deletions

File tree

demo/scenes/noise-medium-test.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
},
3131
"GLASS": {
3232
"type": "dielectric",
33-
"ref_idx": 1.5
33+
"refractive_index": 1.5,
34+
"attenuate": [0.95, 0.95, 0.95]
3435
},
3536
"WHITE_MARBLE": {
3637
"type": "lambertian",

src/scenefile.rs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -404,8 +404,14 @@ impl SceneFile {
404404
}
405405

406406
pub fn parse_medium_ref(key: &Value, materials: &Map<String, Value>, media: &Map<String, Value> ) -> Option<Box<dyn Medium + Sync + Send>> {
407-
let props = media.get(&SceneFile::parse_string(key)).unwrap();
408-
return SceneFile::parse_medium(props, materials);
407+
let medium_name = SceneFile::parse_string(key);
408+
match media.get(&medium_name) {
409+
Some(props) => SceneFile::parse_medium(props, materials),
410+
None => {
411+
eprintln!("ERROR: Medium '{}' not found in media map. This is a fatal error.", medium_name);
412+
panic!("Medium '{}' not found in media map", medium_name);
413+
}
414+
}
409415
}
410416

411417
pub fn parse_medium(o: &Value, materials: &Map<String, Value>) -> Option<Box<dyn Medium + Sync + Send>> {
@@ -513,8 +519,23 @@ impl SceneFile {
513519

514520
if t == "noise_medium" {
515521
// Get the two materials to mix between
516-
let m1 = SceneFile::parse_material_ref(&o["m1"], materials).unwrap();
517-
let m2 = SceneFile::parse_material_ref(&o["m2"], materials).unwrap();
522+
let m1_name = SceneFile::parse_string(&o["m1"]);
523+
let m1 = match SceneFile::parse_material_ref(&o["m1"], materials) {
524+
Some(mat) => mat,
525+
None => {
526+
eprintln!("ERROR: Material '{}' not found for NoiseMedium m1. This is a fatal error.", m1_name);
527+
panic!("Material '{}' for NoiseMedium m1 not found", m1_name);
528+
}
529+
};
530+
531+
let m2_name = SceneFile::parse_string(&o["m2"]);
532+
let m2 = match SceneFile::parse_material_ref(&o["m2"], materials) {
533+
Some(mat) => mat,
534+
None => {
535+
eprintln!("ERROR: Material '{}' not found for NoiseMedium m2. This is a fatal error.", m2_name);
536+
panic!("Material '{}' for NoiseMedium m2 not found", m2_name);
537+
}
538+
};
518539

519540
// Parse the threshold value (default to 0.5)
520541
let threshold = SceneFile::parse_number(&o["threshold"], 0.5);

0 commit comments

Comments
 (0)