|
23 | 23 |
|
24 | 24 | package com.falsepattern.falsetweaks.mixin.mixins.client.mipmapfix; |
25 | 25 |
|
| 26 | +import com.falsepattern.falsetweaks.modules.mipmapfix.Mipmaps; |
26 | 27 | import org.objectweb.asm.Opcodes; |
27 | 28 | import org.spongepowered.asm.mixin.Mixin; |
| 29 | +import org.spongepowered.asm.mixin.Overwrite; |
28 | 30 | import org.spongepowered.asm.mixin.injection.At; |
29 | 31 | import org.spongepowered.asm.mixin.injection.Inject; |
30 | 32 | import org.spongepowered.asm.mixin.injection.Redirect; |
31 | 33 | import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; |
32 | 34 |
|
33 | 35 | import net.minecraft.client.renderer.texture.TextureUtil; |
34 | 36 |
|
35 | | -@Mixin(TextureUtil.class) |
| 37 | +// Ported from Hodgepodge |
| 38 | +@Mixin(value = TextureUtil.class, |
| 39 | + priority = 1100) // Hodgepodge conflict |
36 | 40 | public abstract class TextureUtilMixin { |
37 | | - private static ThreadLocal<int[]> threadLocalBuffer; |
| 41 | + /** |
| 42 | + * @author UltraHex |
| 43 | + * @reason Check entire texture for transparency. |
| 44 | + * @reason Don't divide by zero for small textures. |
| 45 | + */ |
| 46 | + @Overwrite |
| 47 | + public static int[][] generateMipmapData(int levels, int size, int[][] texture) { |
| 48 | + int[][] mipmaps = new int[levels + 1][]; |
| 49 | + mipmaps[0] = texture[0]; |
38 | 50 |
|
39 | | - @Inject(method = "<clinit>", |
40 | | - at = @At("RETURN"), |
41 | | - require = 1) |
42 | | - private static void setupThreadLocal(CallbackInfo ci) { |
43 | | - threadLocalBuffer = ThreadLocal.withInitial(() -> new int[4]); |
| 51 | + if (levels > 0) { |
| 52 | + boolean transparent = false; |
| 53 | + |
| 54 | + for (int l = 0; l < texture[0].length; ++l) { |
| 55 | + if (texture[0][l] >> 24 == 0) { |
| 56 | + transparent = true; |
| 57 | + break; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + for (int level = 1; level <= levels; ++level) { |
| 62 | + if (texture[level] != null) { |
| 63 | + mipmaps[level] = texture[level]; |
| 64 | + } else { |
| 65 | + int[] prevLevel = mipmaps[level - 1]; |
| 66 | + |
| 67 | + int width = size >> level; |
| 68 | + if (width <= 0) { |
| 69 | + mipmaps[level] = prevLevel; |
| 70 | + continue; |
| 71 | + } |
| 72 | + |
| 73 | + int[] mipmap = new int[prevLevel.length >> 2]; |
| 74 | + |
| 75 | + int height = mipmap.length / width; |
| 76 | + int prevWidth = width << 1; |
| 77 | + |
| 78 | + for (int x = 0; x < width; ++x) { |
| 79 | + for (int y = 0; y < height; ++y) { |
| 80 | + int prevPos = 2 * (x + y * prevWidth); |
| 81 | + mipmap[x + y * width] = func_147943_a( |
| 82 | + prevLevel[prevPos], |
| 83 | + prevLevel[prevPos + 1], |
| 84 | + prevLevel[prevPos + prevWidth], |
| 85 | + prevLevel[prevPos + 1 + prevWidth], |
| 86 | + transparent); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + mipmaps[level] = mipmap; |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + return mipmaps; |
44 | 96 | } |
45 | 97 |
|
46 | | - @Redirect(method = "func_147943_a", |
47 | | - at = @At(value = "FIELD", |
48 | | - target = "Lnet/minecraft/client/renderer/texture/TextureUtil;field_147957_g:[I", |
49 | | - opcode = Opcodes.GETSTATIC), |
50 | | - require = 9) |
51 | | - private static int[] swapReferencesWithThreadLocal() { |
52 | | - return threadLocalBuffer.get(); |
| 98 | + /** |
| 99 | + * @author SuperCoder79 |
| 100 | + * @reason Rewrite mipmap color math to use memoized value array instead of using Math.pow directly |
| 101 | + */ |
| 102 | + @Overwrite |
| 103 | + private static int func_147943_a(int one, int two, int three, int four, boolean alpha) { |
| 104 | + if (!alpha) { |
| 105 | + int a = Mipmaps.getColorComponent(one, two, three, four, 24); |
| 106 | + int r = Mipmaps.getColorComponent(one, two, three, four, 16); |
| 107 | + int g = Mipmaps.getColorComponent(one, two, three, four, 8); |
| 108 | + int b = Mipmaps.getColorComponent(one, two, three, four, 0); |
| 109 | + return a << 24 | r << 16 | g << 8 | b; |
| 110 | + } else { |
| 111 | + int n = 0; |
| 112 | + |
| 113 | + float a = 0.0F; |
| 114 | + float r = 0.0F; |
| 115 | + float g = 0.0F; |
| 116 | + float b = 0.0F; |
| 117 | + if (one >> 24 != 0) { |
| 118 | + a += Mipmaps.get(one >> 24); |
| 119 | + r += Mipmaps.get(one >> 16); |
| 120 | + g += Mipmaps.get(one >> 8); |
| 121 | + b += Mipmaps.get(one); |
| 122 | + n++; |
| 123 | + } |
| 124 | + |
| 125 | + if (two >> 24 != 0) { |
| 126 | + a += Mipmaps.get(two >> 24); |
| 127 | + r += Mipmaps.get(two >> 16); |
| 128 | + g += Mipmaps.get(two >> 8); |
| 129 | + b += Mipmaps.get(two); |
| 130 | + n++; |
| 131 | + } |
| 132 | + |
| 133 | + if (three >> 24 != 0) { |
| 134 | + a += Mipmaps.get(three >> 24); |
| 135 | + r += Mipmaps.get(three >> 16); |
| 136 | + g += Mipmaps.get(three >> 8); |
| 137 | + b += Mipmaps.get(three); |
| 138 | + n++; |
| 139 | + } |
| 140 | + |
| 141 | + if (four >> 24 != 0) { |
| 142 | + a += Mipmaps.get(four >> 24); |
| 143 | + r += Mipmaps.get(four >> 16); |
| 144 | + g += Mipmaps.get(four >> 8); |
| 145 | + b += Mipmaps.get(four); |
| 146 | + n++; |
| 147 | + } |
| 148 | + |
| 149 | + a /= 4.0F; |
| 150 | + |
| 151 | + if (n != 0) { |
| 152 | + r /= n; |
| 153 | + g /= n; |
| 154 | + b /= n; |
| 155 | + } |
| 156 | + |
| 157 | + int ia = (int) (Math.pow(a, 0.45454545454545453) * 255.0); |
| 158 | + int ir = (int) (Math.pow(r, 0.45454545454545453) * 255.0); |
| 159 | + int ig = (int) (Math.pow(g, 0.45454545454545453) * 255.0); |
| 160 | + int ib = (int) (Math.pow(b, 0.45454545454545453) * 255.0); |
| 161 | + |
| 162 | + return ia << 24 | ir << 16 | ig << 8 | ib; |
| 163 | + } |
53 | 164 | } |
54 | 165 | } |
0 commit comments