bevy/crates/bevy_pbr/src/material.rs

93 lines
3.7 KiB
Rust
Raw Normal View History

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;
use bevy_render::{color::Color, renderer::RenderResources, shader::ShaderDefs, texture::Texture};
2020-04-06 03:19:02 +00:00
/// A material with "standard" properties used in PBR lighting
/// Standard property values with pictures here <https://google.github.io/filament/Material%20Properties.pdf>
#[derive(Debug, RenderResources, ShaderDefs, TypeUuid)]
#[uuid = "dace545e-4bc6-4595-a79d-c224fc694975"]
2020-02-10 02:09:54 +00:00
pub struct StandardMaterial {
/// 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`
pub base_color: Color,
#[shader_def]
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
/// If used together with a roughness/metallic texture, this is factored into the final base
/// color as `roughness * roughness_texture_value`
pub roughness: f32,
/// From [0.0, 1.0], dielectric to pure metallic
/// If used together with a roughness/metallic texture, this is factored into the final base
/// color as `metallic * metallic_texture_value`
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
#[shader_def]
pub metallic_roughness_texture: Option<Handle<Texture>>,
pub reflectance: f32,
#[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>>,
#[render_resources(ignore)]
#[shader_def]
pub unlit: bool,
2020-02-11 17:31:49 +00:00
}
impl Default for StandardMaterial {
fn default() -> Self {
StandardMaterial {
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/
// 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.
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%
// Expressed in a linear scale and equivalent to 4% reflectance see
// https://google.github.io/filament/Material%20Properties.pdf
metallic_roughness_texture: None,
reflectance: 0.5,
normal_map: None,
double_sided: false,
occlusion_texture: None,
emissive: Color::BLACK,
emissive_texture: None,
unlit: false,
2020-02-11 17:31:49 +00:00
}
}
}
2020-08-01 00:10:29 +00:00
impl From<Color> for StandardMaterial {
fn from(color: Color) -> Self {
StandardMaterial {
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 {
base_color_texture: Some(texture),
2020-08-01 00:10:29 +00:00
..Default::default()
}
}
}