mirror of
https://github.com/bevyengine/bevy
synced 2024-11-22 12:43:34 +00:00
Rebase of existing PBR work (#1554)
This is a rebase of StarArawns PBR work from #261 with IngmarBitters work from #1160 cherry-picked on top. I had to make a few minor changes to make some intermediate commits compile and the end result is not yet 100% what I expected, so there's a bit more work to do. Co-authored-by: John Mitchell <toasterthegamer@gmail.com> Co-authored-by: Ingmar Bitter <ingmar.bitter@gmail.com>
This commit is contained in:
parent
b6be8a5314
commit
45b2db7070
28 changed files with 520 additions and 131 deletions
|
@ -135,6 +135,10 @@ path = "examples/3d/orthographic.rs"
|
||||||
name = "parenting"
|
name = "parenting"
|
||||||
path = "examples/3d/parenting.rs"
|
path = "examples/3d/parenting.rs"
|
||||||
|
|
||||||
|
[[example]]
|
||||||
|
name = "pbr"
|
||||||
|
path = "examples/3d/pbr.rs"
|
||||||
|
|
||||||
[[example]]
|
[[example]]
|
||||||
name = "spawner"
|
name = "spawner"
|
||||||
path = "examples/3d/spawner.rs"
|
path = "examples/3d/spawner.rs"
|
||||||
|
|
|
@ -277,9 +277,12 @@ fn load_material(material: &Material, load_context: &mut LoadContext) -> Handle<
|
||||||
load_context.set_labeled_asset(
|
load_context.set_labeled_asset(
|
||||||
&material_label,
|
&material_label,
|
||||||
LoadedAsset::new(StandardMaterial {
|
LoadedAsset::new(StandardMaterial {
|
||||||
albedo: Color::rgba(color[0], color[1], color[2], color[3]),
|
base_color: Color::rgba(color[0], color[1], color[2], color[3]),
|
||||||
albedo_texture: texture_handle,
|
base_color_texture: texture_handle,
|
||||||
|
roughness: pbr.roughness_factor(),
|
||||||
|
metallic: pbr.metallic_factor(),
|
||||||
unlit: material.unlit(),
|
unlit: material.unlit(),
|
||||||
|
..Default::default()
|
||||||
})
|
})
|
||||||
.with_dependencies(dependencies),
|
.with_dependencies(dependencies),
|
||||||
)
|
)
|
||||||
|
|
|
@ -96,7 +96,6 @@ pub mod gltf {
|
||||||
#[cfg(feature = "bevy_pbr")]
|
#[cfg(feature = "bevy_pbr")]
|
||||||
pub mod pbr {
|
pub mod pbr {
|
||||||
//! Physically based rendering.
|
//! Physically based rendering.
|
||||||
//! **Note**: true PBR has not yet been implemented; the name `pbr` is aspirational.
|
|
||||||
pub use bevy_pbr::*;
|
pub use bevy_pbr::*;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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_asset::Handle;
|
||||||
use bevy_ecs::bundle::Bundle;
|
use bevy_ecs::bundle::Bundle;
|
||||||
use bevy_render::{
|
use bevy_render::{
|
||||||
|
@ -27,7 +27,7 @@ impl Default for PbrBundle {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
render_pipelines: RenderPipelines::from_pipelines(vec![RenderPipeline::new(
|
render_pipelines: RenderPipelines::from_pipelines(vec![RenderPipeline::new(
|
||||||
FORWARD_PIPELINE_HANDLE.typed(),
|
PBR_PIPELINE_HANDLE.typed(),
|
||||||
)]),
|
)]),
|
||||||
mesh: Default::default(),
|
mesh: Default::default(),
|
||||||
visible: Default::default(),
|
visible: Default::default(),
|
||||||
|
|
|
@ -42,9 +42,9 @@ impl Plugin for PbrPlugin {
|
||||||
materials.set_untracked(
|
materials.set_untracked(
|
||||||
Handle::<StandardMaterial>::default(),
|
Handle::<StandardMaterial>::default(),
|
||||||
StandardMaterial {
|
StandardMaterial {
|
||||||
albedo: Color::PINK,
|
base_color: Color::PINK,
|
||||||
unlit: true,
|
unlit: true,
|
||||||
albedo_texture: None,
|
..Default::default()
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,6 +15,8 @@ pub struct Light {
|
||||||
pub color: Color,
|
pub color: Color,
|
||||||
pub fov: f32,
|
pub fov: f32,
|
||||||
pub depth: Range<f32>,
|
pub depth: Range<f32>,
|
||||||
|
pub intensity: f32,
|
||||||
|
pub range: f32,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Light {
|
impl Default for Light {
|
||||||
|
@ -23,6 +25,8 @@ impl Default for Light {
|
||||||
color: Color::rgb(1.0, 1.0, 1.0),
|
color: Color::rgb(1.0, 1.0, 1.0),
|
||||||
depth: 0.1..50.0,
|
depth: 0.1..50.0,
|
||||||
fov: f32::to_radians(60.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 proj = perspective.get_projection_matrix() * global_transform.compute_matrix();
|
||||||
let (x, y, z) = global_transform.translation.into();
|
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 {
|
LightRaw {
|
||||||
proj: proj.to_cols_array_2d(),
|
proj: proj.to_cols_array_2d(),
|
||||||
pos: [x, y, z, 1.0],
|
pos: [x, y, z, 1.0 / (light.range * light.range)], // pos.w is the attenuation.
|
||||||
color: light.color.into(),
|
color,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,12 +3,28 @@ use bevy_reflect::TypeUuid;
|
||||||
use bevy_render::{color::Color, renderer::RenderResources, shader::ShaderDefs, texture::Texture};
|
use bevy_render::{color::Color, renderer::RenderResources, shader::ShaderDefs, texture::Texture};
|
||||||
|
|
||||||
/// A material with "standard" properties used in PBR lighting
|
/// 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)]
|
#[derive(Debug, RenderResources, ShaderDefs, TypeUuid)]
|
||||||
#[uuid = "dace545e-4bc6-4595-a79d-c224fc694975"]
|
#[uuid = "dace545e-4bc6-4595-a79d-c224fc694975"]
|
||||||
pub struct StandardMaterial {
|
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]
|
#[shader_def]
|
||||||
pub albedo_texture: Option<Handle<Texture>>,
|
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
|
||||||
|
pub reflectance: f32,
|
||||||
#[render_resources(ignore)]
|
#[render_resources(ignore)]
|
||||||
#[shader_def]
|
#[shader_def]
|
||||||
pub unlit: bool,
|
pub unlit: bool,
|
||||||
|
@ -17,8 +33,19 @@ pub struct StandardMaterial {
|
||||||
impl Default for StandardMaterial {
|
impl Default for StandardMaterial {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
StandardMaterial {
|
StandardMaterial {
|
||||||
albedo: Color::rgb(1.0, 1.0, 1.0),
|
base_color: Color::rgb(1.0, 1.0, 1.0),
|
||||||
albedo_texture: None,
|
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,
|
unlit: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -27,7 +54,7 @@ impl Default for StandardMaterial {
|
||||||
impl From<Color> for StandardMaterial {
|
impl From<Color> for StandardMaterial {
|
||||||
fn from(color: Color) -> Self {
|
fn from(color: Color) -> Self {
|
||||||
StandardMaterial {
|
StandardMaterial {
|
||||||
albedo: color,
|
base_color: color,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -36,7 +63,7 @@ impl From<Color> for StandardMaterial {
|
||||||
impl From<Handle<Texture>> for StandardMaterial {
|
impl From<Handle<Texture>> for StandardMaterial {
|
||||||
fn from(texture: Handle<Texture>) -> Self {
|
fn from(texture: Handle<Texture>) -> Self {
|
||||||
StandardMaterial {
|
StandardMaterial {
|
||||||
albedo_texture: Some(texture),
|
base_color_texture: Some(texture),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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<int(NumLights.x) && i<MAX_LIGHTS; ++i) {
|
|
||||||
Light light = SceneLights[i];
|
|
||||||
// compute Lambertian diffuse term
|
|
||||||
vec3 light_dir = normalize(light.pos.xyz - v_Position);
|
|
||||||
float diffuse = max(0.0, dot(normal, light_dir));
|
|
||||||
// add light contribution
|
|
||||||
color += diffuse * light.color.xyz;
|
|
||||||
}
|
|
||||||
|
|
||||||
// average the lights so that we will never get something with > 1.0
|
|
||||||
color /= max(float(NumLights.x), 1.0);
|
|
||||||
|
|
||||||
output_color.xyz *= color;
|
|
||||||
# endif
|
|
||||||
|
|
||||||
// multiply the light by material color
|
|
||||||
o_Target = output_color;
|
|
||||||
}
|
|
|
@ -1,9 +1,9 @@
|
||||||
mod forward_pipeline;
|
|
||||||
mod lights_node;
|
mod lights_node;
|
||||||
|
mod pbr_pipeline;
|
||||||
|
|
||||||
use bevy_ecs::world::World;
|
use bevy_ecs::world::World;
|
||||||
pub use forward_pipeline::*;
|
|
||||||
pub use lights_node::*;
|
pub use lights_node::*;
|
||||||
|
pub use pbr_pipeline::*;
|
||||||
|
|
||||||
/// the names of pbr graph nodes
|
/// the names of pbr graph nodes
|
||||||
pub mod node {
|
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)
|
.add_node_edge(node::LIGHTS, base::node::MAIN_PASS)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
}
|
}
|
||||||
let forward_pipeline =
|
let pipeline = build_pbr_pipeline(&mut world.get_resource_mut::<Assets<Shader>>().unwrap());
|
||||||
build_forward_pipeline(&mut world.get_resource_mut::<Assets<Shader>>().unwrap());
|
|
||||||
let mut pipelines = world
|
let mut pipelines = world
|
||||||
.get_resource_mut::<Assets<PipelineDescriptor>>()
|
.get_resource_mut::<Assets<PipelineDescriptor>>()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
pipelines.set_untracked(FORWARD_PIPELINE_HANDLE, forward_pipeline);
|
pipelines.set_untracked(PBR_PIPELINE_HANDLE, pipeline);
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,10 +9,10 @@ use bevy_render::{
|
||||||
texture::TextureFormat,
|
texture::TextureFormat,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const FORWARD_PIPELINE_HANDLE: HandleUntyped =
|
pub const PBR_PIPELINE_HANDLE: HandleUntyped =
|
||||||
HandleUntyped::weak_from_u64(PipelineDescriptor::TYPE_UUID, 13148362314012771389);
|
HandleUntyped::weak_from_u64(PipelineDescriptor::TYPE_UUID, 13148362314012771389);
|
||||||
|
|
||||||
pub(crate) fn build_forward_pipeline(shaders: &mut Assets<Shader>) -> PipelineDescriptor {
|
pub(crate) fn build_pbr_pipeline(shaders: &mut Assets<Shader>) -> PipelineDescriptor {
|
||||||
PipelineDescriptor {
|
PipelineDescriptor {
|
||||||
depth_stencil: Some(DepthStencilState {
|
depth_stencil: Some(DepthStencilState {
|
||||||
format: TextureFormat::Depth32Float,
|
format: TextureFormat::Depth32Float,
|
||||||
|
@ -48,11 +48,11 @@ pub(crate) fn build_forward_pipeline(shaders: &mut Assets<Shader>) -> PipelineDe
|
||||||
..PipelineDescriptor::new(ShaderStages {
|
..PipelineDescriptor::new(ShaderStages {
|
||||||
vertex: shaders.add(Shader::from_glsl(
|
vertex: shaders.add(Shader::from_glsl(
|
||||||
ShaderStage::Vertex,
|
ShaderStage::Vertex,
|
||||||
include_str!("forward.vert"),
|
include_str!("pbr.vert"),
|
||||||
)),
|
)),
|
||||||
fragment: Some(shaders.add(Shader::from_glsl(
|
fragment: Some(shaders.add(Shader::from_glsl(
|
||||||
ShaderStage::Fragment,
|
ShaderStage::Fragment,
|
||||||
include_str!("forward.frag"),
|
include_str!("pbr.frag"),
|
||||||
))),
|
))),
|
||||||
})
|
})
|
||||||
}
|
}
|
323
crates/bevy_pbr/src/render_graph/pbr_pipeline/pbr.frag
Normal file
323
crates/bevy_pbr/src/render_graph/pbr_pipeline/pbr.frag
Normal file
|
@ -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;
|
||||||
|
}
|
|
@ -4,8 +4,8 @@ layout(location = 0) in vec3 Vertex_Position;
|
||||||
layout(location = 1) in vec3 Vertex_Normal;
|
layout(location = 1) in vec3 Vertex_Normal;
|
||||||
layout(location = 2) in vec2 Vertex_Uv;
|
layout(location = 2) in vec2 Vertex_Uv;
|
||||||
|
|
||||||
layout(location = 0) out vec3 v_Position;
|
layout(location = 0) out vec3 v_WorldPosition;
|
||||||
layout(location = 1) out vec3 v_Normal;
|
layout(location = 1) out vec3 v_WorldNormal;
|
||||||
layout(location = 2) out vec2 v_Uv;
|
layout(location = 2) out vec2 v_Uv;
|
||||||
|
|
||||||
layout(set = 0, binding = 0) uniform CameraViewProj {
|
layout(set = 0, binding = 0) uniform CameraViewProj {
|
||||||
|
@ -17,8 +17,9 @@ layout(set = 2, binding = 0) uniform Transform {
|
||||||
};
|
};
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
v_Normal = mat3(Model) * Vertex_Normal;
|
vec4 world_position = Model * vec4(Vertex_Position, 1.0);
|
||||||
v_Position = (Model * vec4(Vertex_Position, 1.0)).xyz;
|
v_WorldPosition = world_position.xyz;
|
||||||
|
v_WorldNormal = mat3(Model) * Vertex_Normal;
|
||||||
v_Uv = Vertex_Uv;
|
v_Uv = Vertex_Uv;
|
||||||
gl_Position = ViewProj * vec4(v_Position, 1.0);
|
gl_Position = ViewProj * world_position;
|
||||||
}
|
}
|
|
@ -59,6 +59,7 @@ impl SystemNode for CameraNode {
|
||||||
|
|
||||||
const CAMERA_VIEW_PROJ: &str = "CameraViewProj";
|
const CAMERA_VIEW_PROJ: &str = "CameraViewProj";
|
||||||
const CAMERA_VIEW: &str = "CameraView";
|
const CAMERA_VIEW: &str = "CameraView";
|
||||||
|
const CAMERA_POSITION: &str = "CameraPosition";
|
||||||
|
|
||||||
#[derive(Debug, Default)]
|
#[derive(Debug, Default)]
|
||||||
pub struct CameraNodeState {
|
pub struct CameraNodeState {
|
||||||
|
@ -68,6 +69,7 @@ pub struct CameraNodeState {
|
||||||
}
|
}
|
||||||
|
|
||||||
const MATRIX_SIZE: usize = std::mem::size_of::<[[f32; 4]; 4]>();
|
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(
|
pub fn camera_node_system(
|
||||||
mut state: Local<CameraNodeState>,
|
mut state: Local<CameraNodeState>,
|
||||||
|
@ -93,7 +95,13 @@ pub fn camera_node_system(
|
||||||
staging_buffer
|
staging_buffer
|
||||||
} else {
|
} else {
|
||||||
let staging_buffer = render_resource_context.create_buffer(BufferInfo {
|
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,
|
buffer_usage: BufferUsage::COPY_SRC | BufferUsage::MAP_WRITE,
|
||||||
mapped_at_creation: true,
|
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 view = global_transform.compute_matrix();
|
||||||
|
let mut offset = 0;
|
||||||
|
|
||||||
if let Some(RenderResourceBinding::Buffer { buffer, .. }) = bindings.get(CAMERA_VIEW) {
|
if let Some(RenderResourceBinding::Buffer { buffer, .. }) = bindings.get(CAMERA_VIEW) {
|
||||||
render_resource_context.write_mapped_buffer(
|
render_resource_context.write_mapped_buffer(
|
||||||
|
@ -151,24 +176,44 @@ pub fn camera_node_system(
|
||||||
0,
|
0,
|
||||||
MATRIX_SIZE as u64,
|
MATRIX_SIZE as u64,
|
||||||
);
|
);
|
||||||
|
offset += MATRIX_SIZE as u64;
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(RenderResourceBinding::Buffer { buffer, .. }) = bindings.get(CAMERA_VIEW_PROJ) {
|
if let Some(RenderResourceBinding::Buffer { buffer, .. }) = bindings.get(CAMERA_VIEW_PROJ) {
|
||||||
let view_proj = camera.projection_matrix * view.inverse();
|
let view_proj = camera.projection_matrix * view.inverse();
|
||||||
render_resource_context.write_mapped_buffer(
|
render_resource_context.write_mapped_buffer(
|
||||||
staging_buffer,
|
staging_buffer,
|
||||||
MATRIX_SIZE as u64..(2 * MATRIX_SIZE) as u64,
|
offset..(offset + MATRIX_SIZE as u64),
|
||||||
&mut |data, _renderer| {
|
&mut |data, _renderer| {
|
||||||
data[0..MATRIX_SIZE].copy_from_slice(view_proj.to_cols_array_2d().as_bytes());
|
data[0..MATRIX_SIZE].copy_from_slice(view_proj.to_cols_array_2d().as_bytes());
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
state.command_queue.copy_buffer_to_buffer(
|
state.command_queue.copy_buffer_to_buffer(
|
||||||
staging_buffer,
|
staging_buffer,
|
||||||
MATRIX_SIZE as u64,
|
offset,
|
||||||
*buffer,
|
*buffer,
|
||||||
0,
|
0,
|
||||||
MATRIX_SIZE as u64,
|
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);
|
render_resource_context.unmap_buffer(staging_buffer);
|
||||||
|
|
|
@ -2,10 +2,7 @@ use crate::{
|
||||||
camera::{ActiveCameras, VisibleEntities},
|
camera::{ActiveCameras, VisibleEntities},
|
||||||
draw::{Draw, RenderCommand},
|
draw::{Draw, RenderCommand},
|
||||||
pass::{ClearColor, LoadOp, PassDescriptor, TextureAttachment},
|
pass::{ClearColor, LoadOp, PassDescriptor, TextureAttachment},
|
||||||
pipeline::{
|
pipeline::{IndexFormat, PipelineDescriptor},
|
||||||
BindGroupDescriptor, BindType, BindingDescriptor, BindingShaderStage, IndexFormat,
|
|
||||||
PipelineDescriptor, UniformProperty,
|
|
||||||
},
|
|
||||||
prelude::Visible,
|
prelude::Visible,
|
||||||
render_graph::{Node, ResourceSlotInfo, ResourceSlots},
|
render_graph::{Node, ResourceSlotInfo, ResourceSlots},
|
||||||
renderer::{
|
renderer::{
|
||||||
|
@ -29,7 +26,6 @@ pub struct PassNode<Q: WorldQuery> {
|
||||||
color_resolve_target_indices: Vec<Option<usize>>,
|
color_resolve_target_indices: Vec<Option<usize>>,
|
||||||
depth_stencil_attachment_input_index: Option<usize>,
|
depth_stencil_attachment_input_index: Option<usize>,
|
||||||
default_clear_color_inputs: Vec<usize>,
|
default_clear_color_inputs: Vec<usize>,
|
||||||
camera_bind_group_descriptor: BindGroupDescriptor,
|
|
||||||
query_state: Option<QueryState<Q>>,
|
query_state: Option<QueryState<Q>>,
|
||||||
commands: Vec<RenderCommand>,
|
commands: Vec<RenderCommand>,
|
||||||
}
|
}
|
||||||
|
@ -56,10 +52,6 @@ impl<Q: WorldQuery> fmt::Debug for PassNode<Q> {
|
||||||
"default_clear_color_inputs",
|
"default_clear_color_inputs",
|
||||||
&self.default_clear_color_inputs,
|
&self.default_clear_color_inputs,
|
||||||
)
|
)
|
||||||
.field(
|
|
||||||
"camera_bind_group_descriptor",
|
|
||||||
&self.camera_bind_group_descriptor,
|
|
||||||
)
|
|
||||||
.finish()
|
.finish()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -102,19 +94,6 @@ impl<Q: WorldQuery> PassNode<Q> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
PassNode {
|
||||||
descriptor,
|
descriptor,
|
||||||
inputs,
|
inputs,
|
||||||
|
@ -123,7 +102,6 @@ impl<Q: WorldQuery> PassNode<Q> {
|
||||||
color_resolve_target_indices,
|
color_resolve_target_indices,
|
||||||
depth_stencil_attachment_input_index,
|
depth_stencil_attachment_input_index,
|
||||||
default_clear_color_inputs: Vec::new(),
|
default_clear_color_inputs: Vec::new(),
|
||||||
camera_bind_group_descriptor,
|
|
||||||
query_state: None,
|
query_state: None,
|
||||||
commands: Vec::new(),
|
commands: Vec::new(),
|
||||||
}
|
}
|
||||||
|
|
|
@ -321,6 +321,7 @@ mod tests {
|
||||||
layout(location = 0) out vec4 v_Position;
|
layout(location = 0) out vec4 v_Position;
|
||||||
layout(set = 0, binding = 0) uniform CameraViewProj {
|
layout(set = 0, binding = 0) uniform CameraViewProj {
|
||||||
mat4 ViewProj;
|
mat4 ViewProj;
|
||||||
|
vec4 CameraPos;
|
||||||
};
|
};
|
||||||
layout(set = 1, binding = 0) uniform texture2D Texture;
|
layout(set = 1, binding = 0) uniform texture2D Texture;
|
||||||
|
|
||||||
|
@ -378,7 +379,10 @@ mod tests {
|
||||||
name: "CameraViewProj".into(),
|
name: "CameraViewProj".into(),
|
||||||
bind_type: BindType::Uniform {
|
bind_type: BindType::Uniform {
|
||||||
has_dynamic_offset: false,
|
has_dynamic_offset: false,
|
||||||
property: UniformProperty::Struct(vec![UniformProperty::Mat4]),
|
property: UniformProperty::Struct(vec![
|
||||||
|
UniformProperty::Mat4,
|
||||||
|
UniformProperty::Vec4
|
||||||
|
]),
|
||||||
},
|
},
|
||||||
shader_stage: BindingShaderStage::VERTEX,
|
shader_stage: BindingShaderStage::VERTEX,
|
||||||
}]
|
}]
|
||||||
|
|
|
@ -4,6 +4,7 @@ layout(location = 0) in vec3 Vertex_Position;
|
||||||
|
|
||||||
layout(set = 0, binding = 0) uniform CameraViewProj {
|
layout(set = 0, binding = 0) uniform CameraViewProj {
|
||||||
mat4 ViewProj;
|
mat4 ViewProj;
|
||||||
|
vec4 CameraPos;
|
||||||
};
|
};
|
||||||
|
|
||||||
layout(set = 1, binding = 0) uniform Transform {
|
layout(set = 1, binding = 0) uniform Transform {
|
||||||
|
|
|
@ -8,6 +8,7 @@ layout(location = 0) out vec2 v_Uv;
|
||||||
|
|
||||||
layout(set = 0, binding = 0) uniform CameraViewProj {
|
layout(set = 0, binding = 0) uniform CameraViewProj {
|
||||||
mat4 ViewProj;
|
mat4 ViewProj;
|
||||||
|
vec4 CameraPos;
|
||||||
};
|
};
|
||||||
|
|
||||||
layout(set = 2, binding = 0) uniform Transform {
|
layout(set = 2, binding = 0) uniform Transform {
|
||||||
|
|
|
@ -9,6 +9,7 @@ layout(location = 1) out vec4 v_Color;
|
||||||
|
|
||||||
layout(set = 0, binding = 0) uniform CameraViewProj {
|
layout(set = 0, binding = 0) uniform CameraViewProj {
|
||||||
mat4 ViewProj;
|
mat4 ViewProj;
|
||||||
|
vec4 CameraPos;
|
||||||
};
|
};
|
||||||
|
|
||||||
// TODO: merge dimensions into "sprites" buffer when that is supported in the Uniforms derive abstraction
|
// TODO: merge dimensions into "sprites" buffer when that is supported in the Uniforms derive abstraction
|
||||||
|
|
|
@ -8,6 +8,7 @@ layout(location = 0) out vec2 v_Uv;
|
||||||
|
|
||||||
layout(set = 0, binding = 0) uniform CameraViewProj {
|
layout(set = 0, binding = 0) uniform CameraViewProj {
|
||||||
mat4 ViewProj;
|
mat4 ViewProj;
|
||||||
|
vec4 CameraPos;
|
||||||
};
|
};
|
||||||
|
|
||||||
layout(set = 1, binding = 0) uniform Transform {
|
layout(set = 1, binding = 0) uniform Transform {
|
||||||
|
|
|
@ -29,7 +29,7 @@ fn setup(
|
||||||
) {
|
) {
|
||||||
let cube_handle = meshes.add(Mesh::from(shape::Cube { size: 2.0 }));
|
let cube_handle = meshes.add(Mesh::from(shape::Cube { size: 2.0 }));
|
||||||
let cube_material_handle = materials.add(StandardMaterial {
|
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()
|
..Default::default()
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
54
examples/3d/pbr.rs
Normal file
54
examples/3d/pbr.rs
Normal file
|
@ -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<Assets<Mesh>>,
|
||||||
|
mut materials: ResMut<Assets<StandardMaterial>>,
|
||||||
|
) {
|
||||||
|
// 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()
|
||||||
|
});
|
||||||
|
}
|
|
@ -30,7 +30,7 @@ fn move_cubes(
|
||||||
for (mut transform, material_handle) in query.iter_mut() {
|
for (mut transform, material_handle) in query.iter_mut() {
|
||||||
let material = materials.get_mut(material_handle).unwrap();
|
let material = materials.get_mut(material_handle).unwrap();
|
||||||
transform.translation += Vec3::new(1.0, 0.0, 0.0) * time.delta_seconds();
|
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());
|
Color::BLUE * Vec3::splat((3.0 * time.seconds_since_startup() as f32).sin());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -58,7 +58,7 @@ fn setup(
|
||||||
commands.spawn(PbrBundle {
|
commands.spawn(PbrBundle {
|
||||||
mesh: cube_handle.clone(),
|
mesh: cube_handle.clone(),
|
||||||
material: materials.add(StandardMaterial {
|
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),
|
rng.gen_range(0.0..1.0),
|
||||||
rng.gen_range(0.0..1.0),
|
rng.gen_range(0.0..1.0),
|
||||||
|
|
|
@ -28,23 +28,25 @@ fn setup(
|
||||||
|
|
||||||
// this material renders the texture normally
|
// this material renders the texture normally
|
||||||
let material_handle = materials.add(StandardMaterial {
|
let material_handle = materials.add(StandardMaterial {
|
||||||
albedo_texture: Some(texture_handle.clone()),
|
base_color_texture: Some(texture_handle.clone()),
|
||||||
unlit: true,
|
unlit: true,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
});
|
});
|
||||||
|
|
||||||
// this material modulates the texture to make it red (and slightly transparent)
|
// this material modulates the texture to make it red (and slightly transparent)
|
||||||
let red_material_handle = materials.add(StandardMaterial {
|
let red_material_handle = materials.add(StandardMaterial {
|
||||||
albedo: Color::rgba(1.0, 0.0, 0.0, 0.5),
|
base_color: Color::rgba(1.0, 0.0, 0.0, 0.5),
|
||||||
albedo_texture: Some(texture_handle.clone()),
|
base_color_texture: Some(texture_handle.clone()),
|
||||||
unlit: true,
|
unlit: true,
|
||||||
|
..Default::default()
|
||||||
});
|
});
|
||||||
|
|
||||||
// and lets make this one blue! (and also slightly transparent)
|
// and lets make this one blue! (and also slightly transparent)
|
||||||
let blue_material_handle = materials.add(StandardMaterial {
|
let blue_material_handle = materials.add(StandardMaterial {
|
||||||
albedo: Color::rgba(0.0, 0.0, 1.0, 0.5),
|
base_color: Color::rgba(0.0, 0.0, 1.0, 0.5),
|
||||||
albedo_texture: Some(texture_handle),
|
base_color_texture: Some(texture_handle),
|
||||||
unlit: true,
|
unlit: true,
|
||||||
|
..Default::default()
|
||||||
});
|
});
|
||||||
|
|
||||||
// add entities to the world
|
// add entities to the world
|
||||||
|
|
|
@ -36,7 +36,7 @@ fn camera_order_color_system(
|
||||||
if let Ok(material_handle) = material_query.get(visible_entity.entity) {
|
if let Ok(material_handle) = material_query.get(visible_entity.entity) {
|
||||||
let material = materials.get_mut(&*material_handle).unwrap();
|
let material = materials.get_mut(&*material_handle).unwrap();
|
||||||
let value = 1.0 - (visible_entity.order.0.sqrt() - 10.0) / 7.0;
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -87,6 +87,7 @@ Example | File | Description
|
||||||
`msaa` | [`3d/msaa.rs`](./3d/msaa.rs) | Configures MSAA (Multi-Sample Anti-Aliasing) for smoother edges
|
`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)
|
`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
|
`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
|
`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
|
`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
|
`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
|
||||||
|
|
|
@ -39,7 +39,7 @@ fn setup(
|
||||||
|
|
||||||
// You can also add assets directly to their Assets<T> storage:
|
// You can also add assets directly to their Assets<T> storage:
|
||||||
let material_handle = materials.add(StandardMaterial {
|
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()
|
..Default::default()
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -31,6 +31,7 @@ const VERTEX_SHADER: &str = r#"
|
||||||
layout(location = 0) in vec3 Vertex_Position;
|
layout(location = 0) in vec3 Vertex_Position;
|
||||||
layout(set = 0, binding = 0) uniform CameraViewProj {
|
layout(set = 0, binding = 0) uniform CameraViewProj {
|
||||||
mat4 ViewProj;
|
mat4 ViewProj;
|
||||||
|
vec4 CameraPos;
|
||||||
};
|
};
|
||||||
layout(set = 1, binding = 0) uniform Transform {
|
layout(set = 1, binding = 0) uniform Transform {
|
||||||
mat4 Model;
|
mat4 Model;
|
||||||
|
|
|
@ -39,6 +39,7 @@ const VERTEX_SHADER: &str = r#"
|
||||||
layout(location = 0) in vec3 Vertex_Position;
|
layout(location = 0) in vec3 Vertex_Position;
|
||||||
layout(set = 0, binding = 0) uniform CameraViewProj {
|
layout(set = 0, binding = 0) uniform CameraViewProj {
|
||||||
mat4 ViewProj;
|
mat4 ViewProj;
|
||||||
|
vec4 CameraPos;
|
||||||
};
|
};
|
||||||
layout(set = 1, binding = 0) uniform Transform {
|
layout(set = 1, binding = 0) uniform Transform {
|
||||||
mat4 Model;
|
mat4 Model;
|
||||||
|
|
Loading…
Reference in a new issue