diff --git a/src/main/java/me/cortex/zenith/client/core/model/ModelManager.java b/src/main/java/me/cortex/zenith/client/core/model/ModelManager.java index f844c7a3..93a4fe02 100644 --- a/src/main/java/me/cortex/zenith/client/core/model/ModelManager.java +++ b/src/main/java/me/cortex/zenith/client/core/model/ModelManager.java @@ -252,10 +252,10 @@ public class ModelManager { boolean needsAlphaDiscard = ((float)writeCount)/area<0.9;//If the amount of area covered by written pixels is less than a threashold, disable discard as its not needed needsAlphaDiscard |= blockRenderLayer != RenderLayer.getSolid(); - + needsAlphaDiscard &= blockRenderLayer != RenderLayer.getTranslucent();//Translucent doesnt have alpha discard faceModelData |= needsAlphaDiscard?1<<22:0; - faceModelData |= (!faceCoversFullBlock)?1<<23:0; + faceModelData |= ((!faceCoversFullBlock)&&blockRenderLayer != RenderLayer.getTranslucent())?1<<23:0;//Alpha discard override, translucency doesnt have alpha discard MemoryUtil.memPutInt(faceUploadPtr, faceModelData); } @@ -296,7 +296,7 @@ public class ModelManager { this.putTextures(modelId, textureData); - glGenerateTextureMipmap(this.textures.id); + //glGenerateTextureMipmap(this.textures.id); return modelId; } @@ -517,6 +517,7 @@ public class ModelManager { private void putTextures(int id, ColourDepthTextureData[] textures) { int X = (id&0xFF) * this.modelTextureSize*3; int Y = ((id>>8)&0xFF) * this.modelTextureSize*2; + for (int subTex = 0; subTex < 6; subTex++) { int x = X + (subTex%3)*this.modelTextureSize; int y = Y + (subTex/3)*this.modelTextureSize; @@ -525,7 +526,25 @@ public class ModelManager { GlStateManager._pixelStore(GlConst.GL_UNPACK_SKIP_PIXELS, 0); GlStateManager._pixelStore(GlConst.GL_UNPACK_SKIP_ROWS, 0); GlStateManager._pixelStore(GlConst.GL_UNPACK_ALIGNMENT, 4); - glTextureSubImage2D(this.textures.id, 0, x, y, this.modelTextureSize, this.modelTextureSize, GL_RGBA, GL_UNSIGNED_BYTE, textures[subTex].colour()); + var current = textures[subTex].colour(); + var next = new int[current.length>>1]; + for (int i = 0; i < 4; i++) { + glTextureSubImage2D(this.textures.id, i, x>>i, y>>i, this.modelTextureSize>>i, this.modelTextureSize>>i, GL_RGBA, GL_UNSIGNED_BYTE, current); + + int size = this.modelTextureSize>>(i+1); + for (int pX = 0; pX < size; pX++) { + for (int pY = 0; pY < size; pY++) { + int C00 = current[(pY*2)*size+pX*2]; + int C01 = current[(pY*2+1)*size+pX*2]; + int C10 = current[(pY*2)*size+pX*2+1]; + int C11 = current[(pY*2+1)*size+pX*2+1]; + next[pY*size+pX] = TextureUtils.mipColours(C00, C01, C10, C11); + } + } + + current = next; + next = new int[current.length>>1]; + } } } diff --git a/src/main/java/me/cortex/zenith/client/core/model/TextureUtils.java b/src/main/java/me/cortex/zenith/client/core/model/TextureUtils.java index 2c828379..1be8948e 100644 --- a/src/main/java/me/cortex/zenith/client/core/model/TextureUtils.java +++ b/src/main/java/me/cortex/zenith/client/core/model/TextureUtils.java @@ -1,5 +1,10 @@ package me.cortex.zenith.client.core.model; +import me.jellysquid.mods.sodium.client.util.color.ColorSRGB; +import net.minecraft.util.math.ColorHelper; +import org.spongepowered.asm.mixin.Overwrite; +import org.spongepowered.asm.mixin.Unique; + import java.util.Map; //Texturing utils to manipulate data from the model bakery @@ -158,4 +163,58 @@ public class TextureUtils { return new int[]{minX, maxX, minY, maxY}; } + + + + + + + + + + + + + + + public static int mipColours(int one, int two, int three, int four) { + return weightedAverageColor(weightedAverageColor(one, two), weightedAverageColor(three, four)); + } + + private static int weightedAverageColor(int one, int two) { + int alphaOne = ColorHelper.Abgr.getAlpha(one); + int alphaTwo = ColorHelper.Abgr.getAlpha(two); + if (alphaOne == alphaTwo) { + return averageRgb(one, two, alphaOne); + } else if (alphaOne == 0) { + return two & 16777215 | alphaTwo >> 2 << 24; + } else if (alphaTwo == 0) { + return one & 16777215 | alphaOne >> 2 << 24; + } else { + float scale = 1.0F / (float)(alphaOne + alphaTwo); + float relativeWeightOne = (float)alphaOne * scale; + float relativeWeightTwo = (float)alphaTwo * scale; + float oneR = ColorSRGB.srgbToLinear(ColorHelper.Abgr.getRed(one)) * relativeWeightOne; + float oneG = ColorSRGB.srgbToLinear(ColorHelper.Abgr.getGreen(one)) * relativeWeightOne; + float oneB = ColorSRGB.srgbToLinear(ColorHelper.Abgr.getBlue(one)) * relativeWeightOne; + float twoR = ColorSRGB.srgbToLinear(ColorHelper.Abgr.getRed(two)) * relativeWeightTwo; + float twoG = ColorSRGB.srgbToLinear(ColorHelper.Abgr.getGreen(two)) * relativeWeightTwo; + float twoB = ColorSRGB.srgbToLinear(ColorHelper.Abgr.getBlue(two)) * relativeWeightTwo; + float linearR = oneR + twoR; + float linearG = oneG + twoG; + float linearB = oneB + twoB; + int averageAlpha = alphaOne + alphaTwo >> 1; + return ColorSRGB.linearToSrgb(linearR, linearG, linearB, averageAlpha); + } + } + + private static int averageRgb(int a, int b, int alpha) { + float ar = ColorSRGB.srgbToLinear(ColorHelper.Abgr.getRed(a)); + float ag = ColorSRGB.srgbToLinear(ColorHelper.Abgr.getGreen(a)); + float ab = ColorSRGB.srgbToLinear(ColorHelper.Abgr.getBlue(a)); + float br = ColorSRGB.srgbToLinear(ColorHelper.Abgr.getRed(b)); + float bg = ColorSRGB.srgbToLinear(ColorHelper.Abgr.getGreen(b)); + float bb = ColorSRGB.srgbToLinear(ColorHelper.Abgr.getBlue(b)); + return ColorSRGB.linearToSrgb((ar + br) * 0.5F, (ag + bg) * 0.5F, (ab + bb) * 0.5F, alpha); + } } diff --git a/src/main/java/me/cortex/zenith/common/world/other/Mapper.java b/src/main/java/me/cortex/zenith/common/world/other/Mapper.java index 1e4b8037..f2be2103 100644 --- a/src/main/java/me/cortex/zenith/common/world/other/Mapper.java +++ b/src/main/java/me/cortex/zenith/common/world/other/Mapper.java @@ -67,6 +67,10 @@ public class Mapper { return (int) ((id>>56)&0xFF); } + public static long withLight(long id, int light) { + return (id&(~(0xFFL<<56)))|(Integer.toUnsignedLong(light)<<56); + } + public void setCallbacks(Consumer stateCallback, Consumer biomeCallback) { this.newStateCallback = stateCallback; this.newBiomeCallback = biomeCallback; diff --git a/src/main/java/me/cortex/zenith/common/world/other/Mipper.java b/src/main/java/me/cortex/zenith/common/world/other/Mipper.java index a65e3d45..fbb28b41 100644 --- a/src/main/java/me/cortex/zenith/common/world/other/Mipper.java +++ b/src/main/java/me/cortex/zenith/common/world/other/Mipper.java @@ -1,5 +1,7 @@ package me.cortex.zenith.common.world.other; +import static me.cortex.zenith.common.world.other.Mapper.withLight; + //Mipper for data public class Mipper { //TODO: also pass in the level its mipping from, cause at lower levels you want to preserve block details @@ -35,6 +37,13 @@ public class Mipper { return I000; } //TODO: need to account for different light levels of "air" - return 0; + int blockLight = (Mapper.getLightId(I000)&0xF0)+(Mapper.getLightId(I001)&0xF0)+(Mapper.getLightId(I010)&0xF0)+(Mapper.getLightId(I011)&0xF0)+ + (Mapper.getLightId(I100)&0xF0)+(Mapper.getLightId(I101)&0xF0)+(Mapper.getLightId(I110)&0xF0)+(Mapper.getLightId(I111)&0xF0); + int skyLight = (Mapper.getLightId(I000)&0x0F)+(Mapper.getLightId(I001)&0x0F)+(Mapper.getLightId(I010)&0x0F)+(Mapper.getLightId(I011)&0x0F)+ + (Mapper.getLightId(I100)&0x0F)+(Mapper.getLightId(I101)&0x0F)+(Mapper.getLightId(I110)&0x0F)+(Mapper.getLightId(I111)&0x0F); + blockLight = blockLight/8; + skyLight = (int) Math.ceil((double)skyLight/8); + + return withLight(I111, (blockLight<<4)|skyLight); } } diff --git a/src/main/resources/assets/zenith/shaders/lod/gl46/quads.frag b/src/main/resources/assets/zenith/shaders/lod/gl46/quads.frag index 05a9dd4c..d1c7dc08 100644 --- a/src/main/resources/assets/zenith/shaders/lod/gl46/quads.frag +++ b/src/main/resources/assets/zenith/shaders/lod/gl46/quads.frag @@ -13,7 +13,7 @@ layout(location = 0) out vec4 outColour; void main() { vec2 uv = mod(uv, vec2(1))*(1f/(vec2(3,2)*256f)); vec4 colour = texture(blockModelAtlas, uv + baseUV); - if (discardAlpha == 1 && colour.a <= 0.001f) { + if (discardAlpha == 1 && colour.a <= 0.25f) { discard; } outColour = colour * colourTinting;