reflections seem correct

hdr
Benjamin Hansen 8 months ago
parent fa43eb6172
commit 49c2cbc8f6

@ -0,0 +1,14 @@
# Blender MTL File: 'cobble_sphere.blend'
# Material Count: 1
newmtl Material
Ns 250.000000
Ka 1.000000 1.000000 1.000000
Kd 0.800000 0.800000 0.800000
Ks 0.500000 0.500000 0.500000
Ke 0.000000 0.000000 0.000000
Ni 1.450000
d 1.000000
illum 2
map_Bump cobble-normal.png
map_Kd cobble-diffuse.png

File diff suppressed because it is too large Load Diff

@ -0,0 +1,13 @@
# Blender 3.6.4 MTL File: 'floor.blend'
# www.blender.org
newmtl Material
Ns 250.000000
Ka 1.000000 1.000000 1.000000
Ks 0.500000 0.500000 0.500000
Ke 0.000000 0.000000 0.000000
Ni 1.450000
d 1.000000
illum 2
map_Kd cobble-diffuse.png
map_Bump cobble-normal.png

@ -0,0 +1,16 @@
# Blender 3.6.4
# www.blender.org
mtllib floor.mtl
o Plane
v -1.000000 0.000000 1.000000
v 1.000000 0.000000 1.000000
v -1.000000 0.000000 -1.000000
v 1.000000 0.000000 -1.000000
vn -0.0000 1.0000 -0.0000
vt 0.000000 0.000000
vt 1.000000 0.000000
vt 1.000000 1.000000
vt 0.000000 1.000000
s 0
usemtl Material
f 1/1/1 2/2/1 4/3/1 3/4/1

@ -0,0 +1,99 @@
use std::mem::size_of;
use wgpu::util::{BufferInitDescriptor, DeviceExt};
use crate::create_render_pipeline;
#[repr(C)]
#[derive(Debug, Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
pub struct PositionColor {
position: [f32; 3],
color: [f32; 3],
}
const AXIS_COLORS: &'static [PositionColor] = &[
// X
PositionColor {
position: [0.0, 0.0, 0.0],
color: [0.5, 0.0, 0.0],
},
PositionColor {
position: [1.0, 0.0, 0.0],
color: [1.0, 0.0, 0.0],
},
// Y
PositionColor {
position: [0.0, 0.0, 0.0],
color: [0.0, 0.5, 0.0],
},
PositionColor {
position: [0.0, 1.0, 0.0],
color: [0.0, 1.0, 0.0],
},
// Z
PositionColor {
position: [0.0, 0.0, 0.0],
color: [0.0, 0.0, 0.5],
},
PositionColor {
position: [0.0, 0.0, 1.0],
color: [0.0, 0.0, 1.0],
},
];
const POSITION_COLOR_LAYOUT: wgpu::VertexBufferLayout<'static> = wgpu::VertexBufferLayout {
array_stride: size_of::<PositionColor>() as _,
step_mode: wgpu::VertexStepMode::Vertex,
attributes: &wgpu::vertex_attr_array![
0 => Float32x3,
1 => Float32x3,
],
};
pub struct Debug {
color_lines: wgpu::RenderPipeline,
axis: wgpu::Buffer,
}
impl Debug {
pub fn new(
device: &wgpu::Device,
camera_layout: &wgpu::BindGroupLayout,
color_format: wgpu::TextureFormat,
) -> Self {
let axis = device.create_buffer_init(&BufferInitDescriptor {
label: Some("Debug::axis"),
contents: bytemuck::cast_slice(AXIS_COLORS),
usage: wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::VERTEX,
});
let shader = wgpu::include_wgsl!("debug.wgsl");
let layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: None,
bind_group_layouts: &[camera_layout],
push_constant_ranges: &[],
});
let color_lines = create_render_pipeline(
device,
&layout,
color_format,
None,
&[POSITION_COLOR_LAYOUT],
wgpu::PrimitiveTopology::LineList,
shader,
);
Self { color_lines, axis }
}
pub fn draw_axis<'a: 'b, 'b>(
&'a self,
pass: &'b mut wgpu::RenderPass<'a>,
camera: &'a wgpu::BindGroup,
) {
pass.set_pipeline(&self.color_lines);
pass.set_bind_group(0, camera, &[]);
pass.set_vertex_buffer(0, self.axis.slice(..));
pass.draw(0..AXIS_COLORS.len() as u32, 0..1);
}
}

@ -0,0 +1,34 @@
struct Camera {
view_pos: vec4<f32>,
view: mat4x4<f32>,
view_proj: mat4x4<f32>,
inv_proj: mat4x4<f32>,
inv_view: mat4x4<f32>,
}
@group(0) @binding(0)
var<uniform> camera: Camera;
struct PositionColor {
@location(0) position: vec3<f32>,
@location(1) color: vec3<f32>,
}
struct VsOut {
@builtin(position) frag_position: vec4<f32>,
@location(0) color: vec3<f32>,
}
@vertex
fn vs_main(
v: PositionColor,
) -> VsOut {
let frag_position = camera.view_proj * vec4(v.position, 1.0);
return VsOut(frag_position, v.color);
}
@fragment
fn fs_main(
v: VsOut,
) -> @location(0) vec4<f32> {
return vec4(v.color, 1.0);
}

@ -73,8 +73,15 @@ impl HdrPipeline {
push_constant_ranges: &[],
});
let pipeline =
create_render_pipeline(device, &pipeline_layout, config.format, None, &[], shader);
let pipeline = create_render_pipeline(
device,
&pipeline_layout,
config.format,
None,
&[],
wgpu::PrimitiveTopology::TriangleList,
shader,
);
Self {
pipeline,

@ -16,6 +16,7 @@ mod hdr;
mod model;
mod resources;
mod texture;
mod debug;
use model::{DrawLight, DrawModel, Vertex};
@ -174,6 +175,7 @@ struct State {
hdr: hdr::HdrPipeline,
environment_bind_group: wgpu::BindGroup,
sky_pipeline: wgpu::RenderPipeline,
debug: debug::Debug,
}
fn create_render_pipeline(
@ -182,6 +184,7 @@ fn create_render_pipeline(
color_format: wgpu::TextureFormat,
depth_format: Option<wgpu::TextureFormat>,
vertex_layouts: &[wgpu::VertexBufferLayout],
topology: wgpu::PrimitiveTopology,
shader: wgpu::ShaderModuleDescriptor,
) -> wgpu::RenderPipeline {
let shader = device.create_shader_module(shader);
@ -204,7 +207,7 @@ fn create_render_pipeline(
})],
}),
primitive: wgpu::PrimitiveState {
topology: wgpu::PrimitiveTopology::TriangleList,
topology,
strip_index_format: None,
front_face: wgpu::FrontFace::Ccw,
cull_mode: Some(wgpu::Face::Back),
@ -402,7 +405,7 @@ impl State {
});
let obj_model =
resources::load_model("cube.obj", &device, &queue, &texture_bind_group_layout)
resources::load_model("floor.obj", &device, &queue, &texture_bind_group_layout)
.await
.unwrap();
@ -520,6 +523,7 @@ impl State {
hdr.format(),
Some(texture::Texture::DEPTH_FORMAT),
&[model::ModelVertex::desc(), InstanceRaw::desc()],
wgpu::PrimitiveTopology::TriangleList,
shader,
)
};
@ -540,6 +544,7 @@ impl State {
hdr.format(),
Some(texture::Texture::DEPTH_FORMAT),
&[model::ModelVertex::desc()],
wgpu::PrimitiveTopology::TriangleList,
shader,
)
};
@ -558,6 +563,7 @@ impl State {
hdr.format(),
Some(texture::Texture::DEPTH_FORMAT),
&[],
wgpu::PrimitiveTopology::TriangleList,
shader,
)
};
@ -592,6 +598,8 @@ impl State {
)
};
let debug = debug::Debug::new(&device, &camera_bind_group_layout, surface_format);
Ok(Self {
window,
surface,
@ -621,6 +629,7 @@ impl State {
hdr,
environment_bind_group,
sky_pipeline,
debug,
})
}
@ -760,6 +769,22 @@ impl State {
// Apply tonemapping
self.hdr.process(&mut encoder, &view);
{
let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("Debug"),
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
view: &view,
resolve_target: None,
ops: wgpu::Operations {
load: wgpu::LoadOp::Load,
store: true,
},
})],
depth_stencil_attachment: None,
});
self.debug.draw_axis(&mut pass, &self.camera_bind_group);
}
self.queue.submit(iter::once(encoder.finish()));
output.present();

@ -73,10 +73,12 @@ pub async fn load_model(
queue: &wgpu::Queue,
layout: &wgpu::BindGroupLayout,
) -> anyhow::Result<model::Model> {
println!("HERE++++++++++++++");
let obj_text = load_string(file_name).await?;
let obj_cursor = Cursor::new(obj_text);
let mut obj_reader = BufReader::new(obj_cursor);
println!("HERE++++++++++++++");
let (models, obj_materials) = tobj::load_obj_buf_async(
&mut obj_reader,
&tobj::LoadOptions {
@ -85,6 +87,7 @@ pub async fn load_model(
..Default::default()
},
|p| async move {
println!("mat: {p}");
let mat_text = load_string(&p).await.unwrap();
tobj::load_mtl_buf(&mut BufReader::new(Cursor::new(mat_text)))
},
@ -93,6 +96,7 @@ pub async fn load_model(
let mut materials = Vec::new();
for m in obj_materials? {
println!("Loading textures");
let diffuse_texture = load_texture(&m.diffuse_texture, false, device, queue).await?;
let normal_texture = load_texture(&m.normal_texture, true, device, queue).await?;

@ -125,24 +125,19 @@ fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
let view_dir = normalize(in.world_view_position - in.world_position);
let half_dir = normalize(view_dir + light_dir);
let diffuse_strength = max(dot(tangent_normal, light_dir), 0.0);
let diffuse_strength = max(dot(world_normal, light_dir), 0.0);
let diffuse_color = light.color * diffuse_strength;
let specular_strength = pow(max(dot(tangent_normal, half_dir), 0.0), 32.0);
let specular_strength = pow(max(dot(world_normal, half_dir), 0.0), 32.0);
let specular_color = specular_strength * light.color;
// NEW!
// Calculate reflections
let world_view = normalize(in.world_view_position - in.world_position);
let world_reflect = reflect(world_view, world_normal);
let env_ambient = textureSample(env_map, env_sampler, world_reflect * vec3(1.0, 1.0, -1.0)).rgb;
// let result = (env_ambient + diffuse_color + specular_color) * object_color.xyz;
// let result = env_ambient;
var result = in.world_normal;
if (in.clip_position.x > 400.0) {
result = world_normal;
}
let rev_view_dir = normalize(in.world_position - in.world_view_position);
let world_reflect = reflect(rev_view_dir, world_normal);
let env_ambient = textureSample(env_map, env_sampler, world_reflect).rgb;
let result = (env_ambient + diffuse_color + specular_color) * object_color.xyz;
return vec4<f32>(result, object_color.a);
}

@ -40,7 +40,7 @@ fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
let view_pos_homogeneous = camera.inv_proj * in.clip_position;
let view_ray_direction = view_pos_homogeneous.xyz / view_pos_homogeneous.w;
var ray_direction = normalize((camera.inv_view * vec4(view_ray_direction, 0.0)).xyz);
ray_direction.z *= -1.0;
// ray_direction.z *= -1.0;
// let sample = vec4(ray_direction, 1.0);
let sample = textureSample(env_map, env_sampler, ray_direction);

Loading…
Cancel
Save