From ceb0c215c52ad5b2dbccbc58a61515b20a018ef6 Mon Sep 17 00:00:00 2001 From: NULL511 Date: Thu, 17 Aug 2023 10:34:54 -0400 Subject: [PATCH 1/6] shader cleanup initial --- core/src/main/resources/shaders/curve.vert | 47 +++++++++---------- .../main/resources/shaders/flat_shaded.frag | 35 +++++--------- core/src/main/resources/shaders/fog/fog.frag | 24 ++++------ .../main/resources/shaders/noise/noise.frag | 35 +++++--------- core/src/main/resources/shaders/ssao/ao.frag | 28 ++++------- .../resources/shaders/ssao/apply-frag.frag | 6 +-- core/src/main/resources/shaders/standard.vert | 3 +- 7 files changed, 70 insertions(+), 108 deletions(-) diff --git a/core/src/main/resources/shaders/curve.vert b/core/src/main/resources/shaders/curve.vert index a5ea716a2..5228831c7 100644 --- a/core/src/main/resources/shaders/curve.vert +++ b/core/src/main/resources/shaders/curve.vert @@ -29,7 +29,7 @@ uniform float earthRadius; */ void main() { - vPos = vec4(vPosition.x, vPosition.y, vPosition.z, vPosition.w); // This is so it can be passed to the fragment shader + vPos = vPosition.x; // This is so it can be passed to the fragment shader vertexWorldPos = vPosition.xyz + modelOffset; @@ -52,36 +52,35 @@ void main() vertexWorldPos.y += my; vertexWorldPos.z += mz; - // Old (disabled) vertex transformation logic - Leetom #if 0 + // Old (disabled) vertex transformation logic - Leetom - // Calculate the vertex pos due to curvature of the earth - // We use spherical coordinates to calculate the vertex position - if(vertexWorldPos.x == 0.0 && vertexWorldPos.z == 0.0) - { - // In the center. No curvature needed - } - else - { - float theta = atan(vertexWorldPos.z, vertexWorldPos.x); // in radians (-pi, pi) - float trueY = earthRadius + vertexWorldPos.y; // true Y position, or height - float phi = sqrt(vertexWorldPos.z * vertexWorldPos.z + vertexWorldPos.x * vertexWorldPos.x) / trueY; - // Convert spherical coordinates to cartesian coordinates - vertexWorldPos.x = trueY * sin(phi) * cos(theta); - vertexWorldPos.z = trueY * sin(phi) * sin(theta); - vertexWorldPos.y = trueY * cos(phi) - earthRadius; - } + // Calculate the vertex pos due to curvature of the earth + // We use spherical coordinates to calculate the vertex position + //if (vertexWorldPos.x == 0.0 && vertexWorldPos.z == 0.0) + //{ + // // In the center. No curvature needed + //} + //else + //{ + float theta = atan(vertexWorldPos.z, vertexWorldPos.x); // in radians (-pi, pi) + float trueY = earthRadius + vertexWorldPos.y; // true Y position, or height + float phi = sqrt(vertexWorldPos.z * vertexWorldPos.z + vertexWorldPos.x * vertexWorldPos.x) / trueY; + // Convert spherical coordinates to cartesian coordinates + vertexWorldPos.x = trueY * sin(phi) * cos(theta); + vertexWorldPos.z = trueY * sin(phi) * sin(theta); + vertexWorldPos.y = trueY * cos(phi) - earthRadius; + //} #else - // new vertex transformation logic - stduhpf + // new vertex transformation logic - stduhpf - float localRadius = earthRadius + vertexYPos;// vertexWorldPos.y + cameraPosition.y - Center_Y; + float localRadius = earthRadius + vertexYPos;// vertexWorldPos.y + cameraPosition.y - Center_Y; - float phi = length(vertexWorldPos.xz) / localRadius; - - vertexWorldPos.y += (cos(phi) - 1.) * localRadius; - vertexWorldPos.xz = vertexWorldPos.xz * sin(phi) / phi; + float phi = length(vertexWorldPos.xz) / localRadius; + vertexWorldPos.y += (cos(phi) - 1.0) * localRadius; + vertexWorldPos.xz = vertexWorldPos.xz * sin(phi) / phi; #endif uint lights = meta & 0xFFu; diff --git a/core/src/main/resources/shaders/flat_shaded.frag b/core/src/main/resources/shaders/flat_shaded.frag index f3bbdedb1..28109a009 100644 --- a/core/src/main/resources/shaders/flat_shaded.frag +++ b/core/src/main/resources/shaders/flat_shaded.frag @@ -18,8 +18,9 @@ uniform int noiseDropoff; // method definitions float fade(float value, float start, float end) { - return (clamp(value,start,end)-start)/(end-start); + 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); } @@ -50,6 +51,7 @@ vec3 RGB2HSV(vec3 c) { 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); @@ -74,15 +76,10 @@ void main() if (noiseEnabled) { vec3 vertexNormal = normalize(cross(dFdx(vPos.xyz), dFdy(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 = vec3( - vPos.x - vertexNormal.x * 0.001, - vPos.y - vertexNormal.y * 0.001, - vPos.z - vertexNormal.z * 0.001 - ); + vec3 fixedVPos = vPos.xyz - vertexNormal * 0.001; - - float noiseAmplification = noiseIntensity / 100; - noiseAmplification = (-1 * pow(2*((fragColor.x + fragColor.y + fragColor.z) / 3) - 1, 2) + 1) * noiseAmplification; // Lessen the effect on depending on how dark the object is, equasion for this is -(2x-1)^{2}+1 + 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 // Random value for each position @@ -91,30 +88,20 @@ void main() quantize(fixedVPos.y, noiseSteps), quantize(fixedVPos.z, noiseSteps) )) - * 2. * noiseAmplification - noiseAmplification; + * 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 + (vec3(1.0) - fragColor.rgb) * randomValue; + vec3 newCol = fragColor.rgb + (1.0 - fragColor.rgb) * randomValue; // Clamps it and turns it back into a vec4 if (noiseDropoff == 0) - fragColor = vec4( - clamp(newCol.r, 0., 1.), - clamp(newCol.g, 0., 1.), - clamp(newCol.b, 0., 1.), - fragColor.w - ); + fragColor = vec4(clamp(newCol.rgb, 0.0, 1.0), fragColor.w); else fragColor = mix( - vec4( - clamp(newCol.r, 0., 1.), - clamp(newCol.g, 0., 1.), - clamp(newCol.b, 0., 1.), - fragColor.w - ), fragColor, - clamp(length(vertexWorldPos) / noiseDropoff, 0., 1.) // The further away it gets, the less noise gets applied + 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 diff --git a/core/src/main/resources/shaders/fog/fog.frag b/core/src/main/resources/shaders/fog/fog.frag index 9ec282886..fe29c3d23 100644 --- a/core/src/main/resources/shaders/fog/fog.frag +++ b/core/src/main/resources/shaders/fog/fog.frag @@ -59,16 +59,12 @@ float mod(float x, int y) { vec3 calcViewPosition(float fragmentDepth) { - vec4 ndc = vec4( - TexCoord.x * 2.0 - 1.0, - TexCoord.y * 2.0 - 1.0, - fragmentDepth * 2.0 - 1.0, - 1.0 - ); + vec4 ndc = vec4(TexCoord.xy, fragmentDepth, 1.0); + ndc.xyz = ndc.xyz * 2.0 - 1.0; + // TODO: This inverse() should be moved CPU side vec4 eyeCoord = inverse(gMvmProj) * ndc; - vec3 cameraPos = eyeCoord.xyz / eyeCoord.w; - return cameraPos; + return eyeCoord.xyz / eyeCoord.w; } /** @@ -85,7 +81,7 @@ void main() // a fragment depth of "1" means the fragment wasn't drawn to, // we only want to apply Fog to LODs, not to the sky outside the LODs // FIXME: This bit of code causes problems on intel integrated graphics - if (fragmentDepth != -420.0) // Should be `1.0`, but set to `-420.0` both so that the compiler doesnt mess with rest of the code, and it always returns true + if (fragmentDepth < 0.99999) { if (fullFogMode == 0) { @@ -99,17 +95,15 @@ void main() float nearFogThickness = getNearFogThickness(horizontalDist); float farFogThickness = getFarFogThickness(farDist); float heightFogThickness = getHeightFogThickness(heightDist); - float mixedFogThickness = - clamp( - mixFogThickness(nearFogThickness, farFogThickness, heightFogThickness) - , 0.0, 1.0); + float mixedFogThickness = mixFogThickness(nearFogThickness, farFogThickness, heightFogThickness) + mixedFogThickness = clamp(mixedFogThickness, 0.0, 1.0); - fragColor = vec4(fogColor.r, fogColor.g, fogColor.b, mixedFogThickness); + fragColor = vec4(fogColor.rgb, mixedFogThickness); } else if (fullFogMode == 1) { // render everything with the fog color - fragColor = vec4(fogColor.r, fogColor.g, fogColor.b, 1.0); + fragColor = vec4(fogColor.rgb, 1.0); } else { diff --git a/core/src/main/resources/shaders/noise/noise.frag b/core/src/main/resources/shaders/noise/noise.frag index 4eeff968c..9ae668cbe 100644 --- a/core/src/main/resources/shaders/noise/noise.frag +++ b/core/src/main/resources/shaders/noise/noise.frag @@ -26,6 +26,10 @@ 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) { @@ -44,11 +48,7 @@ float mod(float x, int y) { void main() { // This bit of code is required to fix the vertex position problem cus of floats in the verted world position varuable vec3 vertexNormal = normalize(cross(dFdx(vPos.xyz), dFdy(vPos.xyz))); - vec3 fixedVPos = vec3( - vPos.x - vertexNormal.x * 0.001, - vPos.y - vertexNormal.y * 0.001, - vPos.z - vertexNormal.z * 0.001 - ); + vec3 fixedVPos = vPos.xyz - vertexNormal * 0.001; float noiseAmplification = noiseIntensity / 100; @@ -56,29 +56,20 @@ void main() { noiseAmplification *= vertexColor.w; // 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. * noiseAmplification - noiseAmplification; + float randomValue = rand(quantize(fixedVPos.xyz, 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 = (vec3(1.0) - vertexColor.rgb) * randomValue; + vec3 newCol = (1.0 - vertexColor.rgb) * randomValue; // Clamps it and turns it back into a vec4 - fragColor = vec4( - clamp(newCol.r, 0., 1.), - clamp(newCol.g, 0., 1.), - clamp(newCol.b, 0., 1.), - clamp(length(vertexWorldPos) * distanceScale * noiseDropoff, 0., 1.) // The further away it gets, the less noise gets applied - ); - fragColor = vec4( - 0f, 0f, 0f, - randomValue // The further away it gets, the less noise gets applied - ); + float distA = length(vertexWorldPos) * distanceScale * noiseDropoff; + fragColor = clamp(vec4(newCol.rgb, distA), 0.0, 1.0); // The further away it gets, the less noise gets applied + + // The further away it gets, the less noise gets applied + fragColor = vec4(0.0, 0.0, 0.0, randomValue); // For testing // if (vertexColor.r != 69420.) { diff --git a/core/src/main/resources/shaders/ssao/ao.frag b/core/src/main/resources/shaders/ssao/ao.frag index 15f648764..c791b458b 100644 --- a/core/src/main/resources/shaders/ssao/ao.frag +++ b/core/src/main/resources/shaders/ssao/ao.frag @@ -17,40 +17,30 @@ uniform vec3 gKernel[MAX_KERNEL_SIZE]; vec3 calcViewPosition(vec2 coords) { float fragmentDepth = texture(gDepthMap, coords).r; + vec4 ndc = vec4(coords.xy, fragmentDepth, 1.0); + ndc.xyz = ndc.xyz * 2.0 - 1.0; - vec4 ndc = vec4( - coords.x * 2.0 - 1.0, - coords.y * 2.0 - 1.0, - fragmentDepth * 2.0 - 1.0, - 1.0 - ); - + // TODO: This inverse() call should be moved CPU side vec4 vs_pos = inverse(gProj) * ndc; - vs_pos.xyz = vs_pos.xyz / vs_pos.w; - return vs_pos.xyz; + return vs_pos.xyz / vs_pos.w; } void main() { vec3 viewPos = calcViewPosition(TexCoord); - vec3 viewNormal = normalize(cross(dFdy(viewPos.xyz), dFdx(viewPos.xyz)) * -1.0); + vec3 viewNormal = normalize(cross(dFdx(viewPos.xyz), dFdy(viewPos.xyz))); - vec3 randomVec = vec3( - 0.0, - -1.0, - 0.0 - ); + vec3 randomVec = vec3(0.0, -1.0, 0.0); vec3 tangent = normalize(randomVec - viewNormal * dot(randomVec, viewNormal)); vec3 bitangent = cross(viewNormal, tangent); mat3 TBN = mat3(tangent, bitangent, viewNormal); + float occlusion_factor = 0.0; for (int i = 0; i < MAX_KERNEL_SIZE; i++) { - vec3 samplePos = vec3(0.0) + (TBN * gKernel[i]); - samplePos = viewPos + samplePos * gSampleRad; + vec3 samplePos = TBN * gKernel[i] * gSampleRad; - vec4 offset = vec4(samplePos, 1.0); - offset = gProj * offset; + vec4 offset = gProj * vec4(samplePos + viewPos, 1.0); offset.xy /= offset.w; offset.xy = offset.xy * HALF_2 + HALF_2; diff --git a/core/src/main/resources/shaders/ssao/apply-frag.frag b/core/src/main/resources/shaders/ssao/apply-frag.frag index 03bb09f18..5c1ec38ca 100644 --- a/core/src/main/resources/shaders/ssao/apply-frag.frag +++ b/core/src/main/resources/shaders/ssao/apply-frag.frag @@ -13,9 +13,9 @@ void main() float fragmentDepth = texture(gDepthMap, TexCoord).r; // a fragment depth of "1" means the fragment wasn't drawn to, // we only want to apply SSAO to LODs, not to the sky outside the LODs - // FIXME: This bit of code causes problems on intel integrated graphics - if (fragmentDepth != -420.0) // Should be `1.0`, but set to `-420.0` both so that the compiler doesnt mess with rest of the code, and it always returns true + if (fragmentDepth < 0.99999) { - fragColor = vec4(0.0, 0.0, 0.0, 1-texture(gSSAOMap, TexCoord).r); + fragColor = vec4(0.0, 0.0, 0.0, 1.0); + fragColor.a -= textureLod(gSSAOMap, TexCoord, 0).r; } } diff --git a/core/src/main/resources/shaders/standard.vert b/core/src/main/resources/shaders/standard.vert index 364bd1722..c7e446cf8 100644 --- a/core/src/main/resources/shaders/standard.vert +++ b/core/src/main/resources/shaders/standard.vert @@ -29,7 +29,7 @@ uniform float mircoOffset; */ void main() { - vPos = vec4(vPosition.x, vPosition.y, vPosition.z, vPosition.w); // This is so it can be passed to the fragment shader + vPos = vPosition; // This is so it can be passed to the fragment shader vertexWorldPos = vPosition.xyz + modelOffset; @@ -54,6 +54,7 @@ void main() float light2 = (mod(float(lights), 16.0)+0.5) / 16.0; float light = (float(lights/16u)+0.5) / 16.0; vertexColor = vec4(texture(lightMap, vec2(light, light2)).xyz, 1.0); + if (!whiteWorld) { vertexColor *= color; From 4563e4872eb343060744f5387af31787568bc799 Mon Sep 17 00:00:00 2001 From: NULL511 Date: Thu, 17 Aug 2023 11:19:38 -0400 Subject: [PATCH 2/6] shader syntax fix --- core/src/main/resources/shaders/fog/fog.frag | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/main/resources/shaders/fog/fog.frag b/core/src/main/resources/shaders/fog/fog.frag index fe29c3d23..b5000241a 100644 --- a/core/src/main/resources/shaders/fog/fog.frag +++ b/core/src/main/resources/shaders/fog/fog.frag @@ -95,7 +95,7 @@ void main() float nearFogThickness = getNearFogThickness(horizontalDist); float farFogThickness = getFarFogThickness(farDist); float heightFogThickness = getHeightFogThickness(heightDist); - float mixedFogThickness = mixFogThickness(nearFogThickness, farFogThickness, heightFogThickness) + float mixedFogThickness = mixFogThickness(nearFogThickness, farFogThickness, heightFogThickness); mixedFogThickness = clamp(mixedFogThickness, 0.0, 1.0); fragColor = vec4(fogColor.rgb, mixedFogThickness); From be1d13e6ad66cab6ac07fccf7263c362913f8759 Mon Sep 17 00:00:00 2001 From: NULL511 Date: Thu, 17 Aug 2023 12:08:45 -0400 Subject: [PATCH 3/6] cpu projection inverse --- .../core/render/renderer/shaders/SSAOShader.java | 4 ++-- core/src/main/resources/shaders/ssao/ao.frag | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/com/seibel/distanthorizons/core/render/renderer/shaders/SSAOShader.java b/core/src/main/java/com/seibel/distanthorizons/core/render/renderer/shaders/SSAOShader.java index 305c7fe34..32c2a1c5a 100644 --- a/core/src/main/java/com/seibel/distanthorizons/core/render/renderer/shaders/SSAOShader.java +++ b/core/src/main/java/com/seibel/distanthorizons/core/render/renderer/shaders/SSAOShader.java @@ -46,9 +46,9 @@ public class SSAOShader extends AbstractShaderRenderer MC_RENDER.getTargetFrameBufferViewportWidth() / (float) MC_RENDER.getTargetFrameBufferViewportHeight(), RenderUtil.getNearClipPlaneDistanceInBlocks(partialTicks), (float) ((RenderUtil.getFarClipPlaneDistanceInBlocks() + LodUtil.REGION_WIDTH) * Math.sqrt(2))); - - + this.shader.setUniform(this.shader.getUniformLocation("gProj"), perspective); + this.shader.setUniform(this.shader.getUniformLocation("gProjInv"), perspective.invert()); this.shader.setUniform(this.shader.getUniformLocation("gSampleRad"), 3.0f); this.shader.setUniform(this.shader.getUniformLocation("gFactor"), 0.8f); this.shader.setUniform(this.shader.getUniformLocation("gPower"), 1.0f); diff --git a/core/src/main/resources/shaders/ssao/ao.frag b/core/src/main/resources/shaders/ssao/ao.frag index c791b458b..f681cb0e8 100644 --- a/core/src/main/resources/shaders/ssao/ao.frag +++ b/core/src/main/resources/shaders/ssao/ao.frag @@ -9,6 +9,7 @@ uniform float gSampleRad; uniform float gFactor; uniform float gPower; uniform mat4 gProj; +uniform mat4 gProjInv; const int MAX_KERNEL_SIZE = 32; const float INV_MAX_KERNEL_SIZE_F = 1.0 / float(MAX_KERNEL_SIZE); @@ -20,8 +21,7 @@ vec3 calcViewPosition(vec2 coords) { vec4 ndc = vec4(coords.xy, fragmentDepth, 1.0); ndc.xyz = ndc.xyz * 2.0 - 1.0; - // TODO: This inverse() call should be moved CPU side - vec4 vs_pos = inverse(gProj) * ndc; + vec4 vs_pos = gProjInv * ndc; return vs_pos.xyz / vs_pos.w; } From ea5b19b4e71a6da604a2ac04d8573471d1ebea02 Mon Sep 17 00:00:00 2001 From: NULL511 Date: Thu, 17 Aug 2023 12:35:16 -0400 Subject: [PATCH 4/6] cpu projection inverse fog --- .../core/render/renderer/shaders/FogShader.java | 6 +++--- core/src/main/resources/shaders/fog/fog.frag | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/core/src/main/java/com/seibel/distanthorizons/core/render/renderer/shaders/FogShader.java b/core/src/main/java/com/seibel/distanthorizons/core/render/renderer/shaders/FogShader.java index 5abae934e..959077b85 100644 --- a/core/src/main/java/com/seibel/distanthorizons/core/render/renderer/shaders/FogShader.java +++ b/core/src/main/java/com/seibel/distanthorizons/core/render/renderer/shaders/FogShader.java @@ -49,7 +49,7 @@ public class FogShader extends AbstractShaderRenderer // because disabling fog can cause the GLSL to optimize out most (if not all) uniforms - this.gModelViewProjectionUniform = this.shader.tryGetUniformLocation("gMvmProj"); + this.gModelViewProjectionUniform = this.shader.tryGetUniformLocation("gMvmProjInv"); this.gDepthMapUniform = this.shader.tryGetUniformLocation("gDepthMap"); // Fog uniforms @@ -115,13 +115,13 @@ public class FogShader extends AbstractShaderRenderer return fogColor; } + private Color getSpecialFogColor(float partialTicks) { return MC_RENDER.getSpecialFogColor(partialTicks); } public void setModelViewProjectionMatrix(Mat4f combinedModelViewProjectionMatrix) { this.shader.bind(); - this.shader.setUniform(this.gModelViewProjectionUniform, combinedModelViewProjectionMatrix); + this.shader.setUniform(this.gModelViewProjectionUniform, combinedModelViewProjectionMatrix.invert()); this.shader.unbind(); } - } diff --git a/core/src/main/resources/shaders/fog/fog.frag b/core/src/main/resources/shaders/fog/fog.frag index b5000241a..6bf4b8467 100644 --- a/core/src/main/resources/shaders/fog/fog.frag +++ b/core/src/main/resources/shaders/fog/fog.frag @@ -5,7 +5,7 @@ out vec4 fragColor; uniform sampler2D gDepthMap; // model view matrix and projection matrix -uniform mat4 gMvmProj; +uniform mat4 gMvmProjInv; uniform float fogScale; uniform float fogVerticalScale; @@ -63,7 +63,7 @@ vec3 calcViewPosition(float fragmentDepth) ndc.xyz = ndc.xyz * 2.0 - 1.0; // TODO: This inverse() should be moved CPU side - vec4 eyeCoord = inverse(gMvmProj) * ndc; + vec4 eyeCoord = gMvmProjInv * ndc; return eyeCoord.xyz / eyeCoord.w; } From c75c830ab247f72756287aa1d37ff65e0594d39b Mon Sep 17 00:00:00 2001 From: NULL511 Date: Thu, 17 Aug 2023 13:06:37 -0400 Subject: [PATCH 5/6] revert cpu projection inverse fog --- .../core/render/renderer/shaders/FogShader.java | 6 +++--- core/src/main/resources/shaders/fog/fog.frag | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/core/src/main/java/com/seibel/distanthorizons/core/render/renderer/shaders/FogShader.java b/core/src/main/java/com/seibel/distanthorizons/core/render/renderer/shaders/FogShader.java index 959077b85..5abae934e 100644 --- a/core/src/main/java/com/seibel/distanthorizons/core/render/renderer/shaders/FogShader.java +++ b/core/src/main/java/com/seibel/distanthorizons/core/render/renderer/shaders/FogShader.java @@ -49,7 +49,7 @@ public class FogShader extends AbstractShaderRenderer // because disabling fog can cause the GLSL to optimize out most (if not all) uniforms - this.gModelViewProjectionUniform = this.shader.tryGetUniformLocation("gMvmProjInv"); + this.gModelViewProjectionUniform = this.shader.tryGetUniformLocation("gMvmProj"); this.gDepthMapUniform = this.shader.tryGetUniformLocation("gDepthMap"); // Fog uniforms @@ -115,13 +115,13 @@ public class FogShader extends AbstractShaderRenderer return fogColor; } - private Color getSpecialFogColor(float partialTicks) { return MC_RENDER.getSpecialFogColor(partialTicks); } public void setModelViewProjectionMatrix(Mat4f combinedModelViewProjectionMatrix) { this.shader.bind(); - this.shader.setUniform(this.gModelViewProjectionUniform, combinedModelViewProjectionMatrix.invert()); + this.shader.setUniform(this.gModelViewProjectionUniform, combinedModelViewProjectionMatrix); this.shader.unbind(); } + } diff --git a/core/src/main/resources/shaders/fog/fog.frag b/core/src/main/resources/shaders/fog/fog.frag index 6bf4b8467..b5000241a 100644 --- a/core/src/main/resources/shaders/fog/fog.frag +++ b/core/src/main/resources/shaders/fog/fog.frag @@ -5,7 +5,7 @@ out vec4 fragColor; uniform sampler2D gDepthMap; // model view matrix and projection matrix -uniform mat4 gMvmProjInv; +uniform mat4 gMvmProj; uniform float fogScale; uniform float fogVerticalScale; @@ -63,7 +63,7 @@ vec3 calcViewPosition(float fragmentDepth) ndc.xyz = ndc.xyz * 2.0 - 1.0; // TODO: This inverse() should be moved CPU side - vec4 eyeCoord = gMvmProjInv * ndc; + vec4 eyeCoord = inverse(gMvmProj) * ndc; return eyeCoord.xyz / eyeCoord.w; } From 155648035bbdb51d0e52056ad0cede492e62abc3 Mon Sep 17 00:00:00 2001 From: James Seibel Date: Thu, 17 Aug 2023 20:56:35 -0500 Subject: [PATCH 6/6] fix SSAO rendering --- .../render/renderer/shaders/SSAOShader.java | 1 - core/src/main/resources/shaders/fog/fog.frag | 8 +++++-- core/src/main/resources/shaders/ssao/ao.frag | 23 ++++++++++++------- .../resources/shaders/ssao/apply-frag.frag | 10 +++++--- 4 files changed, 28 insertions(+), 14 deletions(-) diff --git a/core/src/main/java/com/seibel/distanthorizons/core/render/renderer/shaders/SSAOShader.java b/core/src/main/java/com/seibel/distanthorizons/core/render/renderer/shaders/SSAOShader.java index 32c2a1c5a..ce357c140 100644 --- a/core/src/main/java/com/seibel/distanthorizons/core/render/renderer/shaders/SSAOShader.java +++ b/core/src/main/java/com/seibel/distanthorizons/core/render/renderer/shaders/SSAOShader.java @@ -48,7 +48,6 @@ public class SSAOShader extends AbstractShaderRenderer (float) ((RenderUtil.getFarClipPlaneDistanceInBlocks() + LodUtil.REGION_WIDTH) * Math.sqrt(2))); this.shader.setUniform(this.shader.getUniformLocation("gProj"), perspective); - this.shader.setUniform(this.shader.getUniformLocation("gProjInv"), perspective.invert()); this.shader.setUniform(this.shader.getUniformLocation("gSampleRad"), 3.0f); this.shader.setUniform(this.shader.getUniformLocation("gFactor"), 0.8f); this.shader.setUniform(this.shader.getUniformLocation("gPower"), 1.0f); diff --git a/core/src/main/resources/shaders/fog/fog.frag b/core/src/main/resources/shaders/fog/fog.frag index b5000241a..a9f317700 100644 --- a/core/src/main/resources/shaders/fog/fog.frag +++ b/core/src/main/resources/shaders/fog/fog.frag @@ -80,8 +80,7 @@ void main() // a fragment depth of "1" means the fragment wasn't drawn to, // we only want to apply Fog to LODs, not to the sky outside the LODs - // FIXME: This bit of code causes problems on intel integrated graphics - if (fragmentDepth < 0.99999) + if (fragmentDepth < 1) { if (fullFogMode == 0) { @@ -118,6 +117,11 @@ void main() fragColor = vec4(vec3(depthValue), 1.0); // Convert depth value to grayscale color } } + else + { + // every pixel needs to be set to something, otherwise the pixel may be undefined by some drivers (specifically Intel) + fragColor = vec4(0.0, 0.0, 0.0, 0.0); + } } diff --git a/core/src/main/resources/shaders/ssao/ao.frag b/core/src/main/resources/shaders/ssao/ao.frag index f681cb0e8..33be6a37d 100644 --- a/core/src/main/resources/shaders/ssao/ao.frag +++ b/core/src/main/resources/shaders/ssao/ao.frag @@ -9,7 +9,6 @@ uniform float gSampleRad; uniform float gFactor; uniform float gPower; uniform mat4 gProj; -uniform mat4 gProjInv; const int MAX_KERNEL_SIZE = 32; const float INV_MAX_KERNEL_SIZE_F = 1.0 / float(MAX_KERNEL_SIZE); @@ -18,17 +17,23 @@ uniform vec3 gKernel[MAX_KERNEL_SIZE]; vec3 calcViewPosition(vec2 coords) { float fragmentDepth = texture(gDepthMap, coords).r; - vec4 ndc = vec4(coords.xy, fragmentDepth, 1.0); - ndc.xyz = ndc.xyz * 2.0 - 1.0; - vec4 vs_pos = gProjInv * ndc; - return vs_pos.xyz / vs_pos.w; + vec4 ndc = vec4( + coords.x * 2.0 - 1.0, + coords.y * 2.0 - 1.0, + fragmentDepth * 2.0 - 1.0, + 1.0 + ); + + vec4 vs_pos = inverse(gProj) * ndc; + vs_pos.xyz = vs_pos.xyz / vs_pos.w; + return vs_pos.xyz; } void main() { vec3 viewPos = calcViewPosition(TexCoord); - vec3 viewNormal = normalize(cross(dFdx(viewPos.xyz), dFdy(viewPos.xyz))); + vec3 viewNormal = normalize(cross(dFdy(viewPos.xyz), dFdx(viewPos.xyz)) * -1.0); vec3 randomVec = vec3(0.0, -1.0, 0.0); @@ -37,8 +42,10 @@ void main() mat3 TBN = mat3(tangent, bitangent, viewNormal); float occlusion_factor = 0.0; - for (int i = 0; i < MAX_KERNEL_SIZE; i++) { - vec3 samplePos = TBN * gKernel[i] * gSampleRad; + for (int i = 0; i < MAX_KERNEL_SIZE; i++) + { + vec3 samplePos = vec3(0.0) + (TBN * gKernel[i]); + samplePos = viewPos + samplePos * gSampleRad; vec4 offset = gProj * vec4(samplePos + viewPos, 1.0); offset.xy /= offset.w; diff --git a/core/src/main/resources/shaders/ssao/apply-frag.frag b/core/src/main/resources/shaders/ssao/apply-frag.frag index 5c1ec38ca..534306c50 100644 --- a/core/src/main/resources/shaders/ssao/apply-frag.frag +++ b/core/src/main/resources/shaders/ssao/apply-frag.frag @@ -13,9 +13,13 @@ void main() float fragmentDepth = texture(gDepthMap, TexCoord).r; // a fragment depth of "1" means the fragment wasn't drawn to, // we only want to apply SSAO to LODs, not to the sky outside the LODs - if (fragmentDepth < 0.99999) + if (fragmentDepth < 1) { - fragColor = vec4(0.0, 0.0, 0.0, 1.0); - fragColor.a -= textureLod(gSSAOMap, TexCoord, 0).r; + fragColor = vec4(0.0, 0.0, 0.0, 1-texture(gSSAOMap, TexCoord).r); + } + else + { + // every pixel needs to be set to something, otherwise the pixel may be undefined by some drivers (specifically Intel) + fragColor = vec4(0.0, 0.0, 0.0, 0.0); } }