use crate::{AlphaMode, Material, MaterialPipeline, MaterialPipelineKey, PBR_SHADER_HANDLE}; use bevy_asset::Handle; use bevy_math::Vec4; use bevy_reflect::TypeUuid; use bevy_render::{ color::Color, mesh::MeshVertexBufferLayout, render_asset::RenderAssets, render_resource::*, texture::Image, }; /// A material with "standard" properties used in PBR lighting /// Standard property values with pictures here /// . /// /// May be created directly from a [`Color`] or an [`Image`]. #[derive(AsBindGroup, Debug, Clone, TypeUuid)] #[uuid = "7494888b-c082-457b-aacf-517228cc0c22"] #[bind_group_data(StandardMaterialKey)] #[uniform(0, StandardMaterialUniform)] 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, #[texture(1)] #[sampler(2)] pub base_color_texture: Option>, // 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, #[texture(3)] #[sampler(4)] pub emissive_texture: Option>, /// 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 perceptual_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, #[texture(5)] #[sampler(6)] pub metallic_roughness_texture: Option>, /// 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 pub reflectance: f32, #[texture(9)] #[sampler(10)] pub normal_map_texture: Option>, /// Normal map textures authored for DirectX have their y-component flipped. Set this to flip /// it to right-handed conventions. pub flip_normal_map_y: bool, #[texture(7)] #[sampler(8)] pub occlusion_texture: Option>, /// Support two-sided lighting by automatically flipping the normals for "back" faces /// within the PBR lighting shader. /// Defaults to false. /// This does not automatically configure backface culling, which can be done via /// `cull_mode`. pub double_sided: bool, /// Whether to cull the "front", "back" or neither side of a mesh /// defaults to `Face::Back` pub cull_mode: Option, pub unlit: bool, pub alpha_mode: AlphaMode, pub depth_bias: f32, } impl Default for StandardMaterial { fn default() -> Self { StandardMaterial { base_color: Color::rgb(1.0, 1.0, 1.0), base_color_texture: None, emissive: Color::BLACK, emissive_texture: None, // This is the minimum the roughness is clamped to in shader code // See // 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. perceptual_roughness: 0.089, // Few materials are purely dielectric or metallic // This is just a default for mostly-dielectric metallic: 0.01, metallic_roughness_texture: None, // Minimum real-world reflectance is 2%, most materials between 2-5% // Expressed in a linear scale and equivalent to 4% reflectance see // reflectance: 0.5, occlusion_texture: None, normal_map_texture: None, flip_normal_map_y: false, double_sided: false, cull_mode: Some(Face::Back), unlit: false, alpha_mode: AlphaMode::Opaque, depth_bias: 0.0, } } } impl From for StandardMaterial { fn from(color: Color) -> Self { StandardMaterial { base_color: color, alpha_mode: if color.a() < 1.0 { AlphaMode::Blend } else { AlphaMode::Opaque }, ..Default::default() } } } impl From> for StandardMaterial { fn from(texture: Handle) -> Self { StandardMaterial { base_color_texture: Some(texture), ..Default::default() } } } // NOTE: These must match the bit flags in bevy_pbr/src/render/pbr_types.wgsl! bitflags::bitflags! { #[repr(transparent)] pub struct StandardMaterialFlags: u32 { const BASE_COLOR_TEXTURE = (1 << 0); const EMISSIVE_TEXTURE = (1 << 1); const METALLIC_ROUGHNESS_TEXTURE = (1 << 2); const OCCLUSION_TEXTURE = (1 << 3); const DOUBLE_SIDED = (1 << 4); const UNLIT = (1 << 5); const ALPHA_MODE_OPAQUE = (1 << 6); const ALPHA_MODE_MASK = (1 << 7); const ALPHA_MODE_BLEND = (1 << 8); const TWO_COMPONENT_NORMAL_MAP = (1 << 9); const FLIP_NORMAL_MAP_Y = (1 << 10); const NONE = 0; const UNINITIALIZED = 0xFFFF; } } /// The GPU representation of the uniform data of a [`StandardMaterial`]. #[derive(Clone, Default, ShaderType)] pub struct StandardMaterialUniform { /// Doubles as diffuse albedo for non-metallic, specular for metallic and a mix for everything /// in between. pub base_color: Vec4, // 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: Vec4, /// Linear perceptual roughness, clamped to [0.089, 1.0] in the shader /// Defaults to minimum of 0.089 pub roughness: f32, /// From [0.0, 1.0], dielectric to pure metallic 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 pub reflectance: f32, pub flags: u32, /// When the alpha mode mask flag is set, any base color alpha above this cutoff means fully opaque, /// and any below means fully transparent. pub alpha_cutoff: f32, } impl AsBindGroupShaderType for StandardMaterial { fn as_bind_group_shader_type(&self, images: &RenderAssets) -> StandardMaterialUniform { let mut flags = StandardMaterialFlags::NONE; if self.base_color_texture.is_some() { flags |= StandardMaterialFlags::BASE_COLOR_TEXTURE; } if self.emissive_texture.is_some() { flags |= StandardMaterialFlags::EMISSIVE_TEXTURE; } if self.metallic_roughness_texture.is_some() { flags |= StandardMaterialFlags::METALLIC_ROUGHNESS_TEXTURE; } if self.occlusion_texture.is_some() { flags |= StandardMaterialFlags::OCCLUSION_TEXTURE; } if self.double_sided { flags |= StandardMaterialFlags::DOUBLE_SIDED; } if self.unlit { flags |= StandardMaterialFlags::UNLIT; } let has_normal_map = self.normal_map_texture.is_some(); if has_normal_map { match images .get(self.normal_map_texture.as_ref().unwrap()) .unwrap() .texture_format { // All 2-component unorm formats TextureFormat::Rg8Unorm | TextureFormat::Rg16Unorm | TextureFormat::Bc5RgUnorm | TextureFormat::EacRg11Unorm => { flags |= StandardMaterialFlags::TWO_COMPONENT_NORMAL_MAP; } _ => {} } if self.flip_normal_map_y { flags |= StandardMaterialFlags::FLIP_NORMAL_MAP_Y; } } // NOTE: 0.5 is from the glTF default - do we want this? let mut alpha_cutoff = 0.5; match self.alpha_mode { AlphaMode::Opaque => flags |= StandardMaterialFlags::ALPHA_MODE_OPAQUE, AlphaMode::Mask(c) => { alpha_cutoff = c; flags |= StandardMaterialFlags::ALPHA_MODE_MASK; } AlphaMode::Blend => flags |= StandardMaterialFlags::ALPHA_MODE_BLEND, }; StandardMaterialUniform { base_color: self.base_color.as_linear_rgba_f32().into(), emissive: self.emissive.into(), roughness: self.perceptual_roughness, metallic: self.metallic, reflectance: self.reflectance, flags: flags.bits(), alpha_cutoff, } } } #[derive(Clone, PartialEq, Eq, Hash)] pub struct StandardMaterialKey { normal_map: bool, cull_mode: Option, } impl From<&StandardMaterial> for StandardMaterialKey { fn from(material: &StandardMaterial) -> Self { StandardMaterialKey { normal_map: material.normal_map_texture.is_some(), cull_mode: material.cull_mode, } } } impl Material for StandardMaterial { fn specialize( _pipeline: &MaterialPipeline, descriptor: &mut RenderPipelineDescriptor, _layout: &MeshVertexBufferLayout, key: MaterialPipelineKey, ) -> Result<(), SpecializedMeshPipelineError> { if key.bind_group_data.normal_map { descriptor .fragment .as_mut() .unwrap() .shader_defs .push(String::from("STANDARDMATERIAL_NORMAL_MAP")); } descriptor.primitive.cull_mode = key.bind_group_data.cull_mode; if let Some(label) = &mut descriptor.label { *label = format!("pbr_{}", *label).into(); } Ok(()) } fn fragment_shader() -> ShaderRef { PBR_SHADER_HANDLE.typed().into() } #[inline] fn alpha_mode(&self) -> AlphaMode { self.alpha_mode } #[inline] fn depth_bias(&self) -> f32 { self.depth_bias } }