From 22a58044d52ebda40683911f753a4c7e02f2b6f8 Mon Sep 17 00:00:00 2001 From: Chance Date: Sun, 27 Apr 2025 16:06:59 -0400 Subject: [PATCH] feat(render): basic normal-based lighting --- shaders/shader.frag | 9 +++++++++ shaders/shader.vert | 3 +++ 2 files changed, 12 insertions(+) diff --git a/shaders/shader.frag b/shaders/shader.frag index c9ffc77..72736f8 100644 --- a/shaders/shader.frag +++ b/shaders/shader.frag @@ -1,10 +1,19 @@ #version 450 layout(location = 0) in vec2 tex_coords; +layout(location = 1) in vec3 normal; layout(set = 1, binding = 0) uniform texture2D t_diffuse; layout(set = 1, binding = 1) uniform sampler s_diffuse; layout(location = 0) out vec4 out_color; // layout(group = 0, binding = 0) out texture2D; void main() { + float ambient = 0.2; + vec3 light_dir = normalize(vec3(0.5, 1.0, 0.5)); + float diffuse = clamp(dot(normalize(normal), light_dir), 0.0, 1.0); + float brightness = ambient + (1.0 - ambient) * diffuse; + out_color = texture(sampler2D(t_diffuse, s_diffuse), tex_coords); + out_color.r = clamp(out_color.r * 0.6 * brightness, 0.0, 1.0); + out_color.g = clamp(out_color.g * 0.6 * brightness, 0.0, 1.0); + out_color.b = clamp(out_color.b * 0.9 * brightness, 0.0, 1.0); } diff --git a/shaders/shader.vert b/shaders/shader.vert index 79c95f0..a773c50 100644 --- a/shaders/shader.vert +++ b/shaders/shader.vert @@ -2,14 +2,17 @@ layout(location = 0) in vec3 position; layout(location = 1) in vec3 color; +layout(location = 2) in vec3 normal_in; layout(location = 3) in vec2 tex_coords; layout(location = 0) out vec2 tex_coord; +layout(location = 1) out vec3 normal; layout(set = 0, binding = 0) uniform UniformBufferObject { mat4x4 projection; } view; void main() { gl_Position = view.projection * vec4(position, 1.0); tex_coord = tex_coords; + normal = normal_in; // gl_Position // out_color = color; }