three_d_asset/
material.rs

1//!
2//! Contain material asset definitions.
3//!
4
5#[doc(inline)]
6pub use crate::{prelude::Srgba, texture::texture2d::*};
7
8/// Lighting models which specify how the lighting is computed when rendering a material.
9/// This is a trade-off between how fast the computations are versus how physically correct they look.
10#[derive(Debug, Copy, Clone, PartialEq)]
11#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
12pub enum LightingModel {
13    /// Phong lighting model.
14    /// The fastest lighting model to calculate.
15    Phong,
16    /// Blinn lighting model.
17    /// Almost as fast as Phong and has less artifacts.
18    Blinn,
19    /// Cook-Torrance lighting model with the given normal distribution and geometry functions.
20    /// The most physically correct lighting model but also the most expensive.
21    Cook(NormalDistributionFunction, GeometryFunction),
22}
23
24/// The geometry function used in a Cook-Torrance lighting model.
25#[derive(Debug, Copy, Clone, PartialEq)]
26#[allow(missing_docs)]
27#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
28pub enum GeometryFunction {
29    SmithSchlickGGX,
30}
31
32/// The normal distribution function used in a Cook-Torrance lighting model.
33#[derive(Debug, Copy, Clone, PartialEq)]
34#[allow(missing_docs)]
35#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
36pub enum NormalDistributionFunction {
37    Blinn,
38    Beckmann,
39    TrowbridgeReitzGGX,
40}
41
42///
43/// A CPU-side version of a material used for physically based rendering (PBR).
44///
45#[derive(Debug, Clone)]
46#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
47pub struct PbrMaterial {
48    /// Name. Used for matching geometry and material.
49    pub name: String,
50    /// Albedo base color, also called diffuse color.
51    pub albedo: Srgba,
52    /// Texture with albedo base colors, also called diffuse colors.
53    /// The colors are assumed to be in sRGB (`RgbU8`), sRGB with an alpha channel (`RgbaU8`) or HDR color space.
54    pub albedo_texture: Option<Texture2D>,
55    /// A value in the range `[0..1]` specifying how metallic the material is.
56    pub metallic: f32,
57    /// A value in the range `[0..1]` specifying how rough the material surface is.
58    pub roughness: f32,
59    /// Texture containing the occlusion, metallic and roughness parameters.
60    /// The occlusion values are sampled from the red channel, metallic from the blue channel and the roughness from the green channel.
61    /// Is sometimes in two textures, see [Self::occlusion_texture] and [Self::metallic_roughness_texture].
62    pub occlusion_metallic_roughness_texture: Option<Texture2D>,
63    /// Texture containing the metallic and roughness parameters which are multiplied with the [Self::metallic] and [Self::roughness] to get the final parameter.
64    /// The metallic values are sampled from the blue channel and the roughness from the green channel.
65    /// Can be combined with occlusion into one texture, see [Self::occlusion_metallic_roughness_texture].
66    pub metallic_roughness_texture: Option<Texture2D>,
67    /// A scalar multiplier controlling the amount of occlusion applied from the [Self::occlusion_texture]. A value of 0.0 means no occlusion. A value of 1.0 means full occlusion.
68    pub occlusion_strength: f32,
69    /// An occlusion map. Higher values indicate areas that should receive full indirect lighting and lower values indicate no indirect lighting.
70    /// The occlusion values are sampled from the red channel.
71    /// Can be combined with metallic and roughness into one texture, see [Self::occlusion_metallic_roughness_texture].
72    pub occlusion_texture: Option<Texture2D>,
73    /// A scalar multiplier applied to each normal vector of the [Self::normal_texture].
74    pub normal_scale: f32,
75    /// A tangent space normal map, also known as bump map.
76    pub normal_texture: Option<Texture2D>,
77    /// Color of light shining from an object.
78    pub emissive: Srgba,
79    /// Texture with color of light shining from an object.
80    /// The colors are assumed to be in sRGB (`RgbU8`), sRGB with an alpha channel (`RgbaU8`) or HDR color space.
81    pub emissive_texture: Option<Texture2D>,
82    /// Alpha cutout value for transparency in deferred rendering pipeline.
83    pub alpha_cutout: Option<f32>,
84    /// The lighting model used when rendering this material
85    pub lighting_model: LightingModel,
86    /// The index of refraction for this material    
87    pub index_of_refraction: f32,
88    /// A value in the range `[0..1]` specifying how transmissive the material surface is.
89    pub transmission: f32,
90    /// Texture containing the transmission parameter which are multiplied with the [Self::transmission] to get the final parameter.
91    pub transmission_texture: Option<Texture2D>,
92}
93
94impl Default for PbrMaterial {
95    fn default() -> Self {
96        Self {
97            name: "default".to_string(),
98            albedo: Srgba::WHITE,
99            albedo_texture: None,
100            occlusion_metallic_roughness_texture: None,
101            metallic_roughness_texture: None,
102            occlusion_texture: None,
103            metallic: 0.0,
104            roughness: 1.0,
105            occlusion_strength: 1.0,
106            normal_texture: None,
107            normal_scale: 1.0,
108            emissive: Srgba::BLACK,
109            emissive_texture: None,
110            index_of_refraction: 1.5,
111            transmission: 0.0,
112            transmission_texture: None,
113            alpha_cutout: None,
114            lighting_model: LightingModel::Blinn,
115        }
116    }
117}