| 1 | package net.minecraft.util;
|
| 2 |
|
| 3 | import net.minecraft.core.BlockPos;
|
| 4 | import net.minecraft.world.level.BlockAndLightGetter;
|
| 5 | import net.minecraft.world.level.LightLayer;
|
| 6 | import net.minecraft.world.level.block.state.BlockState;
|
| 7 |
|
| 8 | public class LightCoordsUtil {
|
| 9 | public static final int FULL_BRIGHT = 15728880;
|
| 10 | public static final int FULL_SKY = 15728640;
|
| 11 | private static final int MAX_SMOOTH_LIGHT_LEVEL = 240;
|
| 12 |
|
| 13 | public static int pack(final int block, final int sky) {
|
| 14 | return block << 4 | sky << 20;
|
| 15 | }
|
| 16 |
|
| 17 | public static int block(final int packed) {
|
| 18 | return packed >> 4 & 15;
|
| 19 | }
|
| 20 |
|
| 21 | public static int sky(final int packed) {
|
| 22 | return packed >> 20 & 15;
|
| 23 | }
|
| 24 |
|
| 25 | public static int withBlock(final int coords, final int block) {
|
| 26 | return coords & 0xFF0000 | block << 4;
|
| 27 | }
|
| 28 |
|
| 29 | public static int smoothPack(final int block, final int sky) {
|
| 30 | return block & 0xFF | (sky & 0xFF) << 16;
|
| 31 | }
|
| 32 |
|
| 33 | public static int smoothBlock(final int packed) {
|
| 34 | return packed & 0xFF;
|
| 35 | }
|
| 36 |
|
| 37 | public static int smoothSky(final int packed) {
|
| 38 | return packed >> 16 & 0xFF;
|
| 39 | }
|
| 40 |
|
| 41 | public static int addSmoothBlockEmission(final int lightCoords, float blockLightEmission) {
|
| 42 | blockLightEmission = Mth.clamp(blockLightEmission, 0.0F, 1.0F);
|
| 43 | int emittedBlock = (int)(Mth.clamp(blockLightEmission, 0.0F, 1.0F) * 240.0F);
|
| 44 | int block = Math.min(smoothBlock(lightCoords) + emittedBlock, 240);
|
| 45 | return smoothPack(block, smoothSky(lightCoords));
|
| 46 | }
|
| 47 |
|
| 48 | public static int max(final int coords1, final int coords2) {
|
| 49 | int block1 = block(coords1);
|
| 50 | int block2 = block(coords2);
|
| 51 | int sky1 = sky(coords1);
|
| 52 | int sky2 = sky(coords2);
|
| 53 | return pack(Math.max(block1, block2), Math.max(sky1, sky2));
|
| 54 | }
|
| 55 |
|
| 56 | public static int lightCoordsWithEmission(final int lightCoords, final int emission) {
|
| 57 | if (emission == 0) {
|
| 58 | return lightCoords;
|
| 59 | }
|
| 60 |
|
| 61 | int sky = Math.max(sky(lightCoords), emission);
|
| 62 | int block = Math.max(block(lightCoords), emission);
|
| 63 | return pack(block, sky);
|
| 64 | }
|
| 65 |
|
| 66 | public static int smoothBlend(int neighbor1, int neighbor2, int neighbor3, final int center) {
|
| 67 | if (sky(center) > 2 || block(center) > 2) {
|
| 68 | if (neighbor1 == 0) {
|
| 69 | neighbor1 = center;
|
| 70 | } else if (sky(neighbor1) == 0) {
|
| 71 | neighbor1 |= center & 0xFF0000;
|
| 72 | }
|
| 73 |
|
| 74 | if (neighbor2 == 0) {
|
| 75 | neighbor2 = center;
|
| 76 | } else if (sky(neighbor2) == 0) {
|
| 77 | neighbor2 |= center & 0xFF0000;
|
| 78 | }
|
| 79 |
|
| 80 | if (neighbor3 == 0) {
|
| 81 | neighbor3 = center;
|
| 82 | } else if (sky(neighbor3) == 0) {
|
| 83 | neighbor3 |= center & 0xFF0000;
|
| 84 | }
|
| 85 | }
|
| 86 |
|
| 87 | return neighbor1 + neighbor2 + neighbor3 + center >> 2 & 16711935;
|
| 88 | }
|
| 89 |
|
| 90 | public static int smoothWeightedBlend(
|
| 91 | final int coords1,
|
| 92 | final int coords2,
|
| 93 | final int coords3,
|
| 94 | final int coords4,
|
| 95 | final float weight1,
|
| 96 | final float weight2,
|
| 97 | final float weight3,
|
| 98 | final float weight4
|
| 99 | ) {
|
| 100 | int sky = (int)(smoothSky(coords1) * weight1 + smoothSky(coords2) * weight2 + smoothSky(coords3) * weight3 + smoothSky(coords4) * weight4);
|
| 101 | int block = (int)(smoothBlock(coords1) * weight1 + smoothBlock(coords2) * weight2 + smoothBlock(coords3) * weight3 + smoothBlock(coords4) * weight4);
|
| 102 | return smoothPack(block, sky);
|
| 103 | }
|
| 104 |
|
| 105 | public static int getLightCoords(final BlockAndLightGetter level, final BlockPos pos) {
|
| 106 | return getLightCoords(LightCoordsUtil.BrightnessGetter.DEFAULT, level, level.getBlockState(pos), pos);
|
| 107 | }
|
| 108 |
|
| 109 | public static int getLightCoords(
|
| 110 | final LightCoordsUtil.BrightnessGetter brightnessGetter, final BlockAndLightGetter level, final BlockState state, final BlockPos pos
|
| 111 | ) {
|
| 112 | if (state.emissiveRendering()) {
|
| 113 | return 15728880;
|
| 114 | }
|
| 115 |
|
| 116 | int packedBrightness = brightnessGetter.packedBrightness(level, pos);
|
| 117 | int block = block(packedBrightness);
|
| 118 | int blockSelfEmission = state.getLightEmission();
|
| 119 | return block < blockSelfEmission ? withBlock(packedBrightness, blockSelfEmission) : packedBrightness;
|
| 120 | }
|
| 121 |
|
| 122 | @FunctionalInterface
|
| 123 | public interface BrightnessGetter {
|
| 124 | LightCoordsUtil.BrightnessGetter DEFAULT = (level, pos) -> {
|
| 125 | int sky = level.getBrightness(LightLayer.SKY, pos);
|
| 126 | int block = level.getBrightness(LightLayer.BLOCK, pos);
|
| 127 | return LightCoordsUtil.pack(block, sky);
|
| 128 | };
|
| 129 |
|
| 130 | int packedBrightness(BlockAndLightGetter level, BlockPos pos);
|
| 131 | }
|
| 132 | }
|
| 133 |
|