armory/Shaders/std/ssrs.glsl

37 lines
970 B
Plaintext
Raw Normal View History

2019-05-30 20:22:57 +02:00
#ifndef _SSRS_GLSL_
#define _SSRS_GLSL_
2017-12-15 12:52:58 +01:00
#include "std/gbuffer.glsl"
2017-07-05 23:26:13 +02:00
uniform mat4 VP;
vec2 getProjectedCoord(vec3 hitCoord) {
vec4 projectedCoord = VP * vec4(hitCoord, 1.0);
projectedCoord.xy /= projectedCoord.w;
projectedCoord.xy = projectedCoord.xy * 0.5 + 0.5;
2020-05-11 09:03:13 +02:00
#if defined(HLSL) || defined(METAL)
2017-07-05 23:26:13 +02:00
projectedCoord.y = 1.0 - projectedCoord.y;
#endif
return projectedCoord.xy;
}
2018-11-22 18:07:32 +01:00
float getDeltaDepth(vec3 hitCoord, sampler2D gbufferD, mat4 invVP, vec3 eye) {
2017-07-05 23:26:13 +02:00
vec2 texCoord = getProjectedCoord(hitCoord);
2018-12-06 15:23:08 +01:00
float depth = textureLod(gbufferD, texCoord, 0.0).r * 2.0 - 1.0;
2017-07-05 23:26:13 +02:00
vec3 wpos = getPos2(invVP, depth, texCoord);
float d1 = length(eye - wpos);
float d2 = length(eye - hitCoord);
return d1 - d2;
}
2018-11-22 18:07:32 +01:00
float traceShadowSS(vec3 dir, vec3 hitCoord, sampler2D gbufferD, mat4 invVP, vec3 eye) {
2017-07-05 23:26:13 +02:00
dir *= ssrsRayStep;
2019-05-30 20:22:57 +02:00
for (int i = 0; i < 4; i++) {
2017-07-05 23:26:13 +02:00
hitCoord += dir;
2019-05-30 20:22:57 +02:00
if (getDeltaDepth(hitCoord, gbufferD, invVP, eye) > 0.0) return 1.0;
}
return 0.0;
2017-07-05 23:26:13 +02:00
}
2019-05-30 20:22:57 +02:00
#endif