feat(rendering): add basic diffuse and ambient lighting to block rendering

This commit is contained in:
2026-06-16 13:53:47 +08:00
parent f4114c2699
commit f43ef64691
11 changed files with 146 additions and 21 deletions

View File

@@ -1,15 +1,35 @@
#version 460
in vec2 tc;
in vec3 normal;
in vec3 vert_pos;
flat in int tex_layer;
out vec4 color;
layout (binding = 0) uniform sampler2DArray samp;
uniform float ambientStrength;
uniform vec3 sunlightColor;
uniform vec3 sunlightDir;
void main(void) {
color = texture(samp, vec3(tc, tex_layer));
if (color.a < 0.8) {
vec4 objectColor = texture(samp, vec3(tc, tex_layer));
if (objectColor.a < 0.8) {
discard;
}
vec3 lightDir = normalize(sunlightDir);
vec3 ambient = ambientStrength * sunlightColor;
vec3 norm = normalize(normal);
float diff = max(dot(norm, lightDir), 0.0);
vec3 diffuse = diff * sunlightColor;
color = vec4((ambient + diffuse) * objectColor.rgb, objectColor.a);
//color = varyingColor;
}

View File

@@ -3,7 +3,10 @@
layout (location = 0) in vec3 pos;
layout (location = 1) in vec2 texCoord;
layout (location = 2) in float layer;
layout (location = 3) in vec3 aNormal;
out vec2 tc;
out vec3 normal;
out vec3 vert_pos;
flat out int tex_layer;
mat4 buildRotateX(float rad);
@@ -13,13 +16,21 @@ mat4 buildTranslate(float x, float y, float z);
uniform mat4 mv_matrix;
uniform mat4 proj_matrix;
uniform mat4 norm_matrix;
void main(void) {
gl_Position = proj_matrix * mv_matrix * vec4(pos, 1.0);
vec4 viewPos = mv_matrix * vec4(pos, 1.0);
vert_pos = viewPos.xyz;
tc = texCoord;
tex_layer = int(layer);
normal = mat3(norm_matrix) * aNormal;
gl_Position = proj_matrix * viewPos;
}
mat4 buildTranslate(float x, float y, float z) {