improve shadows

This commit is contained in:
MihailRis
2025-05-21 21:41:01 +03:00
parent 44f7051a94
commit ed5efd9c3a
5 changed files with 68 additions and 65 deletions
+14 -17
View File
@@ -16,24 +16,21 @@ vec4 effect() {
vec3 bitangent = cross(normal, tangent);
mat3 tbn = mat3(tangent, bitangent, normal);
float occlusion = 1.0;
if (u_enableShadows) {
occlusion = 0.0;
for (int i = 0; i < kernelSize; i++) {
vec3 samplePos = tbn * u_ssaoSamples[i];
samplePos = position + samplePos * radius;
vec4 offset = vec4(samplePos, 1.0);
offset = u_projection * offset;
offset.xyz /= offset.w;
offset.xyz = offset.xyz * 0.5 + 0.5;
float sampleDepth = texture(u_position, offset.xy).z;
float rangeCheck = smoothstep(0.0, 1.0, radius / abs(position.z - sampleDepth));
occlusion += (sampleDepth >= samplePos.z + bias ? 1.0 : 0.0) * rangeCheck;
}
occlusion = min(1.0, 1.05 - (occlusion / kernelSize));
float occlusion = 0.0;
for (int i = 0; i < kernelSize; i++) {
vec3 samplePos = tbn * u_ssaoSamples[i];
samplePos = position + samplePos * radius;
vec4 offset = vec4(samplePos, 1.0);
offset = u_projection * offset;
offset.xyz /= offset.w;
offset.xyz = offset.xyz * 0.5 + 0.5;
float sampleDepth = texture(u_position, offset.xy).z;
float rangeCheck = smoothstep(0.0, 1.0, radius / abs(position.z - sampleDepth));
occlusion += (sampleDepth >= samplePos.z + bias ? 1.0 : 0.0) * rangeCheck;
}
occlusion = min(1.0, 1.05 - (occlusion / kernelSize));
float z = -position.z * 0.02;
z = max(0.0, 1.0 - z);
+32 -18
View File
@@ -1,22 +1,21 @@
#ifndef SHADOWS_GLSL_
#define SHADOWS_GLSL_
uniform sampler2DShadow u_shadows;
uniform sampler2DShadow u_wideShadows;
uniform sampler2DShadow u_shadows[2];
uniform mat4 u_shadowsMatrix[2];
uniform int u_shadowsRes;
uniform bool u_blurShadows;
uniform mat4 u_shadowsMatrix;
uniform mat4 u_wideShadowsMatrix;
float calc_shadow() {
float shadow = 1.0;
if (u_enableShadows) {
vec4 mpos = u_shadowsMatrix * vec4(a_modelpos.xyz + a_realnormal * 0.04, 1.0);
vec4 mpos = u_shadowsMatrix[0] * vec4(a_modelpos.xyz + a_realnormal * 0.04, 1.0);
vec3 projCoords = mpos.xyz / mpos.w;
projCoords = projCoords * 0.5 + 0.5;
projCoords.z -= 0.0001;
vec4 wmpos = u_wideShadowsMatrix * vec4(a_modelpos.xyz + a_realnormal * 0.2, 1.0);
vec4 wmpos = u_shadowsMatrix[1] * vec4(a_modelpos.xyz + a_realnormal * 0.2, 1.0);
vec3 wprojCoords = wmpos.xyz / wmpos.w;
wprojCoords = wprojCoords * 0.5 + 0.5;
wprojCoords.z -= 0.0001;
@@ -24,22 +23,37 @@ float calc_shadow() {
shadow = 0.0;
if (dot(a_realnormal, u_sunDir) < 0.0) {
if (u_blurShadows) {
const vec3 offsets[4] = vec3[4](
vec3(0.5, 0.5, 0.0),
vec3(-0.5, 0.5, 0.0),
vec3(0.5, -0.5, 0.0),
vec3(-0.5, -0.5, 0.0)
);
for (int i = 0; i < 4; i++) {
shadow += texture(u_shadows, projCoords.xyz + offsets[i] / u_shadowsRes);
if (u_blurShadows || true) {
if (a_distance > 64) {
for (int y = -1; y <= 1; y++) {
for (int x = -1; x <= 1; x++) {
shadow += texture(
u_shadows[1],
wprojCoords.xyz +
vec3(x, y, -(abs(x) + abs(y))) /
u_shadowsRes
);
}
}
shadow /= 9;
} else {
for (int y = -2; y <= 2; y++) {
for (int x = -2; x <= 2; x++) {
shadow += texture(
u_shadows[0],
projCoords.xyz +
vec3(x, y, -(abs(x) + abs(y))) /
u_shadowsRes * 1.0
);
}
}
shadow /= 25;
}
shadow /= 4;
} else {
if (a_distance > 32.0) {
shadow = texture(u_wideShadows, wprojCoords.xyz);
shadow = texture(u_shadows[1], wprojCoords.xyz);
} else {
shadow = texture(u_shadows, projCoords.xyz);
shadow = texture(u_shadows[0], projCoords.xyz);
}
}
shadow = shadow * 0.5 + 0.5;