fix tangent space lighting

This commit is contained in:
Lauri Räsänen 2023-01-24 02:21:20 +02:00
parent a17a751fc4
commit de50520b02
4 changed files with 99 additions and 213 deletions

View file

@ -28,6 +28,9 @@ struct InstanceInput {
@location(6) model_matrix_1: vec4<f32>,
@location(7) model_matrix_2: vec4<f32>,
@location(8) model_matrix_3: vec4<f32>,
@location(9) normal_matrix_0: vec3<f32>,
@location(10) normal_matrix_1: vec3<f32>,
@location(11) normal_matrix_2: vec3<f32>,
}
struct VertexOutput {
@ -37,7 +40,6 @@ struct VertexOutput {
@location(2) tangent_light_position: vec3<f32>,
@location(3) tangent_view_position: vec3<f32>,
@location(4) world_position: vec3<f32>,
@location(5) world_normal: vec3<f32>,
}
@vertex
@ -51,18 +53,23 @@ fn vs_main(
instance.model_matrix_2,
instance.model_matrix_3,
);
let normal_matrix = mat3x3<f32>(
instance.normal_matrix_0,
instance.normal_matrix_1,
instance.normal_matrix_2,
);
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 world_normal = normalize(normal_matrix * model.normal);
let world_tangent = normalize(normal_matrix * model.tangent);
let world_bitangent = normalize(normal_matrix * model.bitangent);
let tangent_matrix = transpose(mat3x3<f32>(
world_tangent,
world_bitangent,
world_normal,
));
let world_position = model_matrix * vec4<f32>(model.position, 1.0);
var out: VertexOutput;
out.clip_position = camera.proj * camera.view * world_position;
out.tex_coords = model.tex_coords;
@ -70,7 +77,6 @@ fn vs_main(
out.tangent_light_position = tangent_matrix * light.position;
out.tangent_view_position = tangent_matrix * camera.position.xyz;
out.world_normal = world_normal.xyz;
out.world_position = world_position.xyz;
return out;
@ -88,39 +94,37 @@ var t_normal: texture_2d<f32>;
@group(0) @binding(3)
var s_normal: sampler;
// TODO: fix using tangent space and normal texture instead of world
@fragment
fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
// textures
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);
// lighting vecs
let tangent_normal = object_normal.xyz * 2.0 - 1.0;
// let light_dir = normalize(in.tangent_light_position - in.tangent_position);
var light_dir = light.position - in.world_position;
let light_dist = length(light_dir);
light_dir = normalize(light_dir);
var 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);
// attenuation
let light_dist = length(light.position - in.world_position);
let coef_a = 0.0;
let coef_b = 1.25;
let light_attenuation = 1.0 / (1.0 + coef_a * light_dist + coef_b * light_dist * light_dist);
// let view_dir = normalize(in.tangent_view_position - in.tangent_position);
let view_dir = normalize(camera.position.xyz - in.world_position);
let half_dir = normalize(view_dir + light_dir);
// ambient
let ambient_strength = 0.025;
let ambient_color = vec3(1.0) * ambient_strength;
// diffuse
// let diffuse_strength = max(dot(tangent_normal, light_dir), 0.0);
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 = diffuse_strength * light.color.xyz * light.color.w * light_attenuation;
// specular
// let specular_strength = pow(max(dot(tangent_normal, half_dir), 0.0), 32.0);
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.xyz * light.color.w * light_attenuation;
// ambient
let ambient_light_color = vec3(1.0);
let ambient_strength = 0.025;
let ambient_color = ambient_light_color * ambient_strength;
let result = (ambient_color + diffuse_color + specular_color) * object_color.xyz;
return vec4<f32>(result, object_color.a);