Skip to content

Commit fa7e1ff

Browse files
Fixed elytra transparency issue
1 parent 7b533c6 commit fa7e1ff

File tree

1 file changed

+28
-26
lines changed

1 file changed

+28
-26
lines changed

src/main/java/me/reconcubed/communityupdate/client/render/LayeredColorMaskTextureCustom.java

+28-26
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
import net.minecraftforge.api.distmarker.OnlyIn;
1818
import org.apache.logging.log4j.LogManager;
1919
import org.apache.logging.log4j.Logger;
20+
import org.lwjgl.opengl.GL11;
2021

22+
// Cheers to TheGreyGhost on Minecraft Forge Forums for help with this one!
2123
@OnlyIn(Dist.CLIENT)
2224
public class LayeredColorMaskTextureCustom extends Texture {
2325
private static final Logger LOGGER = LogManager.getLogger();
@@ -34,34 +36,35 @@ public LayeredColorMaskTextureCustom(ResourceLocation textureLocationIn, List<St
3436
public void loadTexture(IResourceManager manager) throws IOException {
3537
try (
3638
IResource iresource = manager.getResource(this.textureLocation);
37-
NativeImage nativeimage = NativeImage.read(iresource.getInputStream());
38-
NativeImage nativeimage1 = new NativeImage(nativeimage.getWidth(), nativeimage.getHeight(), false);
39+
NativeImage baseElytra = NativeImage.read(iresource.getInputStream());
40+
NativeImage overlaidElytra = new NativeImage(baseElytra.getWidth(), baseElytra.getHeight(), false);
3941
) {
40-
nativeimage1.copyImageData(nativeimage);
42+
overlaidElytra.copyImageData(baseElytra);
4143

4244
for (int i = 0; i < 17 && i < this.listTextures.size() && i < this.listDyeColors.size(); ++i) {
43-
String s = this.listTextures.get(i);
44-
if (s != null) {
45+
String bannerTextureRL = this.listTextures.get(i);
46+
if (bannerTextureRL != null) {
4547
try (
46-
NativeImage nativeimage2 = net.minecraftforge.client.MinecraftForgeClient.getImageLayer(new ResourceLocation(s), manager);
48+
NativeImage bannerLayer = net.minecraftforge.client.MinecraftForgeClient.getImageLayer(new ResourceLocation(bannerTextureRL), manager);
4749
) {
48-
int swappedColorValue = this.listDyeColors.get(i).getSwappedColorValue();
49-
if (nativeimage2.getWidth() == nativeimage1.getWidth() && nativeimage2.getHeight() == nativeimage1.getHeight()) {
50-
for (int height = 0; height < nativeimage2.getHeight(); ++height) {
51-
for (int width = 0; width < nativeimage2.getWidth(); ++width) {
50+
int bannerLayerColour = this.listDyeColors.get(i).getSwappedColorValue();
51+
if (bannerLayer.getWidth() == overlaidElytra.getWidth() && bannerLayer.getHeight() == overlaidElytra.getHeight()) {
52+
for (int height = 0; height < bannerLayer.getHeight(); ++height) {
53+
for (int width = 0; width < bannerLayer.getWidth(); ++width) {
5254

53-
int pixelRGBA = nativeimage2.getPixelRGBA(width, height);
55+
int alphaBanner = bannerLayer.getPixelRGBA(width, height) & 0xff; // extract the red channel, could have used green or blue also.
56+
int alphaElytra = baseElytra.getPixelLuminanceOrAlpha(width, height) & 0xff;
57+
// algorithm is:
58+
// if elytra pixel is transparent, do nothing
59+
// otherwise:
60+
// the banner blend layer is a greyscale which is converted to a transparency:
61+
// blend the banner's colour into elytra pixel using the banner blend transparency
5462

55-
if (nativeimage2.getPixelLuminanceOrAlpha(width, height) == 0) {
56-
57-
int color = (pixelRGBA & 255) << 24 & -16777216;
58-
59-
nativeimage1.setPixelRGBA(width, height, color);
60-
} else if ((pixelRGBA & -16777216) != 0) {
61-
int shiftedPixelRGBA = (pixelRGBA & 255) << 24 & -16777216;
62-
int pixelRGBA1 = nativeimage.getPixelRGBA(width, height);
63-
int multipliedColor = MathHelper.multiplyColor(pixelRGBA1, swappedColorValue) & 16777215;
64-
nativeimage1.blendPixel(width, height, shiftedPixelRGBA | multipliedColor);
63+
if (alphaElytra != 0 && alphaBanner != 0) {
64+
int elytraPixelRGBA = baseElytra.getPixelRGBA(width, height);
65+
int multipliedColorRGB = MathHelper.multiplyColor(elytraPixelRGBA, bannerLayerColour) & 0xFFFFFF;
66+
int multipliedColorRGBA = multipliedColorRGB | (alphaBanner << 24);
67+
overlaidElytra.blendPixel(width, height, multipliedColorRGBA);
6568
}
6669

6770
}
@@ -71,13 +74,12 @@ public void loadTexture(IResourceManager manager) throws IOException {
7174
}
7275
}
7376

74-
TextureUtil.prepareImage(this.getGlTextureId(), nativeimage1.getWidth(), nativeimage1.getHeight());
75-
GlStateManager.pixelTransfer(3357, Float.MAX_VALUE);
76-
nativeimage1.uploadTextureSub(0, 0, 0, false);
77-
GlStateManager.pixelTransfer(3357, 0.0F);
77+
TextureUtil.prepareImage(this.getGlTextureId(), overlaidElytra.getWidth(), overlaidElytra.getHeight());
78+
GlStateManager.pixelTransfer(GL11.GL_ALPHA_BIAS, 0.0F);
79+
overlaidElytra.uploadTextureSub(0, 0, 0, false);
7880
} catch (IOException ioexception) {
7981
LOGGER.error("Couldn't load layered color mask image", (Throwable) ioexception);
8082
}
8183

8284
}
83-
}
85+
}

0 commit comments

Comments
 (0)