| 1 | package net.minecraft.client.renderer.block;
|
| 2 |
|
| 3 | import com.mojang.blaze3d.vertex.QuadInstance;
|
| 4 | import it.unimi.dsi.fastutil.longs.Long2FloatLinkedOpenHashMap;
|
| 5 | import it.unimi.dsi.fastutil.longs.Long2IntLinkedOpenHashMap;
|
| 6 | import net.fabricmc.api.EnvType;
|
| 7 | import net.fabricmc.api.Environment;
|
| 8 | import net.minecraft.client.renderer.block.BlockModelLighter.Cache.1;
|
| 9 | import net.minecraft.client.renderer.block.BlockModelLighter.Cache.2;
|
| 10 | import net.minecraft.client.resources.model.geometry.BakedQuad;
|
| 11 | import net.minecraft.core.BlockPos;
|
| 12 | import net.minecraft.core.Direction;
|
| 13 | import net.minecraft.core.BlockPos.MutableBlockPos;
|
| 14 | import net.minecraft.util.ARGB;
|
| 15 | import net.minecraft.util.LightCoordsUtil;
|
| 16 | import net.minecraft.util.Util;
|
| 17 | import net.minecraft.util.LightCoordsUtil.BrightnessGetter;
|
| 18 | import net.minecraft.world.level.CardinalLighting;
|
| 19 | import net.minecraft.world.level.block.state.BlockState;
|
| 20 | import org.joml.Vector3fc;
|
| 21 |
|
| 22 | @Environment(EnvType.CLIENT)
|
| 23 | public class BlockModelLighter {
|
| 24 | private static final int CACHE_SIZE = 100;
|
| 25 | private static final ThreadLocal<BlockModelLighter.Cache> CACHE = ThreadLocal.withInitial(BlockModelLighter.Cache::new);
|
| 26 | public static final int CHECK_LIGHT = -1;
|
| 27 | private final BlockModelLighter.Cache cache;
|
| 28 | private final MutableBlockPos scratchPos = new MutableBlockPos();
|
| 29 | private boolean faceCubic;
|
| 30 | private boolean facePartial;
|
| 31 | private final float[] faceShape = new float[BlockModelLighter.SizeInfo.COUNT];
|
| 32 |
|
| 33 | public BlockModelLighter() {
|
| 34 | this.cache = CACHE.get();
|
| 35 | }
|
| 36 |
|
| 37 | public int getLightCoords(final BlockState state, final BlockAndTintGetter level, final BlockPos relativePos) {
|
| 38 | return this.cache.getLightCoords(state, level, relativePos);
|
| 39 | }
|
| 40 |
|
| 41 | public void prepareQuadAmbientOcclusion(
|
| 42 | final BlockAndTintGetter level, final BlockState state, final BlockPos centerPosition, final BakedQuad quad, final QuadInstance outputInstance
|
| 43 | ) {
|
| 44 | this.prepareQuadShape(level, state, centerPosition, quad, true);
|
| 45 | Direction direction = quad.direction();
|
| 46 | BlockPos basePosition = this.faceCubic ? centerPosition.relative(direction) : centerPosition;
|
| 47 | BlockModelLighter.AdjacencyInfo info = BlockModelLighter.AdjacencyInfo.fromFacing(direction);
|
| 48 | MutableBlockPos pos = this.scratchPos;
|
| 49 | pos.setWithOffset(basePosition, info.corners[0]);
|
| 50 | BlockState state0 = level.getBlockState(pos);
|
| 51 | int light0 = this.cache.getLightCoords(state0, level, pos);
|
| 52 | float shade0 = this.cache.getShadeBrightness(state0, level, pos);
|
| 53 | pos.setWithOffset(basePosition, info.corners[1]);
|
| 54 | BlockState state1 = level.getBlockState(pos);
|
| 55 | int light1 = this.cache.getLightCoords(state1, level, pos);
|
| 56 | float shade1 = this.cache.getShadeBrightness(state1, level, pos);
|
| 57 | pos.setWithOffset(basePosition, info.corners[2]);
|
| 58 | BlockState state2 = level.getBlockState(pos);
|
| 59 | int light2 = this.cache.getLightCoords(state2, level, pos);
|
| 60 | float shade2 = this.cache.getShadeBrightness(state2, level, pos);
|
| 61 | pos.setWithOffset(basePosition, info.corners[3]);
|
| 62 | BlockState state3 = level.getBlockState(pos);
|
| 63 | int light3 = this.cache.getLightCoords(state3, level, pos);
|
| 64 | float shade3 = this.cache.getShadeBrightness(state3, level, pos);
|
| 65 | BlockState corner0 = level.getBlockState(pos.setWithOffset(basePosition, info.corners[0]).move(direction));
|
| 66 | boolean lightPermeable0 = corner0.isLightPermeable();
|
| 67 | BlockState corner1 = level.getBlockState(pos.setWithOffset(basePosition, info.corners[1]).move(direction));
|
| 68 | boolean lightPermeable1 = corner1.isLightPermeable();
|
| 69 | BlockState corner2 = level.getBlockState(pos.setWithOffset(basePosition, info.corners[2]).move(direction));
|
| 70 | boolean lightPermeable2 = corner2.isLightPermeable();
|
| 71 | BlockState corner3 = level.getBlockState(pos.setWithOffset(basePosition, info.corners[3]).move(direction));
|
| 72 | boolean lightPermeable3 = corner3.isLightPermeable();
|
| 73 | float shadeCorner02;
|
| 74 | int lightCorner02;
|
| 75 | if (!lightPermeable2 && !lightPermeable0) {
|
| 76 | shadeCorner02 = shade0;
|
| 77 | lightCorner02 = light0;
|
| 78 | } else {
|
| 79 | pos.setWithOffset(basePosition, info.corners[0]).move(info.corners[2]);
|
| 80 | BlockState state02 = level.getBlockState(pos);
|
| 81 | shadeCorner02 = this.cache.getShadeBrightness(state02, level, pos);
|
| 82 | lightCorner02 = this.cache.getLightCoords(state02, level, pos);
|
| 83 | }
|
| 84 |
|
| 85 | float shadeCorner03;
|
| 86 | int lightCorner03;
|
| 87 | if (!lightPermeable3 && !lightPermeable0) {
|
| 88 | shadeCorner03 = shade0;
|
| 89 | lightCorner03 = light0;
|
| 90 | } else {
|
| 91 | pos.setWithOffset(basePosition, info.corners[0]).move(info.corners[3]);
|
| 92 | BlockState state03 = level.getBlockState(pos);
|
| 93 | shadeCorner03 = this.cache.getShadeBrightness(state03, level, pos);
|
| 94 | lightCorner03 = this.cache.getLightCoords(state03, level, pos);
|
| 95 | }
|
| 96 |
|
| 97 | float shadeCorner12;
|
| 98 | int lightCorner12;
|
| 99 | if (!lightPermeable2 && !lightPermeable1) {
|
| 100 | shadeCorner12 = shade0;
|
| 101 | lightCorner12 = light0;
|
| 102 | } else {
|
| 103 | pos.setWithOffset(basePosition, info.corners[1]).move(info.corners[2]);
|
| 104 | BlockState state12 = level.getBlockState(pos);
|
| 105 | shadeCorner12 = this.cache.getShadeBrightness(state12, level, pos);
|
| 106 | lightCorner12 = this.cache.getLightCoords(state12, level, pos);
|
| 107 | }
|
| 108 |
|
| 109 | float shadeCorner13;
|
| 110 | int lightCorner13;
|
| 111 | if (!lightPermeable3 && !lightPermeable1) {
|
| 112 | shadeCorner13 = shade0;
|
| 113 | lightCorner13 = light0;
|
| 114 | } else {
|
| 115 | pos.setWithOffset(basePosition, info.corners[1]).move(info.corners[3]);
|
| 116 | BlockState state13 = level.getBlockState(pos);
|
| 117 | shadeCorner13 = this.cache.getShadeBrightness(state13, level, pos);
|
| 118 | lightCorner13 = this.cache.getLightCoords(state13, level, pos);
|
| 119 | }
|
| 120 |
|
| 121 | int lightCenter = this.cache.getLightCoords(state, level, centerPosition);
|
| 122 | pos.setWithOffset(centerPosition, direction);
|
| 123 | BlockState nextState = level.getBlockState(pos);
|
| 124 | if (this.faceCubic || !nextState.isSolidRender()) {
|
| 125 | lightCenter = this.cache.getLightCoords(nextState, level, pos);
|
| 126 | }
|
| 127 |
|
| 128 | float shadeCenter = this.faceCubic
|
| 129 | ? this.cache.getShadeBrightness(level.getBlockState(basePosition), level, basePosition)
|
| 130 | : this.cache.getShadeBrightness(level.getBlockState(centerPosition), level, centerPosition);
|
| 131 | BlockModelLighter.AmbientVertexRemap remap = BlockModelLighter.AmbientVertexRemap.fromFacing(direction);
|
| 132 | if (this.facePartial && info.doNonCubicWeight) {
|
| 133 | float tempShade1 = (shade3 + shade0 + shadeCorner03 + shadeCenter) * 0.25F;
|
| 134 | float tempShade2 = (shade2 + shade0 + shadeCorner02 + shadeCenter) * 0.25F;
|
| 135 | float tempShade3 = (shade2 + shade1 + shadeCorner12 + shadeCenter) * 0.25F;
|
| 136 | float tempShade4 = (shade3 + shade1 + shadeCorner13 + shadeCenter) * 0.25F;
|
| 137 | float vert0weight01 = this.faceShape[info.vert0Weights[0].index] * this.faceShape[info.vert0Weights[1].index];
|
| 138 | float vert0weight23 = this.faceShape[info.vert0Weights[2].index] * this.faceShape[info.vert0Weights[3].index];
|
| 139 | float vert0weight45 = this.faceShape[info.vert0Weights[4].index] * this.faceShape[info.vert0Weights[5].index];
|
| 140 | float vert0weight67 = this.faceShape[info.vert0Weights[6].index] * this.faceShape[info.vert0Weights[7].index];
|
| 141 | float vert1weight01 = this.faceShape[info.vert1Weights[0].index] * this.faceShape[info.vert1Weights[1].index];
|
| 142 | float vert1weight23 = this.faceShape[info.vert1Weights[2].index] * this.faceShape[info.vert1Weights[3].index];
|
| 143 | float vert1weight45 = this.faceShape[info.vert1Weights[4].index] * this.faceShape[info.vert1Weights[5].index];
|
| 144 | float vert1weight67 = this.faceShape[info.vert1Weights[6].index] * this.faceShape[info.vert1Weights[7].index];
|
| 145 | float vert2weight01 = this.faceShape[info.vert2Weights[0].index] * this.faceShape[info.vert2Weights[1].index];
|
| 146 | float vert2weight23 = this.faceShape[info.vert2Weights[2].index] * this.faceShape[info.vert2Weights[3].index];
|
| 147 | float vert2weight45 = this.faceShape[info.vert2Weights[4].index] * this.faceShape[info.vert2Weights[5].index];
|
| 148 | float vert2weight67 = this.faceShape[info.vert2Weights[6].index] * this.faceShape[info.vert2Weights[7].index];
|
| 149 | float vert3weight01 = this.faceShape[info.vert3Weights[0].index] * this.faceShape[info.vert3Weights[1].index];
|
| 150 | float vert3weight23 = this.faceShape[info.vert3Weights[2].index] * this.faceShape[info.vert3Weights[3].index];
|
| 151 | float vert3weight45 = this.faceShape[info.vert3Weights[4].index] * this.faceShape[info.vert3Weights[5].index];
|
| 152 | float vert3weight67 = this.faceShape[info.vert3Weights[6].index] * this.faceShape[info.vert3Weights[7].index];
|
| 153 | outputInstance.setColor(
|
| 154 | remap.vert0,
|
| 155 | ARGB.gray(Math.clamp(tempShade1 * vert0weight01 + tempShade2 * vert0weight23 + tempShade3 * vert0weight45 + tempShade4 * vert0weight67, 0.0F, 1.0F))
|
| 156 | );
|
| 157 | outputInstance.setColor(
|
| 158 | remap.vert1,
|
| 159 | ARGB.gray(Math.clamp(tempShade1 * vert1weight01 + tempShade2 * vert1weight23 + tempShade3 * vert1weight45 + tempShade4 * vert1weight67, 0.0F, 1.0F))
|
| 160 | );
|
| 161 | outputInstance.setColor(
|
| 162 | remap.vert2,
|
| 163 | ARGB.gray(Math.clamp(tempShade1 * vert2weight01 + tempShade2 * vert2weight23 + tempShade3 * vert2weight45 + tempShade4 * vert2weight67, 0.0F, 1.0F))
|
| 164 | );
|
| 165 | outputInstance.setColor(
|
| 166 | remap.vert3,
|
| 167 | ARGB.gray(Math.clamp(tempShade1 * vert3weight01 + tempShade2 * vert3weight23 + tempShade3 * vert3weight45 + tempShade4 * vert3weight67, 0.0F, 1.0F))
|
| 168 | );
|
| 169 | int _tc1 = LightCoordsUtil.smoothBlend(light3, light0, lightCorner03, lightCenter);
|
| 170 | int _tc2 = LightCoordsUtil.smoothBlend(light2, light0, lightCorner02, lightCenter);
|
| 171 | int _tc3 = LightCoordsUtil.smoothBlend(light2, light1, lightCorner12, lightCenter);
|
| 172 | int _tc4 = LightCoordsUtil.smoothBlend(light3, light1, lightCorner13, lightCenter);
|
| 173 | outputInstance.setLightCoords(
|
| 174 | remap.vert0, LightCoordsUtil.smoothWeightedBlend(_tc1, _tc2, _tc3, _tc4, vert0weight01, vert0weight23, vert0weight45, vert0weight67)
|
| 175 | );
|
| 176 | outputInstance.setLightCoords(
|
| 177 | remap.vert1, LightCoordsUtil.smoothWeightedBlend(_tc1, _tc2, _tc3, _tc4, vert1weight01, vert1weight23, vert1weight45, vert1weight67)
|
| 178 | );
|
| 179 | outputInstance.setLightCoords(
|
| 180 | remap.vert2, LightCoordsUtil.smoothWeightedBlend(_tc1, _tc2, _tc3, _tc4, vert2weight01, vert2weight23, vert2weight45, vert2weight67)
|
| 181 | );
|
| 182 | outputInstance.setLightCoords(
|
| 183 | remap.vert3, LightCoordsUtil.smoothWeightedBlend(_tc1, _tc2, _tc3, _tc4, vert3weight01, vert3weight23, vert3weight45, vert3weight67)
|
| 184 | );
|
| 185 | } else {
|
| 186 | float lightLevel1 = (shade3 + shade0 + shadeCorner03 + shadeCenter) * 0.25F;
|
| 187 | float lightLevel2 = (shade2 + shade0 + shadeCorner02 + shadeCenter) * 0.25F;
|
| 188 | float lightLevel3 = (shade2 + shade1 + shadeCorner12 + shadeCenter) * 0.25F;
|
| 189 | float lightLevel4 = (shade3 + shade1 + shadeCorner13 + shadeCenter) * 0.25F;
|
| 190 | outputInstance.setLightCoords(remap.vert0, LightCoordsUtil.smoothBlend(light3, light0, lightCorner03, lightCenter));
|
| 191 | outputInstance.setLightCoords(remap.vert1, LightCoordsUtil.smoothBlend(light2, light0, lightCorner02, lightCenter));
|
| 192 | outputInstance.setLightCoords(remap.vert2, LightCoordsUtil.smoothBlend(light2, light1, lightCorner12, lightCenter));
|
| 193 | outputInstance.setLightCoords(remap.vert3, LightCoordsUtil.smoothBlend(light3, light1, lightCorner13, lightCenter));
|
| 194 | outputInstance.setColor(remap.vert0, ARGB.gray(lightLevel1));
|
| 195 | outputInstance.setColor(remap.vert1, ARGB.gray(lightLevel2));
|
| 196 | outputInstance.setColor(remap.vert2, ARGB.gray(lightLevel3));
|
| 197 | outputInstance.setColor(remap.vert3, ARGB.gray(lightLevel4));
|
| 198 | }
|
| 199 |
|
| 200 | CardinalLighting cardinalLighting = level.cardinalLighting();
|
| 201 | outputInstance.scaleColor(getDirectionalBrightness(cardinalLighting, quad, direction));
|
| 202 | }
|
| 203 |
|
| 204 | public void prepareQuadFlat(
|
| 205 | final BlockAndTintGetter level, final BlockState state, final BlockPos pos, final int lightCoords, final BakedQuad quad, final QuadInstance outputInstance
|
| 206 | ) {
|
| 207 | if (lightCoords == -1) {
|
| 208 | this.prepareQuadShape(level, state, pos, quad, false);
|
| 209 | BlockPos lightPos = this.faceCubic ? this.scratchPos.setWithOffset(pos, quad.direction()) : pos;
|
| 210 | outputInstance.setLightCoords(this.cache.getLightCoords(state, level, lightPos));
|
| 211 | } else {
|
| 212 | outputInstance.setLightCoords(lightCoords);
|
| 213 | }
|
| 214 |
|
| 215 | CardinalLighting cardinalLighting = level.cardinalLighting();
|
| 216 | outputInstance.setColor(ARGB.gray(getDirectionalBrightness(cardinalLighting, quad, quad.direction())));
|
| 217 | }
|
| 218 |
|
| 219 | private void prepareQuadShape(final BlockAndTintGetter level, final BlockState state, final BlockPos pos, final BakedQuad quad, final boolean ambientOcclusion) {
|
| 220 | float minX = 32.0F;
|
| 221 | float minY = 32.0F;
|
| 222 | float minZ = 32.0F;
|
| 223 | float maxX = -32.0F;
|
| 224 | float maxY = -32.0F;
|
| 225 | float maxZ = -32.0F;
|
| 226 |
|
| 227 | for (int i = 0; i < 4; i++) {
|
| 228 | Vector3fc position = quad.position(i);
|
| 229 | float x = position.x();
|
| 230 | float y = position.y();
|
| 231 | float z = position.z();
|
| 232 | minX = Math.min(minX, x);
|
| 233 | minY = Math.min(minY, y);
|
| 234 | minZ = Math.min(minZ, z);
|
| 235 | maxX = Math.max(maxX, x);
|
| 236 | maxY = Math.max(maxY, y);
|
| 237 | maxZ = Math.max(maxZ, z);
|
| 238 | }
|
| 239 |
|
| 240 | if (ambientOcclusion) {
|
| 241 | this.faceShape[BlockModelLighter.SizeInfo.WEST.index] = minX;
|
| 242 | this.faceShape[BlockModelLighter.SizeInfo.EAST.index] = maxX;
|
| 243 | this.faceShape[BlockModelLighter.SizeInfo.DOWN.index] = minY;
|
| 244 | this.faceShape[BlockModelLighter.SizeInfo.UP.index] = maxY;
|
| 245 | this.faceShape[BlockModelLighter.SizeInfo.NORTH.index] = minZ;
|
| 246 | this.faceShape[BlockModelLighter.SizeInfo.SOUTH.index] = maxZ;
|
| 247 | this.faceShape[BlockModelLighter.SizeInfo.FLIP_WEST.index] = 1.0F - minX;
|
| 248 | this.faceShape[BlockModelLighter.SizeInfo.FLIP_EAST.index] = 1.0F - maxX;
|
| 249 | this.faceShape[BlockModelLighter.SizeInfo.FLIP_DOWN.index] = 1.0F - minY;
|
| 250 | this.faceShape[BlockModelLighter.SizeInfo.FLIP_UP.index] = 1.0F - maxY;
|
| 251 | this.faceShape[BlockModelLighter.SizeInfo.FLIP_NORTH.index] = 1.0F - minZ;
|
| 252 | this.faceShape[BlockModelLighter.SizeInfo.FLIP_SOUTH.index] = 1.0F - maxZ;
|
| 253 | }
|
| 254 |
|
| 255 | float minEpsilon = 1.0E-4F;
|
| 256 | float maxEpsilon = 0.9999F;
|
| 257 |
|
| 258 | this.facePartial = switch (quad.direction()) {
|
| 259 | case DOWN, UP -> minX >= 1.0E-4F || minZ >= 1.0E-4F || maxX <= 0.9999F || maxZ <= 0.9999F;
|
| 260 | case NORTH, SOUTH -> minX >= 1.0E-4F || minY >= 1.0E-4F || maxX <= 0.9999F || maxY <= 0.9999F;
|
| 261 | case WEST, EAST -> minY >= 1.0E-4F || minZ >= 1.0E-4F || maxY <= 0.9999F || maxZ <= 0.9999F;
|
| 262 | };
|
| 263 |
|
| 264 | this.faceCubic = switch (quad.direction()) {
|
| 265 | case DOWN -> minY == maxY && (minY < 1.0E-4F || state.isCollisionShapeFullBlock(level, pos));
|
| 266 | case UP -> minY == maxY && (maxY > 0.9999F || state.isCollisionShapeFullBlock(level, pos));
|
| 267 | case NORTH -> minZ == maxZ && (minZ < 1.0E-4F || state.isCollisionShapeFullBlock(level, pos));
|
| 268 | case SOUTH -> minZ == maxZ && (maxZ > 0.9999F || state.isCollisionShapeFullBlock(level, pos));
|
| 269 | case WEST -> minX == maxX && (minX < 1.0E-4F || state.isCollisionShapeFullBlock(level, pos));
|
| 270 | case EAST -> minX == maxX && (maxX > 0.9999F || state.isCollisionShapeFullBlock(level, pos));
|
| 271 | };
|
| 272 | }
|
| 273 |
|
| 274 | private static float getDirectionalBrightness(final CardinalLighting cardinalLighting, final BakedQuad quad, final Direction actualDirection) {
|
| 275 | Direction shadeDirectionOverride = quad.materialInfo().shadeDirectionOverride();
|
| 276 | return shadeDirectionOverride != null ? cardinalLighting.byFace(shadeDirectionOverride) : cardinalLighting.byFace(actualDirection);
|
| 277 | }
|
| 278 |
|
| 279 | public static void enableCaching() {
|
| 280 | CACHE.get().enable();
|
| 281 | }
|
| 282 |
|
| 283 | public static void clearCache() {
|
| 284 | CACHE.get().disable();
|
| 285 | }
|
| 286 |
|
| 287 | @Environment(EnvType.CLIENT)
|
| 288 | private enum AdjacencyInfo {
|
| 289 | DOWN(
|
| 290 | new Direction[]{Direction.WEST, Direction.EAST, Direction.NORTH, Direction.SOUTH},
|
| 291 | 0.5F,
|
| 292 | true,
|
| 293 | new BlockModelLighter.SizeInfo[]{
|
| 294 | BlockModelLighter.SizeInfo.FLIP_WEST,
|
| 295 | BlockModelLighter.SizeInfo.SOUTH,
|
| 296 | BlockModelLighter.SizeInfo.FLIP_WEST,
|
| 297 | BlockModelLighter.SizeInfo.FLIP_SOUTH,
|
| 298 | BlockModelLighter.SizeInfo.WEST,
|
| 299 | BlockModelLighter.SizeInfo.FLIP_SOUTH,
|
| 300 | BlockModelLighter.SizeInfo.WEST,
|
| 301 | BlockModelLighter.SizeInfo.SOUTH
|
| 302 | },
|
| 303 | new BlockModelLighter.SizeInfo[]{
|
| 304 | BlockModelLighter.SizeInfo.FLIP_WEST,
|
| 305 | BlockModelLighter.SizeInfo.NORTH,
|
| 306 | BlockModelLighter.SizeInfo.FLIP_WEST,
|
| 307 | BlockModelLighter.SizeInfo.FLIP_NORTH,
|
| 308 | BlockModelLighter.SizeInfo.WEST,
|
| 309 | BlockModelLighter.SizeInfo.FLIP_NORTH,
|
| 310 | BlockModelLighter.SizeInfo.WEST,
|
| 311 | BlockModelLighter.SizeInfo.NORTH
|
| 312 | },
|
| 313 | new BlockModelLighter.SizeInfo[]{
|
| 314 | BlockModelLighter.SizeInfo.FLIP_EAST,
|
| 315 | BlockModelLighter.SizeInfo.NORTH,
|
| 316 | BlockModelLighter.SizeInfo.FLIP_EAST,
|
| 317 | BlockModelLighter.SizeInfo.FLIP_NORTH,
|
| 318 | BlockModelLighter.SizeInfo.EAST,
|
| 319 | BlockModelLighter.SizeInfo.FLIP_NORTH,
|
| 320 | BlockModelLighter.SizeInfo.EAST,
|
| 321 | BlockModelLighter.SizeInfo.NORTH
|
| 322 | },
|
| 323 | new BlockModelLighter.SizeInfo[]{
|
| 324 | BlockModelLighter.SizeInfo.FLIP_EAST,
|
| 325 | BlockModelLighter.SizeInfo.SOUTH,
|
| 326 | BlockModelLighter.SizeInfo.FLIP_EAST,
|
| 327 | BlockModelLighter.SizeInfo.FLIP_SOUTH,
|
| 328 | BlockModelLighter.SizeInfo.EAST,
|
| 329 | BlockModelLighter.SizeInfo.FLIP_SOUTH,
|
| 330 | BlockModelLighter.SizeInfo.EAST,
|
| 331 | BlockModelLighter.SizeInfo.SOUTH
|
| 332 | }
|
| 333 | ),
|
| 334 | UP(
|
| 335 | new Direction[]{Direction.EAST, Direction.WEST, Direction.NORTH, Direction.SOUTH},
|
| 336 | 1.0F,
|
| 337 | true,
|
| 338 | new BlockModelLighter.SizeInfo[]{
|
| 339 | BlockModelLighter.SizeInfo.EAST,
|
| 340 | BlockModelLighter.SizeInfo.SOUTH,
|
| 341 | BlockModelLighter.SizeInfo.EAST,
|
| 342 | BlockModelLighter.SizeInfo.FLIP_SOUTH,
|
| 343 | BlockModelLighter.SizeInfo.FLIP_EAST,
|
| 344 | BlockModelLighter.SizeInfo.FLIP_SOUTH,
|
| 345 | BlockModelLighter.SizeInfo.FLIP_EAST,
|
| 346 | BlockModelLighter.SizeInfo.SOUTH
|
| 347 | },
|
| 348 | new BlockModelLighter.SizeInfo[]{
|
| 349 | BlockModelLighter.SizeInfo.EAST,
|
| 350 | BlockModelLighter.SizeInfo.NORTH,
|
| 351 | BlockModelLighter.SizeInfo.EAST,
|
| 352 | BlockModelLighter.SizeInfo.FLIP_NORTH,
|
| 353 | BlockModelLighter.SizeInfo.FLIP_EAST,
|
| 354 | BlockModelLighter.SizeInfo.FLIP_NORTH,
|
| 355 | BlockModelLighter.SizeInfo.FLIP_EAST,
|
| 356 | BlockModelLighter.SizeInfo.NORTH
|
| 357 | },
|
| 358 | new BlockModelLighter.SizeInfo[]{
|
| 359 | BlockModelLighter.SizeInfo.WEST,
|
| 360 | BlockModelLighter.SizeInfo.NORTH,
|
| 361 | BlockModelLighter.SizeInfo.WEST,
|
| 362 | BlockModelLighter.SizeInfo.FLIP_NORTH,
|
| 363 | BlockModelLighter.SizeInfo.FLIP_WEST,
|
| 364 | BlockModelLighter.SizeInfo.FLIP_NORTH,
|
| 365 | BlockModelLighter.SizeInfo.FLIP_WEST,
|
| 366 | BlockModelLighter.SizeInfo.NORTH
|
| 367 | },
|
| 368 | new BlockModelLighter.SizeInfo[]{
|
| 369 | BlockModelLighter.SizeInfo.WEST,
|
| 370 | BlockModelLighter.SizeInfo.SOUTH,
|
| 371 | BlockModelLighter.SizeInfo.WEST,
|
| 372 | BlockModelLighter.SizeInfo.FLIP_SOUTH,
|
| 373 | BlockModelLighter.SizeInfo.FLIP_WEST,
|
| 374 | BlockModelLighter.SizeInfo.FLIP_SOUTH,
|
| 375 | BlockModelLighter.SizeInfo.FLIP_WEST,
|
| 376 | BlockModelLighter.SizeInfo.SOUTH
|
| 377 | }
|
| 378 | ),
|
| 379 | NORTH(
|
| 380 | new Direction[]{Direction.UP, Direction.DOWN, Direction.EAST, Direction.WEST},
|
| 381 | 0.8F,
|
| 382 | true,
|
| 383 | new BlockModelLighter.SizeInfo[]{
|
| 384 | BlockModelLighter.SizeInfo.UP,
|
| 385 | BlockModelLighter.SizeInfo.FLIP_WEST,
|
| 386 | BlockModelLighter.SizeInfo.UP,
|
| 387 | BlockModelLighter.SizeInfo.WEST,
|
| 388 | BlockModelLighter.SizeInfo.FLIP_UP,
|
| 389 | BlockModelLighter.SizeInfo.WEST,
|
| 390 | BlockModelLighter.SizeInfo.FLIP_UP,
|
| 391 | BlockModelLighter.SizeInfo.FLIP_WEST
|
| 392 | },
|
| 393 | new BlockModelLighter.SizeInfo[]{
|
| 394 | BlockModelLighter.SizeInfo.UP,
|
| 395 | BlockModelLighter.SizeInfo.FLIP_EAST,
|
| 396 | BlockModelLighter.SizeInfo.UP,
|
| 397 | BlockModelLighter.SizeInfo.EAST,
|
| 398 | BlockModelLighter.SizeInfo.FLIP_UP,
|
| 399 | BlockModelLighter.SizeInfo.EAST,
|
| 400 | BlockModelLighter.SizeInfo.FLIP_UP,
|
| 401 | BlockModelLighter.SizeInfo.FLIP_EAST
|
| 402 | },
|
| 403 | new BlockModelLighter.SizeInfo[]{
|
| 404 | BlockModelLighter.SizeInfo.DOWN,
|
| 405 | BlockModelLighter.SizeInfo.FLIP_EAST,
|
| 406 | BlockModelLighter.SizeInfo.DOWN,
|
| 407 | BlockModelLighter.SizeInfo.EAST,
|
| 408 | BlockModelLighter.SizeInfo.FLIP_DOWN,
|
| 409 | BlockModelLighter.SizeInfo.EAST,
|
| 410 | BlockModelLighter.SizeInfo.FLIP_DOWN,
|
| 411 | BlockModelLighter.SizeInfo.FLIP_EAST
|
| 412 | },
|
| 413 | new BlockModelLighter.SizeInfo[]{
|
| 414 | BlockModelLighter.SizeInfo.DOWN,
|
| 415 | BlockModelLighter.SizeInfo.FLIP_WEST,
|
| 416 | BlockModelLighter.SizeInfo.DOWN,
|
| 417 | BlockModelLighter.SizeInfo.WEST,
|
| 418 | BlockModelLighter.SizeInfo.FLIP_DOWN,
|
| 419 | BlockModelLighter.SizeInfo.WEST,
|
| 420 | BlockModelLighter.SizeInfo.FLIP_DOWN,
|
| 421 | BlockModelLighter.SizeInfo.FLIP_WEST
|
| 422 | }
|
| 423 | ),
|
| 424 | SOUTH(
|
| 425 | new Direction[]{Direction.WEST, Direction.EAST, Direction.DOWN, Direction.UP},
|
| 426 | 0.8F,
|
| 427 | true,
|
| 428 | new BlockModelLighter.SizeInfo[]{
|
| 429 | BlockModelLighter.SizeInfo.UP,
|
| 430 | BlockModelLighter.SizeInfo.FLIP_WEST,
|
| 431 | BlockModelLighter.SizeInfo.FLIP_UP,
|
| 432 | BlockModelLighter.SizeInfo.FLIP_WEST,
|
| 433 | BlockModelLighter.SizeInfo.FLIP_UP,
|
| 434 | BlockModelLighter.SizeInfo.WEST,
|
| 435 | BlockModelLighter.SizeInfo.UP,
|
| 436 | BlockModelLighter.SizeInfo.WEST
|
| 437 | },
|
| 438 | new BlockModelLighter.SizeInfo[]{
|
| 439 | BlockModelLighter.SizeInfo.DOWN,
|
| 440 | BlockModelLighter.SizeInfo.FLIP_WEST,
|
| 441 | BlockModelLighter.SizeInfo.FLIP_DOWN,
|
| 442 | BlockModelLighter.SizeInfo.FLIP_WEST,
|
| 443 | BlockModelLighter.SizeInfo.FLIP_DOWN,
|
| 444 | BlockModelLighter.SizeInfo.WEST,
|
| 445 | BlockModelLighter.SizeInfo.DOWN,
|
| 446 | BlockModelLighter.SizeInfo.WEST
|
| 447 | },
|
| 448 | new BlockModelLighter.SizeInfo[]{
|
| 449 | BlockModelLighter.SizeInfo.DOWN,
|
| 450 | BlockModelLighter.SizeInfo.FLIP_EAST,
|
| 451 | BlockModelLighter.SizeInfo.FLIP_DOWN,
|
| 452 | BlockModelLighter.SizeInfo.FLIP_EAST,
|
| 453 | BlockModelLighter.SizeInfo.FLIP_DOWN,
|
| 454 | BlockModelLighter.SizeInfo.EAST,
|
| 455 | BlockModelLighter.SizeInfo.DOWN,
|
| 456 | BlockModelLighter.SizeInfo.EAST
|
| 457 | },
|
| 458 | new BlockModelLighter.SizeInfo[]{
|
| 459 | BlockModelLighter.SizeInfo.UP,
|
| 460 | BlockModelLighter.SizeInfo.FLIP_EAST,
|
| 461 | BlockModelLighter.SizeInfo.FLIP_UP,
|
| 462 | BlockModelLighter.SizeInfo.FLIP_EAST,
|
| 463 | BlockModelLighter.SizeInfo.FLIP_UP,
|
| 464 | BlockModelLighter.SizeInfo.EAST,
|
| 465 | BlockModelLighter.SizeInfo.UP,
|
| 466 | BlockModelLighter.SizeInfo.EAST
|
| 467 | }
|
| 468 | ),
|
| 469 | WEST(
|
| 470 | new Direction[]{Direction.UP, Direction.DOWN, Direction.NORTH, Direction.SOUTH},
|
| 471 | 0.6F,
|
| 472 | true,
|
| 473 | new BlockModelLighter.SizeInfo[]{
|
| 474 | BlockModelLighter.SizeInfo.UP,
|
| 475 | BlockModelLighter.SizeInfo.SOUTH,
|
| 476 | BlockModelLighter.SizeInfo.UP,
|
| 477 | BlockModelLighter.SizeInfo.FLIP_SOUTH,
|
| 478 | BlockModelLighter.SizeInfo.FLIP_UP,
|
| 479 | BlockModelLighter.SizeInfo.FLIP_SOUTH,
|
| 480 | BlockModelLighter.SizeInfo.FLIP_UP,
|
| 481 | BlockModelLighter.SizeInfo.SOUTH
|
| 482 | },
|
| 483 | new BlockModelLighter.SizeInfo[]{
|
| 484 | BlockModelLighter.SizeInfo.UP,
|
| 485 | BlockModelLighter.SizeInfo.NORTH,
|
| 486 | BlockModelLighter.SizeInfo.UP,
|
| 487 | BlockModelLighter.SizeInfo.FLIP_NORTH,
|
| 488 | BlockModelLighter.SizeInfo.FLIP_UP,
|
| 489 | BlockModelLighter.SizeInfo.FLIP_NORTH,
|
| 490 | BlockModelLighter.SizeInfo.FLIP_UP,
|
| 491 | BlockModelLighter.SizeInfo.NORTH
|
| 492 | },
|
| 493 | new BlockModelLighter.SizeInfo[]{
|
| 494 | BlockModelLighter.SizeInfo.DOWN,
|
| 495 | BlockModelLighter.SizeInfo.NORTH,
|
| 496 | BlockModelLighter.SizeInfo.DOWN,
|
| 497 | BlockModelLighter.SizeInfo.FLIP_NORTH,
|
| 498 | BlockModelLighter.SizeInfo.FLIP_DOWN,
|
| 499 | BlockModelLighter.SizeInfo.FLIP_NORTH,
|
| 500 | BlockModelLighter.SizeInfo.FLIP_DOWN,
|
| 501 | BlockModelLighter.SizeInfo.NORTH
|
| 502 | },
|
| 503 | new BlockModelLighter.SizeInfo[]{
|
| 504 | BlockModelLighter.SizeInfo.DOWN,
|
| 505 | BlockModelLighter.SizeInfo.SOUTH,
|
| 506 | BlockModelLighter.SizeInfo.DOWN,
|
| 507 | BlockModelLighter.SizeInfo.FLIP_SOUTH,
|
| 508 | BlockModelLighter.SizeInfo.FLIP_DOWN,
|
| 509 | BlockModelLighter.SizeInfo.FLIP_SOUTH,
|
| 510 | BlockModelLighter.SizeInfo.FLIP_DOWN,
|
| 511 | BlockModelLighter.SizeInfo.SOUTH
|
| 512 | }
|
| 513 | ),
|
| 514 | EAST(
|
| 515 | new Direction[]{Direction.DOWN, Direction.UP, Direction.NORTH, Direction.SOUTH},
|
| 516 | 0.6F,
|
| 517 | true,
|
| 518 | new BlockModelLighter.SizeInfo[]{
|
| 519 | BlockModelLighter.SizeInfo.FLIP_DOWN,
|
| 520 | BlockModelLighter.SizeInfo.SOUTH,
|
| 521 | BlockModelLighter.SizeInfo.FLIP_DOWN,
|
| 522 | BlockModelLighter.SizeInfo.FLIP_SOUTH,
|
| 523 | BlockModelLighter.SizeInfo.DOWN,
|
| 524 | BlockModelLighter.SizeInfo.FLIP_SOUTH,
|
| 525 | BlockModelLighter.SizeInfo.DOWN,
|
| 526 | BlockModelLighter.SizeInfo.SOUTH
|
| 527 | },
|
| 528 | new BlockModelLighter.SizeInfo[]{
|
| 529 | BlockModelLighter.SizeInfo.FLIP_DOWN,
|
| 530 | BlockModelLighter.SizeInfo.NORTH,
|
| 531 | BlockModelLighter.SizeInfo.FLIP_DOWN,
|
| 532 | BlockModelLighter.SizeInfo.FLIP_NORTH,
|
| 533 | BlockModelLighter.SizeInfo.DOWN,
|
| 534 | BlockModelLighter.SizeInfo.FLIP_NORTH,
|
| 535 | BlockModelLighter.SizeInfo.DOWN,
|
| 536 | BlockModelLighter.SizeInfo.NORTH
|
| 537 | },
|
| 538 | new BlockModelLighter.SizeInfo[]{
|
| 539 | BlockModelLighter.SizeInfo.FLIP_UP,
|
| 540 | BlockModelLighter.SizeInfo.NORTH,
|
| 541 | BlockModelLighter.SizeInfo.FLIP_UP,
|
| 542 | BlockModelLighter.SizeInfo.FLIP_NORTH,
|
| 543 | BlockModelLighter.SizeInfo.UP,
|
| 544 | BlockModelLighter.SizeInfo.FLIP_NORTH,
|
| 545 | BlockModelLighter.SizeInfo.UP,
|
| 546 | BlockModelLighter.SizeInfo.NORTH
|
| 547 | },
|
| 548 | new BlockModelLighter.SizeInfo[]{
|
| 549 | BlockModelLighter.SizeInfo.FLIP_UP,
|
| 550 | BlockModelLighter.SizeInfo.SOUTH,
|
| 551 | BlockModelLighter.SizeInfo.FLIP_UP,
|
| 552 | BlockModelLighter.SizeInfo.FLIP_SOUTH,
|
| 553 | BlockModelLighter.SizeInfo.UP,
|
| 554 | BlockModelLighter.SizeInfo.FLIP_SOUTH,
|
| 555 | BlockModelLighter.SizeInfo.UP,
|
| 556 | BlockModelLighter.SizeInfo.SOUTH
|
| 557 | }
|
| 558 | );
|
| 559 |
|
| 560 | private final Direction[] corners;
|
| 561 | private final boolean doNonCubicWeight;
|
| 562 | private final BlockModelLighter.SizeInfo[] vert0Weights;
|
| 563 | private final BlockModelLighter.SizeInfo[] vert1Weights;
|
| 564 | private final BlockModelLighter.SizeInfo[] vert2Weights;
|
| 565 | private final BlockModelLighter.SizeInfo[] vert3Weights;
|
| 566 | private static final BlockModelLighter.AdjacencyInfo[] BY_FACING = Util.make(new BlockModelLighter.AdjacencyInfo[6], map -> {
|
| 567 | map[Direction.DOWN.get3DDataValue()] = DOWN;
|
| 568 | map[Direction.UP.get3DDataValue()] = UP;
|
| 569 | map[Direction.NORTH.get3DDataValue()] = NORTH;
|
| 570 | map[Direction.SOUTH.get3DDataValue()] = SOUTH;
|
| 571 | map[Direction.WEST.get3DDataValue()] = WEST;
|
| 572 | map[Direction.EAST.get3DDataValue()] = EAST;
|
| 573 | });
|
| 574 |
|
| 575 | AdjacencyInfo(
|
| 576 | final Direction[] corners,
|
| 577 | final float shadeWeight,
|
| 578 | final boolean doNonCubicWeight,
|
| 579 | final BlockModelLighter.SizeInfo[] vert0Weights,
|
| 580 | final BlockModelLighter.SizeInfo[] vert1Weights,
|
| 581 | final BlockModelLighter.SizeInfo[] vert2Weights,
|
| 582 | final BlockModelLighter.SizeInfo[] vert3Weights
|
| 583 | ) {
|
| 584 | this.corners = corners;
|
| 585 | this.doNonCubicWeight = doNonCubicWeight;
|
| 586 | this.vert0Weights = vert0Weights;
|
| 587 | this.vert1Weights = vert1Weights;
|
| 588 | this.vert2Weights = vert2Weights;
|
| 589 | this.vert3Weights = vert3Weights;
|
| 590 | }
|
| 591 |
|
| 592 | public static BlockModelLighter.AdjacencyInfo fromFacing(final Direction direction) {
|
| 593 | return BY_FACING[direction.get3DDataValue()];
|
| 594 | }
|
| 595 | }
|
| 596 |
|
| 597 | @Environment(EnvType.CLIENT)
|
| 598 | private enum AmbientVertexRemap {
|
| 599 | DOWN(0, 1, 2, 3),
|
| 600 | UP(2, 3, 0, 1),
|
| 601 | NORTH(3, 0, 1, 2),
|
| 602 | SOUTH(0, 1, 2, 3),
|
| 603 | WEST(3, 0, 1, 2),
|
| 604 | EAST(1, 2, 3, 0);
|
| 605 |
|
| 606 | private final int vert0;
|
| 607 | private final int vert1;
|
| 608 | private final int vert2;
|
| 609 | private final int vert3;
|
| 610 | private static final BlockModelLighter.AmbientVertexRemap[] BY_FACING = Util.make(new BlockModelLighter.AmbientVertexRemap[6], map -> {
|
| 611 | map[Direction.DOWN.get3DDataValue()] = DOWN;
|
| 612 | map[Direction.UP.get3DDataValue()] = UP;
|
| 613 | map[Direction.NORTH.get3DDataValue()] = NORTH;
|
| 614 | map[Direction.SOUTH.get3DDataValue()] = SOUTH;
|
| 615 | map[Direction.WEST.get3DDataValue()] = WEST;
|
| 616 | map[Direction.EAST.get3DDataValue()] = EAST;
|
| 617 | });
|
| 618 |
|
| 619 | AmbientVertexRemap(final int vert0, final int vert1, final int vert2, final int vert3) {
|
| 620 | this.vert0 = vert0;
|
| 621 | this.vert1 = vert1;
|
| 622 | this.vert2 = vert2;
|
| 623 | this.vert3 = vert3;
|
| 624 | }
|
| 625 |
|
| 626 | public static BlockModelLighter.AmbientVertexRemap fromFacing(final Direction direction) {
|
| 627 | return BY_FACING[direction.get3DDataValue()];
|
| 628 | }
|
| 629 | }
|
| 630 |
|
| 631 | @Environment(EnvType.CLIENT)
|
| 632 | public static class Cache {
|
| 633 | private boolean enabled;
|
| 634 | private final Long2IntLinkedOpenHashMap colorCache = Util.make(() -> {
|
| 635 | Long2IntLinkedOpenHashMap map = new 1(this, 100, 0.25F);
|
| 636 | map.defaultReturnValue(Integer.MAX_VALUE);
|
| 637 | return map;
|
| 638 | });
|
| 639 | private final Long2FloatLinkedOpenHashMap brightnessCache = Util.make(() -> {
|
| 640 | Long2FloatLinkedOpenHashMap map = new 2(this, 100, 0.25F);
|
| 641 | map.defaultReturnValue(Float.NaN);
|
| 642 | return map;
|
| 643 | });
|
| 644 | private final BrightnessGetter cachedBrightnessGetter = (level, pos) -> {
|
| 645 | long key = pos.asLong();
|
| 646 | int cached = this.colorCache.get(key);
|
| 647 | if (cached != Integer.MAX_VALUE) {
|
| 648 | return cached;
|
| 649 | }
|
| 650 |
|
| 651 | int value = BrightnessGetter.DEFAULT.packedBrightness(level, pos);
|
| 652 | if (this.colorCache.size() == 100) {
|
| 653 | this.colorCache.removeFirstInt();
|
| 654 | }
|
| 655 |
|
| 656 | this.colorCache.put(key, value);
|
| 657 | return value;
|
| 658 | };
|
| 659 |
|
| 660 | public void enable() {
|
| 661 | this.enabled = true;
|
| 662 | }
|
| 663 |
|
| 664 | public void disable() {
|
| 665 | this.enabled = false;
|
| 666 | this.colorCache.clear();
|
| 667 | this.brightnessCache.clear();
|
| 668 | }
|
| 669 |
|
| 670 | public int getLightCoords(final BlockState state, final BlockAndTintGetter level, final BlockPos pos) {
|
| 671 | return LightCoordsUtil.getLightCoords(this.enabled ? this.cachedBrightnessGetter : BrightnessGetter.DEFAULT, level, state, pos);
|
| 672 | }
|
| 673 |
|
| 674 | public float getShadeBrightness(final BlockState state, final BlockAndTintGetter level, final BlockPos pos) {
|
| 675 | long key = pos.asLong();
|
| 676 | if (this.enabled) {
|
| 677 | float cached = this.brightnessCache.get(key);
|
| 678 | if (!Float.isNaN(cached)) {
|
| 679 | return cached;
|
| 680 | }
|
| 681 | }
|
| 682 |
|
| 683 | float brightness = state.getShadeBrightness(level, pos);
|
| 684 | if (this.enabled) {
|
| 685 | if (this.brightnessCache.size() == 100) {
|
| 686 | this.brightnessCache.removeFirstFloat();
|
| 687 | }
|
| 688 |
|
| 689 | this.brightnessCache.put(key, brightness);
|
| 690 | }
|
| 691 |
|
| 692 | return brightness;
|
| 693 | }
|
| 694 | }
|
| 695 |
|
| 696 | @Environment(EnvType.CLIENT)
|
| 697 | private enum SizeInfo {
|
| 698 | DOWN(0),
|
| 699 | UP(1),
|
| 700 | NORTH(2),
|
| 701 | SOUTH(3),
|
| 702 | WEST(4),
|
| 703 | EAST(5),
|
| 704 | FLIP_DOWN(6),
|
| 705 | FLIP_UP(7),
|
| 706 | FLIP_NORTH(8),
|
| 707 | FLIP_SOUTH(9),
|
| 708 | FLIP_WEST(10),
|
| 709 | FLIP_EAST(11);
|
| 710 |
|
| 711 | public static final int COUNT = values().length;
|
| 712 | private final int index;
|
| 713 |
|
| 714 | SizeInfo(final int index) {
|
| 715 | this.index = index;
|
| 716 | }
|
| 717 | }
|
| 718 | }
|
| 719 |
|