rusterix/
material_profile.rs1use theframework::prelude::*;
2
3#[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 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; let brightness = 0.2126 * r + 0.7152 * g + 0.0722 * b; match *self {
29 MaterialProfile::Wood => {
30 let metallic = 0.05 * saturation; let base_r = 0.70 - 0.20 * brightness + 0.20 * (1.0 - saturation);
32 let roughness = base_r - 0.50; (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; (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; (metallic, roughness)
46 }
47 MaterialProfile::Metal => {
48 let metallic = 0.60 + 0.40 * saturation + 0.20; let base_r = 0.60 + 0.40 * (1.0 - brightness);
50 let roughness = base_r - 0.60; (metallic, roughness)
52 }
53 MaterialProfile::Water => {
54 let metallic = 0.0;
55 let base_r = 0.10;
56 let roughness = base_r - 0.09; (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}