broken normal maps
This commit is contained in:
parent
89789d5f4a
commit
19c5434d0f
7 changed files with 193 additions and 60 deletions
|
@ -1,10 +1,3 @@
|
|||
struct InstanceInput {
|
||||
@location(5) model_matrix_0: vec4<f32>,
|
||||
@location(6) model_matrix_1: vec4<f32>,
|
||||
@location(7) model_matrix_2: vec4<f32>,
|
||||
@location(8) model_matrix_3: vec4<f32>,
|
||||
}
|
||||
|
||||
// Vertex shader
|
||||
|
||||
struct CameraUniform {
|
||||
|
@ -26,13 +19,23 @@ struct VertexInput {
|
|||
@location(0) position: vec3<f32>,
|
||||
@location(1) tex_coords: vec2<f32>,
|
||||
@location(2) normal: vec3<f32>,
|
||||
@location(3) tangent: vec3<f32>,
|
||||
@location(4) bitangent: vec3<f32>,
|
||||
}
|
||||
|
||||
struct InstanceInput {
|
||||
@location(5) model_matrix_0: vec4<f32>,
|
||||
@location(6) model_matrix_1: vec4<f32>,
|
||||
@location(7) model_matrix_2: vec4<f32>,
|
||||
@location(8) model_matrix_3: vec4<f32>,
|
||||
}
|
||||
|
||||
struct VertexOutput {
|
||||
@builtin(position) clip_position: vec4<f32>,
|
||||
@location(0) tex_coords: vec2<f32>,
|
||||
@location(1) world_normal: vec3<f32>,
|
||||
@location(2) world_position: vec3<f32>,
|
||||
@location(1) tangent_position: vec3<f32>,
|
||||
@location(2) tangent_light_position: vec3<f32>,
|
||||
@location(3) tangent_view_position: vec3<f32>,
|
||||
}
|
||||
|
||||
@vertex
|
||||
|
@ -47,17 +50,23 @@ fn vs_main(
|
|||
instance.model_matrix_3,
|
||||
);
|
||||
|
||||
let world_normal = normalize((model_matrix * vec4<f32>(model.normal, 0.0)).xyz);
|
||||
let world_tangent = normalize((model_matrix * vec4<f32>(model.tangent, 0.0)).xyz);
|
||||
let world_bitangent = normalize((model_matrix * vec4<f32>(model.bitangent, 0.0)).xyz);
|
||||
let world_position = model_matrix * vec4<f32>(model.position, 1.0);
|
||||
|
||||
let tangent_matrix = transpose(mat3x3<f32>(
|
||||
world_tangent,
|
||||
world_bitangent,
|
||||
world_normal,
|
||||
));
|
||||
|
||||
var out: VertexOutput;
|
||||
|
||||
out.tex_coords = model.tex_coords;
|
||||
|
||||
out.world_normal = normalize((model_matrix * vec4<f32>(model.normal, 0.0)).xyz);
|
||||
|
||||
var world_position: vec4<f32> = model_matrix * vec4<f32>(model.position, 1.0);
|
||||
out.world_position = world_position.xyz;
|
||||
|
||||
out.clip_position = camera.proj * camera.view * world_position;
|
||||
|
||||
out.tex_coords = model.tex_coords;
|
||||
out.tangent_position = tangent_matrix * world_position.xyz;
|
||||
out.tangent_light_position = tangent_matrix * light.position;
|
||||
out.tangent_view_position = tangent_matrix * camera.position.xyz;
|
||||
return out;
|
||||
}
|
||||
|
||||
|
@ -68,24 +77,32 @@ var t_diffuse: texture_2d<f32>;
|
|||
@group(0)@binding(1)
|
||||
var s_diffuse: sampler;
|
||||
|
||||
@group(0)@binding(2)
|
||||
var t_normal: texture_2d<f32>;
|
||||
@group(0) @binding(3)
|
||||
var s_normal: sampler;
|
||||
|
||||
@fragment
|
||||
fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
|
||||
let object_color: vec4<f32> = textureSample(t_diffuse, s_diffuse, in.tex_coords);
|
||||
let object_normal: vec4<f32> = textureSample(t_normal, s_normal, in.tex_coords);
|
||||
|
||||
let light_dir = normalize(light.position - in.world_position);
|
||||
// lighting vecs
|
||||
let tangent_normal = object_normal.xyz * 2.0 - 1.0;
|
||||
let light_dir = normalize(in.tangent_light_position - in.tangent_position);
|
||||
let view_dir = normalize(in.tangent_view_position - in.tangent_position);
|
||||
let half_dir = normalize(view_dir + light_dir);
|
||||
|
||||
// ambient
|
||||
let ambient_strength = 0.05;
|
||||
let ambient_color = light.color * ambient_strength;
|
||||
|
||||
// diffuse
|
||||
let diffuse_strength = max(dot(in.world_normal, light_dir), 0.0);
|
||||
let diffuse_strength = max(dot(tangent_normal, light_dir), 0.0);
|
||||
let diffuse_color = light.color * diffuse_strength;
|
||||
|
||||
// specular
|
||||
let view_dir = normalize(camera.position.xyz - in.world_position);
|
||||
let half_dir = normalize(view_dir + light_dir);
|
||||
let specular_strength = pow(max(dot(in.world_normal, half_dir), 0.0), 32.0);
|
||||
let specular_strength = pow(max(dot(tangent_normal, half_dir), 0.0), 32.0);
|
||||
let specular_color = specular_strength * light.color;
|
||||
|
||||
let result = (ambient_color + diffuse_color + specular_color) * object_color.xyz;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue