1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/// Surface material properties for Phong/PBR shading.
///
/// Analogous to VTK's `vtkProperty`. Controls how light interacts
/// with the surface.
///
/// # Examples
///
/// ```
/// use crate::render::Material;
///
/// let matte = Material::matte();
/// assert_eq!(matte.specular, 0.0);
///
/// let metal = Material::pbr_metal(0.3);
/// assert!(metal.pbr);
/// assert_eq!(metal.metallic, 1.0);
/// ```
#[derive(Debug, Clone)]
pub struct Material {
/// Ambient light contribution. Default: 0.1
pub ambient: f64,
/// Diffuse light contribution. Default: 0.7
pub diffuse: f64,
/// Specular highlight contribution. Default: 0.3
pub specular: f64,
/// Specular power (shininess). Higher = tighter highlights. Default: 32.0
pub specular_power: f64,
/// Specular highlight color. Default: white [1, 1, 1]
pub specular_color: [f32; 3],
/// Edge color for wireframe/edge overlay. Default: black [0, 0, 0]
pub edge_color: [f32; 3],
/// Edge visibility (for edge overlay mode). Default: false
pub edge_visibility: bool,
/// Line width for wireframe/edge rendering. Default: 1.0
pub line_width: f32,
/// Point size for point rendering. Default: 3.0
pub point_size: f32,
/// Whether to use flat shading (per-face normals). Default: false
pub flat_shading: bool,
/// Backface culling. Default: false
pub backface_culling: bool,
/// Metallic factor for PBR (0.0 = dielectric, 1.0 = metal). Default: 0.0
pub metallic: f64,
/// Roughness factor for PBR (0.0 = mirror, 1.0 = fully rough). Default: 0.5
pub roughness: f64,
/// Whether to use PBR shading instead of Blinn-Phong. Default: false
pub pbr: bool,
}
impl Default for Material {
fn default() -> Self {
Self {
ambient: 0.1,
diffuse: 0.7,
specular: 0.3,
specular_power: 32.0,
specular_color: [1.0, 1.0, 1.0],
edge_color: [0.0, 0.0, 0.0],
edge_visibility: false,
line_width: 1.0,
point_size: 3.0,
flat_shading: false,
backface_culling: false,
metallic: 0.0,
roughness: 0.5,
pbr: false,
}
}
}
impl Material {
/// Create a default material.
pub fn new() -> Self {
Self::default()
}
/// Create a matte (no specular) material.
pub fn matte() -> Self {
Self {
specular: 0.0,
..Default::default()
}
}
/// Create a shiny/metallic material.
pub fn shiny() -> Self {
Self {
specular: 0.8,
specular_power: 128.0,
diffuse: 0.5,
..Default::default()
}
}
/// Create a flat-shaded material (per-face coloring).
pub fn flat() -> Self {
Self {
flat_shading: true,
..Default::default()
}
}
/// Create a PBR metal material.
pub fn pbr_metal(roughness: f64) -> Self {
Self {
pbr: true,
metallic: 1.0,
roughness,
..Default::default()
}
}
/// Create a PBR dielectric (plastic/ceramic) material.
pub fn pbr_dielectric(roughness: f64) -> Self {
Self {
pbr: true,
metallic: 0.0,
roughness,
..Default::default()
}
}
/// Create a material with edge overlay.
pub fn with_edges(mut self) -> Self {
self.edge_visibility = true;
self
}
}
impl std::fmt::Display for Material {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if self.pbr {
write!(f, "Material(PBR: metallic={:.1}, roughness={:.1})", self.metallic, self.roughness)
} else {
write!(f, "Material(Phong: ambient={:.1}, diffuse={:.1}, specular={:.1})",
self.ambient, self.diffuse, self.specular)
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn default_material() {
let m = Material::default();
assert_eq!(m.ambient, 0.1);
assert_eq!(m.diffuse, 0.7);
assert!(!m.flat_shading);
}
#[test]
fn matte_no_specular() {
let m = Material::matte();
assert_eq!(m.specular, 0.0);
}
#[test]
fn shiny_high_power() {
let m = Material::shiny();
assert_eq!(m.specular_power, 128.0);
}
#[test]
fn with_edges_builder() {
let m = Material::flat().with_edges();
assert!(m.flat_shading);
assert!(m.edge_visibility);
}
#[test]
fn pbr_metal() {
let m = Material::pbr_metal(0.3);
assert!(m.pbr);
assert_eq!(m.metallic, 1.0);
assert_eq!(m.roughness, 0.3);
}
#[test]
fn pbr_dielectric() {
let m = Material::pbr_dielectric(0.8);
assert!(m.pbr);
assert_eq!(m.metallic, 0.0);
assert_eq!(m.roughness, 0.8);
}
#[test]
fn display_phong() {
let m = Material::default();
let s = format!("{m}");
assert!(s.contains("Phong"));
}
#[test]
fn display_pbr() {
let m = Material::pbr_metal(0.3);
let s = format!("{m}");
assert!(s.contains("PBR"));
assert!(s.contains("metallic"));
}
}