Skip to content

Commit

Permalink
Merge pull request #12
Browse files Browse the repository at this point in the history
High Resolution Texture Fix
  • Loading branch information
MartinSVK12 authored Sep 28, 2023
2 parents 3eb143d + 43bbb7d commit b56a9a2
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 21 deletions.
12 changes: 12 additions & 0 deletions src/main/java/turniplabs/halplibe/helper/TextureHelper.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package turniplabs.halplibe.helper;

import net.minecraft.core.Global;
import net.minecraft.core.block.Block;
import turniplabs.halplibe.util.TextureHandler;

Expand All @@ -12,6 +13,17 @@ public class TextureHelper {
public static List<TextureHandler> textureHandlers = new ArrayList<>();
public static Map<String, int[]> registeredBlockTextures = new HashMap<>();
public static Map<String, int[]> registeredItemTextures = new HashMap<>();
public static Map<String, Integer> textureDestinationResolutions = new HashMap<>();
public static Map<String, Integer> textureAtlasWidths = new HashMap<>();

static {
// Assign Default Resolutions
textureDestinationResolutions.put("/terrain.png", 16);
textureDestinationResolutions.put("/gui/items.png", 16);
// Assign Default Atlas Widths
textureAtlasWidths.put("/terrain.png", Global.TEXTURE_ATLAS_WIDTH_TILES);
textureAtlasWidths.put("/gui/items.png", Global.TEXTURE_ATLAS_WIDTH_TILES);
}

@Deprecated
public static int[] registerBlockTexture(String modId, String blockTexture) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import net.minecraft.client.Minecraft;
import net.minecraft.client.render.RenderEngine;
import net.minecraft.client.render.dynamictexture.DynamicTexture;
import org.lwjgl.opengl.GL11;
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.callback.CallbackInfo;
Expand All @@ -16,9 +18,17 @@
public abstract class RenderEngineMixin {
@Shadow
private List<DynamicTexture> dynamicTextures;
@Unique
private final RenderEngine thisAsRenderEngine = (RenderEngine)(Object)this;
@Inject(method = "initDynamicTextures", at = @At("TAIL"))
private void initDynamicTextures(CallbackInfo ci) {
Minecraft mc = Minecraft.getMinecraft(Minecraft.class);
for (String key: TextureHelper.textureDestinationResolutions.keySet()) {
// Updates atlas resolution references
GL11.glBindTexture(3553, thisAsRenderEngine.getTexture(key));
int destinationResolution = GL11.glGetTexLevelParameteri(3553, 0, 4096) / TextureHelper.textureAtlasWidths.get(key);
TextureHelper.textureDestinationResolutions.put(key, destinationResolution);
}
TextureHelper.textureHandlers.forEach((handler) -> dynamicTextures.add(handler.newHandler(mc)));
}
}
55 changes: 34 additions & 21 deletions src/main/java/turniplabs/halplibe/util/TextureHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import net.minecraft.client.render.dynamictexture.DynamicTexture;
import net.minecraft.client.render.texturepack.TexturePackList;
import net.minecraft.core.util.helper.Textures;
import turniplabs.halplibe.helper.TextureHelper;

import java.awt.image.BufferedImage;
import java.io.File;
Expand All @@ -15,72 +16,84 @@ public class TextureHandler extends DynamicTexture {
private final String animationSource;
private final int textureIndex;
private final int resolution;
private final int defaultResolution;
private final int width;
private final byte[] frames;
private int elapsedTicks = 0;
private static final Minecraft fakeMc;
private final float scale;

public TextureHandler(String textureName, String animationSource, int textureIndex, int resolution, int width) {
this(textureName, animationSource, textureIndex, resolution, width, fakeMc);
}
public TextureHandler(String textureName, String animationSource, int textureIndex, int resolution, int width, Minecraft mc) {
super(textureIndex, resolution, width);
public TextureHandler(String textureName, String animationSource, int textureIndex, int defaultResolution, int width, Minecraft mc) {
super(textureIndex, getAtlasResolution(textureName, defaultResolution), width);
this.textureName = textureName;
this.animationSource = animationSource;
this.textureIndex = textureIndex;
this.resolution = resolution;
this.defaultResolution = defaultResolution;
BufferedImage image = Textures.readImage(mc.texturePackList.selectedTexturePack.getResourceAsStream(animationSource));
this.resolution = image.getWidth();
// Scaling factor from source resolution to destination resolution
this.scale = getScale(textureName, resolution);
this.width = width;

BufferedImage image = Textures.readImage(mc.texturePackList.selectedTexturePack.getResourceAsStream(animationSource));

if (image == Textures.missingTexture) {
throw new RuntimeException("Animation " + animationSource + " couldn't be found!");
} else if (image.getWidth() != resolution) {
throw new RuntimeException("Animation " + animationSource + " doesn't have the same width as textures in " + textureName + "!");
} else if (image.getHeight() % image.getWidth() != 0) {
throw new RuntimeException("Invalid Height for animation! " + animationSource);
} else {
this.frameCount = image.getHeight() / image.getWidth();
System.out.println("Frame Count: " + this.frameCount);
this.frames = new byte[resolution * resolution * 4 * this.frameCount];
this.frames = new byte[(int) (resolution * resolution * 4 * this.frameCount * scale * scale)];

for (int frame = 0; frame < this.frameCount; ++frame) {
for (int x = 0; x < resolution; ++x) {
for (int y = 0; y < resolution; ++y) {
int c = image.getRGB(x, frame * resolution + y);
putPixel(this.frames, frame * resolution * resolution + y * resolution + x, c);
for (int x = 0; x < resolution * scale; ++x) {
for (int y = 0; y < resolution * scale; ++y) {
int c = image.getRGB((int) (x/scale), (frame * resolution + (int)(y/scale)));
putPixel(this.frames, (int) (frame * resolution * scale * scale * resolution + y * resolution * scale + x), c);
}
}
}

}
}
public TextureHandler newHandler(Minecraft mc){
// Returns a new TextureHandler using the current state of Minecraft, If the texturepack has changed it will then use the new texturepack's textures
return new TextureHandler(textureName, animationSource, textureIndex, resolution, width, mc);
return new TextureHandler(textureName, animationSource, textureIndex, defaultResolution, width, mc);
}
public void update() {
this.elapsedTicks = (this.elapsedTicks + 1) % this.frameCount;

for (int i = 0; i < this.resolution; ++i) {
for (int j = 0; j < this.resolution; ++j) {
transferPixel(this.frames, this.elapsedTicks * this.resolution * this.resolution + j * this.resolution + i, this.imageData, j * this.resolution + i);
for (int i = 0; i < this.resolution * scale; ++i) {
for (int j = 0; j < this.resolution * scale; ++j) {
transferPixel(this.frames, (int) (this.elapsedTicks * this.resolution * this.resolution * scale * scale + j * this.resolution * scale + i), this.imageData, (int) (j * this.resolution * scale + i));
}
}

}

public String getTextureName() {
return this.textureName;
}
public static float getScale(String textureName, int resolution){
if (TextureHelper.textureDestinationResolutions.get(textureName) != null){
return (float)TextureHelper.textureDestinationResolutions.get(textureName)/resolution;
}
return 1f;
}
public static int getAtlasResolution(String textureName, int defaultResolution){
if (TextureHelper.textureDestinationResolutions.get(textureName) != null){
return TextureHelper.textureDestinationResolutions.get(textureName);
}
return defaultResolution;
}

static {
// Create a Minecraft Container with the needed information to load the texture-pack
Minecraft mc = Minecraft.getMinecraft(Minecraft.class);
File mcdir = mc.getMinecraftDir();
GameSettings gameSettings = new GameSettings(mc, mcdir);
mc.gameSettings = gameSettings;
TexturePackList packList = new TexturePackList(mc, mcdir);
mc.texturePackList = packList;
mc.gameSettings = new GameSettings(mc, mcdir);
mc.texturePackList = new TexturePackList(mc, mcdir);
fakeMc = mc;
}
}

0 comments on commit b56a9a2

Please sign in to comment.