Skip to main content

viewport_lib/scene/
material.rs

1/// Per-object material properties for Blinn-Phong and PBR shading.
2///
3/// Materials carry all shading parameters that were previously global in `LightingSettings`.
4/// Each `SceneRenderItem` now has its own `Material`, enabling per-object visual distinction.
5#[derive(Debug, Clone, Copy)]
6pub struct Material {
7    /// Base diffuse color [r, g, b] in linear 0..1 range. Default [0.7, 0.7, 0.7].
8    pub base_color: [f32; 3],
9    /// Ambient light coefficient. Default 0.15.
10    pub ambient: f32,
11    /// Diffuse light coefficient. Default 0.75.
12    pub diffuse: f32,
13    /// Specular highlight coefficient. Default 0.4.
14    pub specular: f32,
15    /// Specular shininess exponent. Default 32.0.
16    pub shininess: f32,
17    /// Metallic factor for PBR Cook-Torrance shading. 0=dielectric, 1=metal. Default 0.0.
18    pub metallic: f32,
19    /// Roughness factor for PBR microfacet distribution. 0=mirror, 1=fully rough. Default 0.5.
20    pub roughness: f32,
21    /// Opacity (1.0 = fully opaque, 0.0 = fully transparent). Default 1.0.
22    pub opacity: f32,
23    /// Optional albedo texture identifier. None = no texture applied. Default None.
24    pub texture_id: Option<u64>,
25    /// Optional normal map texture identifier. None = no normal mapping. Default None.
26    ///
27    /// The normal map must be in tangent-space with XY encoded as RG (0..1 -> -1..+1).
28    /// Requires UVs and tangents on the mesh for correct TBN construction.
29    pub normal_map_id: Option<u64>,
30    /// Optional ambient occlusion map texture identifier. None = no AO map. Default None.
31    ///
32    /// The AO map R channel encodes cavity factor (0=fully occluded, 1=fully lit).
33    /// Applied multiplicatively to ambient and diffuse terms.
34    pub ao_map_id: Option<u64>,
35    /// Use Cook-Torrance PBR shading instead of Blinn-Phong. Default false.
36    ///
37    /// When true, `metallic` and `roughness` drive the GGX BRDF.
38    /// PBR outputs linear HDR values; enable `post_process.enabled` for correct tone mapping.
39    pub use_pbr: bool,
40}
41
42impl Default for Material {
43    fn default() -> Self {
44        Self {
45            base_color: [0.7, 0.7, 0.7],
46            ambient: 0.15,
47            diffuse: 0.75,
48            specular: 0.4,
49            shininess: 32.0,
50            metallic: 0.0,
51            roughness: 0.5,
52            opacity: 1.0,
53            texture_id: None,
54            normal_map_id: None,
55            ao_map_id: None,
56            use_pbr: false,
57        }
58    }
59}
60
61impl Material {
62    /// Reproduce pre-Phase-2 appearance from a plain color.
63    ///
64    /// All other material parameters take their defaults, preserving the previous
65    /// Blinn-Phong shading for objects that only had a color override.
66    pub fn from_color(color: [f32; 3]) -> Self {
67        Self {
68            base_color: color,
69            ..Default::default()
70        }
71    }
72}