Pass roughness & metalness factors to shader

This commit is contained in:
Lauri Räsänen 2023-11-11 17:31:04 +02:00
parent 8b99a617f4
commit f9798743b8
4 changed files with 50 additions and 10 deletions

View file

@ -27,6 +27,13 @@ struct GlobalUniforms {
@group(0) @binding(2)
var<uniform> global_uniforms: GlobalUniforms;
struct MaterialUniform {
metallic_factor: f32,
rougness_factor: f32,
_padding1: f32,
_padding2: f32,
}
struct VertexInput {
@location(0) position: vec3<f32>,
@location(1) tex_coords: vec2<f32>,

View file

@ -67,6 +67,9 @@ var t_roughness_metalness: texture_2d<f32>;
@group(2) @binding(5)
var s_roughness_metalness: sampler;
@group(2) @binding(6)
var<uniform> material_uniform: MaterialUniform;
@fragment
fn fs_main(vert: VertexOutput) -> @location(0) vec4<f32> {
// textures
@ -76,9 +79,8 @@ fn fs_main(vert: VertexOutput) -> @location(0) vec4<f32> {
t_roughness_metalness, s_roughness_metalness, vert.tex_coords);
let albedo = object_color.xyz;
// TODO: pass factors to shader
let roughness = object_roughness_metalness.y * 1.0;
let metalness = object_roughness_metalness.z * 1.0;
let roughness = object_roughness_metalness.y * material_uniform.rougness_factor;
let metalness = object_roughness_metalness.z * material_uniform.metallic_factor;
var total_radiance: vec3<f32>;

View file

@ -1,3 +1,4 @@
use wgpu::util::DeviceExt;
use crate::core::texture::Texture;
pub struct Material {
@ -5,13 +6,17 @@ pub struct Material {
pub diffuse_texture: Texture,
pub normal_texture: Texture,
pub metallic_roughness_texture: Texture,
// TODO pass to shader
pub metallic_factor: f32,
// TODO pass to shader
pub roughness_factor: f32,
pub material_uniform: MaterialUniform,
pub bind_group: wgpu::BindGroup,
}
#[repr(C)]
#[derive(Debug, Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
pub struct MaterialUniform {
// metallic, roughness, none, none
pub factors: [f32; 4],
}
impl Material {
pub fn new(
device: &wgpu::Device,
@ -23,6 +28,15 @@ impl Material {
roughness_factor: f32,
layout: &wgpu::BindGroupLayout,
) -> Self {
let material_uniform = MaterialUniform {
factors: [metallic_factor, roughness_factor, 0.0, 0.0]
};
let material_uniform_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Material Uniform UB"),
contents: bytemuck::cast_slice(&[material_uniform]),
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
});
let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
layout,
entries: &[
@ -53,6 +67,11 @@ impl Material {
binding: 5,
resource: wgpu::BindingResource::Sampler(&metallic_roughness_texture.sampler),
},
// uniform
wgpu::BindGroupEntry {
binding: 6,
resource: material_uniform_buffer.as_entire_binding(),
}
],
label: None,
});
@ -62,8 +81,7 @@ impl Material {
diffuse_texture,
normal_texture,
metallic_roughness_texture,
metallic_factor,
roughness_factor,
material_uniform,
bind_group,
}
}

View file

@ -5,6 +5,7 @@ use std::time::Duration;
use wgpu::util::DeviceExt;
use winit::{event::*, window::Window};
use crate::core::material::MaterialUniform;
use super::camera::{Camera, CameraController, CameraUniform};
use super::instance::{Instance, InstanceRaw};
@ -305,6 +306,7 @@ impl State {
let geometry_depth_bind_group = State::create_geometry_depth_bind_group(&device, &geometry_depth_bind_group_layout, &geometry_depth_texture);
let material_uniform_size = mem::size_of::<MaterialUniform>() as u64;
let texture_bind_group_layout =
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
entries: &[
@ -359,6 +361,17 @@ impl State {
ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
count: None,
},
// material uniform
wgpu::BindGroupLayoutEntry {
binding: 6,
visibility: wgpu::ShaderStages::FRAGMENT,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Uniform,
has_dynamic_offset: false,
min_binding_size: wgpu::BufferSize::new(material_uniform_size),
},
count: None,
},
],
label: Some("texture_bind_group_layout"),
});