This commit is contained in:
marceline-cramer 2022-02-16 21:32:38 -07:00
parent e18a434581
commit cb5b559bbd
5 changed files with 130 additions and 15 deletions

View File

@ -3,6 +3,7 @@ use std::time::Instant;
use winit::event::{ElementState, VirtualKeyCode}; use winit::event::{ElementState, VirtualKeyCode};
pub trait Camera { pub trait Camera {
fn get_eye(&self) -> [f32; 4];
fn get_vp(&self) -> [[f32; 4]; 4]; fn get_vp(&self) -> [[f32; 4]; 4];
} }
@ -148,6 +149,10 @@ impl Flycam {
} }
impl Camera for Flycam { impl Camera for Flycam {
fn get_eye(&self) -> [f32; 4] {
self.position.extend(0.0).to_array()
}
fn get_vp(&self) -> [[f32; 4]; 4] { fn get_vp(&self) -> [[f32; 4]; 4] {
let orientation = Quat::from_euler(glam::EulerRot::XYZ, self.tilt, self.pan, 0.0); let orientation = Quat::from_euler(glam::EulerRot::XYZ, self.tilt, self.pan, 0.0);
let rotation = Mat4::from_quat(orientation); let rotation = Mat4::from_quat(orientation);

View File

@ -93,8 +93,10 @@ impl ObjModel {
let albedo = on_load.load_texture(&albedo_data); let albedo = on_load.load_texture(&albedo_data);
let material_data = MaterialData { albedo }; let material_handle = on_load.load_material(&MaterialData {
let material_handle = on_load.load_material(&material_data); albedo,
metallic_roughness: albedo, // TODO better placeholder MR texture
});
let mesh = BasicMesh { let mesh = BasicMesh {
mesh_handle, mesh_handle,
@ -201,7 +203,13 @@ impl<'a, T: OnLoad> GltfLoader<'a, T> {
let base_color = pbr.base_color_texture().unwrap().texture(); let base_color = pbr.base_color_texture().unwrap().texture();
let albedo = self.load_texture(base_color); let albedo = self.load_texture(base_color);
self.on_load.load_material(&MaterialData { albedo }) let metallic_roughness = pbr.metallic_roughness_texture().unwrap().texture();
let metallic_roughness = self.load_texture(metallic_roughness);
self.on_load.load_material(&MaterialData {
albedo,
metallic_roughness,
})
} }
pub fn load_texture(&mut self, texture: gltf::Texture) -> TextureHandle { pub fn load_texture(&mut self, texture: gltf::Texture) -> TextureHandle {

View File

@ -145,6 +145,7 @@ impl TexturePool {
pub struct MaterialData { pub struct MaterialData {
pub albedo: TextureHandle, pub albedo: TextureHandle,
pub metallic_roughness: TextureHandle,
} }
pub struct Material { pub struct Material {
@ -158,6 +159,17 @@ pub struct MaterialPool {
impl MaterialPool { impl MaterialPool {
pub fn new(device: &wgpu::Device) -> Self { pub fn new(device: &wgpu::Device) -> Self {
let texture_entry = wgpu::BindGroupLayoutEntry {
binding: u32::MAX,
visibility: wgpu::ShaderStages::FRAGMENT,
ty: wgpu::BindingType::Texture {
multisampled: false,
view_dimension: wgpu::TextureViewDimension::D2,
sample_type: wgpu::TextureSampleType::Float { filterable: true },
},
count: None,
};
let bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { let bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
entries: &[ entries: &[
wgpu::BindGroupLayoutEntry { wgpu::BindGroupLayoutEntry {
@ -168,13 +180,11 @@ impl MaterialPool {
}, },
wgpu::BindGroupLayoutEntry { wgpu::BindGroupLayoutEntry {
binding: 1, binding: 1,
visibility: wgpu::ShaderStages::FRAGMENT, ..texture_entry
ty: wgpu::BindingType::Texture { },
multisampled: false, wgpu::BindGroupLayoutEntry {
view_dimension: wgpu::TextureViewDimension::D2, binding: 2,
sample_type: wgpu::TextureSampleType::Float { filterable: true }, ..texture_entry
},
count: None,
}, },
], ],
label: Some("Texture Bind Group Layout"), label: Some("Texture Bind Group Layout"),
@ -193,6 +203,7 @@ impl MaterialPool {
data: &MaterialData, data: &MaterialData,
) -> MaterialHandle { ) -> MaterialHandle {
let albedo_view = &texture_pool.textures[data.albedo.id].texture_view; let albedo_view = &texture_pool.textures[data.albedo.id].texture_view;
let mr_view = &texture_pool.textures[data.metallic_roughness.id].texture_view;
let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor { let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
layout: &self.bind_group_layout, layout: &self.bind_group_layout,
@ -205,6 +216,10 @@ impl MaterialPool {
binding: 1, binding: 1,
resource: wgpu::BindingResource::TextureView(albedo_view), resource: wgpu::BindingResource::TextureView(albedo_view),
}, },
wgpu::BindGroupEntry {
binding: 2,
resource: wgpu::BindingResource::TextureView(mr_view),
},
], ],
label: None, label: None,
}); });

View File

@ -61,7 +61,7 @@ impl Renderer {
entries: &[ entries: &[
wgpu::BindGroupLayoutEntry { wgpu::BindGroupLayoutEntry {
binding: 0, binding: 0,
visibility: wgpu::ShaderStages::VERTEX, visibility: wgpu::ShaderStages::VERTEX_FRAGMENT,
ty: wgpu::BindingType::Buffer { ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Uniform, ty: wgpu::BufferBindingType::Uniform,
has_dynamic_offset: false, has_dynamic_offset: false,
@ -378,17 +378,20 @@ impl OnLoad for &mut Renderer {
#[repr(C)] #[repr(C)]
#[derive(Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)] #[derive(Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
struct CameraUniform { struct CameraUniform {
eye: [f32; 4],
vp: [[f32; 4]; 4], vp: [[f32; 4]; 4],
} }
impl CameraUniform { impl CameraUniform {
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
eye: [0.0; 4],
vp: glam::Mat4::IDENTITY.to_cols_array_2d(), vp: glam::Mat4::IDENTITY.to_cols_array_2d(),
} }
} }
pub fn update(&mut self, camera: &impl Camera) { pub fn update(&mut self, camera: &impl Camera) {
self.eye = camera.get_eye();
self.vp = camera.get_vp(); self.vp = camera.get_vp();
} }
} }

View File

@ -1,4 +1,5 @@
struct CameraUniform { struct CameraUniform {
eye: vec4<f32>;
vp: mat4x4<f32>; vp: mat4x4<f32>;
}; };
@ -44,6 +45,76 @@ var<storage,read> meshes: MeshData;
[[group(2), binding(0)]] var m_sampler: sampler; [[group(2), binding(0)]] var m_sampler: sampler;
[[group(2), binding(1)]] var m_albedo: texture_2d<f32>; [[group(2), binding(1)]] var m_albedo: texture_2d<f32>;
[[group(2), binding(2)]] var m_metallic_roughness: texture_2d<f32>;
let PI: f32 = 3.141592;
fn D_GGX(NoH: f32, roughness: f32) -> f32 {
let a = roughness * roughness;
let a2 = a * a;
let NoH2 = NoH * NoH;
let f = NoH * (a2 - 1.0) + 1.0;
return a2 / (PI * f * f);
}
fn g1(NoV: f32, roughness: f32, k: f32) -> f32 {
let denom = NoV * (1.0 - k) + k;
return NoV / denom;
}
fn G_SmithGGXCorrelated(NoV: f32, NoL: f32, roughness: f32) -> f32 {
let r = roughness + 1.0;
let k = (r * r) / 8.0;
let g1l = g1(NoV, roughness, k);
let g1v = g1(NoL, roughness, k);
return g1l * g1v;
}
fn F_Schlick(u: f32, f0: vec3<f32>) -> vec3<f32> {
let f = pow(1.0 - u, 5.0);
return f + f0 * (1.0 - f);
}
fn BRDF(
l: vec3<f32>, // normalized light direction
n: vec3<f32>, // normalized surface normal
v: vec3<f32>, // normalized view direction
albedo: vec3<f32>, // surface albedo
metallic: f32, // surface metallic
roughness: f32, // surface roughness
) -> vec3<f32> {
let h = normalize(v + l);
let NoL = max(dot(n, l), 0.0);
let NoV = max(dot(n, v), 0.0);
let NoH = max(dot(n, h), 0.0);
let LoH = max(dot(l, h), 0.0);
// calculate reflectance at surface incidence
let f0 = mix(vec3<f32>(0.04), albedo, metallic);
// specular BRDF
let D = D_GGX(NoH, roughness);
let G = G_SmithGGXCorrelated(NoV, NoL, roughness);
let F = F_Schlick(LoH, f0);
let numerator = (D * G) * F;
let denominator = 4.0 * NoV * NoL;
let Fr = numerator / max(denominator, 0.01);
// diffuse BRDF
let diffuse_fresnel =
(vec3<f32>(1.0) - F_Schlick(NoL, f0)) *
(vec3<f32>(1.0) - F_Schlick(NoV, f0));
let lambertian = albedo / PI;
let Fd = diffuse_fresnel * lambertian;
// TODO multiple scattering
return (Fr + Fd) * NoL;
}
[[stage(vertex)]] [[stage(vertex)]]
fn vs_main( fn vs_main(
@ -68,14 +139,27 @@ fn fs_main(
) -> [[location(0)]] vec4<f32> { ) -> [[location(0)]] vec4<f32> {
let albedo = textureSample(m_albedo, m_sampler, frag.tex_coords).rgb; let albedo = textureSample(m_albedo, m_sampler, frag.tex_coords).rgb;
let normal = normalize(frag.normal); let normal = normalize(frag.normal);
let view = normalize(camera.eye.xyz - frag.position);
let metallic_roughness = textureSample(m_metallic_roughness, m_sampler, frag.tex_coords).bg;
let metallic = metallic_roughness.x;
let roughness = metallic_roughness.y;
var lum = vec3<f32>(0.0); var lum = vec3<f32>(0.0);
for(var i = 0; i < 4; i = i + 1) { for(var i = 0; i < 4; i = i + 1) {
let light = point_lights.lights[i]; let light = point_lights.lights[i];
let light_relative = light.center.xyz - frag.position; let light_position = light.center.xyz - frag.position;
let received = dot(normal, normalize(light_relative)); let light_intensity = light.intensity.rgb;
let diffuse = (albedo * received) / dot(light_relative, light_relative); let light_direction = normalize(light_position);
lum = lum + (diffuse * light.intensity.rgb);
let radiance = light_intensity / dot(light_position, light_position);
let reflected = BRDF(
light_direction, normal, view,
albedo, metallic, roughness
);
lum = lum + (radiance * reflected);
} }
return vec4<f32>(lum, 1.0); return vec4<f32>(lum, 1.0);