diff --git a/OrangeX/src/main/java/cn/xiaozhou233/orangex/render/internal/v26_2/V26_2FontBackend.java b/OrangeX/src/main/java/cn/xiaozhou233/orangex/render/internal/v26_2/V26_2FontBackend.java index 2996f63..ecf1c72 100644 --- a/OrangeX/src/main/java/cn/xiaozhou233/orangex/render/internal/v26_2/V26_2FontBackend.java +++ b/OrangeX/src/main/java/cn/xiaozhou233/orangex/render/internal/v26_2/V26_2FontBackend.java @@ -30,7 +30,7 @@ public final class V26_2FontBackend implements FontBackend { int height = atlas.getHeight(); byte[] coverage = atlas.getPixels(); - V26_2GpuTexture texture = V26_2TextureHelper.create( + V26_2GpuTexture texture = V26_2TextureHelper.createFromCoverage( loader, width, height, coverage); return new V26_2FontTexture(texture.texture, texture.textureView, texture.sampler, width, height); diff --git a/OrangeX/src/main/java/cn/xiaozhou233/orangex/render/internal/v26_2/V26_2TextureHelper.java b/OrangeX/src/main/java/cn/xiaozhou233/orangex/render/internal/v26_2/V26_2TextureHelper.java index 5655bd3..57b188c 100644 --- a/OrangeX/src/main/java/cn/xiaozhou233/orangex/render/internal/v26_2/V26_2TextureHelper.java +++ b/OrangeX/src/main/java/cn/xiaozhou233/orangex/render/internal/v26_2/V26_2TextureHelper.java @@ -5,13 +5,43 @@ import java.nio.ByteBuffer; import java.util.function.Supplier; /** - * Creates a {@code DynamicTexture} from an RGBA byte array without linking - * against Mojang's classes. + * Creates a {@code DynamicTexture} without linking against Mojang's classes. + * + *
Two input formats are supported so callers cannot mix them up: raw RGBA + * (shapes, sprites) and one-byte coverage/font atlases, which are expanded to + * white RGB + coverage alpha directly inside the {@code NativeImage} buffer.
*/ final class V26_2TextureHelper { static V26_2GpuTexture create(ClassLoader loader, int width, int height, - byte[] coverage) { + final byte[] rgba) { + return create(loader, width, height, new PixelWriter() { + @Override + public void write(ByteBuffer buffer) { + buffer.put(rgba); + } + }); + } + + static V26_2GpuTexture createFromCoverage(ClassLoader loader, int width, + int height, final byte[] coverage) { + return create(loader, width, height, new PixelWriter() { + @Override + public void write(ByteBuffer buffer) { + // Expand straight into the NativeImage buffer: avoids a second + // full-size RGBA array on top of the atlas and the image. + for (byte alpha : coverage) { + buffer.put((byte) 0xFF); + buffer.put((byte) 0xFF); + buffer.put((byte) 0xFF); + buffer.put(alpha); + } + } + }); + } + + private static V26_2GpuTexture create(ClassLoader loader, int width, + int height, PixelWriter writer) { try { Class> nativeImageClass = Class.forName( "com.mojang.blaze3d.platform.NativeImage", false, loader); @@ -19,16 +49,9 @@ final class V26_2TextureHelper { .getDeclaredConstructor(int.class, int.class, boolean.class); Object image = nativeImageConstructor.newInstance(width, height, false); - // Expand straight into the NativeImage buffer: avoids a second - // full-size RGBA array on top of the atlas and the image. ByteBuffer buffer = (ByteBuffer) nativeImageClass .getMethod("getPixelBytes").invoke(image); - for (byte alpha : coverage) { - buffer.put((byte) 0xFF); - buffer.put((byte) 0xFF); - buffer.put((byte) 0xFF); - buffer.put(alpha); - } + writer.write(buffer); Class> dynamicTextureClass = Class.forName( "net.minecraft.client.renderer.texture.DynamicTexture", false, loader); @@ -53,6 +76,10 @@ final class V26_2TextureHelper { } } + private interface PixelWriter { + void write(ByteBuffer buffer); + } + private V26_2TextureHelper() { } }