aboutsummaryrefslogtreecommitdiff
path: root/client/shaders/shadow_shaders
diff options
context:
space:
mode:
authorLiso <anlismon@gmail.com>2021-06-06 18:51:21 +0200
committerGitHub <noreply@github.com>2021-06-06 18:51:21 +0200
commitc47313db65f968559711ac1b505ef341a9872017 (patch)
tree63d9b1b2be512918e2361d96e4fb52ff1ec3f9de /client/shaders/shadow_shaders
parent46f42e15c41cf4ab23c5ff4cd8a7d99d94d10d7b (diff)
downloadminetest-c47313db65f968559711ac1b505ef341a9872017.tar.gz
minetest-c47313db65f968559711ac1b505ef341a9872017.tar.bz2
minetest-c47313db65f968559711ac1b505ef341a9872017.zip
Shadow mapping render pass (#11244)
Co-authored-by: x2048 <codeforsmile@gmail.com>
Diffstat (limited to 'client/shaders/shadow_shaders')
-rw-r--r--client/shaders/shadow_shaders/pass1_fragment.glsl13
-rw-r--r--client/shaders/shadow_shaders/pass1_trans_fragment.glsl38
-rw-r--r--client/shaders/shadow_shaders/pass1_trans_vertex.glsl26
-rw-r--r--client/shaders/shadow_shaders/pass1_vertex.glsl26
-rw-r--r--client/shaders/shadow_shaders/pass2_fragment.glsl23
-rw-r--r--client/shaders/shadow_shaders/pass2_vertex.glsl9
6 files changed, 135 insertions, 0 deletions
diff --git a/client/shaders/shadow_shaders/pass1_fragment.glsl b/client/shaders/shadow_shaders/pass1_fragment.glsl
new file mode 100644
index 000000000..2105def96
--- /dev/null
+++ b/client/shaders/shadow_shaders/pass1_fragment.glsl
@@ -0,0 +1,13 @@
+uniform sampler2D ColorMapSampler;
+varying vec4 tPos;
+
+void main()
+{
+ vec4 col = texture2D(ColorMapSampler, gl_TexCoord[0].st);
+
+ if (col.a < 0.70)
+ discard;
+
+ float depth = 0.5 + tPos.z * 0.5;
+ gl_FragColor = vec4(depth, 0.0, 0.0, 1.0);
+}
diff --git a/client/shaders/shadow_shaders/pass1_trans_fragment.glsl b/client/shaders/shadow_shaders/pass1_trans_fragment.glsl
new file mode 100644
index 000000000..9f9e5be8c
--- /dev/null
+++ b/client/shaders/shadow_shaders/pass1_trans_fragment.glsl
@@ -0,0 +1,38 @@
+uniform sampler2D ColorMapSampler;
+varying vec4 tPos;
+
+#ifdef COLORED_SHADOWS
+// c_precision of 128 fits within 7 base-10 digits
+const float c_precision = 128.0;
+const float c_precisionp1 = c_precision + 1.0;
+
+float packColor(vec3 color)
+{
+ return floor(color.b * c_precision + 0.5)
+ + floor(color.g * c_precision + 0.5) * c_precisionp1
+ + floor(color.r * c_precision + 0.5) * c_precisionp1 * c_precisionp1;
+}
+
+const vec3 black = vec3(0.0);
+#endif
+
+void main()
+{
+ vec4 col = texture2D(ColorMapSampler, gl_TexCoord[0].st);
+#ifndef COLORED_SHADOWS
+ if (col.a < 0.5)
+ discard;
+#endif
+
+ float depth = 0.5 + tPos.z * 0.5;
+ // ToDo: Liso: Apply movement on waving plants
+ // depth in [0, 1] for texture
+
+ //col.rgb = col.a == 1.0 ? vec3(1.0) : col.rgb;
+#ifdef COLORED_SHADOWS
+ float packedColor = packColor(mix(col.rgb, black, col.a));
+ gl_FragColor = vec4(depth, packedColor, 0.0,1.0);
+#else
+ gl_FragColor = vec4(depth, 0.0, 0.0, 1.0);
+#endif
+}
diff --git a/client/shaders/shadow_shaders/pass1_trans_vertex.glsl b/client/shaders/shadow_shaders/pass1_trans_vertex.glsl
new file mode 100644
index 000000000..ca59f2796
--- /dev/null
+++ b/client/shaders/shadow_shaders/pass1_trans_vertex.glsl
@@ -0,0 +1,26 @@
+uniform mat4 LightMVP; // world matrix
+varying vec4 tPos;
+
+const float bias0 = 0.9;
+const float zPersFactor = 0.5;
+const float bias1 = 1.0 - bias0 + 1e-6;
+
+vec4 getPerspectiveFactor(in vec4 shadowPosition)
+{
+ float pDistance = length(shadowPosition.xy);
+ float pFactor = pDistance * bias0 + bias1;
+ shadowPosition.xyz *= vec3(vec2(1.0 / pFactor), zPersFactor);
+
+ return shadowPosition;
+}
+
+
+void main()
+{
+ vec4 pos = LightMVP * gl_Vertex;
+
+ tPos = getPerspectiveFactor(LightMVP * gl_Vertex);
+
+ gl_Position = vec4(tPos.xyz, 1.0);
+ gl_TexCoord[0].st = gl_MultiTexCoord0.st;
+}
diff --git a/client/shaders/shadow_shaders/pass1_vertex.glsl b/client/shaders/shadow_shaders/pass1_vertex.glsl
new file mode 100644
index 000000000..a6d8b3db8
--- /dev/null
+++ b/client/shaders/shadow_shaders/pass1_vertex.glsl
@@ -0,0 +1,26 @@
+uniform mat4 LightMVP; // world matrix
+varying vec4 tPos;
+
+const float bias0 = 0.9;
+const float zPersFactor = 0.5;
+const float bias1 = 1.0 - bias0 + 1e-6;
+
+vec4 getPerspectiveFactor(in vec4 shadowPosition)
+{
+ float pDistance = length(shadowPosition.xy);
+ float pFactor = pDistance * bias0 + bias1;
+ shadowPosition.xyz *= vec3(vec2(1.0 / pFactor), zPersFactor);
+
+ return shadowPosition;
+}
+
+
+void main()
+{
+ vec4 pos = LightMVP * gl_Vertex;
+
+ tPos = getPerspectiveFactor(pos);
+
+ gl_Position = vec4(tPos.xyz, 1.0);
+ gl_TexCoord[0].st = gl_MultiTexCoord0.st;
+}
diff --git a/client/shaders/shadow_shaders/pass2_fragment.glsl b/client/shaders/shadow_shaders/pass2_fragment.glsl
new file mode 100644
index 000000000..00b4f9f6c
--- /dev/null
+++ b/client/shaders/shadow_shaders/pass2_fragment.glsl
@@ -0,0 +1,23 @@
+uniform sampler2D ShadowMapClientMap;
+#ifdef COLORED_SHADOWS
+uniform sampler2D ShadowMapClientMapTraslucent;
+#endif
+uniform sampler2D ShadowMapSamplerdynamic;
+
+void main() {
+
+#ifdef COLORED_SHADOWS
+ vec2 first_depth = texture2D(ShadowMapClientMap, gl_TexCoord[0].st).rg;
+ vec2 depth_splitdynamics = vec2(texture2D(ShadowMapSamplerdynamic, gl_TexCoord[2].st).r, 0.0);
+ if (first_depth.r > depth_splitdynamics.r)
+ first_depth = depth_splitdynamics;
+ vec2 depth_color = texture2D(ShadowMapClientMapTraslucent, gl_TexCoord[1].st).rg;
+ gl_FragColor = vec4(first_depth.r, first_depth.g, depth_color.r, depth_color.g);
+#else
+ float first_depth = texture2D(ShadowMapClientMap, gl_TexCoord[0].st).r;
+ float depth_splitdynamics = texture2D(ShadowMapSamplerdynamic, gl_TexCoord[2].st).r;
+ first_depth = min(first_depth, depth_splitdynamics);
+ gl_FragColor = vec4(first_depth, 0.0, 0.0, 1.0);
+#endif
+
+}
diff --git a/client/shaders/shadow_shaders/pass2_vertex.glsl b/client/shaders/shadow_shaders/pass2_vertex.glsl
new file mode 100644
index 000000000..ac445c9c7
--- /dev/null
+++ b/client/shaders/shadow_shaders/pass2_vertex.glsl
@@ -0,0 +1,9 @@
+
+void main()
+{
+ vec4 uv = vec4(gl_Vertex.xyz, 1.0) * 0.5 + 0.5;
+ gl_TexCoord[0] = uv;
+ gl_TexCoord[1] = uv;
+ gl_TexCoord[2] = uv;
+ gl_Position = vec4(gl_Vertex.xyz, 1.0);
+}