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, PartialEq)]
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    /// Optional matcap texture identifier. When set, matcap shading replaces
41    /// Blinn-Phong/PBR. Default None.
42    ///
43    /// Obtain a `MatcapId` from [`ViewportGpuResources::builtin_matcap_id`] or
44    /// [`ViewportGpuResources::upload_matcap`].  Blendable matcaps (alpha-channel)
45    /// tint the result with `base_color`; static matcaps override color entirely.
46    pub matcap_id: Option<crate::resources::MatcapId>,
47}
48
49impl Default for Material {
50    fn default() -> Self {
51        Self {
52            base_color: [0.7, 0.7, 0.7],
53            ambient: 0.15,
54            diffuse: 0.75,
55            specular: 0.4,
56            shininess: 32.0,
57            metallic: 0.0,
58            roughness: 0.5,
59            opacity: 1.0,
60            texture_id: None,
61            normal_map_id: None,
62            ao_map_id: None,
63            use_pbr: false,
64            matcap_id: None,
65        }
66    }
67}
68
69impl Material {
70    /// Reproduce pre-Phase-2 appearance from a plain color.
71    ///
72    /// All other material parameters take their defaults, preserving the previous
73    /// Blinn-Phong shading for objects that only had a color override.
74    pub fn from_color(color: [f32; 3]) -> Self {
75        Self {
76            base_color: color,
77            ..Default::default()
78        }
79    }
80}