Fixed For emissive texture loading

This commit is contained in:
Frank 2022-10-25 23:57:27 +02:00
parent bfebba6791
commit 9fcec2b033
3 changed files with 145 additions and 85 deletions

View file

@ -229,7 +229,6 @@ public class NamedPathConfig extends PathConfig {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private <T> T _get(ConfigToken<T> what, boolean raw) { private <T> T _get(ConfigToken<T> what, boolean raw) {
//TODO: Check if we can make config fully Generic to avoid runtime type checks...
if (ConfigKeeper.BooleanEntry.class.isAssignableFrom(what.type)) { if (ConfigKeeper.BooleanEntry.class.isAssignableFrom(what.type)) {
return (T) _getBoolean((ConfigToken<Boolean>) what, raw); return (T) _getBoolean((ConfigToken<Boolean>) what, raw);
} }

View file

@ -1,97 +1,158 @@
package org.betterx.bclib.mixin.client; package org.betterx.bclib.mixin.client;
import net.minecraft.client.renderer.texture.TextureAtlas; import org.betterx.bclib.BCLib;
import org.betterx.bclib.client.render.EmissiveTextureInfo;
import com.mojang.blaze3d.platform.NativeImage;
import net.minecraft.client.renderer.texture.SpriteContents;
import net.minecraft.client.renderer.texture.SpriteLoader;
import net.minecraft.client.resources.metadata.animation.AnimationMetadataSection;
import net.minecraft.client.resources.metadata.animation.FrameSize;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.resources.Resource;
import net.minecraft.server.packs.resources.ResourceManager;
import net.minecraft.util.Mth;
import net.fabricmc.loader.api.FabricLoader;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
@Mixin(TextureAtlas.class) import java.io.IOException;
import java.util.Optional;
import java.util.function.BiConsumer;
@Mixin(SpriteLoader.class)
public class TextureAtlasMixin { public class TextureAtlasMixin {
@Shadow
@Final
private ResourceLocation location;
private static final int EMISSIVE_ALPHA = 254 << 24; private static final int EMISSIVE_ALPHA = 254 << 24;
private boolean bclib_modifyAtlas; private boolean bclib_modifyAtlas;
private static ResourceManager bclib_resourceManager;
//TODO: 1.19.3 How were FabricSprites replaced? //TODO: 1.19.3 How were FabricSprites replaced?
// @Inject(method = "<init>*", at = @At("TAIL")) @Inject(method = "<init>*", at = @At("TAIL"))
// private void bclib_onAtlasInit(ResourceLocation resourceLocation, CallbackInfo info) { private void bclib_onAtlasInit(
// boolean hasOptifine = FabricLoader.getInstance().isModLoaded("optifabric"); ResourceLocation textureAtlasLocation,
// bclib_modifyAtlas = !hasOptifine && resourceLocation.toString().equals("minecraft:textures/atlas/blocks.png"); int maxSupportedTextureSize,
// if (bclib_modifyAtlas) { CallbackInfo ci
// EmissiveTextureInfo.clear(); ) {
// }
// } boolean hasOptifine = FabricLoader.getInstance().isModLoaded("optifabric");
// bclib_modifyAtlas = !hasOptifine && textureAtlasLocation.toString()
// @Inject(method = "load(Lnet/minecraft/server/packs/resources/ResourceManager;Lnet/minecraft/client/renderer/texture/TextureAtlasSprite$Info;IIIII)Lnet/minecraft/client/renderer/texture/TextureAtlasSprite;", at = @At("HEAD"), cancellable = true) .equals("minecraft:textures/atlas/blocks.png");
// private void bclib_loadSprite( if (bclib_modifyAtlas) {
// ResourceManager resourceManager, EmissiveTextureInfo.clear();
// TextureAtlasSprite.Info spriteInfo, }
// int atlasWidth, }
// int atlasHeight,
// int maxLevel, @Inject(method = "listSprites(Lnet/minecraft/server/packs/resources/ResourceManager;Ljava/lang/String;Ljava/lang/String;Ljava/util/function/BiConsumer;)V", at = @At("HEAD"), cancellable = true)
// int posX, private static void bclib_listSprites(
// int posY, ResourceManager resourceManager,
// CallbackInfoReturnable<TextureAtlasSprite> info String string,
// ) { String string2,
// if (!bclib_modifyAtlas) { BiConsumer<ResourceLocation, Resource> biConsumer,
// return; CallbackInfo ci
// } ) {
// bclib_resourceManager = resourceManager;
// ResourceLocation location = spriteInfo.name(); }
// if (!location.getPath().startsWith("block")) {
// return; @Inject(method = "loadSprite", at = @At("HEAD"), cancellable = true)
// } private void bclib_loadSprite(
// ResourceLocation location, Resource resource, CallbackInfoReturnable<SpriteContents> cir
// ResourceLocation emissiveLocation = new ResourceLocation( ) {
// location.getNamespace(), if (!bclib_modifyAtlas || bclib_resourceManager == null) {
// "textures/" + location.getPath() + "_e.png" return;
// ); }
// Optional<Resource> emissiveRes = resourceManager.getResource(emissiveLocation);
// if (emissiveRes.isPresent()) { if (!location.getPath().startsWith("block")) {
// NativeImage sprite = null; return;
// NativeImage emission = null; }
// try {
AnimationMetadataSection animationMetadataSection;
try {
animationMetadataSection = resource.metadata()
.getSection(AnimationMetadataSection.SERIALIZER)
.orElse(AnimationMetadataSection.EMPTY);
} catch (Exception exception) {
BCLib.LOGGER.error("Unable to parse metadata from {} : {}", (Object) this.location, (Object) exception);
cir.setReturnValue(null);
cir.cancel();
return;
}
ResourceLocation emissiveLocation = new ResourceLocation(
location.getNamespace(),
"textures/" + location.getPath() + "_e.png"
);
Optional<Resource> emissiveRes = bclib_resourceManager.getResource(emissiveLocation);
if (emissiveRes.isPresent()) {
NativeImage sprite = null;
NativeImage emission = null;
try {
// ResourceLocation spriteLocation = new ResourceLocation( // ResourceLocation spriteLocation = new ResourceLocation(
// location.getNamespace(), // location.getNamespace(),
// "textures/" + location.getPath() + ".png" // "textures/" + location.getPath() + ".png"
// ); // );
// Resource resource = resourceManager.getResource(spriteLocation).orElse(null); //Resource resource = resourceManager.getResource(spriteLocation).orElse(null);
// sprite = NativeImage.read(resource.open()); sprite = NativeImage.read(resource.open());
//
// resource = emissiveRes.get(); resource = emissiveRes.get();
// emission = NativeImage.read(resource.open()); emission = NativeImage.read(resource.open());
// } catch (IOException e) { } catch (IOException e) {
// BCLib.LOGGER.warning(e.getMessage()); BCLib.LOGGER.warning(e.getMessage());
// } }
// if (sprite != null && emission != null) { if (sprite != null && emission != null) {
// int width = Math.min(sprite.getWidth(), emission.getWidth()); int width = Math.min(sprite.getWidth(), emission.getWidth());
// int height = Math.min(sprite.getHeight(), emission.getHeight()); int height = Math.min(sprite.getHeight(), emission.getHeight());
// for (int x = 0; x < width; x++) { for (int x = 0; x < width; x++) {
// for (int y = 0; y < height; y++) { for (int y = 0; y < height; y++) {
// int argb = emission.getPixelRGBA(x, y); int argb = emission.getPixelRGBA(x, y);
// int alpha = (argb >> 24) & 255; int alpha = (argb >> 24) & 255;
// if (alpha > 127) { if (alpha > 127) {
// int r = (argb >> 16) & 255; int r = (argb >> 16) & 255;
// int g = (argb >> 8) & 255; int g = (argb >> 8) & 255;
// int b = argb & 255; int b = argb & 255;
// if (r > 0 || g > 0 || b > 0) { if (r > 0 || g > 0 || b > 0) {
// argb = (argb & 0x00FFFFFF) | EMISSIVE_ALPHA; argb = (argb & 0x00FFFFFF) | EMISSIVE_ALPHA;
// sprite.setPixelRGBA(x, y, argb); sprite.setPixelRGBA(x, y, argb);
// } }
// } }
// } }
// } }
// TextureAtlas self = (TextureAtlas) (Object) this;
// FabricSprite result = new FabricSprite( FrameSize frameSize = animationMetadataSection.calculateFrameSize(
// self, sprite.getWidth(),
// spriteInfo, sprite.getHeight()
// maxLevel, );
// atlasWidth, if (!Mth.isDivisionInteger(
// atlasHeight, sprite.getWidth(),
// posX, frameSize.width()
// posY, ) || !Mth.isDivisionInteger(sprite.getHeight(), frameSize.height())) {
// sprite BCLib.LOGGER.error(
// ); "Image {} size {},{} is not multiple of frame size {},{}",
// EmissiveTextureInfo.addTexture(location); this.location,
// info.setReturnValue(result); sprite.getWidth(),
// } sprite.getHeight(),
// } frameSize.width(),
// } frameSize.height()
);
sprite.close();
cir.setReturnValue(null);
cir.cancel();
}
SpriteContents result = new SpriteContents(location, frameSize, sprite, animationMetadataSection);
EmissiveTextureInfo.addTexture(location);
cir.setReturnValue(result);
}
}
}
} }

View file

@ -60,8 +60,8 @@ public final class Logger {
LOGGER.error(modPref + message, o1, o2, ex); LOGGER.error(modPref + message, o1, o2, ex);
} }
public void error(String message, Object o1, Object o2) { public void error(String message, Object... params) {
LOGGER.error(modPref + message, o1, o2); LOGGER.error(modPref + message, params);
} }
public void error(String message, Exception ex) { public void error(String message, Exception ex) {