diff --git a/Cargo.toml b/Cargo.toml index c1a688239d..ee901649c3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -135,6 +135,10 @@ path = "examples/3d/orthographic.rs" name = "parenting" path = "examples/3d/parenting.rs" +[[example]] +name = "pbr" +path = "examples/3d/pbr.rs" + [[example]] name = "spawner" path = "examples/3d/spawner.rs" diff --git a/crates/bevy_gltf/src/loader.rs b/crates/bevy_gltf/src/loader.rs index dec120bc5b..7f7b6a71a0 100644 --- a/crates/bevy_gltf/src/loader.rs +++ b/crates/bevy_gltf/src/loader.rs @@ -277,9 +277,12 @@ fn load_material(material: &Material, load_context: &mut LoadContext) -> Handle< load_context.set_labeled_asset( &material_label, LoadedAsset::new(StandardMaterial { - albedo: Color::rgba(color[0], color[1], color[2], color[3]), - albedo_texture: texture_handle, + base_color: Color::rgba(color[0], color[1], color[2], color[3]), + base_color_texture: texture_handle, + roughness: pbr.roughness_factor(), + metallic: pbr.metallic_factor(), unlit: material.unlit(), + ..Default::default() }) .with_dependencies(dependencies), ) diff --git a/crates/bevy_internal/src/lib.rs b/crates/bevy_internal/src/lib.rs index 331a5e90ab..62fb7cf270 100644 --- a/crates/bevy_internal/src/lib.rs +++ b/crates/bevy_internal/src/lib.rs @@ -96,7 +96,6 @@ pub mod gltf { #[cfg(feature = "bevy_pbr")] pub mod pbr { //! Physically based rendering. - //! **Note**: true PBR has not yet been implemented; the name `pbr` is aspirational. pub use bevy_pbr::*; } diff --git a/crates/bevy_pbr/src/entity.rs b/crates/bevy_pbr/src/entity.rs index d03b0d89cf..fe7ea9d8f7 100644 --- a/crates/bevy_pbr/src/entity.rs +++ b/crates/bevy_pbr/src/entity.rs @@ -1,4 +1,4 @@ -use crate::{light::Light, material::StandardMaterial, render_graph::FORWARD_PIPELINE_HANDLE}; +use crate::{light::Light, material::StandardMaterial, render_graph::PBR_PIPELINE_HANDLE}; use bevy_asset::Handle; use bevy_ecs::bundle::Bundle; use bevy_render::{ @@ -27,7 +27,7 @@ impl Default for PbrBundle { fn default() -> Self { Self { render_pipelines: RenderPipelines::from_pipelines(vec![RenderPipeline::new( - FORWARD_PIPELINE_HANDLE.typed(), + PBR_PIPELINE_HANDLE.typed(), )]), mesh: Default::default(), visible: Default::default(), diff --git a/crates/bevy_pbr/src/lib.rs b/crates/bevy_pbr/src/lib.rs index 73790b395f..cb130ab9d9 100644 --- a/crates/bevy_pbr/src/lib.rs +++ b/crates/bevy_pbr/src/lib.rs @@ -42,9 +42,9 @@ impl Plugin for PbrPlugin { materials.set_untracked( Handle::::default(), StandardMaterial { - albedo: Color::PINK, + base_color: Color::PINK, unlit: true, - albedo_texture: None, + ..Default::default() }, ); } diff --git a/crates/bevy_pbr/src/light.rs b/crates/bevy_pbr/src/light.rs index 8deee5fdb9..149bfd92b2 100644 --- a/crates/bevy_pbr/src/light.rs +++ b/crates/bevy_pbr/src/light.rs @@ -15,6 +15,8 @@ pub struct Light { pub color: Color, pub fov: f32, pub depth: Range, + pub intensity: f32, + pub range: f32, } impl Default for Light { @@ -23,6 +25,8 @@ impl Default for Light { color: Color::rgb(1.0, 1.0, 1.0), depth: 0.1..50.0, fov: f32::to_radians(60.0), + intensity: 200.0, + range: 20.0, } } } @@ -48,10 +52,14 @@ impl LightRaw { let proj = perspective.get_projection_matrix() * global_transform.compute_matrix(); let (x, y, z) = global_transform.translation.into(); + + // premultiply color by intensity + // we don't use the alpha at all, so no reason to multiply only [0..3] + let color: [f32; 4] = (light.color * light.intensity).into(); LightRaw { proj: proj.to_cols_array_2d(), - pos: [x, y, z, 1.0], - color: light.color.into(), + pos: [x, y, z, 1.0 / (light.range * light.range)], // pos.w is the attenuation. + color, } } } diff --git a/crates/bevy_pbr/src/material.rs b/crates/bevy_pbr/src/material.rs index 1244d23a11..7ccf32b8f6 100644 --- a/crates/bevy_pbr/src/material.rs +++ b/crates/bevy_pbr/src/material.rs @@ -3,12 +3,28 @@ use bevy_reflect::TypeUuid; use bevy_render::{color::Color, renderer::RenderResources, shader::ShaderDefs, texture::Texture}; /// 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"] pub struct StandardMaterial { - pub albedo: Color, + /// 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 albedo_texture: Option>, + pub base_color_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 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 + pub reflectance: f32, #[render_resources(ignore)] #[shader_def] pub unlit: bool, @@ -17,8 +33,19 @@ pub struct StandardMaterial { impl Default for StandardMaterial { fn default() -> Self { StandardMaterial { - albedo: Color::rgb(1.0, 1.0, 1.0), - albedo_texture: None, + 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 + reflectance: 0.5, unlit: false, } } @@ -27,7 +54,7 @@ impl Default for StandardMaterial { impl From for StandardMaterial { fn from(color: Color) -> Self { StandardMaterial { - albedo: color, + base_color: color, ..Default::default() } } @@ -36,7 +63,7 @@ impl From for StandardMaterial { impl From> for StandardMaterial { fn from(texture: Handle) -> Self { StandardMaterial { - albedo_texture: Some(texture), + base_color_texture: Some(texture), ..Default::default() } } diff --git a/crates/bevy_pbr/src/render_graph/forward_pipeline/forward.frag b/crates/bevy_pbr/src/render_graph/forward_pipeline/forward.frag deleted file mode 100644 index cf6e3bcf79..0000000000 --- a/crates/bevy_pbr/src/render_graph/forward_pipeline/forward.frag +++ /dev/null @@ -1,65 +0,0 @@ -#version 450 - -const int MAX_LIGHTS = 10; - -struct Light { - mat4 proj; - vec4 pos; - vec4 color; -}; - -layout(location = 0) in vec3 v_Position; -layout(location = 1) in vec3 v_Normal; -layout(location = 2) in vec2 v_Uv; - -layout(location = 0) out vec4 o_Target; - -layout(set = 0, binding = 0) uniform CameraViewProj { - mat4 ViewProj; -}; - -layout(set = 1, binding = 0) uniform Lights { - vec3 AmbientColor; - uvec4 NumLights; - Light SceneLights[MAX_LIGHTS]; -}; - -layout(set = 3, binding = 0) uniform StandardMaterial_albedo { - vec4 Albedo; -}; - -# ifdef STANDARDMATERIAL_ALBEDO_TEXTURE -layout(set = 3, binding = 1) uniform texture2D StandardMaterial_albedo_texture; -layout(set = 3, binding = 2) uniform sampler StandardMaterial_albedo_texture_sampler; -# endif - -void main() { - vec4 output_color = Albedo; -# ifdef STANDARDMATERIAL_ALBEDO_TEXTURE - output_color *= texture( - sampler2D(StandardMaterial_albedo_texture, StandardMaterial_albedo_texture_sampler), - v_Uv); -# endif - -# ifndef STANDARDMATERIAL_UNLIT - vec3 normal = normalize(v_Normal); - // accumulate color - vec3 color = AmbientColor; - for (int i=0; i 1.0 - color /= max(float(NumLights.x), 1.0); - - output_color.xyz *= color; -# endif - - // multiply the light by material color - o_Target = output_color; -} diff --git a/crates/bevy_pbr/src/render_graph/mod.rs b/crates/bevy_pbr/src/render_graph/mod.rs index 5eccb6799f..507cd3fdb2 100644 --- a/crates/bevy_pbr/src/render_graph/mod.rs +++ b/crates/bevy_pbr/src/render_graph/mod.rs @@ -1,9 +1,9 @@ -mod forward_pipeline; mod lights_node; +mod pbr_pipeline; use bevy_ecs::world::World; -pub use forward_pipeline::*; pub use lights_node::*; +pub use pbr_pipeline::*; /// the names of pbr graph nodes pub mod node { @@ -50,10 +50,9 @@ pub(crate) fn add_pbr_graph(world: &mut World) { .add_node_edge(node::LIGHTS, base::node::MAIN_PASS) .unwrap(); } - let forward_pipeline = - build_forward_pipeline(&mut world.get_resource_mut::>().unwrap()); + let pipeline = build_pbr_pipeline(&mut world.get_resource_mut::>().unwrap()); let mut pipelines = world .get_resource_mut::>() .unwrap(); - pipelines.set_untracked(FORWARD_PIPELINE_HANDLE, forward_pipeline); + pipelines.set_untracked(PBR_PIPELINE_HANDLE, pipeline); } diff --git a/crates/bevy_pbr/src/render_graph/forward_pipeline/mod.rs b/crates/bevy_pbr/src/render_graph/pbr_pipeline/mod.rs similarity index 89% rename from crates/bevy_pbr/src/render_graph/forward_pipeline/mod.rs rename to crates/bevy_pbr/src/render_graph/pbr_pipeline/mod.rs index f8f606ae0d..8868f818ce 100644 --- a/crates/bevy_pbr/src/render_graph/forward_pipeline/mod.rs +++ b/crates/bevy_pbr/src/render_graph/pbr_pipeline/mod.rs @@ -9,10 +9,10 @@ use bevy_render::{ texture::TextureFormat, }; -pub const FORWARD_PIPELINE_HANDLE: HandleUntyped = +pub const PBR_PIPELINE_HANDLE: HandleUntyped = HandleUntyped::weak_from_u64(PipelineDescriptor::TYPE_UUID, 13148362314012771389); -pub(crate) fn build_forward_pipeline(shaders: &mut Assets) -> PipelineDescriptor { +pub(crate) fn build_pbr_pipeline(shaders: &mut Assets) -> PipelineDescriptor { PipelineDescriptor { depth_stencil: Some(DepthStencilState { format: TextureFormat::Depth32Float, @@ -48,11 +48,11 @@ pub(crate) fn build_forward_pipeline(shaders: &mut Assets) -> PipelineDe ..PipelineDescriptor::new(ShaderStages { vertex: shaders.add(Shader::from_glsl( ShaderStage::Vertex, - include_str!("forward.vert"), + include_str!("pbr.vert"), )), fragment: Some(shaders.add(Shader::from_glsl( ShaderStage::Fragment, - include_str!("forward.frag"), + include_str!("pbr.frag"), ))), }) } diff --git a/crates/bevy_pbr/src/render_graph/pbr_pipeline/pbr.frag b/crates/bevy_pbr/src/render_graph/pbr_pipeline/pbr.frag new file mode 100644 index 0000000000..f34b2b222f --- /dev/null +++ b/crates/bevy_pbr/src/render_graph/pbr_pipeline/pbr.frag @@ -0,0 +1,323 @@ +// From the Filament design doc +// https://google.github.io/filament/Filament.html#table_symbols +// Symbol Definition +// v View unit vector +// l Incident light unit vector +// n Surface normal unit vector +// h Half unit vector between l and v +// f BRDF +// f_d Diffuse component of a BRDF +// f_r Specular component of a BRDF +// α Roughness, remapped from using input perceptualRoughness +// σ Diffuse reflectance +// Ω Spherical domain +// f0 Reflectance at normal incidence +// f90 Reflectance at grazing angle +// χ+(a) Heaviside function (1 if a>0 and 0 otherwise) +// nior Index of refraction (IOR) of an interface +// ⟨n⋅l⟩ Dot product clamped to [0..1] +// ⟨a⟩ Saturated value (clamped to [0..1]) + +// The Bidirectional Reflectance Distribution Function (BRDF) describes the surface response of a standard material +// and consists of two components, the diffuse component (f_d) and the specular component (f_r): +// f(v,l) = f_d(v,l) + f_r(v,l) +// +// The form of the microfacet model is the same for diffuse and specular +// f_r(v,l) = f_d(v,l) = 1 / { |n⋅v||n⋅l| } ∫_Ω D(m,α) G(v,l,m) f_m(v,l,m) (v⋅m) (l⋅m) dm +// +// In which: +// D, also called the Normal Distribution Function (NDF) models the distribution of the microfacets +// G models the visibility (or occlusion or shadow-masking) of the microfacets +// f_m is the microfacet BRDF and differs between specular and diffuse components +// +// The above integration needs to be approximated. + +#version 450 + +const int MAX_LIGHTS = 10; + +struct Light { + mat4 proj; + vec3 pos; + float inverseRadiusSquared; + vec3 color; + float unused; // unused 4th element of vec4; +}; + +layout(location = 0) in vec3 v_WorldPosition; +layout(location = 1) in vec3 v_WorldNormal; +layout(location = 2) in vec2 v_Uv; + +layout(location = 0) out vec4 o_Target; + +layout(set = 0, binding = 0) uniform CameraViewProj { + mat4 ViewProj; +}; +layout(set = 0, binding = 1) uniform CameraPosition { + vec3 CameraPos; +}; + +layout(set = 1, binding = 0) uniform Lights { + vec3 AmbientColor; + uvec4 NumLights; + Light SceneLights[MAX_LIGHTS]; +}; + +layout(set = 3, binding = 0) uniform StandardMaterial_base_color { + vec4 base_color; +}; + +#ifdef STANDARDMATERIAL_BASE_COLOR_TEXTURE +layout(set = 3, binding = 1) uniform texture2D StandardMaterial_base_color_texture; +layout(set = 3, + binding = 2) uniform sampler StandardMaterial_base_color_texture_sampler; +#endif + +#ifndef STANDARDMATERIAL_UNLIT + +layout(set = 3, binding = 3) uniform StandardMaterial_roughness { + float perceptual_roughness; +}; + +layout(set = 3, binding = 4) uniform StandardMaterial_metallic { + float metallic; +}; + +layout(set = 3, binding = 5) uniform StandardMaterial_reflectance { + float reflectance; +}; + +# define saturate(x) clamp(x, 0.0, 1.0) +const float PI = 3.141592653589793; + +float pow5(float x) { + float x2 = x * x; + return x2 * x2 * x; +} + +// distanceAttenuation is simply the square falloff of light intensity +// combined with a smooth attenuation at the edge of the light radius +// +// light radius is a non-physical construct for efficiency purposes, +// because otherwise every light affects every fragment in the scene +float getDistanceAttenuation(const vec3 posToLight, float inverseRadiusSquared) { + float distanceSquare = dot(posToLight, posToLight); + float factor = distanceSquare * inverseRadiusSquared; + float smoothFactor = saturate(1.0 - factor * factor); + float attenuation = smoothFactor * smoothFactor; + return attenuation * 1.0 / max(distanceSquare, 1e-4); +} + +// Normal distribution function (specular D) +// Based on https://google.github.io/filament/Filament.html#citation-walter07 + +// D_GGX(h,α) = α^2 / { π ((n⋅h)^2 (α2−1) + 1)^2 } + +// Simple implementation, has precision problems when using fp16 instead of fp32 +// see https://google.github.io/filament/Filament.html#listing_speculardfp16 +float D_GGX(float roughness, float NoH, const vec3 h) { + float oneMinusNoHSquared = 1.0 - NoH * NoH; + float a = NoH * roughness; + float k = roughness / (oneMinusNoHSquared + a * a); + float d = k * k * (1.0 / PI); + return d; +} + +// Visibility function (Specular G) +// V(v,l,a) = G(v,l,α) / { 4 (n⋅v) (n⋅l) } +// such that f_r becomes +// f_r(v,l) = D(h,α) V(v,l,α) F(v,h,f0) +// where +// V(v,l,α) = 0.5 / { n⋅l sqrt((n⋅v)^2 (1−α2) + α2) + n⋅v sqrt((n⋅l)^2 (1−α2) + α2) } +// Note the two sqrt's, that may be slow on mobile, see https://google.github.io/filament/Filament.html#listing_approximatedspecularv +float V_SmithGGXCorrelated(float roughness, float NoV, float NoL) { + float a2 = roughness * roughness; + float lambdaV = NoL * sqrt((NoV - a2 * NoV) * NoV + a2); + float lambdaL = NoV * sqrt((NoL - a2 * NoL) * NoL + a2); + float v = 0.5 / (lambdaV + lambdaL); + return v; +} + +// Fresnel function +// see https://google.github.io/filament/Filament.html#citation-schlick94 +// F_Schlick(v,h,f_0,f_90) = f_0 + (f_90 − f_0) (1 − v⋅h)^5 +vec3 F_Schlick(const vec3 f0, float f90, float VoH) { + // not using mix to keep the vec3 and float versions identical + return f0 + (f90 - f0) * pow5(1.0 - VoH); +} + +float F_Schlick(float f0, float f90, float VoH) { + // not using mix to keep the vec3 and float versions identical + return f0 + (f90 - f0) * pow5(1.0 - VoH); +} + +vec3 fresnel(vec3 f0, float LoH) { + // f_90 suitable for ambient occlusion + // see https://google.github.io/filament/Filament.html#lighting/occlusion + float f90 = saturate(dot(f0, vec3(50.0 * 0.33))); + return F_Schlick(f0, f90, LoH); +} + +// Specular BRDF +// https://google.github.io/filament/Filament.html#materialsystem/specularbrdf + +// Cook-Torrance approximation of the microfacet model integration using Fresnel law F to model f_m +// f_r(v,l) = { D(h,α) G(v,l,α) F(v,h,f0) } / { 4 (n⋅v) (n⋅l) } +vec3 specular(vec3 f0, float roughness, const vec3 h, float NoV, float NoL, + float NoH, float LoH) { + float D = D_GGX(roughness, NoH, h); + float V = V_SmithGGXCorrelated(roughness, NoV, NoL); + vec3 F = fresnel(f0, LoH); + + return (D * V) * F; +} + +// Diffuse BRDF +// https://google.github.io/filament/Filament.html#materialsystem/diffusebrdf +// fd(v,l) = σ/π * 1 / { |n⋅v||n⋅l| } ∫Ω D(m,α) G(v,l,m) (v⋅m) (l⋅m) dm + +// simplest approximation +// float Fd_Lambert() { +// return 1.0 / PI; +// } +// +// vec3 Fd = diffuseColor * Fd_Lambert(); + +// Disney approximation +// See https://google.github.io/filament/Filament.html#citation-burley12 +// minimal quality difference +float Fd_Burley(float roughness, float NoV, float NoL, float LoH) { + float f90 = 0.5 + 2.0 * roughness * LoH * LoH; + float lightScatter = F_Schlick(1.0, f90, NoL); + float viewScatter = F_Schlick(1.0, f90, NoV); + return lightScatter * viewScatter * (1.0 / PI); +} + +// From https://www.unrealengine.com/en-US/blog/physically-based-shading-on-mobile +vec3 EnvBRDFApprox(vec3 f0, float perceptual_roughness, float NoV) { + const vec4 c0 = { -1, -0.0275, -0.572, 0.022 }; + const vec4 c1 = { 1, 0.0425, 1.04, -0.04 }; + vec4 r = perceptual_roughness * c0 + c1; + float a004 = min(r.x * r.x, exp2(-9.28 * NoV)) * r.x + r.y; + vec2 AB = vec2(-1.04, 1.04) * a004 + r.zw; + return f0 * AB.x + AB.y; +} + +float perceptualRoughnessToRoughness(float perceptualRoughness) { + // clamp perceptual roughness to prevent precision problems + // According to Filament design 0.089 is recommended for mobile + // Filament uses 0.045 for non-mobile + float clampedPerceptualRoughness = clamp(perceptualRoughness, 0.089, 1.0); + return clampedPerceptualRoughness * clampedPerceptualRoughness; +} + +// from https://64.github.io/tonemapping/ +// reinhard on RGB oversaturates colors +vec3 reinhard(vec3 color) { + return color / (1.0 + color); +} + +vec3 reinhard_extended(vec3 color, float max_white) { + vec3 numerator = color * (1.0f + (color / vec3(max_white * max_white))); + return numerator / (1.0 + color); +} + +// luminance coefficients from Rec. 709. +// https://en.wikipedia.org/wiki/Rec._709 +float luminance(vec3 v) { + return dot(v, vec3(0.2126, 0.7152, 0.0722)); +} + +vec3 change_luminance(vec3 c_in, float l_out) { + float l_in = luminance(c_in); + return c_in * (l_out / l_in); +} + +vec3 reinhard_luminance(vec3 color) { + float l_old = luminance(color); + float l_new = l_old / (1.0f + l_old); + return change_luminance(color, l_new); +} + +vec3 reinhard_extended_luminance(vec3 color, float max_white_l) { + float l_old = luminance(color); + float numerator = l_old * (1.0f + (l_old / (max_white_l * max_white_l))); + float l_new = numerator / (1.0f + l_old); + return change_luminance(color, l_new); +} + +#endif + +void main() { + vec4 output_color = base_color; +#ifdef STANDARDMATERIAL_BASE_COLOR_TEXTURE + output_color *= texture(sampler2D(StandardMaterial_base_color_texture, + StandardMaterial_base_color_texture_sampler), + v_Uv); +#endif + +#ifndef STANDARDMATERIAL_UNLIT + // calculate non-linear roughness from linear perceptualRoughness + float roughness = perceptualRoughnessToRoughness(perceptual_roughness); + + vec3 N = normalize(v_WorldNormal); + + vec3 V = normalize(CameraPos.xyz - v_WorldPosition.xyz); + // Neubelt and Pettineo 2013, "Crafting a Next-gen Material Pipeline for The Order: 1886" + float NdotV = max(dot(N, V), 1e-4); + + // Remapping [0,1] reflectance to F0 + // See https://google.github.io/filament/Filament.html#materialsystem/parameterization/remapping + vec3 F0 = 0.16 * reflectance * reflectance * (1.0 - metallic) + output_color.rgb * metallic; + + // Diffuse strength inversely related to metallicity + vec3 diffuseColor = output_color.rgb * (1.0 - metallic); + + // accumulate color + vec3 light_accum = vec3(0.0); + for (int i = 0; i < int(NumLights.x) && i < MAX_LIGHTS; ++i) { + Light light = SceneLights[i]; + + vec3 lightDir = light.pos.xyz - v_WorldPosition.xyz; + vec3 L = normalize(lightDir); + + float rangeAttenuation = + getDistanceAttenuation(lightDir, light.inverseRadiusSquared); + + vec3 H = normalize(L + V); + float NoL = saturate(dot(N, L)); + float NoH = saturate(dot(N, H)); + float LoH = saturate(dot(L, H)); + + vec3 specular = specular(F0, roughness, H, NdotV, NoL, NoH, LoH); + vec3 diffuse = diffuseColor * Fd_Burley(roughness, NdotV, NoL, LoH); + + // Lout = f(v,l) Φ / { 4 π d^2 }⟨n⋅l⟩ + // where + // f(v,l) = (f_d(v,l) + f_r(v,l)) * light_color + // Φ is light intensity + + // our rangeAttentuation = 1 / d^2 multiplied with an attenuation factor for smoothing at the edge of the non-physical maximum light radius + // It's not 100% clear where the 1/4π goes in the derivation, but we follow the filament shader and leave it out + + // See https://google.github.io/filament/Filament.html#mjx-eqn-pointLightLuminanceEquation + // TODO compensate for energy loss https://google.github.io/filament/Filament.html#materialsystem/improvingthebrdfs/energylossinspecularreflectance + // light.color.rgb is premultiplied with light.intensity on the CPU + light_accum += + ((diffuse + specular) * light.color.rgb) * (rangeAttenuation * NoL); + } + + vec3 diffuse_ambient = EnvBRDFApprox(diffuseColor, 1.0, NdotV); + vec3 specular_ambient = EnvBRDFApprox(F0, perceptual_roughness, NdotV); + + output_color.rgb = light_accum + (diffuse_ambient + specular_ambient) * AmbientColor; + + // tone_mapping + output_color.rgb = reinhard_luminance(output_color.rgb); + // Gamma correction. + // Not needed with sRGB buffer + // output_color.rgb = pow(output_color.rgb, vec3(1.0 / 2.2)); +#endif + + o_Target = output_color; +} diff --git a/crates/bevy_pbr/src/render_graph/forward_pipeline/forward.vert b/crates/bevy_pbr/src/render_graph/pbr_pipeline/pbr.vert similarity index 55% rename from crates/bevy_pbr/src/render_graph/forward_pipeline/forward.vert rename to crates/bevy_pbr/src/render_graph/pbr_pipeline/pbr.vert index bbba55ea7c..78c1312e1e 100644 --- a/crates/bevy_pbr/src/render_graph/forward_pipeline/forward.vert +++ b/crates/bevy_pbr/src/render_graph/pbr_pipeline/pbr.vert @@ -4,8 +4,8 @@ layout(location = 0) in vec3 Vertex_Position; layout(location = 1) in vec3 Vertex_Normal; layout(location = 2) in vec2 Vertex_Uv; -layout(location = 0) out vec3 v_Position; -layout(location = 1) out vec3 v_Normal; +layout(location = 0) out vec3 v_WorldPosition; +layout(location = 1) out vec3 v_WorldNormal; layout(location = 2) out vec2 v_Uv; layout(set = 0, binding = 0) uniform CameraViewProj { @@ -17,8 +17,9 @@ layout(set = 2, binding = 0) uniform Transform { }; void main() { - v_Normal = mat3(Model) * Vertex_Normal; - v_Position = (Model * vec4(Vertex_Position, 1.0)).xyz; + vec4 world_position = Model * vec4(Vertex_Position, 1.0); + v_WorldPosition = world_position.xyz; + v_WorldNormal = mat3(Model) * Vertex_Normal; v_Uv = Vertex_Uv; - gl_Position = ViewProj * vec4(v_Position, 1.0); + gl_Position = ViewProj * world_position; } diff --git a/crates/bevy_render/src/render_graph/nodes/camera_node.rs b/crates/bevy_render/src/render_graph/nodes/camera_node.rs index fdf2c021e6..d86b0e4d0a 100644 --- a/crates/bevy_render/src/render_graph/nodes/camera_node.rs +++ b/crates/bevy_render/src/render_graph/nodes/camera_node.rs @@ -59,6 +59,7 @@ impl SystemNode for CameraNode { const CAMERA_VIEW_PROJ: &str = "CameraViewProj"; const CAMERA_VIEW: &str = "CameraView"; +const CAMERA_POSITION: &str = "CameraPosition"; #[derive(Debug, Default)] pub struct CameraNodeState { @@ -68,6 +69,7 @@ pub struct CameraNodeState { } const MATRIX_SIZE: usize = std::mem::size_of::<[[f32; 4]; 4]>(); +const VEC3_SIZE: usize = std::mem::size_of::<[f32; 3]>(); pub fn camera_node_system( mut state: Local, @@ -93,7 +95,13 @@ pub fn camera_node_system( staging_buffer } else { let staging_buffer = render_resource_context.create_buffer(BufferInfo { - size: MATRIX_SIZE * 2, + size: + // ViewProj + MATRIX_SIZE + + // View + MATRIX_SIZE + + // Position + VEC3_SIZE, buffer_usage: BufferUsage::COPY_SRC | BufferUsage::MAP_WRITE, mapped_at_creation: true, }); @@ -134,7 +142,24 @@ pub fn camera_node_system( ); } + if bindings.get(CAMERA_POSITION).is_none() { + let buffer = render_resource_context.create_buffer(BufferInfo { + size: VEC3_SIZE, + buffer_usage: BufferUsage::COPY_DST | BufferUsage::UNIFORM, + ..Default::default() + }); + bindings.set( + CAMERA_POSITION, + RenderResourceBinding::Buffer { + buffer, + range: 0..VEC3_SIZE as u64, + dynamic_index: None, + }, + ); + } + let view = global_transform.compute_matrix(); + let mut offset = 0; if let Some(RenderResourceBinding::Buffer { buffer, .. }) = bindings.get(CAMERA_VIEW) { render_resource_context.write_mapped_buffer( @@ -151,24 +176,44 @@ pub fn camera_node_system( 0, MATRIX_SIZE as u64, ); + offset += MATRIX_SIZE as u64; } if let Some(RenderResourceBinding::Buffer { buffer, .. }) = bindings.get(CAMERA_VIEW_PROJ) { let view_proj = camera.projection_matrix * view.inverse(); render_resource_context.write_mapped_buffer( staging_buffer, - MATRIX_SIZE as u64..(2 * MATRIX_SIZE) as u64, + offset..(offset + MATRIX_SIZE as u64), &mut |data, _renderer| { data[0..MATRIX_SIZE].copy_from_slice(view_proj.to_cols_array_2d().as_bytes()); }, ); state.command_queue.copy_buffer_to_buffer( staging_buffer, - MATRIX_SIZE as u64, + offset, *buffer, 0, MATRIX_SIZE as u64, ); + offset += MATRIX_SIZE as u64; + } + + if let Some(RenderResourceBinding::Buffer { buffer, .. }) = bindings.get(CAMERA_POSITION) { + let position: [f32; 3] = global_transform.translation.into(); + render_resource_context.write_mapped_buffer( + staging_buffer, + offset..(offset + VEC3_SIZE as u64), + &mut |data, _renderer| { + data[0..VEC3_SIZE].copy_from_slice(position.as_bytes()); + }, + ); + state.command_queue.copy_buffer_to_buffer( + staging_buffer, + offset, + *buffer, + 0, + VEC3_SIZE as u64, + ); } render_resource_context.unmap_buffer(staging_buffer); diff --git a/crates/bevy_render/src/render_graph/nodes/pass_node.rs b/crates/bevy_render/src/render_graph/nodes/pass_node.rs index 66fef3c857..16cf5fef31 100644 --- a/crates/bevy_render/src/render_graph/nodes/pass_node.rs +++ b/crates/bevy_render/src/render_graph/nodes/pass_node.rs @@ -2,10 +2,7 @@ use crate::{ camera::{ActiveCameras, VisibleEntities}, draw::{Draw, RenderCommand}, pass::{ClearColor, LoadOp, PassDescriptor, TextureAttachment}, - pipeline::{ - BindGroupDescriptor, BindType, BindingDescriptor, BindingShaderStage, IndexFormat, - PipelineDescriptor, UniformProperty, - }, + pipeline::{IndexFormat, PipelineDescriptor}, prelude::Visible, render_graph::{Node, ResourceSlotInfo, ResourceSlots}, renderer::{ @@ -29,7 +26,6 @@ pub struct PassNode { color_resolve_target_indices: Vec>, depth_stencil_attachment_input_index: Option, default_clear_color_inputs: Vec, - camera_bind_group_descriptor: BindGroupDescriptor, query_state: Option>, commands: Vec, } @@ -56,10 +52,6 @@ impl fmt::Debug for PassNode { "default_clear_color_inputs", &self.default_clear_color_inputs, ) - .field( - "camera_bind_group_descriptor", - &self.camera_bind_group_descriptor, - ) .finish() } } @@ -102,19 +94,6 @@ impl PassNode { } } - let camera_bind_group_descriptor = BindGroupDescriptor::new( - 0, - vec![BindingDescriptor { - name: "Camera".to_string(), - index: 0, - bind_type: BindType::Uniform { - has_dynamic_offset: false, - property: UniformProperty::Struct(vec![UniformProperty::Mat4]), - }, - shader_stage: BindingShaderStage::VERTEX | BindingShaderStage::FRAGMENT, - }], - ); - PassNode { descriptor, inputs, @@ -123,7 +102,6 @@ impl PassNode { color_resolve_target_indices, depth_stencil_attachment_input_index, default_clear_color_inputs: Vec::new(), - camera_bind_group_descriptor, query_state: None, commands: Vec::new(), } diff --git a/crates/bevy_render/src/shader/shader_reflect.rs b/crates/bevy_render/src/shader/shader_reflect.rs index 78a5f306f1..e35d339ee2 100644 --- a/crates/bevy_render/src/shader/shader_reflect.rs +++ b/crates/bevy_render/src/shader/shader_reflect.rs @@ -321,6 +321,7 @@ mod tests { layout(location = 0) out vec4 v_Position; layout(set = 0, binding = 0) uniform CameraViewProj { mat4 ViewProj; + vec4 CameraPos; }; layout(set = 1, binding = 0) uniform texture2D Texture; @@ -378,7 +379,10 @@ mod tests { name: "CameraViewProj".into(), bind_type: BindType::Uniform { has_dynamic_offset: false, - property: UniformProperty::Struct(vec![UniformProperty::Mat4]), + property: UniformProperty::Struct(vec![ + UniformProperty::Mat4, + UniformProperty::Vec4 + ]), }, shader_stage: BindingShaderStage::VERTEX, }] diff --git a/crates/bevy_render/src/wireframe/wireframe.vert b/crates/bevy_render/src/wireframe/wireframe.vert index 87b32a667a..f1cc11d1f7 100644 --- a/crates/bevy_render/src/wireframe/wireframe.vert +++ b/crates/bevy_render/src/wireframe/wireframe.vert @@ -4,6 +4,7 @@ layout(location = 0) in vec3 Vertex_Position; layout(set = 0, binding = 0) uniform CameraViewProj { mat4 ViewProj; + vec4 CameraPos; }; layout(set = 1, binding = 0) uniform Transform { diff --git a/crates/bevy_sprite/src/render/sprite.vert b/crates/bevy_sprite/src/render/sprite.vert index 2a0cc68e3a..c311dfaf7a 100644 --- a/crates/bevy_sprite/src/render/sprite.vert +++ b/crates/bevy_sprite/src/render/sprite.vert @@ -8,6 +8,7 @@ layout(location = 0) out vec2 v_Uv; layout(set = 0, binding = 0) uniform CameraViewProj { mat4 ViewProj; + vec4 CameraPos; }; layout(set = 2, binding = 0) uniform Transform { diff --git a/crates/bevy_sprite/src/render/sprite_sheet.vert b/crates/bevy_sprite/src/render/sprite_sheet.vert index 72e0f5de93..25bd5a9a5a 100644 --- a/crates/bevy_sprite/src/render/sprite_sheet.vert +++ b/crates/bevy_sprite/src/render/sprite_sheet.vert @@ -9,6 +9,7 @@ layout(location = 1) out vec4 v_Color; layout(set = 0, binding = 0) uniform CameraViewProj { mat4 ViewProj; + vec4 CameraPos; }; // TODO: merge dimensions into "sprites" buffer when that is supported in the Uniforms derive abstraction diff --git a/crates/bevy_ui/src/render/ui.vert b/crates/bevy_ui/src/render/ui.vert index 3a00b11f40..bcfe502802 100644 --- a/crates/bevy_ui/src/render/ui.vert +++ b/crates/bevy_ui/src/render/ui.vert @@ -8,6 +8,7 @@ layout(location = 0) out vec2 v_Uv; layout(set = 0, binding = 0) uniform CameraViewProj { mat4 ViewProj; + vec4 CameraPos; }; layout(set = 1, binding = 0) uniform Transform { diff --git a/examples/3d/parenting.rs b/examples/3d/parenting.rs index c4b95b755f..42f6c7b224 100644 --- a/examples/3d/parenting.rs +++ b/examples/3d/parenting.rs @@ -29,7 +29,7 @@ fn setup( ) { let cube_handle = meshes.add(Mesh::from(shape::Cube { size: 2.0 })); let cube_material_handle = materials.add(StandardMaterial { - albedo: Color::rgb(0.8, 0.7, 0.6), + base_color: Color::rgb(0.8, 0.7, 0.6), ..Default::default() }); diff --git a/examples/3d/pbr.rs b/examples/3d/pbr.rs new file mode 100644 index 0000000000..7154c0b489 --- /dev/null +++ b/examples/3d/pbr.rs @@ -0,0 +1,54 @@ +use bevy::prelude::*; + +/// This example shows how to configure Physically Based Rendering (PBR) parameters. +fn main() { + App::build() + .insert_resource(Msaa { samples: 4 }) + .add_plugins(DefaultPlugins) + .add_startup_system(setup.system()) + .run(); +} + +/// set up a simple 3D scene +fn setup( + mut commands: Commands, + mut meshes: ResMut>, + mut materials: ResMut>, +) { + // add entities to the world + for y in -2..=2 { + for x in -5..=5 { + let x01 = (x + 5) as f32 / 10.0; + let y01 = (y + 2) as f32 / 4.0; + commands + // spheres + .spawn(PbrBundle { + mesh: meshes.add(Mesh::from(shape::Icosphere { + radius: 0.45, + subdivisions: 32, + })), + material: materials.add(StandardMaterial { + base_color: Color::hex("ffd891").unwrap(), + // vary key PBR parameters on a grid of spheres to show the effect + metallic: y01, + roughness: x01, + ..Default::default() + }), + transform: Transform::from_xyz(x as f32, y as f32, 0.0), + ..Default::default() + }); + } + } + commands + // light + .spawn(LightBundle { + transform: Transform::from_translation(Vec3::new(0.0, 5.0, 5.0)), + ..Default::default() + }) + // camera + .spawn(PerspectiveCameraBundle { + transform: Transform::from_translation(Vec3::new(0.0, 0.0, 8.0)) + .looking_at(Vec3::default(), Vec3::Y), + ..Default::default() + }); +} diff --git a/examples/3d/spawner.rs b/examples/3d/spawner.rs index 892933e56a..6423207847 100644 --- a/examples/3d/spawner.rs +++ b/examples/3d/spawner.rs @@ -30,7 +30,7 @@ fn move_cubes( for (mut transform, material_handle) in query.iter_mut() { let material = materials.get_mut(material_handle).unwrap(); transform.translation += Vec3::new(1.0, 0.0, 0.0) * time.delta_seconds(); - material.albedo = + material.base_color = Color::BLUE * Vec3::splat((3.0 * time.seconds_since_startup() as f32).sin()); } } @@ -58,7 +58,7 @@ fn setup( commands.spawn(PbrBundle { mesh: cube_handle.clone(), material: materials.add(StandardMaterial { - albedo: Color::rgb( + base_color: Color::rgb( rng.gen_range(0.0..1.0), rng.gen_range(0.0..1.0), rng.gen_range(0.0..1.0), diff --git a/examples/3d/texture.rs b/examples/3d/texture.rs index 663875f0b3..b59b3aa324 100644 --- a/examples/3d/texture.rs +++ b/examples/3d/texture.rs @@ -28,23 +28,25 @@ fn setup( // this material renders the texture normally let material_handle = materials.add(StandardMaterial { - albedo_texture: Some(texture_handle.clone()), + base_color_texture: Some(texture_handle.clone()), unlit: true, ..Default::default() }); // this material modulates the texture to make it red (and slightly transparent) let red_material_handle = materials.add(StandardMaterial { - albedo: Color::rgba(1.0, 0.0, 0.0, 0.5), - albedo_texture: Some(texture_handle.clone()), + base_color: Color::rgba(1.0, 0.0, 0.0, 0.5), + base_color_texture: Some(texture_handle.clone()), unlit: true, + ..Default::default() }); // and lets make this one blue! (and also slightly transparent) let blue_material_handle = materials.add(StandardMaterial { - albedo: Color::rgba(0.0, 0.0, 1.0, 0.5), - albedo_texture: Some(texture_handle), + base_color: Color::rgba(0.0, 0.0, 1.0, 0.5), + base_color_texture: Some(texture_handle), unlit: true, + ..Default::default() }); // add entities to the world diff --git a/examples/3d/z_sort_debug.rs b/examples/3d/z_sort_debug.rs index 41667cc569..5d972504e3 100644 --- a/examples/3d/z_sort_debug.rs +++ b/examples/3d/z_sort_debug.rs @@ -36,7 +36,7 @@ fn camera_order_color_system( if let Ok(material_handle) = material_query.get(visible_entity.entity) { let material = materials.get_mut(&*material_handle).unwrap(); let value = 1.0 - (visible_entity.order.0.sqrt() - 10.0) / 7.0; - material.albedo = Color::rgb(value, value, value); + material.base_color = Color::rgb(value, value, value); } } } diff --git a/examples/README.md b/examples/README.md index 0fe69bfa5d..f1df0b3d2f 100644 --- a/examples/README.md +++ b/examples/README.md @@ -87,6 +87,7 @@ Example | File | Description `msaa` | [`3d/msaa.rs`](./3d/msaa.rs) | Configures MSAA (Multi-Sample Anti-Aliasing) for smoother edges `orthographic` | [`3d/orthographic.rs`](./3d/orthographic.rs) | Shows how to create a 3D orthographic view (for isometric-look games or CAD applications) `parenting` | [`3d/parenting.rs`](./3d/parenting.rs) | Demonstrates parent->child relationships and relative transformations +`pbr` | [`3d/pbr.rs`](./3d/[pbr].rs) | Demonstrates PBR properties Roughness/Metallic `spawner` | [`3d/spawner.rs`](./3d/spawner.rs) | Renders a large number of cubes with changing position and material `texture` | [`3d/texture.rs`](./3d/texture.rs) | Shows configuration of texture materials `update_gltf_scene` | [`3d/update_gltf_scene.rs`](./3d/update_gltf_scene.rs) | Update a scene from a gltf file, either by spawning the scene as a child of another entity, or by accessing the entities of the scene diff --git a/examples/asset/asset_loading.rs b/examples/asset/asset_loading.rs index 51f7f63cbd..2eac853d69 100644 --- a/examples/asset/asset_loading.rs +++ b/examples/asset/asset_loading.rs @@ -39,7 +39,7 @@ fn setup( // You can also add assets directly to their Assets storage: let material_handle = materials.add(StandardMaterial { - albedo: Color::rgb(0.8, 0.7, 0.6), + base_color: Color::rgb(0.8, 0.7, 0.6), ..Default::default() }); diff --git a/examples/shader/shader_custom_material.rs b/examples/shader/shader_custom_material.rs index acd9b3146b..ca420a57d0 100644 --- a/examples/shader/shader_custom_material.rs +++ b/examples/shader/shader_custom_material.rs @@ -31,6 +31,7 @@ const VERTEX_SHADER: &str = r#" layout(location = 0) in vec3 Vertex_Position; layout(set = 0, binding = 0) uniform CameraViewProj { mat4 ViewProj; + vec4 CameraPos; }; layout(set = 1, binding = 0) uniform Transform { mat4 Model; diff --git a/examples/shader/shader_defs.rs b/examples/shader/shader_defs.rs index 52827d6e95..1b4a928f00 100644 --- a/examples/shader/shader_defs.rs +++ b/examples/shader/shader_defs.rs @@ -39,6 +39,7 @@ const VERTEX_SHADER: &str = r#" layout(location = 0) in vec3 Vertex_Position; layout(set = 0, binding = 0) uniform CameraViewProj { mat4 ViewProj; + vec4 CameraPos; }; layout(set = 1, binding = 0) uniform Transform { mat4 Model;