Skip to content
Snippets Groups Projects
Commit fea731ec authored by David Parks's avatar David Parks
Browse files

SL-19023 Reformat SSR shaders to match style standard.

parent a851aa83
No related branches found
No related tags found
No related merge requests found
......@@ -58,8 +58,12 @@ vec4 getPositionWithDepth(vec2 pos_screen, float depth);
vec4 getPosition(vec2 pos_screen);
vec4 getNormalEnvIntensityFlags(vec2 screenpos, out vec3 n, out float envIntensity);
bool traceScreenRay(vec3 position, vec3 reflection, out vec4 hitColor, out float hitDepth, float depth, sampler2D textureFrame);
float random (vec2 uv);
void main() {
void main()
{
vec2 tc = vary_fragcoord.xy;
float depth = linearDepth01(getDepth(tc), zNear, zFar);
vec3 n = vec3(0, 0, 1);
......@@ -74,7 +78,8 @@ void main() {
vec4 diffuse = texture2D(diffuseRect, tc);
vec3 specCol = spec.rgb;
if (GET_GBUFFER_FLAG(GBUFFER_FLAG_HAS_PBR)) {
if (GET_GBUFFER_FLAG(GBUFFER_FLAG_HAS_PBR))
{
vec3 orm = specCol.rgb;
float perceptualRoughness = orm.g;
float metallic = orm.b;
......@@ -86,12 +91,12 @@ void main() {
specCol = mix(f0, baseColor.rgb, metallic);
}
vec2 uv2 = tc * screen_res;
float c = (uv2.x + uv2.y) * 0.125;
float jitter = mod( c, 1.0);
vec2 uv2 = tc * screen_res;
float c = (uv2.x + uv2.y) * 0.125;
float jitter = mod( c, 1.0);
vec3 firstBasis = normalize(cross(vec3(1.f, 1.f, 1.f), rayDirection));
vec3 secondBasis = normalize(cross(rayDirection, firstBasis));
vec3 secondBasis = normalize(cross(rayDirection, firstBasis));
frag_color = texture(diffuseMap, tc);
vec4 collectedColor;
......@@ -100,17 +105,22 @@ void main() {
float vignette = clamp((screenpos.x * screenpos.y) * 16,0, 1);
vignette *= clamp((dot(normalize(viewPos), n) * 0.5 + 0.5 - 0.2) * 8, 0, 1);
vignette *= min(linearDepth(getDepth(tc), zNear, zFar) / zFar, 1);
int totalSamples = 4;
for (int i = 0; i < totalSamples; i++) {
vec2 coeffs = vec2(random(tc + vec2(0, i)) + random(tc + vec2(i, 0)));
vec3 reflectionDirectionRandomized = rayDirection + firstBasis * coeffs.x + secondBasis * coeffs.y;
for (int i = 0; i < totalSamples; i++)
{
vec2 coeffs = vec2(random(tc + vec2(0, i)) + random(tc + vec2(i, 0)));
vec3 reflectionDirectionRandomized = rayDirection + firstBasis * coeffs.x + secondBasis * coeffs.y;
bool hit = traceScreenRay(pos, reflectionDirectionRandomized, hitpoint, depth, depth, diffuseMap);
if (hit) {
if (hit)
{
collectedColor += hitpoint;
collectedColor.rgb *= specCol.rgb;
}
}
}
collectedColor *= vignette;
......
......@@ -37,17 +37,19 @@ float linearDepth(float depth, float near, float far);
float getDepth(vec2 pos_screen);
float linearDepth01(float d, float znear, float zfar);
float random (vec2 uv) {
return fract(sin(dot(uv, vec2(12.9898, 78.233))) * 43758.5453123); //simple random function
float random (vec2 uv)
{
return fract(sin(dot(uv, vec2(12.9898, 78.233))) * 43758.5453123); //simple random function
}
// Based off of https://github.com/RoundedGlint585/ScreenSpaceReflection/
// A few tweaks here and there to suit our needs.
vec2 generateProjectedPosition(vec3 pos){
vec4 samplePosition = projection_matrix * vec4(pos, 1.f);
samplePosition.xy = (samplePosition.xy / samplePosition.w) * 0.5 + 0.5;
return samplePosition.xy;
vec2 generateProjectedPosition(vec3 pos)
{
vec4 samplePosition = projection_matrix * vec4(pos, 1.f);
samplePosition.xy = (samplePosition.xy / samplePosition.w) * 0.5 + 0.5;
return samplePosition.xy;
}
bool isBinarySearchEnabled = true;
......@@ -60,79 +62,91 @@ float distanceBias = 0.02;
float depthRejectBias = 0.001;
float epsilon = 0.1;
bool traceScreenRay(vec3 position, vec3 reflection, out vec4 hitColor, out float hitDepth, float depth, sampler2D textureFrame) {
vec3 step = rayStep * reflection;
vec3 marchingPosition = position + step;
float delta;
float depthFromScreen;
vec2 screenPosition;
bool traceScreenRay(vec3 position, vec3 reflection, out vec4 hitColor, out float hitDepth, float depth, sampler2D textureFrame)
{
vec3 step = rayStep * reflection;
vec3 marchingPosition = position + step;
float delta;
float depthFromScreen;
vec2 screenPosition;
bool hit = false;
hitColor = vec4(0);
int i = 0;
if (depth > depthRejectBias) {
for (; i < iterationCount && !hit; i++) {
screenPosition = generateProjectedPosition(marchingPosition);
depthFromScreen = linearDepth(getDepth(screenPosition), zNear, zFar);
delta = abs(marchingPosition.z) - depthFromScreen;
if (depth < depthFromScreen + epsilon && depth > depthFromScreen - epsilon) {
break;
}
int i = 0;
if (depth > depthRejectBias)
{
for (; i < iterationCount && !hit; i++)
{
screenPosition = generateProjectedPosition(marchingPosition);
depthFromScreen = linearDepth(getDepth(screenPosition), zNear, zFar);
delta = abs(marchingPosition.z) - depthFromScreen;
if (depth < depthFromScreen + epsilon && depth > depthFromScreen - epsilon)
{
break;
}
if (abs(delta) < distanceBias) {
vec4 color = vec4(1);
if(debugDraw)
color = vec4( 0.5+ sign(delta)/2,0.3,0.5- sign(delta)/2, 0);
hitColor = texture(textureFrame, screenPosition) * color;
hitDepth = depthFromScreen;
hit = true;
break;
}
if (isBinarySearchEnabled && delta > 0) {
break;
}
if (isAdaptiveStepEnabled){
float directionSign = sign(abs(marchingPosition.z) - depthFromScreen);
//this is sort of adapting step, should prevent lining reflection by doing sort of iterative converging
//some implementation doing it by binary search, but I found this idea more cheaty and way easier to implement
step = step * (1.0 - rayStep * max(directionSign, 0.0));
marchingPosition += step * (-directionSign);
}
else {
marchingPosition += step;
}
if (abs(delta) < distanceBias)
{
vec4 color = vec4(1);
if(debugDraw)
color = vec4( 0.5+ sign(delta)/2,0.3,0.5- sign(delta)/2, 0);
hitColor = texture(textureFrame, screenPosition) * color;
hitDepth = depthFromScreen;
hit = true;
break;
}
if (isBinarySearchEnabled && delta > 0)
{
break;
}
if (isAdaptiveStepEnabled)
{
float directionSign = sign(abs(marchingPosition.z) - depthFromScreen);
//this is sort of adapting step, should prevent lining reflection by doing sort of iterative converging
//some implementation doing it by binary search, but I found this idea more cheaty and way easier to implement
step = step * (1.0 - rayStep * max(directionSign, 0.0));
marchingPosition += step * (-directionSign);
}
else
{
marchingPosition += step;
}
if (isExponentialStepEnabled){
step *= 1.05;
}
}
if(isBinarySearchEnabled){
for(; i < iterationCount && !hit; i++){
step *= 0.5;
marchingPosition = marchingPosition - step * sign(delta);
screenPosition = generateProjectedPosition(marchingPosition);
depthFromScreen = linearDepth(getDepth(screenPosition), zNear, zFar);
delta = abs(marchingPosition.z) - depthFromScreen;
if (isExponentialStepEnabled)
{
step *= 1.05;
}
}
if(isBinarySearchEnabled)
{
for(; i < iterationCount && !hit; i++)
{
step *= 0.5;
marchingPosition = marchingPosition - step * sign(delta);
screenPosition = generateProjectedPosition(marchingPosition);
depthFromScreen = linearDepth(getDepth(screenPosition), zNear, zFar);
delta = abs(marchingPosition.z) - depthFromScreen;
if (depth < depthFromScreen + epsilon && depth > depthFromScreen - epsilon) {
break;
}
if (depth < depthFromScreen + epsilon && depth > depthFromScreen - epsilon)
{
break;
}
if (abs(delta) < distanceBias && depthFromScreen != (depth - distanceBias)) {
vec4 color = vec4(1);
if(debugDraw)
color = vec4( 0.5+ sign(delta)/2,0.3,0.5- sign(delta)/2, 0);
hitColor = texture(textureFrame, screenPosition) * color;
hitDepth = depthFromScreen;
hit = true;
break;
}
}
}
}
if (abs(delta) < distanceBias && depthFromScreen != (depth - distanceBias))
{
vec4 color = vec4(1);
if(debugDraw)
color = vec4( 0.5+ sign(delta)/2,0.3,0.5- sign(delta)/2, 0);
hitColor = texture(textureFrame, screenPosition) * color;
hitDepth = depthFromScreen;
hit = true;
break;
}
}
}
}
return hit;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment