Skip to main content

rusterix/
material_profile.rs

1use theframework::prelude::*;
2
3/// High-level PBR material profiles with a single adjustment knob `k`.
4#[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq)]
5pub enum MaterialProfile {
6    Wood,
7    Stone,
8    Dirt,
9    Metal,
10    Water,
11    Plastic,
12    Fabric,
13    Glass,
14}
15
16impl MaterialProfile {
17    /// Compute the *target* (metallic, roughness) at full effect for a given color.
18    /// No clamping here; your pipeline can decide ranges.
19    pub fn evaluate_target(&self, color: Vec3<f32>) -> (f32, f32) {
20        let r = color.x;
21        let g = color.y;
22        let b = color.z;
23        let max_c = r.max(g).max(b);
24        let min_c = r.min(g).min(b);
25        let saturation = max_c - min_c; // chroma
26        let brightness = 0.2126 * r + 0.7152 * g + 0.0722 * b; // luma-ish
27
28        match *self {
29            MaterialProfile::Wood => {
30                let metallic = 0.05 * saturation; // tiny tint-driven metallic feel
31                let base_r = 0.70 - 0.20 * brightness + 0.20 * (1.0 - saturation);
32                let roughness = base_r - 0.50; // full-effect: polish by ~0.5
33                (metallic, roughness)
34            }
35            MaterialProfile::Stone => {
36                let metallic = 0.0;
37                let base_r = 0.80 + 0.10 * (1.0 - brightness);
38                let roughness = base_r - 0.60; // full-effect: strong polish
39                (metallic, roughness)
40            }
41            MaterialProfile::Dirt => {
42                let metallic = 0.0;
43                let base_r = 0.90 + 0.30 * (1.0 - saturation);
44                let roughness = base_r - 0.70; // wet/polished dirt at max
45                (metallic, roughness)
46            }
47            MaterialProfile::Metal => {
48                let metallic = 0.60 + 0.40 * saturation + 0.20; // extra push at max effect
49                let base_r = 0.60 + 0.40 * (1.0 - brightness);
50                let roughness = base_r - 0.60; // more mirror-like
51                (metallic, roughness)
52            }
53            MaterialProfile::Water => {
54                let metallic = 0.0;
55                let base_r = 0.10;
56                let roughness = base_r - 0.09; // smoother
57                (metallic, roughness)
58            }
59            MaterialProfile::Plastic => {
60                let metallic = 0.0;
61                let base_r = 0.70 + 0.20 * (1.0 - saturation);
62                let roughness = base_r - 0.60;
63                (metallic, roughness)
64            }
65            MaterialProfile::Fabric => {
66                let metallic = 0.0;
67                let base_r = 0.70 + 0.20 * (1.0 - brightness);
68                let roughness = base_r - 0.40;
69                (metallic, roughness)
70            }
71            MaterialProfile::Glass => {
72                let metallic = 0.0;
73                let base_r = 0.05;
74                let roughness = base_r - 0.04;
75                (metallic, roughness)
76            }
77        }
78    }
79}