2020-04-25 01:55:15 +00:00
|
|
|
use bevy_asset::{self, Handle};
|
2020-11-28 00:39:59 +00:00
|
|
|
use bevy_reflect::TypeUuid;
|
2020-07-17 01:26:21 +00:00
|
|
|
use bevy_render::{color::Color, renderer::RenderResources, shader::ShaderDefs, texture::Texture};
|
2020-04-06 03:19:02 +00:00
|
|
|
|
2020-08-09 23:13:04 +00:00
|
|
|
/// A material with "standard" properties used in PBR lighting
|
2021-09-14 22:46:18 +00:00
|
|
|
/// Standard property values with pictures here <https://google.github.io/filament/Material%20Properties.pdf>
|
2020-10-18 20:48:15 +00:00
|
|
|
#[derive(Debug, RenderResources, ShaderDefs, TypeUuid)]
|
|
|
|
#[uuid = "dace545e-4bc6-4595-a79d-c224fc694975"]
|
2020-02-10 02:09:54 +00:00
|
|
|
pub struct StandardMaterial {
|
2021-04-21 23:19:34 +00:00
|
|
|
/// Doubles as diffuse albedo for non-metallic, specular for metallic and a mix for everything
|
|
|
|
/// in between If used together with a base_color_texture, this is factored into the final
|
|
|
|
/// base color as `base_color * base_color_texture_value`
|
2021-03-20 03:22:33 +00:00
|
|
|
pub base_color: Color,
|
2020-06-08 05:24:53 +00:00
|
|
|
#[shader_def]
|
2021-03-20 03:22:33 +00:00
|
|
|
pub base_color_texture: Option<Handle<Texture>>,
|
|
|
|
/// Linear perceptual roughness, clamped to [0.089, 1.0] in the shader
|
|
|
|
/// Defaults to minimum of 0.089
|
2021-04-21 23:19:34 +00:00
|
|
|
/// If used together with a roughness/metallic texture, this is factored into the final base
|
|
|
|
/// color as `roughness * roughness_texture_value`
|
2021-03-20 03:22:33 +00:00
|
|
|
pub roughness: f32,
|
|
|
|
/// From [0.0, 1.0], dielectric to pure metallic
|
2021-04-21 23:19:34 +00:00
|
|
|
/// If used together with a roughness/metallic texture, this is factored into the final base
|
|
|
|
/// color as `metallic * metallic_texture_value`
|
2021-03-20 03:22:33 +00:00
|
|
|
pub metallic: f32,
|
|
|
|
/// Specular intensity for non-metals on a linear scale of [0.0, 1.0]
|
|
|
|
/// defaults to 0.5 which is mapped to 4% reflectance in the shader
|
2021-03-26 21:00:34 +00:00
|
|
|
#[shader_def]
|
|
|
|
pub metallic_roughness_texture: Option<Handle<Texture>>,
|
2021-03-20 03:22:33 +00:00
|
|
|
pub reflectance: f32,
|
2021-03-26 21:00:34 +00:00
|
|
|
#[shader_def]
|
|
|
|
pub normal_map: Option<Handle<Texture>>,
|
|
|
|
#[render_resources(ignore)]
|
|
|
|
#[shader_def]
|
|
|
|
pub double_sided: bool,
|
|
|
|
#[shader_def]
|
|
|
|
pub occlusion_texture: Option<Handle<Texture>>,
|
|
|
|
// Use a color for user friendliness even though we technically don't use the alpha channel
|
|
|
|
// Might be used in the future for exposure correction in HDR
|
|
|
|
pub emissive: Color,
|
|
|
|
#[shader_def]
|
|
|
|
pub emissive_texture: Option<Handle<Texture>>,
|
2020-06-22 23:11:30 +00:00
|
|
|
#[render_resources(ignore)]
|
|
|
|
#[shader_def]
|
2021-02-01 01:13:16 +00:00
|
|
|
pub unlit: bool,
|
2020-02-11 17:31:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for StandardMaterial {
|
|
|
|
fn default() -> Self {
|
|
|
|
StandardMaterial {
|
2021-03-20 03:22:33 +00:00
|
|
|
base_color: Color::rgb(1.0, 1.0, 1.0),
|
|
|
|
base_color_texture: None,
|
|
|
|
// This is the minimum the roughness is clamped to in shader code
|
|
|
|
// See https://google.github.io/filament/Filament.html#materialsystem/parameterization/
|
2021-04-21 23:19:34 +00:00
|
|
|
// It's the minimum floating point value that won't be rounded down to 0 in the
|
|
|
|
// calculations used. Although technically for 32-bit floats, 0.045 could be
|
|
|
|
// used.
|
2021-03-20 03:22:33 +00:00
|
|
|
roughness: 0.089,
|
|
|
|
// Few materials are purely dielectric or metallic
|
|
|
|
// This is just a default for mostly-dielectric
|
|
|
|
metallic: 0.01,
|
|
|
|
// Minimum real-world reflectance is 2%, most materials between 2-5%
|
2021-09-14 22:46:18 +00:00
|
|
|
// Expressed in a linear scale and equivalent to 4% reflectance see
|
|
|
|
// https://google.github.io/filament/Material%20Properties.pdf
|
2021-03-26 21:00:34 +00:00
|
|
|
metallic_roughness_texture: None,
|
2021-03-20 03:22:33 +00:00
|
|
|
reflectance: 0.5,
|
2021-03-26 21:00:34 +00:00
|
|
|
normal_map: None,
|
|
|
|
double_sided: false,
|
|
|
|
occlusion_texture: None,
|
|
|
|
emissive: Color::BLACK,
|
|
|
|
emissive_texture: None,
|
2021-02-01 01:13:16 +00:00
|
|
|
unlit: false,
|
2020-02-11 17:31:49 +00:00
|
|
|
}
|
|
|
|
}
|
2020-02-12 03:09:05 +00:00
|
|
|
}
|
2020-08-01 00:10:29 +00:00
|
|
|
|
|
|
|
impl From<Color> for StandardMaterial {
|
|
|
|
fn from(color: Color) -> Self {
|
|
|
|
StandardMaterial {
|
2021-03-20 03:22:33 +00:00
|
|
|
base_color: color,
|
2020-08-01 00:10:29 +00:00
|
|
|
..Default::default()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Handle<Texture>> for StandardMaterial {
|
|
|
|
fn from(texture: Handle<Texture>) -> Self {
|
|
|
|
StandardMaterial {
|
2021-03-20 03:22:33 +00:00
|
|
|
base_color_texture: Some(texture),
|
2020-08-01 00:10:29 +00:00
|
|
|
..Default::default()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|