From 3ee3b9c98cf29f74e2a918bfdbf9d8c32ef1f7c3 Mon Sep 17 00:00:00 2001 From: NULL511 Date: Fri, 1 Sep 2023 17:18:29 -0400 Subject: [PATCH] vanilla-matched ssao --- .../render/renderer/shaders/SSAORenderer.java | 6 +- .../main/resources/shaders/flat_shaded.frag | 106 ++++-------------- .../main/resources/shaders/noise/noise.frag | 8 -- core/src/main/resources/shaders/ssao/ao.frag | 13 ++- 4 files changed, 31 insertions(+), 102 deletions(-) diff --git a/core/src/main/java/com/seibel/distanthorizons/core/render/renderer/shaders/SSAORenderer.java b/core/src/main/java/com/seibel/distanthorizons/core/render/renderer/shaders/SSAORenderer.java index 9d3321b2f..935112376 100644 --- a/core/src/main/java/com/seibel/distanthorizons/core/render/renderer/shaders/SSAORenderer.java +++ b/core/src/main/java/com/seibel/distanthorizons/core/render/renderer/shaders/SSAORenderer.java @@ -156,7 +156,7 @@ public class SSAORenderer Mat4f perspective = Mat4f.perspective( (float) MC_RENDER.getFov(partialTicks), - MC_RENDER.getTargetFrameBufferViewportWidth() / (float) MC_RENDER.getTargetFrameBufferViewportHeight(), + width / (float) height, RenderUtil.getNearClipPlaneDistanceInBlocks(partialTicks), (float) ((RenderUtil.getFarClipPlaneDistanceInBlocks() + LodUtil.REGION_WIDTH) * Math.sqrt(2))); @@ -167,8 +167,8 @@ public class SSAORenderer this.ssaoShader.setUniform(this.ssaoShaderUniforms.gProjUniform, perspective); this.ssaoShader.setUniform(this.ssaoShaderUniforms.gInvProjUniform, invertedPerspective); this.ssaoShader.setUniform(this.ssaoShaderUniforms.gSampleRadUniform, 3.0f); - this.ssaoShader.setUniform(this.ssaoShaderUniforms.gFactorUniform, 0.8f); - this.ssaoShader.setUniform(this.ssaoShaderUniforms.gPowerUniform, 1.0f); + this.ssaoShader.setUniform(this.ssaoShaderUniforms.gFactorUniform, 0.5f); + this.ssaoShader.setUniform(this.ssaoShaderUniforms.gPowerUniform, 6.0f); this.va.bind(); this.va.bindBufferToAllBindingPoint(this.boxBuffer.getId()); diff --git a/core/src/main/resources/shaders/flat_shaded.frag b/core/src/main/resources/shaders/flat_shaded.frag index 28109a009..edcddbcca 100644 --- a/core/src/main/resources/shaders/flat_shaded.frag +++ b/core/src/main/resources/shaders/flat_shaded.frag @@ -2,7 +2,7 @@ in vec4 vertexColor; in vec3 vertexWorldPos; -in float vertexYPos; +//in float vertexYPos; in vec4 vPos; out vec4 fragColor; @@ -15,50 +15,19 @@ uniform float noiseIntensity; uniform int noiseDropoff; -// method definitions - -float fade(float value, float start, float end) { - return (clamp(value, start, end) - start) / (end - start); -} - // The random functions for diffrent dimentions float rand(float co) { return fract(sin(co*(91.3458)) * 47453.5453); } -float rand(vec2 co){ return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453); } -float rand(vec3 co){ return rand(co.xy+rand(co.z)); } +float rand(vec2 co) { return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453); } +float rand(vec3 co) { return rand(co.xy + rand(co.z)); } // Puts steps in a float // EG. setting stepSize to 4 then this would be the result of this function // In: 0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, ..., 1.1, 1.2, 1.3 // Out: 0.0, 0.0, 0.0, 0.25, 0.25, 0.5, 0.5, ..., 1.0, 1.0, 1.25 -float quantize(float val, int stepSize) { - return floor(val*stepSize)/stepSize; +vec3 quantize(vec3 val, int stepSize) { + return floor(val * stepSize) / stepSize; } -// The modulus function dosnt exist in GLSL so I made my own -// To speed up the mod function, this only accepts full numbers for y -float mod(float x, int y) { - return x - y * floor(x/y); -} - - -// Some HSV functions I stole somewhere online -vec3 RGB2HSV(vec3 c) { - vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); - vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); - vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); - - float d = q.x - min(q.w, q.y); - float e = 1.0e-10; - return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); -} - -vec3 HSV2RGB(vec3 c) { - vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); - vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); - return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); -} - - /** * Fragment Shader @@ -71,66 +40,31 @@ void main() { fragColor = vertexColor; - // TODO: Move into its own function instead of in an if statement if (noiseEnabled) { - vec3 vertexNormal = normalize(cross(dFdx(vPos.xyz), dFdy(vPos.xyz))); + vec3 vertexNormal = normalize(cross(dFdy(vPos.xyz), dFdx(vPos.xyz))); // This bit of code is required to fix the vertex position problem cus of floats in the verted world position varuable - vec3 fixedVPos = vPos.xyz - vertexNormal * 0.001; + vec3 fixedVPos = vPos.xyz + vertexNormal * 0.001; float noiseAmplification = noiseIntensity * 0.01; - noiseAmplification = (-1.0 * pow(2.0*((fragColor.x + fragColor.y + fragColor.z) / 3.0) - 1.0, 2.0) + 1.0) * noiseAmplification; // Lessen the effect on depending on how dark the object is, equasion for this is -(2x-1)^{2}+1 - noiseAmplification *= fragColor.w; // The effect would lessen on transparent objects + float lum = (fragColor.r + fragColor.g + fragColor.b) / 3.0; + noiseAmplification = (1.0 - pow(lum * 2.0 - 1.0, 2.0)) * noiseAmplification; // Lessen the effect on depending on how dark the object is, equasion for this is -(2x-1)^{2}+1 + noiseAmplification *= fragColor.a; // The effect would lessen on transparent objects // Random value for each position - float randomValue = rand(vec3( - quantize(fixedVPos.x, noiseSteps), - quantize(fixedVPos.y, noiseSteps), - quantize(fixedVPos.z, noiseSteps) - )) - * 2.0 * noiseAmplification - noiseAmplification; - - + float randomValue = rand(quantize(fixedVPos, noiseSteps)) + * 2.0 * noiseAmplification - noiseAmplification; + // Modifies the color // A value of 0 on the randomValue will result in the original color, while a value of 1 will result in a fully bright color vec3 newCol = fragColor.rgb + (1.0 - fragColor.rgb) * randomValue; + newCol = clamp(newCol, 0.0, 1.0); + + if (noiseDropoff != 0) { + float distF = min(length(vertexWorldPos) / noiseDropoff, 1.0); + newCol = mix(newCol, fragColor.rgb, distF); // The further away it gets, the less noise gets applied + } - // Clamps it and turns it back into a vec4 - if (noiseDropoff == 0) - fragColor = vec4(clamp(newCol.rgb, 0.0, 1.0), fragColor.w); - else - fragColor = mix( - vec4(clamp(newCol.rgb, 0.0, 1.0), fragColor.w), fragColor, - min(length(vertexWorldPos) / noiseDropoff, 1.0) // The further away it gets, the less noise gets applied - ); - - // For testing - // if (fragColor.r != 69420.) { - // fragColor = vec4( - // mod(fixedVPos.x, 1), - // mod(fixedVPos.y, 1), - // mod(fixedVPos.z, 1), - // fragColor.w); - // } + fragColor.rgb = newCol; } } - - - -// Are these still needed? -float linearFog(float x, float fogStart, float fogLength, float fogMin, float fogRange) { - x = clamp((x-fogStart)/fogLength, 0.0, 1.0); - return fogMin + fogRange * x; -} - -float exponentialFog(float x, float fogStart, float fogLength, -float fogMin, float fogRange, float fogDensity) { - x = max((x-fogStart)/fogLength, 0.0) * fogDensity; - return fogMin + fogRange - fogRange/exp(x); -} - -float exponentialSquaredFog(float x, float fogStart, float fogLength, -float fogMin, float fogRange, float fogDensity) { - x = max((x-fogStart)/fogLength, 0.0) * fogDensity; - return fogMin + fogRange - fogRange/exp(x*x); -} \ No newline at end of file diff --git a/core/src/main/resources/shaders/noise/noise.frag b/core/src/main/resources/shaders/noise/noise.frag index 9ae668cbe..1156311fc 100644 --- a/core/src/main/resources/shaders/noise/noise.frag +++ b/core/src/main/resources/shaders/noise/noise.frag @@ -30,14 +30,6 @@ vec3 quantize(vec3 val, int stepSize) { return floor(val*stepSize)/stepSize; } -// The modulus function dosnt exist in GLSL so I made my own -// To speed up the mod function, this only accepts full numbers for y -float mod(float x, int y) { - return x - y * floor(x/y); -} - - - /** * Fragment shader for adding noise to lods. diff --git a/core/src/main/resources/shaders/ssao/ao.frag b/core/src/main/resources/shaders/ssao/ao.frag index 32124d957..b23f6c901 100644 --- a/core/src/main/resources/shaders/ssao/ao.frag +++ b/core/src/main/resources/shaders/ssao/ao.frag @@ -12,7 +12,8 @@ uniform float gPower; uniform mat4 gProj; uniform mat4 gInvProj; -const float SAMPLE_BIAS = 0.1; +const float MIN_LIGHT = 0.6; +const float SAMPLE_BIAS = 0.5; const int MAX_KERNEL_SIZE = 32; const float INV_MAX_KERNEL_SIZE_F = 1.0 / float(MAX_KERNEL_SIZE); uniform vec3 gKernel[MAX_KERNEL_SIZE]; @@ -40,12 +41,13 @@ void main() { vec3 viewPos = calcViewPosition(vec3(TexCoord, fragmentDepth)); #ifdef GL_ARB_derivative_control - vec3 viewNormal = normalize(cross(dFdxFine(viewPos.xyz), dFdyFine(viewPos.xyz))); + vec3 viewNormal = cross(dFdxFine(viewPos.xyz), dFdyFine(viewPos.xyz)); #else - vec3 viewNormal = normalize(cross(dFdx(viewPos.xyz), dFdy(viewPos.xyz))); + vec3 viewNormal = cross(dFdx(viewPos.xyz), dFdy(viewPos.xyz)); #endif - const vec3 upVec = vec3(0.0, -1.0, 0.0); + viewNormal = normalize(viewNormal); + //const vec3 upVec = vec3(0.0, -1.0, 0.0); float angle = dither * (PI * 2.0); vec3 rotation = vec3(sin(angle), cos(angle), 0.0); @@ -58,6 +60,7 @@ void main() { float occlusion_factor = 0.0; for (int i = 0; i < MAX_KERNEL_SIZE; i++) { vec3 samplePos = TBN * gKernel[i]; + samplePos *= sign(dot(samplePos, viewNormal)); samplePos = viewPos + samplePos * gSampleRad; vec4 sampleNdcPos = gProj * vec4(samplePos + viewPos, 1.0); @@ -77,7 +80,7 @@ void main() { float visibility_factor = 1.0 - (occlusion_factor / maxWeight); occlusion = (1.0 - pow(visibility_factor, gFactor)) * gPower; - occlusion = clamp(1.0 - occlusion, 0.25, 1.0); + occlusion = clamp(1.0 - occlusion, MIN_LIGHT, 1.0); } fragColor = vec4(vec3(1.0), occlusion);