gltf loading; world space lighting for now
This commit is contained in:
parent
db95acf998
commit
1a7a6707d9
10 changed files with 546 additions and 56 deletions
|
@ -10,7 +10,7 @@ var<uniform> camera: CameraUniform;
|
|||
|
||||
struct Light {
|
||||
position: vec3<f32>,
|
||||
color: vec3<f32>,
|
||||
color: vec4<f32>,
|
||||
}
|
||||
@group(2) @binding(0)
|
||||
var<uniform> light: Light;
|
||||
|
@ -36,6 +36,8 @@ struct VertexOutput {
|
|||
@location(1) tangent_position: vec3<f32>,
|
||||
@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
|
||||
|
@ -67,6 +69,10 @@ fn vs_main(
|
|||
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;
|
||||
|
||||
out.world_normal = world_normal.xyz;
|
||||
out.world_position = world_position.xyz;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
|
@ -82,6 +88,7 @@ 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> {
|
||||
let object_color: vec4<f32> = textureSample(t_diffuse, s_diffuse, in.tex_coords);
|
||||
|
@ -89,21 +96,30 @@ fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
|
|||
|
||||
// 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 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);
|
||||
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 = light.color * ambient_strength;
|
||||
let ambient_color = vec3(1.0) * ambient_strength;
|
||||
|
||||
// diffuse
|
||||
let diffuse_strength = max(dot(tangent_normal, light_dir), 0.0);
|
||||
let diffuse_color = light.color * diffuse_strength;
|
||||
// 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_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_color = specular_strength * light.color;
|
||||
// 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_color = specular_strength * light.color.xyz * light.color.w * light_attenuation;
|
||||
|
||||
let result = (ambient_color + diffuse_color + specular_color) * object_color.xyz;
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue