From 79f42760b072189bb8cd45b849c820848c302d01 Mon Sep 17 00:00:00 2001 From: mcrcortex <18544518+MCRcortex@users.noreply.github.com> Date: Thu, 8 May 2025 22:56:58 +1000 Subject: [PATCH] Fix a bunch of lighting issues --- .../voxy/client/core/model/ModelFactory.java | 2 +- .../rendering/building/RenderDataFactory.java | 48 +++++++++++-------- .../core/rendering/util/LightMapHelper.java | 1 + .../voxy/shaders/lod/gl46/bindings.glsl | 2 +- .../assets/voxy/shaders/lod/gl46/quads2.vert | 8 ++-- .../shaders/lod/hierarchical/screenspace.glsl | 14 +++--- 6 files changed, 42 insertions(+), 33 deletions(-) diff --git a/src/main/java/me/cortex/voxy/client/core/model/ModelFactory.java b/src/main/java/me/cortex/voxy/client/core/model/ModelFactory.java index 1f44fb26..5bfca821 100644 --- a/src/main/java/me/cortex/voxy/client/core/model/ModelFactory.java +++ b/src/main/java/me/cortex/voxy/client/core/model/ModelFactory.java @@ -385,7 +385,7 @@ public class ModelFactory { metadata |= canBeOccluded?4:0; //Face uses its own lighting if its not flat against the adjacent block & isnt traslucent - metadata |= (offset != 0 || blockRenderLayer == RenderLayer.getTranslucent())?0b1000:0; + metadata |= (offset > 0.01 || blockRenderLayer == RenderLayer.getTranslucent())?0b1000:0; diff --git a/src/main/java/me/cortex/voxy/client/core/rendering/building/RenderDataFactory.java b/src/main/java/me/cortex/voxy/client/core/rendering/building/RenderDataFactory.java index dc4af9b5..5b4a9202 100644 --- a/src/main/java/me/cortex/voxy/client/core/rendering/building/RenderDataFactory.java +++ b/src/main/java/me/cortex/voxy/client/core/rendering/building/RenderDataFactory.java @@ -198,7 +198,7 @@ public class RenderDataFactory { private static long packPartialQuadData(int modelId, long state, long metadata) { //This uses hardcoded data to shuffle things long lightAndBiome = (state&((0x1FFL<<47)|(0xFFL<<56)))>>1; - lightAndBiome &= ModelQueries.isBiomeColoured(metadata)?-1:~(0x1FFL<<47); + lightAndBiome &= ModelQueries.isBiomeColoured(metadata)?-1:~(0x1FFL<<46);//46 not 47 because is already shifted by 1 THIS WASTED 4 HOURS ;-; aaaaaAAAAAA lightAndBiome &= ModelQueries.isFullyOpaque(metadata)?~(0xFFL<<55):-1;//If its fully opaque it always uses neighbor light? long quadData = lightAndBiome; @@ -329,6 +329,8 @@ public class RenderDataFactory { } } + private static final long LM = (0xFFL<<55); + private static boolean shouldMeshNonOpaqueBlockFace(int face, long quad, long meta, long neighborQuad, long neighborMeta) { if (((quad^neighborQuad)&(0xFFFFL<<26))==0) return false;//This is a hack, if the neigbor and this are the same, dont mesh the face if (!ModelQueries.faceExists(meta, face)) return false;//Dont mesh if no face @@ -340,8 +342,8 @@ public class RenderDataFactory { private static void meshNonOpaqueFace(int face, long quad, long meta, long neighborQuad, long neighborMeta, Mesher mesher) { if (shouldMeshNonOpaqueBlockFace(face, quad, meta, neighborQuad, neighborMeta)) { mesher.putNext((long) (face&1) | - quad | - ((ModelQueries.faceUsesSelfLighting(meta, face)?quad:neighborQuad) & (0xFFL << 55))); + (quad&~LM) | + ((ModelQueries.faceUsesSelfLighting(meta, face)?quad:neighborQuad) & LM)); } else { mesher.skip(1); } @@ -397,8 +399,8 @@ public class RenderDataFactory { long selfModel = this.sectionData[iA]; long nextModel = this.sectionData[iB]; this.blockMesher.putNext(((long) facingForward) |//Facing - selfModel | - (nextModel & (0xFFL << 55))//Apply lighting + (selfModel&~LM) | + (nextModel&LM)//Apply lighting ); } } @@ -462,9 +464,9 @@ public class RenderDataFactory { long A = this.sectionData[idx * 2]; - this.blockMesher.putNext((side == 0 ? 0L : 1L) | - A | - ((neighborId&(0xFFL<<56))>>1) + this.blockMesher.putNext(((side == 0) ? 0L : 1L) | + (A&~LM) | + ((neighborId & (0xFFL << 56)) >> 1) ); } } @@ -547,8 +549,8 @@ public class RenderDataFactory { //} this.blockMesher.putNext(((long) facingForward) |//Facing - A | - (lighter&(0xFFL<<55))//Apply lighting + (A&~LM) | + (lighter&LM)//Apply lighting ); } } @@ -629,7 +631,7 @@ public class RenderDataFactory { } this.blockMesher.putNext((side == 0 ? 0L : 1L) | - A | + (A&~LM) | ((neighborId&(0xFFL<<56))>>1) ); } @@ -764,6 +766,8 @@ public class RenderDataFactory { } } + + //TODO: LIGHTING if (ModelQueries.faceExists(B, (axis<<1)|1) && ((side==1&&!fail) || (side==0&&!failB))) { this.blockMesher.putNext((long) (false ? 0L : 1L) | A | @@ -928,8 +932,8 @@ public class RenderDataFactory { //Example thing thats just wrong but as example mesher.putNext(((long) facingForward) |//Facing - selfModel | - (nextModel&(0xFFL<<55)) + (selfModel&~LM) | + (nextModel&LM) ); } } @@ -990,7 +994,7 @@ public class RenderDataFactory { ma.skip(skipA); skipA = 0; long A = this.sectionData[(i<<5) * 2]; ma.putNext(0L | - A | + (A&~LM) | ((neighborId&(0xFFL<<56))>>1) ); } else {skipA++;} @@ -1011,7 +1015,7 @@ public class RenderDataFactory { mb.skip(skipB); skipB = 0; long A = this.sectionData[(i*32+31) * 2]; mb.putNext(1L | - A | + (A&~LM) | ((neighborId&(0xFFL<<56))>>1) ); } else {skipB++;} @@ -1136,8 +1140,8 @@ public class RenderDataFactory { //Example thing thats just wrong but as example mesher.putNext(((long) facingForward) |//Facing - A | - (lighter&(0xFFL<<55))//Lighting + (A&~LM) | + (lighter&LM)//Lighting ); } } @@ -1232,13 +1236,14 @@ public class RenderDataFactory { if (oki) { ma.skip(skipA); skipA = 0; + //TODO: LIGHTING long lightData = ((neighborId&(0xFFL<<56))>>1);//A; //if (!ModelQueries.faceUsesSelfLighting(Am, facingForward|(axis*2))) {//TODO: check this is right // lighter = this.sectionData[bi]; //} ma.putNext(0L | - A | + (A&~LM) | lightData ); } else {skipA++;} @@ -1292,13 +1297,14 @@ public class RenderDataFactory { if (oki) { mb.skip(skipB); skipB = 0; + //TODO: LIGHTING long lightData = ((neighborId&(0xFFL<<56))>>1);//A; //if (!ModelQueries.faceUsesSelfLighting(Am, facingForward|(axis*2))) {//TODO: check this is right // lighter = this.sectionData[bi]; //} mb.putNext(1L | - A | + (A&~LM) | lightData ); } else {skipB++;} @@ -1426,7 +1432,7 @@ public class RenderDataFactory { //TODO: Check (neighborAId!=0) && works oki if ((neighborAId==0 && ModelQueries.faceExists(meta, ((2<<1)|0)^side))||(neighborAId!=0&&shouldMeshNonOpaqueBlockFace(((2<<1)|0)^side, quad, meta, ((long)neighborAId)<<26, neighborAMeta))) { ma.putNext(((long)side)| - quad | + (quad&~LM) | (ModelQueries.faceUsesSelfLighting(meta, ((2<<1)|0)^side)?quad:(((long)neighborLight)<<55)) ); } else { @@ -1435,7 +1441,7 @@ public class RenderDataFactory { if (shouldMeshNonOpaqueBlockFace(((2<<1)|1)^side, quad, meta, neighborBQuad, neighborBMeta)) { mb.putNext(((long)(side^1))| - quad | + (quad&~LM) | ((ModelQueries.faceUsesSelfLighting(meta, ((2<<1)|1)^side)?quad:neighborBQuad)&(0xFFL<<55)) ); } else { diff --git a/src/main/java/me/cortex/voxy/client/core/rendering/util/LightMapHelper.java b/src/main/java/me/cortex/voxy/client/core/rendering/util/LightMapHelper.java index 1f873765..40f0df4d 100644 --- a/src/main/java/me/cortex/voxy/client/core/rendering/util/LightMapHelper.java +++ b/src/main/java/me/cortex/voxy/client/core/rendering/util/LightMapHelper.java @@ -14,6 +14,7 @@ import static org.lwjgl.opengl.GL45.glBindTextureUnit; public class LightMapHelper { public static void bind(int lightingIndex) { + glBindSampler(lightingIndex, 0); glBindTextureUnit(lightingIndex, ((net.minecraft.client.texture.GlTexture)(MinecraftClient.getInstance().gameRenderer.getLightmapTextureManager().getGlTexture())).getGlId()); } } \ No newline at end of file diff --git a/src/main/resources/assets/voxy/shaders/lod/gl46/bindings.glsl b/src/main/resources/assets/voxy/shaders/lod/gl46/bindings.glsl index e47e4295..04e5de6f 100644 --- a/src/main/resources/assets/voxy/shaders/lod/gl46/bindings.glsl +++ b/src/main/resources/assets/voxy/shaders/lod/gl46/bindings.glsl @@ -118,7 +118,7 @@ layout(binding = LIGHTING_SAMPLER_BINDING) uniform sampler2D lightSampler; vec4 getLighting(uint index) { int i2 = int(index); - return texelFetch(lightSampler, ivec2((i2>>4)&0xF, i2&0xF), 0); + return texture(lightSampler, clamp((vec2((i2>>4)&0xF, i2&0xF))/16, vec2(8.0f/255), vec2(248.0f/255))); } #endif diff --git a/src/main/resources/assets/voxy/shaders/lod/gl46/quads2.vert b/src/main/resources/assets/voxy/shaders/lod/gl46/quads2.vert index bc54bbe8..bd07f8db 100644 --- a/src/main/resources/assets/voxy/shaders/lod/gl46/quads2.vert +++ b/src/main/resources/assets/voxy/shaders/lod/gl46/quads2.vert @@ -119,7 +119,7 @@ void main() { flags |= uint(!modelHasMipmaps(model))<<1; //Compute lighting - tinting = getLighting(extractLightId(quad)); + tinting = getLighting(extractLightId(quad)).rgb; //Apply model colour tinting uint tintColour = model.colourTint; @@ -148,11 +148,11 @@ void main() { if (isShaded) { //TODO: make branchless, infact apply ahead of time to the texture itself in ModelManager since that is // per face - if ((face>>1) == 1) { + if ((face>>1) == 1) {//NORTH, SOUTH tinting.xyz *= 0.8f; - } else if ((face>>1) == 2) { + } else if ((face>>1) == 2) {//EAST, WEST tinting.xyz *= 0.6f; - } else if (face == 0){ + } else if (face == 0) {//DOWN tinting.xyz *= 0.5f; } } diff --git a/src/main/resources/assets/voxy/shaders/lod/hierarchical/screenspace.glsl b/src/main/resources/assets/voxy/shaders/lod/hierarchical/screenspace.glsl index 1b15fce1..0d4f97a1 100644 --- a/src/main/resources/assets/voxy/shaders/lod/hierarchical/screenspace.glsl +++ b/src/main/resources/assets/voxy/shaders/lod/hierarchical/screenspace.glsl @@ -131,21 +131,23 @@ bool isCulledByHiz() { vec2 ssize = size * vec2(screenW, screenH); float miplevel = log2(max(max(ssize.x, ssize.y),1)); - if (miplevel > 9.5f) { + //TODO: make a path for if the miplevel would result in the textureSampler sampling a size of 1 + + + miplevel = ceil(miplevel); + miplevel = clamp(miplevel, 0, 20); + + if (miplevel >= 10f) {//Level 9 or 10// TODO: FIX THIS JANK SHIT return false; } - miplevel = ceil(miplevel); - //miplevel = clamp(miplevel, 0, 20); - vec2 midpoint = (maxBB.xy + minBB.xy)*0.5f; - //midpoint = clamp(midpoint, vec2(0), vec2(1)); float testAgainst = minBB.z; //the *2.0f-1.0f converts from the 0->1 range to -1->1 range that depth is in (not having this causes tighter bounds, but causes culling issues in caves) testAgainst = testAgainst*2.0f-1.0f; - bool culled = textureLod(hizDepthSampler, vec3(midpoint, testAgainst), miplevel) < 0.0001f; + bool culled = textureLod(hizDepthSampler, clamp(vec3(midpoint, testAgainst), vec3(0), vec3(1)), miplevel) < 0.0001f; //printf("HiZ sample point: (%f,%f)@%f against %f", midpoint.x, midpoint.y, miplevel, minBB.z); //if ((culled) && node22.lodLevel == 0) {