feat(shaders): add lighting to block accumulation OIT shaders

This commit is contained in:
2026-06-19 12:42:56 +08:00
parent ca3fc5e3bf
commit 738f56831a
3 changed files with 101 additions and 3 deletions

View File

@@ -4,10 +4,21 @@ layout (location = 0) out vec4 accum;
layout (location = 1) out float reveal;
in vec2 tc;
in vec3 normal;
in vec3 vert_pos;
in float roughness;
flat in int tex_layer;
in float v_depth;
layout (binding = 0) uniform sampler2DArray samp;
uniform float ambientStrength;
uniform vec3 sunlightColor;
uniform vec3 ambientColor;
uniform vec3 sunlightDir;
uniform vec3 cameraPos;
uniform bool shader_on;
uniform float specularStrength;
float weight(float z, float a) {
float intermediate = 0.03 / (1e-5 + pow(z / 200.0, 4.0));
@@ -15,7 +26,71 @@ float weight(float z, float a) {
}
void main() {
vec4 color = texture(samp, vec3(tc, tex_layer));
vec4 objectColor = texture(samp, vec3(tc, tex_layer));
if (!shader_on) {
vec4 color = objectColor;
float alpha = color.a;
if (alpha < 1e-4) discard;
float w = weight(v_depth, alpha);
accum = vec4(color.rgb * alpha * w, alpha * w);
reveal = alpha;
return;
}
vec3 lightDir = normalize(-sunlightDir);
vec3 norm = normalize(normal);
vec3 V =
normalize(cameraPos - vert_pos);
vec3 H =
normalize(lightDir + V);
vec3 ambient = ambientStrength * ambientColor;
float diff = max(dot(norm, lightDir), 0.0);
vec3 diffuse = diff * sunlightColor;
float r =
clamp(roughness, 0.0, 1.0);
float shininess =
mix(
512.0,
4.0,
r
);
float ks =
mix(
0.8,
0.02,
r
);
float spec = 0.0;
if(diff > 0.0)
{
spec =
ks *
pow(
max(dot(norm,H),0.0),
shininess
);
}
vec3 specular = spec * sunlightColor * specularStrength;
vec4 color = vec4((ambient + diffuse) * objectColor.rgb + specular, objectColor.a);
float alpha = color.a;
if (alpha < 1e-4) discard;

View File

@@ -3,19 +3,27 @@
layout (location = 0) in vec3 pos;
layout (location = 1) in vec2 texCoord;
layout (location = 2) in float layer;
layout (location = 3) in vec3 aNormal;
layout (location = 4) in float Roughness;
out vec2 tc;
out vec3 normal;
out vec3 vert_pos;
flat out int tex_layer;
out float roughness;
out float v_depth;
uniform mat4 mv_matrix;
uniform mat4 proj_matrix;
uniform mat4 norm_matrix;
void main(void) {
vec4 view_pos = mv_matrix * vec4(pos, 1.0);
gl_Position = proj_matrix * view_pos;
vert_pos = pos;
roughness = Roughness;
tc = texCoord;
tex_layer = int(layer);
v_depth = -view_pos.z;
gl_Position = proj_matrix * view_pos;
}

View File

@@ -805,6 +805,21 @@ void Renderer::render_world() {
glUniformMatrix4fv(mv_loc, 1, GL_FALSE, glm::value_ptr(m_mv_mat));
glUniformMatrix4fv(proj_loc, 1, GL_FALSE, glm::value_ptr(m_p_mat));
glUniformMatrix4fv(accum_shader.loc("norm_matrix"), 1, GL_FALSE,
glm::value_ptr(m_norm_mat));
glUniformMatrix4fv(accum_shader.loc("lightSpaceMatrix"), 1, GL_FALSE,
glm::value_ptr(light_space_matrix));
glUniform1f(accum_shader.loc("ambientStrength"), m_ambient_strength);
glUniform3fv(accum_shader.loc("sunlightColor"), 1,
glm::value_ptr(m_parallel_light.directional_light_color));
glUniform3fv(accum_shader.loc("ambientColor"), 1,
glm::value_ptr(m_parallel_light.finnal_ambient_color));
glUniform3fv(accum_shader.loc("sunlightDir"), 1,
glm::value_ptr(light_dir_view));
glUniform1i(accum_shader.loc("shader_on"), m_shader_on);
glUniform1f(accum_shader.loc("specularStrength"), m_specular_strength);
glUniform3fv(accum_shader.loc("cameraPos"), 1,
glm::value_ptr(m_camera.get_camera_pos()));
glBindFramebuffer(GL_FRAMEBUFFER, m_oit_fbo);