feat: deferred shading (day 2)

This commit is contained in:
MihailRis
2025-07-08 23:56:22 +03:00
parent a585b52aa2
commit 0bd901d1a6
23 changed files with 236 additions and 92 deletions
+4 -3
View File
@@ -3,11 +3,12 @@ layout (location = 0) out vec4 f_color;
layout (location = 1) out vec4 f_position;
layout (location = 2) out vec4 f_normal;
uniform mat4 u_view;
uniform samplerCube u_skybox;
void main(){
vec3 dir = normalize(v_coord);
f_position = vec4(0.0, 0.0, -1e9, 1.0);
f_normal = vec4(0.0);
vec3 dir = normalize(v_coord) * 1e6;
f_position = u_view * vec4(dir, 1.0);
f_normal = vec4(0.0, 0.0, 1.0, 1.0);
f_color = texture(u_skybox, dir);
}
+2 -2
View File
@@ -7,6 +7,6 @@ uniform float u_ar;
uniform float u_zoom;
void main(){
v_coord = (vec4(v_position*vec2(u_ar, 1.0f)*u_zoom, -1.0, 1.0) * u_view).xyz;
gl_Position = vec4(v_position, 0.0, 1.0);
v_coord = (vec4(v_position * vec2(u_ar, 1.0f) * u_zoom, -1.0, 1.0) * u_view).xyz;
gl_Position = vec4(v_position, 1.0 - 1e-6, 1.0);
}
+5 -1
View File
@@ -6,13 +6,17 @@ uniform sampler2D u_position;
uniform sampler2D u_normal;
uniform sampler2D u_noise;
uniform sampler2D u_ssao;
uniform sampler2D u_shadows;
uniform samplerCube u_skybox;
uniform ivec2 u_screenSize;
uniform float u_intensity;
uniform float u_timer;
uniform bool u_enableShadows;
uniform mat4 u_projection;
uniform mat4 u_view;
uniform mat4 u_inverseView;
uniform vec3 u_sunDir;
uniform vec3 u_cameraPos;
#include <__effect__>
+15 -6
View File
@@ -1,15 +1,24 @@
#include <shadows>
#include <fog>
vec4 effect() {
float ssao = 0.0;
float z = texture(u_position, v_uv).z;
vec4 pos = texture(u_position, v_uv);
float z = pos.z;
for (int y = -2; y <= 2; y++) {
for (int x = -2; x <= 2; x++) {
vec2 offset = vec2(x, y) / u_screenSize;
if (abs(z - texture(u_position, v_uv + offset * 2.0).z) > 0.05)
ssao += 1.0;
else
ssao += texture(u_ssao, v_uv + offset * 2.0).r;
ssao += texture(u_ssao, v_uv + offset * 2.0).r;
}
}
ssao /= 24.0;
return vec4(texture(u_screen, v_uv).rgb * mix(1.0, ssao, 1.0), 1.0);
vec4 modelpos = u_inverseView * pos;
vec3 normal = transpose(mat3(u_view)) * texture(u_normal, v_uv).xyz;
vec3 dir = modelpos.xyz - u_cameraPos;
#ifdef ENABLE_SHADOWS
ssao *= calc_shadow(modelpos, normal, length(pos));
#endif
vec3 fogColor = texture(u_skybox, dir).rgb;
float fog = calc_fog(length(u_view * vec4((modelpos.xyz - u_cameraPos) * FOG_POS_SCALE, 0.0)) / 256.0);
return vec4(mix(texture(u_screen, v_uv).rgb * mix(1.0, ssao, 1.0), fogColor, fog), 1.0);
}
+1 -1
View File
@@ -23,7 +23,7 @@ uniform vec3 u_sunDir;
#include <shadows>
void main() {
float shadow = calc_shadow();
float shadow = calc_shadow(a_modelpos, a_realnormal, a_distance);
vec3 fogColor = texture(u_skybox, a_dir).rgb;
vec4 tex_color = texture(u_texture0, a_texCoord);
float alpha = a_color.a * tex_color.a;
+1
View File
@@ -29,6 +29,7 @@ uniform vec3 u_torchlightColor;
uniform float u_torchlightDistance;
#include <lighting>
#include <fog>
void main() {
a_modelpos = u_model * vec4(v_position, 1.0);
+19
View File
@@ -0,0 +1,19 @@
#ifndef FOG_GLSL_
#define FOG_GLSL_
uniform float u_fogFactor;
uniform float u_fogCurve;
uniform float u_weatherFogOpacity;
uniform float u_weatherFogDencity;
uniform float u_weatherFogCurve;
float calc_fog(float depth) {
return min(
1.0,
max(pow(depth * u_fogFactor, u_fogCurve),
min(pow(depth * u_weatherFogDencity, u_weatherFogCurve),
u_weatherFogOpacity))
);
}
#endif // FOG_GLSL_
-15
View File
@@ -1,12 +1,6 @@
#ifndef LIGHTING_GLSL_
#define LIGHTING_GLSL_
uniform float u_fogFactor;
uniform float u_fogCurve;
uniform float u_weatherFogOpacity;
uniform float u_weatherFogDencity;
uniform float u_weatherFogCurve;
float calc_torch_light(vec3 normal, vec3 modelpos) {
return max(0.0, 1.0 - distance(u_cameraPos, modelpos) / u_torchlightDistance)
* max(0.0, -dot(normal, normalize(modelpos - u_cameraPos)));
@@ -16,13 +10,4 @@ vec3 calc_screen_normal(vec3 normal) {
return transpose(inverse(mat3(u_view * u_model))) * normal;
}
float calc_fog(float depth) {
return min(
1.0,
max(pow(depth * u_fogFactor, u_fogCurve),
min(pow(depth * u_weatherFogDencity, u_weatherFogCurve),
u_weatherFogOpacity))
);
}
#endif // LIGHTING_GLSL_
+5 -5
View File
@@ -10,14 +10,14 @@ uniform int u_shadowsRes;
uniform float u_shadowsOpacity;
uniform float u_shadowsSoftness;
float calc_shadow() {
float calc_shadow(vec4 modelPos, vec3 realnormal, float distance) {
#ifdef ENABLE_SHADOWS
float step = 1.0 / float(u_shadowsRes);
float s = pow(abs(cos(u_dayTime * PI2)), 0.25) * u_shadowsOpacity;
vec3 normalOffset = a_realnormal * (a_distance > 64.0 ? 0.2 : 0.04);
int shadowIdx = a_distance > 80.0 ? 1 : 0;
vec3 normalOffset = realnormal * (distance > 64.0 ? 0.2 : 0.04);
int shadowIdx = distance > 80.0 ? 1 : 0;
vec4 mpos = u_shadowsMatrix[shadowIdx] * vec4(a_modelpos.xyz + normalOffset, 1.0);
vec4 mpos = u_shadowsMatrix[shadowIdx] * vec4(modelPos.xyz + normalOffset, 1.0);
vec3 projCoords = mpos.xyz / mpos.w;
projCoords = projCoords * 0.5 + 0.5;
projCoords.z -= 0.00001 / u_shadowsRes;
@@ -26,7 +26,7 @@ float calc_shadow() {
}
float shadow = 0.0;
if (dot(a_realnormal, u_sunDir) < 0.0) {
if (dot(realnormal, u_sunDir) < 0.0) {
// 3x3 kernel
for (int y = -1; y <= 1; y++) {
for (int x = -1; x <= 1; x++) {
+3 -4
View File
@@ -25,8 +25,7 @@ uniform bool u_debugNormals;
#include <shadows>
void main() {
float shadow = calc_shadow();
vec3 fogColor = texture(u_skybox, a_dir).rgb;
//vec3 fogColor = texture(u_skybox, a_dir).rgb;
vec4 texColor = texture(u_texture0, a_texCoord);
float alpha = texColor.a;
if (u_alphaClip) {
@@ -43,8 +42,8 @@ void main() {
texColor.rgb *= a_normal * 0.5 + 0.5;
}
f_color = texColor;
f_color.rgb *= min(vec3(1.0), a_torchLight.rgb + a_skyLight * shadow);
f_color = mix(f_color, vec4(fogColor, 1.0), a_fog);
f_color.rgb *= min(vec3(1.0), a_torchLight.rgb + a_skyLight);
//f_color = mix(f_color, vec4(fogColor, 1.0), a_fog * 0.0);
f_color.a = alpha;
f_position = vec4(a_position, 1.0);
f_normal = vec4(a_normal, 1.0);
+1
View File
@@ -28,6 +28,7 @@ uniform vec3 u_torchlightColor;
uniform float u_torchlightDistance;
#include <lighting>
#include <fog>
void main() {
a_modelpos = u_model * vec4(v_position, 1.0f);