| 1 | package net.minecraft.client.renderer.texture;
|
| 2 |
|
| 3 | import com.mojang.blaze3d.platform.NativeImage;
|
| 4 | import com.mojang.blaze3d.platform.TextureUtil;
|
| 5 | import com.mojang.blaze3d.platform.Transparency;
|
| 6 | import net.fabricmc.api.EnvType;
|
| 7 | import net.fabricmc.api.Environment;
|
| 8 | import net.minecraft.resources.Identifier;
|
| 9 | import net.minecraft.util.ARGB;
|
| 10 |
|
| 11 | @Environment(EnvType.CLIENT)
|
| 12 | public class MipmapGenerator {
|
| 13 | private static final String ITEM_PREFIX = "item/";
|
| 14 | private static final float ALPHA_CUTOFF = 0.5F;
|
| 15 | private static final float STRICT_ALPHA_CUTOFF = 0.3F;
|
| 16 |
|
| 17 | private MipmapGenerator() {
|
| 18 | }
|
| 19 |
|
| 20 | private static float alphaTestCoverage(final NativeImage image, final float alphaRef, final float alphaScale) {
|
| 21 | int width = image.getWidth();
|
| 22 | int height = image.getHeight();
|
| 23 | float coverage = 0.0F;
|
| 24 | int subsample_count = 4;
|
| 25 |
|
| 26 | for (int y = 0; y < height - 1; y++) {
|
| 27 | for (int x = 0; x < width - 1; x++) {
|
| 28 | float alpha00 = Math.clamp(ARGB.alphaFloat(image.getPixel(x, y)) * alphaScale, 0.0F, 1.0F);
|
| 29 | float alpha10 = Math.clamp(ARGB.alphaFloat(image.getPixel(x + 1, y)) * alphaScale, 0.0F, 1.0F);
|
| 30 | float alpha01 = Math.clamp(ARGB.alphaFloat(image.getPixel(x, y + 1)) * alphaScale, 0.0F, 1.0F);
|
| 31 | float alpha11 = Math.clamp(ARGB.alphaFloat(image.getPixel(x + 1, y + 1)) * alphaScale, 0.0F, 1.0F);
|
| 32 | float texelCoverage = 0.0F;
|
| 33 |
|
| 34 | for (int subsample_y = 0; subsample_y < 4; subsample_y++) {
|
| 35 | float fy = (subsample_y + 0.5F) / 4.0F;
|
| 36 |
|
| 37 | for (int subsample_x = 0; subsample_x < 4; subsample_x++) {
|
| 38 | float fx = (subsample_x + 0.5F) / 4.0F;
|
| 39 | float alpha = alpha00 * (1.0F - fx) * (1.0F - fy) + alpha10 * fx * (1.0F - fy) + alpha01 * (1.0F - fx) * fy + alpha11 * fx * fy;
|
| 40 | if (alpha > alphaRef) {
|
| 41 | texelCoverage++;
|
| 42 | }
|
| 43 | }
|
| 44 | }
|
| 45 |
|
| 46 | coverage += texelCoverage / 16.0F;
|
| 47 | }
|
| 48 | }
|
| 49 |
|
| 50 | return coverage / ((width - 1) * (height - 1));
|
| 51 | }
|
| 52 |
|
| 53 | private static void scaleAlphaToCoverage(final NativeImage image, final float desiredCoverage, final float alphaRef, final float alphaCutoffBias) {
|
| 54 | float minAlphaScale = 0.0F;
|
| 55 | float maxAlphaScale = 4.0F;
|
| 56 | float alphaScale = 1.0F;
|
| 57 | float bestAlphaScale = 1.0F;
|
| 58 | float bestError = Float.MAX_VALUE;
|
| 59 | int width = image.getWidth();
|
| 60 | int height = image.getHeight();
|
| 61 |
|
| 62 | for (int i = 0; i < 5; i++) {
|
| 63 | float currentCoverage = alphaTestCoverage(image, alphaRef, alphaScale);
|
| 64 | float error = Math.abs(currentCoverage - desiredCoverage);
|
| 65 | if (error < bestError) {
|
| 66 | bestError = error;
|
| 67 | bestAlphaScale = alphaScale;
|
| 68 | }
|
| 69 |
|
| 70 | if (currentCoverage < desiredCoverage) {
|
| 71 | minAlphaScale = alphaScale;
|
| 72 | } else {
|
| 73 | if (!(currentCoverage > desiredCoverage)) {
|
| 74 | break;
|
| 75 | }
|
| 76 |
|
| 77 | maxAlphaScale = alphaScale;
|
| 78 | }
|
| 79 |
|
| 80 | alphaScale = (minAlphaScale + maxAlphaScale) * 0.5F;
|
| 81 | }
|
| 82 |
|
| 83 | for (int y = 0; y < height; y++) {
|
| 84 | for (int x = 0; x < width; x++) {
|
| 85 | int pixel = image.getPixel(x, y);
|
| 86 | float alpha = ARGB.alphaFloat(pixel);
|
| 87 | alpha = alpha * bestAlphaScale + alphaCutoffBias + 0.025F;
|
| 88 | alpha = Math.clamp(alpha, 0.0F, 1.0F);
|
| 89 | image.setPixel(x, y, ARGB.color(alpha, pixel));
|
| 90 | }
|
| 91 | }
|
| 92 | }
|
| 93 |
|
| 94 | public static NativeImage[] generateMipLevels(
|
| 95 | final Identifier name,
|
| 96 | final NativeImage[] currentMips,
|
| 97 | final int newMipLevel,
|
| 98 | MipmapStrategy mipmapStrategy,
|
| 99 | final float alphaCutoffBias,
|
| 100 | final Transparency transparency
|
| 101 | ) {
|
| 102 | if (mipmapStrategy == MipmapStrategy.AUTO) {
|
| 103 | mipmapStrategy = transparency.hasTransparent() ? MipmapStrategy.CUTOUT : MipmapStrategy.MEAN;
|
| 104 | }
|
| 105 |
|
| 106 | if (currentMips.length == 1 && !name.getPath().startsWith("item/")) {
|
| 107 | if (mipmapStrategy == MipmapStrategy.CUTOUT || mipmapStrategy == MipmapStrategy.STRICT_CUTOUT) {
|
| 108 | TextureUtil.solidify(currentMips[0]);
|
| 109 | } else if (mipmapStrategy == MipmapStrategy.DARK_CUTOUT) {
|
| 110 | TextureUtil.fillEmptyAreasWithDarkColor(currentMips[0]);
|
| 111 | }
|
| 112 | }
|
| 113 |
|
| 114 | if (newMipLevel + 1 <= currentMips.length) {
|
| 115 | return currentMips;
|
| 116 | }
|
| 117 |
|
| 118 | NativeImage[] result = new NativeImage[newMipLevel + 1];
|
| 119 | result[0] = currentMips[0];
|
| 120 | boolean isCutoutMip = mipmapStrategy == MipmapStrategy.CUTOUT
|
| 121 | || mipmapStrategy == MipmapStrategy.STRICT_CUTOUT
|
| 122 | || mipmapStrategy == MipmapStrategy.DARK_CUTOUT;
|
| 123 | float cutoutRef = mipmapStrategy == MipmapStrategy.STRICT_CUTOUT ? 0.3F : 0.5F;
|
| 124 | float originalCoverage = isCutoutMip ? alphaTestCoverage(currentMips[0], cutoutRef, 1.0F) : 0.0F;
|
| 125 |
|
| 126 | for (int level = 1; level <= newMipLevel; level++) {
|
| 127 | if (level < currentMips.length) {
|
| 128 | result[level] = currentMips[level];
|
| 129 | } else {
|
| 130 | NativeImage lastData = result[level - 1];
|
| 131 | NativeImage data = new NativeImage(lastData.getWidth() >> 1, lastData.getHeight() >> 1, false);
|
| 132 | int width = data.getWidth();
|
| 133 | int height = data.getHeight();
|
| 134 |
|
| 135 | for (int x = 0; x < width; x++) {
|
| 136 | for (int y = 0; y < height; y++) {
|
| 137 | int color1 = lastData.getPixel(x * 2 + 0, y * 2 + 0);
|
| 138 | int color2 = lastData.getPixel(x * 2 + 1, y * 2 + 0);
|
| 139 | int color3 = lastData.getPixel(x * 2 + 0, y * 2 + 1);
|
| 140 | int color4 = lastData.getPixel(x * 2 + 1, y * 2 + 1);
|
| 141 | int color;
|
| 142 | if (mipmapStrategy == MipmapStrategy.DARK_CUTOUT) {
|
| 143 | color = darkenedAlphaBlend(color1, color2, color3, color4);
|
| 144 | } else {
|
| 145 | color = ARGB.meanLinear(color1, color2, color3, color4);
|
| 146 | }
|
| 147 |
|
| 148 | data.setPixel(x, y, color);
|
| 149 | }
|
| 150 | }
|
| 151 |
|
| 152 | result[level] = data;
|
| 153 | }
|
| 154 |
|
| 155 | if (isCutoutMip) {
|
| 156 | scaleAlphaToCoverage(result[level], originalCoverage, cutoutRef, alphaCutoffBias);
|
| 157 | }
|
| 158 | }
|
| 159 |
|
| 160 | return result;
|
| 161 | }
|
| 162 |
|
| 163 | private static int darkenedAlphaBlend(final int color1, final int color2, final int color3, final int color4) {
|
| 164 | float aTotal = 0.0F;
|
| 165 | float rTotal = 0.0F;
|
| 166 | float gTotal = 0.0F;
|
| 167 | float bTotal = 0.0F;
|
| 168 | if (ARGB.alpha(color1) != 0) {
|
| 169 | aTotal += ARGB.srgbToLinearChannel(ARGB.alpha(color1));
|
| 170 | rTotal += ARGB.srgbToLinearChannel(ARGB.red(color1));
|
| 171 | gTotal += ARGB.srgbToLinearChannel(ARGB.green(color1));
|
| 172 | bTotal += ARGB.srgbToLinearChannel(ARGB.blue(color1));
|
| 173 | }
|
| 174 |
|
| 175 | if (ARGB.alpha(color2) != 0) {
|
| 176 | aTotal += ARGB.srgbToLinearChannel(ARGB.alpha(color2));
|
| 177 | rTotal += ARGB.srgbToLinearChannel(ARGB.red(color2));
|
| 178 | gTotal += ARGB.srgbToLinearChannel(ARGB.green(color2));
|
| 179 | bTotal += ARGB.srgbToLinearChannel(ARGB.blue(color2));
|
| 180 | }
|
| 181 |
|
| 182 | if (ARGB.alpha(color3) != 0) {
|
| 183 | aTotal += ARGB.srgbToLinearChannel(ARGB.alpha(color3));
|
| 184 | rTotal += ARGB.srgbToLinearChannel(ARGB.red(color3));
|
| 185 | gTotal += ARGB.srgbToLinearChannel(ARGB.green(color3));
|
| 186 | bTotal += ARGB.srgbToLinearChannel(ARGB.blue(color3));
|
| 187 | }
|
| 188 |
|
| 189 | if (ARGB.alpha(color4) != 0) {
|
| 190 | aTotal += ARGB.srgbToLinearChannel(ARGB.alpha(color4));
|
| 191 | rTotal += ARGB.srgbToLinearChannel(ARGB.red(color4));
|
| 192 | gTotal += ARGB.srgbToLinearChannel(ARGB.green(color4));
|
| 193 | bTotal += ARGB.srgbToLinearChannel(ARGB.blue(color4));
|
| 194 | }
|
| 195 |
|
| 196 | aTotal /= 4.0F;
|
| 197 | rTotal /= 4.0F;
|
| 198 | gTotal /= 4.0F;
|
| 199 | bTotal /= 4.0F;
|
| 200 | return ARGB.color(ARGB.linearToSrgbChannel(aTotal), ARGB.linearToSrgbChannel(rTotal), ARGB.linearToSrgbChannel(gTotal), ARGB.linearToSrgbChannel(bTotal));
|
| 201 | }
|
| 202 | }
|
| 203 |
|