From ec7c292adc3faf83b162fe837e85e8118455c80e Mon Sep 17 00:00:00 2001 From: UselessBullets <80850784+UselessBullets@users.noreply.github.com> Date: Sun, 5 Nov 2023 15:35:06 -0600 Subject: [PATCH 1/3] Mostly Functional TextureSpace --- .../java/turniplabs/halplibe/HalpLibe.java | 2 + .../mixin/mixins/RenderEngineMixin.java | 64 +++++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/src/main/java/turniplabs/halplibe/HalpLibe.java b/src/main/java/turniplabs/halplibe/HalpLibe.java index d192854..e2cef38 100644 --- a/src/main/java/turniplabs/halplibe/HalpLibe.java +++ b/src/main/java/turniplabs/halplibe/HalpLibe.java @@ -2,6 +2,7 @@ import net.fabricmc.api.ModInitializer; import net.fabricmc.loader.api.entrypoint.PreLaunchEntrypoint; +import net.minecraft.core.Global; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import turniplabs.halplibe.helper.AchievementHelper; @@ -14,6 +15,7 @@ public class HalpLibe implements ModInitializer, PreLaunchEntrypoint { public static final String MOD_ID = "halplibe"; public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID); static { + Global.TEXTURE_ATLAS_WIDTH_TILES = 64; // this is here to possibly fix some class loading issues but might not work anyway, delete if it causes even more problems try { Class.forName("net.minecraft.core.block.Block"); diff --git a/src/main/java/turniplabs/halplibe/mixin/mixins/RenderEngineMixin.java b/src/main/java/turniplabs/halplibe/mixin/mixins/RenderEngineMixin.java index 7ae5ee8..f2ac657 100644 --- a/src/main/java/turniplabs/halplibe/mixin/mixins/RenderEngineMixin.java +++ b/src/main/java/turniplabs/halplibe/mixin/mixins/RenderEngineMixin.java @@ -1,23 +1,41 @@ package turniplabs.halplibe.mixin.mixins; import net.minecraft.client.Minecraft; +import net.minecraft.client.option.enums.MipmapType; import net.minecraft.client.render.RenderEngine; import net.minecraft.client.render.dynamictexture.DynamicTexture; +import net.minecraft.core.util.helper.Buffer; +import net.minecraft.core.util.helper.Textures; import org.lwjgl.opengl.GL11; +import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.Unique; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.Redirect; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; import turniplabs.halplibe.helper.TextureHelper; +import java.awt.*; +import java.awt.image.BufferedImage; +import java.awt.image.WritableRaster; +import java.nio.ByteBuffer; import java.util.List; @Mixin(value = RenderEngine.class, remap = false) public abstract class RenderEngineMixin { @Shadow private List dynamicTextures; + + @Shadow public abstract int getTexture(String name); + + @Shadow protected abstract void generateMipmaps(ByteBuffer buffer, BufferedImage image, int levels, boolean smooth); + + @Shadow @Final public Minecraft minecraft; + @Shadow private boolean clampTexture; + @Shadow private boolean blurTexture; @Unique private final RenderEngine thisAsRenderEngine = (RenderEngine)(Object)this; @Inject(method = "initDynamicTextures", at = @At("TAIL")) @@ -31,4 +49,50 @@ private void initDynamicTextures(CallbackInfo ci) { } TextureHelper.textureHandlers.forEach((handler) -> dynamicTextures.add(handler.newHandler(mc))); } + private String textureName = ""; + @Inject(method = "getTexture(Ljava/lang/String;)I", at = @At("HEAD")) + private void redirectAtlasTextures(String name, CallbackInfoReturnable cir){ + textureName = name; + } + @Inject(method = "setupTexture(Ljava/lang/String;)V", at = @At("HEAD")) + private void redirectAtlasTextures2(String name, CallbackInfo ci){ + textureName = name; + } + @Inject(method = "setupTexture(Ljava/awt/image/BufferedImage;IZ)V", at = @At("HEAD"), cancellable = true) + private void adjustAtlasSize(BufferedImage img, int id, boolean mipmap, CallbackInfo ci){ + if (textureName.equals("/terrain.png") || textureName.equals("/gui/items.png")){ + textureName = ""; + BufferedImage testImage = new BufferedImage(img.getWidth()*2, img.getHeight()*2, img.getType()); + for (int x = 0; x < img.getWidth(); x++) { + for (int y = 0; y < img.getHeight(); y++) { + testImage.setRGB(x, y, img.getRGB(x, y)); + } + } + + GL11.glBindTexture(3553, id); + if (this.blurTexture) { + GL11.glTexParameteri(3553, 10241, 9729); + GL11.glTexParameteri(3553, 10240, 9729); + } else { + GL11.glTexParameteri(3553, 10241, 9728); + GL11.glTexParameteri(3553, 10240, 9728); + } + if (this.clampTexture) { + GL11.glTexParameteri(3553, 10242, 10496); + GL11.glTexParameteri(3553, 10243, 10496); + } else { + GL11.glTexParameteri(3553, 10242, 10497); + GL11.glTexParameteri(3553, 10243, 10497); + } + int w = testImage.getWidth(); + int h = testImage.getHeight(); + Buffer.put(testImage); + GL11.glTexImage2D(3553, 0, 6408, w, h, 0, 6408, 5121, Buffer.buffer); + if (mipmap) { + this.generateMipmaps(Buffer.buffer, testImage, (Integer)this.minecraft.gameSettings.mipmapLevels.value, this.minecraft.gameSettings.mipmapType.value == MipmapType.SMOOTH); + } + ci.cancel(); + } + textureName = ""; + } } \ No newline at end of file From 8fa06887e487b43cf38a85ae2f2358814851db02 Mon Sep 17 00:00:00 2001 From: UselessBullets <80850784+UselessBullets@users.noreply.github.com> Date: Sun, 5 Nov 2023 16:15:46 -0600 Subject: [PATCH 2/3] Replaced hacky texture name, with hacky flag --- .../halplibe/helper/BlockCoords.java | 5 ++-- .../halplibe/helper/ItemCoords.java | 9 ++++--- .../mixin/mixins/RenderEngineMixin.java | 26 +++++++++---------- 3 files changed, 21 insertions(+), 19 deletions(-) diff --git a/src/main/java/turniplabs/halplibe/helper/BlockCoords.java b/src/main/java/turniplabs/halplibe/helper/BlockCoords.java index bbddf99..74d96ac 100644 --- a/src/main/java/turniplabs/halplibe/helper/BlockCoords.java +++ b/src/main/java/turniplabs/halplibe/helper/BlockCoords.java @@ -1,5 +1,6 @@ package turniplabs.halplibe.helper; +import net.minecraft.core.Global; import turniplabs.halplibe.HalpLibe; public class BlockCoords { @@ -12,9 +13,9 @@ public static int[] nextCoords() { case 0: { int x = lastX; int y = lastY; - if (++lastX > 31) { + if (++lastX > Global.TEXTURE_ATLAS_WIDTH_TILES-1) { lastX = 22; - if (++lastY > 31) { + if (++lastY > Global.TEXTURE_ATLAS_WIDTH_TILES-1) { area = 1; lastX = 7; lastY = 16; diff --git a/src/main/java/turniplabs/halplibe/helper/ItemCoords.java b/src/main/java/turniplabs/halplibe/helper/ItemCoords.java index fd03d96..504b933 100644 --- a/src/main/java/turniplabs/halplibe/helper/ItemCoords.java +++ b/src/main/java/turniplabs/halplibe/helper/ItemCoords.java @@ -1,5 +1,6 @@ package turniplabs.halplibe.helper; +import net.minecraft.core.Global; import turniplabs.halplibe.HalpLibe; public class ItemCoords { @@ -12,9 +13,9 @@ public static int[] nextCoords() { case 0: { int x = lastX; int y = lastY; - if (++lastX > 31) { + if (++lastX > Global.TEXTURE_ATLAS_WIDTH_TILES-1) { lastX = 16; - if (++lastY > 31) { + if (++lastY > Global.TEXTURE_ATLAS_WIDTH_TILES-1) { area = 1; lastX = 0; lastY = 16; @@ -27,7 +28,7 @@ public static int[] nextCoords() { int y = lastY; if (++lastX > 15) { lastX = 0; - if (++lastY > 31) { + if (++lastY > Global.TEXTURE_ATLAS_WIDTH_TILES-1) { area = 2; HalpLibe.LOGGER.info("Reached the end of item texture space!"); } @@ -36,7 +37,7 @@ public static int[] nextCoords() { } default: HalpLibe.LOGGER.info("No more item texture spaces are available!"); - return new int[]{15, 31}; + return new int[]{15, Global.TEXTURE_ATLAS_WIDTH_TILES-1}; } } } diff --git a/src/main/java/turniplabs/halplibe/mixin/mixins/RenderEngineMixin.java b/src/main/java/turniplabs/halplibe/mixin/mixins/RenderEngineMixin.java index f2ac657..580626a 100644 --- a/src/main/java/turniplabs/halplibe/mixin/mixins/RenderEngineMixin.java +++ b/src/main/java/turniplabs/halplibe/mixin/mixins/RenderEngineMixin.java @@ -23,6 +23,7 @@ import java.awt.image.WritableRaster; import java.nio.ByteBuffer; import java.util.List; +import java.util.Map; @Mixin(value = RenderEngine.class, remap = false) public abstract class RenderEngineMixin { @@ -36,6 +37,7 @@ public abstract class RenderEngineMixin { @Shadow @Final public Minecraft minecraft; @Shadow private boolean clampTexture; @Shadow private boolean blurTexture; + @Shadow private Map textureMap; @Unique private final RenderEngine thisAsRenderEngine = (RenderEngine)(Object)this; @Inject(method = "initDynamicTextures", at = @At("TAIL")) @@ -49,19 +51,18 @@ private void initDynamicTextures(CallbackInfo ci) { } TextureHelper.textureHandlers.forEach((handler) -> dynamicTextures.add(handler.newHandler(mc))); } - private String textureName = ""; - @Inject(method = "getTexture(Ljava/lang/String;)I", at = @At("HEAD")) - private void redirectAtlasTextures(String name, CallbackInfoReturnable cir){ - textureName = name; - } - @Inject(method = "setupTexture(Ljava/lang/String;)V", at = @At("HEAD")) - private void redirectAtlasTextures2(String name, CallbackInfo ci){ - textureName = name; - } @Inject(method = "setupTexture(Ljava/awt/image/BufferedImage;IZ)V", at = @At("HEAD"), cancellable = true) private void adjustAtlasSize(BufferedImage img, int id, boolean mipmap, CallbackInfo ci){ - if (textureName.equals("/terrain.png") || textureName.equals("/gui/items.png")){ - textureName = ""; + boolean doResize = false; + for (String key : textureMap.keySet()) { + if (key.equals("/terrain.png") || key.equals("/gui/items.png")) { + doResize = textureMap.get(key) == id; + if (doResize) { + break; + } + } + } + if (mipmap){ BufferedImage testImage = new BufferedImage(img.getWidth()*2, img.getHeight()*2, img.getType()); for (int x = 0; x < img.getWidth(); x++) { for (int y = 0; y < img.getHeight(); y++) { @@ -89,10 +90,9 @@ private void adjustAtlasSize(BufferedImage img, int id, boolean mipmap, Callback Buffer.put(testImage); GL11.glTexImage2D(3553, 0, 6408, w, h, 0, 6408, 5121, Buffer.buffer); if (mipmap) { - this.generateMipmaps(Buffer.buffer, testImage, (Integer)this.minecraft.gameSettings.mipmapLevels.value, this.minecraft.gameSettings.mipmapType.value == MipmapType.SMOOTH); + this.generateMipmaps(Buffer.buffer, testImage, this.minecraft.gameSettings.mipmapLevels.value, this.minecraft.gameSettings.mipmapType.value == MipmapType.SMOOTH); } ci.cancel(); } - textureName = ""; } } \ No newline at end of file From 02ac17093b693a0f5ddda8f9fd75a72f7dd0cee1 Mon Sep 17 00:00:00 2001 From: UselessBullets <80850784+UselessBullets@users.noreply.github.com> Date: Mon, 6 Nov 2023 02:30:31 -0600 Subject: [PATCH 3/3] Texture Atlas can be set to any arbitrary Number Texture Atlas can be set to any arbitrary Number Added Config file for changing the Atlas size Only Rescale when needed Cleanup --- .../java/turniplabs/halplibe/HalpLibe.java | 13 +++++- .../mixin/mixins/RenderEngineMixin.java | 44 ++++++++----------- .../halplibe/util/TextureHandler.java | 2 +- 3 files changed, 31 insertions(+), 28 deletions(-) diff --git a/src/main/java/turniplabs/halplibe/HalpLibe.java b/src/main/java/turniplabs/halplibe/HalpLibe.java index e2cef38..21d83e2 100644 --- a/src/main/java/turniplabs/halplibe/HalpLibe.java +++ b/src/main/java/turniplabs/halplibe/HalpLibe.java @@ -7,16 +7,25 @@ import org.slf4j.LoggerFactory; import turniplabs.halplibe.helper.AchievementHelper; import turniplabs.halplibe.helper.NetworkHelper; +import turniplabs.halplibe.util.TomlConfigHandler; import turniplabs.halplibe.util.achievements.AchievementPage; import turniplabs.halplibe.util.achievements.VanillaAchievementsPage; import turniplabs.halplibe.util.network.PacketExtendedMobSpawn; +import turniplabs.halplibe.util.toml.Toml; public class HalpLibe implements ModInitializer, PreLaunchEntrypoint { public static final String MOD_ID = "halplibe"; public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID); + public static final TomlConfigHandler CONFIG; static { - Global.TEXTURE_ATLAS_WIDTH_TILES = 64; - // this is here to possibly fix some class loading issues but might not work anyway, delete if it causes even more problems + Toml toml = new Toml(); + toml.addCategory("Experimental"); + toml.addEntry("Experimental.AtlasWidth", "Dynamically resized the Terrain and Item atlases, value must be an integer greater than or equal to 32",32); + + CONFIG = new TomlConfigHandler(MOD_ID, toml); + + Global.TEXTURE_ATLAS_WIDTH_TILES = Math.max(32, CONFIG.getInt("Experimental.AtlasWidth")); + // Initialize Block and Item static fields try { Class.forName("net.minecraft.core.block.Block"); Class.forName("net.minecraft.core.item.Item"); diff --git a/src/main/java/turniplabs/halplibe/mixin/mixins/RenderEngineMixin.java b/src/main/java/turniplabs/halplibe/mixin/mixins/RenderEngineMixin.java index 580626a..1a4f047 100644 --- a/src/main/java/turniplabs/halplibe/mixin/mixins/RenderEngineMixin.java +++ b/src/main/java/turniplabs/halplibe/mixin/mixins/RenderEngineMixin.java @@ -4,8 +4,8 @@ import net.minecraft.client.option.enums.MipmapType; import net.minecraft.client.render.RenderEngine; import net.minecraft.client.render.dynamictexture.DynamicTexture; +import net.minecraft.core.Global; import net.minecraft.core.util.helper.Buffer; -import net.minecraft.core.util.helper.Textures; import org.lwjgl.opengl.GL11; import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; @@ -13,31 +13,22 @@ import org.spongepowered.asm.mixin.Unique; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.Redirect; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; import turniplabs.halplibe.helper.TextureHelper; -import java.awt.*; import java.awt.image.BufferedImage; -import java.awt.image.WritableRaster; import java.nio.ByteBuffer; import java.util.List; -import java.util.Map; @Mixin(value = RenderEngine.class, remap = false) public abstract class RenderEngineMixin { @Shadow private List dynamicTextures; - - @Shadow public abstract int getTexture(String name); - @Shadow protected abstract void generateMipmaps(ByteBuffer buffer, BufferedImage image, int levels, boolean smooth); @Shadow @Final public Minecraft minecraft; @Shadow private boolean clampTexture; @Shadow private boolean blurTexture; - @Shadow private Map textureMap; @Unique private final RenderEngine thisAsRenderEngine = (RenderEngine)(Object)this; @Inject(method = "initDynamicTextures", at = @At("TAIL")) @@ -53,20 +44,23 @@ private void initDynamicTextures(CallbackInfo ci) { } @Inject(method = "setupTexture(Ljava/awt/image/BufferedImage;IZ)V", at = @At("HEAD"), cancellable = true) private void adjustAtlasSize(BufferedImage img, int id, boolean mipmap, CallbackInfo ci){ - boolean doResize = false; - for (String key : textureMap.keySet()) { - if (key.equals("/terrain.png") || key.equals("/gui/items.png")) { - doResize = textureMap.get(key) == id; - if (doResize) { - break; - } +// boolean doResize = false; +// for (String key : textureMap.keySet()) { +// if (key.equals("/terrain.png") || key.equals("/gui/items.png")) { +// doResize = textureMap.get(key) == id; +// if (doResize) { +// break; +// } +// } +// } + if (mipmap){ // TODO Replace with a way to directly detect if a texture is Terrain.png or Items.png + BufferedImage resizedAtlas = new BufferedImage((img.getWidth()/32) * Global.TEXTURE_ATLAS_WIDTH_TILES, (img.getHeight()/32) * Global.TEXTURE_ATLAS_WIDTH_TILES, img.getType()); + if (img.getWidth() == resizedAtlas.getWidth() && img.getHeight() == resizedAtlas.getHeight()){ + return; // Don't scale and transfer image if they're the same size } - } - if (mipmap){ - BufferedImage testImage = new BufferedImage(img.getWidth()*2, img.getHeight()*2, img.getType()); for (int x = 0; x < img.getWidth(); x++) { for (int y = 0; y < img.getHeight(); y++) { - testImage.setRGB(x, y, img.getRGB(x, y)); + resizedAtlas.setRGB(x, y, img.getRGB(x, y)); } } @@ -85,12 +79,12 @@ private void adjustAtlasSize(BufferedImage img, int id, boolean mipmap, Callback GL11.glTexParameteri(3553, 10242, 10497); GL11.glTexParameteri(3553, 10243, 10497); } - int w = testImage.getWidth(); - int h = testImage.getHeight(); - Buffer.put(testImage); + int w = resizedAtlas.getWidth(); + int h = resizedAtlas.getHeight(); + Buffer.put(resizedAtlas); GL11.glTexImage2D(3553, 0, 6408, w, h, 0, 6408, 5121, Buffer.buffer); if (mipmap) { - this.generateMipmaps(Buffer.buffer, testImage, this.minecraft.gameSettings.mipmapLevels.value, this.minecraft.gameSettings.mipmapType.value == MipmapType.SMOOTH); + this.generateMipmaps(Buffer.buffer, resizedAtlas, this.minecraft.gameSettings.mipmapLevels.value, this.minecraft.gameSettings.mipmapType.value == MipmapType.SMOOTH); } ci.cancel(); } diff --git a/src/main/java/turniplabs/halplibe/util/TextureHandler.java b/src/main/java/turniplabs/halplibe/util/TextureHandler.java index e350e09..4df7ac1 100644 --- a/src/main/java/turniplabs/halplibe/util/TextureHandler.java +++ b/src/main/java/turniplabs/halplibe/util/TextureHandler.java @@ -45,7 +45,7 @@ public TextureHandler(String textureName, String animationSource, int textureInd throw new RuntimeException("Invalid Height for animation! " + animationSource); } else { this.frameCount = image.getHeight() / image.getWidth(); - System.out.println(textureName + " frame Count: " + this.frameCount); + System.out.println(animationSource + " frame Count: " + this.frameCount); this.frames = new byte[(int) (resolution * resolution * 4 * this.frameCount * scale * scale)]; for (int frame = 0; frame < this.frameCount; ++frame) {