Surface normals
This commit is contained in:
parent
70336446ac
commit
306a983d14
|
@ -46,6 +46,11 @@ fn load_model() -> (MeshData, TextureData) {
|
|||
m.mesh.positions[t + 2],
|
||||
-m.mesh.positions[t + 1],
|
||||
],
|
||||
normal: [
|
||||
m.mesh.normals[t],
|
||||
m.mesh.normals[t + 2],
|
||||
-m.mesh.normals[t + 1],
|
||||
],
|
||||
tex_coords: [m.mesh.texcoords[i * 2], 1.0 - m.mesh.texcoords[i * 2 + 1]],
|
||||
});
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
#[derive(Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
|
||||
pub struct Vertex {
|
||||
pub position: [f32; 3],
|
||||
pub normal: [f32; 3],
|
||||
pub tex_coords: [f32; 2],
|
||||
}
|
||||
|
||||
|
@ -19,6 +20,11 @@ impl Vertex {
|
|||
wgpu::VertexAttribute {
|
||||
offset: std::mem::size_of::<[f32; 3]>() as wgpu::BufferAddress,
|
||||
shader_location: 1,
|
||||
format: wgpu::VertexFormat::Float32x3,
|
||||
},
|
||||
wgpu::VertexAttribute {
|
||||
offset: std::mem::size_of::<[f32; 6]>() as wgpu::BufferAddress,
|
||||
shader_location: 2,
|
||||
format: wgpu::VertexFormat::Float32x2,
|
||||
},
|
||||
],
|
||||
|
|
|
@ -159,7 +159,7 @@ impl Renderer {
|
|||
topology: wgpu::PrimitiveTopology::TriangleList,
|
||||
strip_index_format: None,
|
||||
front_face: wgpu::FrontFace::Ccw,
|
||||
cull_mode: None,
|
||||
cull_mode: Some(wgpu::Face::Back),
|
||||
polygon_mode: wgpu::PolygonMode::Fill,
|
||||
unclipped_depth: false,
|
||||
conservative: false,
|
||||
|
|
|
@ -22,13 +22,15 @@ struct PointLightData {
|
|||
|
||||
struct VertexInput {
|
||||
[[location(0)]] position: vec3<f32>;
|
||||
[[location(1)]] tex_coords: vec2<f32>;
|
||||
[[location(1)]] normal: vec3<f32>;
|
||||
[[location(2)]] tex_coords: vec2<f32>;
|
||||
};
|
||||
|
||||
struct VertexOutput {
|
||||
[[builtin(position)]] clip_position: vec4<f32>;
|
||||
[[location(0)]] position: vec3<f32>;
|
||||
[[location(1)]] tex_coords: vec2<f32>;
|
||||
[[location(1)]] normal: vec3<f32>;
|
||||
[[location(2)]] tex_coords: vec2<f32>;
|
||||
};
|
||||
|
||||
[[group(0), binding(0)]]
|
||||
|
@ -50,10 +52,12 @@ fn vs_main(
|
|||
) -> VertexOutput {
|
||||
let transform = meshes.instances[mesh_idx].transform;
|
||||
let world_pos = transform * vec4<f32>(vertex.position, 1.0);
|
||||
let world_normal = transform * vec4<f32>(vertex.normal, 0.0);
|
||||
|
||||
var out: VertexOutput;
|
||||
out.clip_position = camera.vp * world_pos;
|
||||
out.position = world_pos.xyz;
|
||||
out.normal = world_normal.xyz;
|
||||
out.tex_coords = vertex.tex_coords;
|
||||
return out;
|
||||
}
|
||||
|
@ -63,12 +67,14 @@ fn fs_main(
|
|||
frag: VertexOutput,
|
||||
) -> [[location(0)]] vec4<f32> {
|
||||
let albedo = textureSample(t_albedo, s_albedo, frag.tex_coords).rgb;
|
||||
let normal = normalize(frag.normal);
|
||||
|
||||
var lum = vec3<f32>(0.0);
|
||||
for(var i = 0; i < 4; i = i + 1) {
|
||||
let light = point_lights.lights[i];
|
||||
let light_relative = frag.position - light.center.xyz;
|
||||
let diffuse = albedo / dot(light_relative, light_relative);
|
||||
let light_relative = light.center.xyz - frag.position;
|
||||
let received = dot(normal, normalize(light_relative));
|
||||
let diffuse = (albedo * received) / dot(light_relative, light_relative);
|
||||
lum = lum + (diffuse * light.intensity.rgb);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue