| 1 | package net.minecraft.client.renderer.block;
|
| 2 |
|
| 3 | import com.mojang.blaze3d.vertex.QuadInstance;
|
| 4 | import it.unimi.dsi.fastutil.ints.IntArrayList;
|
| 5 | import it.unimi.dsi.fastutil.ints.IntList;
|
| 6 | import it.unimi.dsi.fastutil.objects.ObjectArrayList;
|
| 7 | import java.util.List;
|
| 8 | import net.fabricmc.api.EnvType;
|
| 9 | import net.fabricmc.api.Environment;
|
| 10 | import net.minecraft.client.color.block.BlockColors;
|
| 11 | import net.minecraft.client.color.block.BlockTintSource;
|
| 12 | import net.minecraft.client.renderer.block.dispatch.BlockStateModel;
|
| 13 | import net.minecraft.client.renderer.block.dispatch.BlockStateModelPart;
|
| 14 | import net.minecraft.client.resources.model.geometry.BakedQuad;
|
| 15 | import net.minecraft.core.BlockPos;
|
| 16 | import net.minecraft.core.Direction;
|
| 17 | import net.minecraft.core.BlockPos.MutableBlockPos;
|
| 18 | import net.minecraft.util.RandomSource;
|
| 19 | import net.minecraft.world.level.block.Block;
|
| 20 | import net.minecraft.world.level.block.LeavesBlock;
|
| 21 | import net.minecraft.world.level.block.state.BlockState;
|
| 22 | import net.minecraft.world.phys.Vec3;
|
| 23 | import org.jspecify.annotations.Nullable;
|
| 24 |
|
| 25 | @Environment(EnvType.CLIENT)
|
| 26 | public class ModelBlockRenderer {
|
| 27 | private static final Direction[] DIRECTIONS = Direction.values();
|
| 28 | private final BlockModelLighter lighter;
|
| 29 | private final boolean ambientOcclusion;
|
| 30 | private final boolean cull;
|
| 31 | private final BlockColors blockColors;
|
| 32 | private final RandomSource random = RandomSource.createThreadLocalInstance(0L);
|
| 33 | private final List<BlockStateModelPart> parts = new ObjectArrayList<>();
|
| 34 | private final MutableBlockPos scratchPos = new MutableBlockPos();
|
| 35 | private final QuadInstance quadInstance = new QuadInstance();
|
| 36 | private int tintCacheIndex = -1;
|
| 37 | private int tintCacheValue;
|
| 38 | private boolean tintSourcesInitialized;
|
| 39 | private final List<@Nullable BlockTintSource> tintSources = new ObjectArrayList<>();
|
| 40 | private final IntList computedTintValues = new IntArrayList();
|
| 41 |
|
| 42 | public ModelBlockRenderer(final boolean ambientOcclusion, final boolean cull, final BlockColors blockColors) {
|
| 43 | this.lighter = new BlockModelLighter();
|
| 44 | this.ambientOcclusion = ambientOcclusion;
|
| 45 | this.cull = cull;
|
| 46 | this.blockColors = blockColors;
|
| 47 | }
|
| 48 |
|
| 49 | public static boolean forceOpaque(final boolean cutoutLeaves, final BlockState blockState) {
|
| 50 | return !cutoutLeaves && blockState.getBlock() instanceof LeavesBlock;
|
| 51 | }
|
| 52 |
|
| 53 | public void tesselateBlock(
|
| 54 | final BlockQuadOutput output,
|
| 55 | final float x,
|
| 56 | final float y,
|
| 57 | final float z,
|
| 58 | final BlockAndTintGetter level,
|
| 59 | final BlockPos pos,
|
| 60 | final BlockState blockState,
|
| 61 | final BlockStateModel model,
|
| 62 | final long seed
|
| 63 | ) {
|
| 64 | this.random.setSeed(seed);
|
| 65 | model.collectParts(this.random, this.parts);
|
| 66 | if (!this.parts.isEmpty()) {
|
| 67 | try {
|
| 68 | Vec3 offset = blockState.getOffset(pos);
|
| 69 | if (this.ambientOcclusion && blockState.getLightEmission() == 0 && this.parts.getFirst().useAmbientOcclusion()) {
|
| 70 | this.tesselateAmbientOcclusion(output, x + (float)offset.x, y + (float)offset.y, z + (float)offset.z, this.parts, level, blockState, pos);
|
| 71 | } else {
|
| 72 | this.tesselateFlat(output, x + (float)offset.x, y + (float)offset.y, z + (float)offset.z, this.parts, level, blockState, pos);
|
| 73 | }
|
| 74 | } finally {
|
| 75 | this.parts.clear();
|
| 76 | this.resetTintCache();
|
| 77 | }
|
| 78 | }
|
| 79 | }
|
| 80 |
|
| 81 | private void configureTintCache(final BlockState blockState) {
|
| 82 | List<BlockTintSource> tintSources = this.blockColors.getTintSources(blockState);
|
| 83 | int tintSourceCount = tintSources.size();
|
| 84 | if (tintSourceCount > 0) {
|
| 85 | this.tintSources.addAll(tintSources);
|
| 86 |
|
| 87 | for (int i = 0; i < tintSourceCount; i++) {
|
| 88 | this.computedTintValues.add(-1);
|
| 89 | }
|
| 90 | }
|
| 91 | }
|
| 92 |
|
| 93 | private void resetTintCache() {
|
| 94 | this.tintCacheIndex = -1;
|
| 95 | if (this.tintSourcesInitialized) {
|
| 96 | this.tintSources.clear();
|
| 97 | this.computedTintValues.clear();
|
| 98 | this.tintSourcesInitialized = false;
|
| 99 | }
|
| 100 | }
|
| 101 |
|
| 102 | private void tesselateAmbientOcclusion(
|
| 103 | final BlockQuadOutput output,
|
| 104 | final float x,
|
| 105 | final float y,
|
| 106 | final float z,
|
| 107 | final List<BlockStateModelPart> parts,
|
| 108 | final BlockAndTintGetter level,
|
| 109 | final BlockState state,
|
| 110 | final BlockPos pos
|
| 111 | ) {
|
| 112 | int cacheValid = 0;
|
| 113 | int shouldRenderFaceCache = 0;
|
| 114 |
|
| 115 | for (BlockStateModelPart part : parts) {
|
| 116 | for (Direction direction : DIRECTIONS) {
|
| 117 | int cacheMask = 1 << direction.ordinal();
|
| 118 | boolean validCacheForDirection = (cacheValid & cacheMask) != 0;
|
| 119 | boolean shouldRenderFace = (shouldRenderFaceCache & cacheMask) != 0;
|
| 120 | if (!validCacheForDirection || shouldRenderFace) {
|
| 121 | List<BakedQuad> culledQuads = part.getQuads(direction);
|
| 122 | if (!culledQuads.isEmpty()) {
|
| 123 | if (!validCacheForDirection) {
|
| 124 | shouldRenderFace = this.shouldRenderFace(level, state, direction, this.scratchPos.setWithOffset(pos, direction));
|
| 125 | cacheValid |= cacheMask;
|
| 126 | if (shouldRenderFace) {
|
| 127 | shouldRenderFaceCache |= cacheMask;
|
| 128 | }
|
| 129 | }
|
| 130 |
|
| 131 | if (shouldRenderFace) {
|
| 132 | for (BakedQuad quad : culledQuads) {
|
| 133 | this.lighter.prepareQuadAmbientOcclusion(level, state, pos, quad, this.quadInstance);
|
| 134 | this.putQuadWithTint(output, x, y, z, level, state, pos, quad);
|
| 135 | }
|
| 136 | }
|
| 137 | }
|
| 138 | }
|
| 139 | }
|
| 140 |
|
| 141 | for (BakedQuad quad : part.getQuads(null)) {
|
| 142 | this.lighter.prepareQuadAmbientOcclusion(level, state, pos, quad, this.quadInstance);
|
| 143 | this.putQuadWithTint(output, x, y, z, level, state, pos, quad);
|
| 144 | }
|
| 145 | }
|
| 146 | }
|
| 147 |
|
| 148 | private void tesselateFlat(
|
| 149 | final BlockQuadOutput output,
|
| 150 | final float x,
|
| 151 | final float y,
|
| 152 | final float z,
|
| 153 | final List<BlockStateModelPart> parts,
|
| 154 | final BlockAndTintGetter level,
|
| 155 | final BlockState state,
|
| 156 | final BlockPos pos
|
| 157 | ) {
|
| 158 | int cacheValid = 0;
|
| 159 | int shouldRenderFaceCache = 0;
|
| 160 |
|
| 161 | for (BlockStateModelPart part : parts) {
|
| 162 | for (Direction direction : DIRECTIONS) {
|
| 163 | int cacheMask = 1 << direction.ordinal();
|
| 164 | boolean validCacheForDirection = (cacheValid & cacheMask) != 0;
|
| 165 | boolean shouldRenderFace = (shouldRenderFaceCache & cacheMask) != 0;
|
| 166 | if (!validCacheForDirection || shouldRenderFace) {
|
| 167 | List<BakedQuad> culledQuads = part.getQuads(direction);
|
| 168 | if (!culledQuads.isEmpty()) {
|
| 169 | BlockPos relativePos = this.scratchPos.setWithOffset(pos, direction);
|
| 170 | if (!validCacheForDirection) {
|
| 171 | shouldRenderFace = this.shouldRenderFace(level, state, direction, relativePos);
|
| 172 | cacheValid |= cacheMask;
|
| 173 | if (shouldRenderFace) {
|
| 174 | shouldRenderFaceCache |= cacheMask;
|
| 175 | }
|
| 176 | }
|
| 177 |
|
| 178 | if (shouldRenderFace) {
|
| 179 | int lightCoords = this.lighter.getLightCoords(state, level, relativePos);
|
| 180 |
|
| 181 | for (BakedQuad quad : culledQuads) {
|
| 182 | this.lighter.prepareQuadFlat(level, state, pos, lightCoords, quad, this.quadInstance);
|
| 183 | this.putQuadWithTint(output, x, y, z, level, state, pos, quad);
|
| 184 | }
|
| 185 | }
|
| 186 | }
|
| 187 | }
|
| 188 | }
|
| 189 |
|
| 190 | for (BakedQuad quad : part.getQuads(null)) {
|
| 191 | this.lighter.prepareQuadFlat(level, state, pos, -1, quad, this.quadInstance);
|
| 192 | this.putQuadWithTint(output, x, y, z, level, state, pos, quad);
|
| 193 | }
|
| 194 | }
|
| 195 | }
|
| 196 |
|
| 197 | private boolean shouldRenderFace(final BlockAndTintGetter level, final BlockState state, final Direction direction, final BlockPos neighborPos) {
|
| 198 | if (!this.cull) {
|
| 199 | return true;
|
| 200 | }
|
| 201 |
|
| 202 | BlockState neighborState = level.getBlockState(neighborPos);
|
| 203 | return Block.shouldRenderFace(state, neighborState, direction);
|
| 204 | }
|
| 205 |
|
| 206 | private void putQuadWithTint(
|
| 207 | final BlockQuadOutput output,
|
| 208 | final float x,
|
| 209 | final float y,
|
| 210 | final float z,
|
| 211 | final BlockAndTintGetter level,
|
| 212 | final BlockState state,
|
| 213 | final BlockPos pos,
|
| 214 | final BakedQuad quad
|
| 215 | ) {
|
| 216 | int tintIndex = quad.materialInfo().tintIndex();
|
| 217 | if (tintIndex != -1) {
|
| 218 | this.quadInstance.multiplyColor(this.getTintColor(level, state, pos, tintIndex));
|
| 219 | }
|
| 220 |
|
| 221 | output.put(x, y, z, quad, this.quadInstance);
|
| 222 | }
|
| 223 |
|
| 224 | private int getTintColor(final BlockAndTintGetter level, final BlockState state, final BlockPos pos, final int tintIndex) {
|
| 225 | if (this.tintCacheIndex == tintIndex) {
|
| 226 | return this.tintCacheValue;
|
| 227 | }
|
| 228 |
|
| 229 | int tintColor = this.computeTintColor(level, state, pos, tintIndex);
|
| 230 | this.tintCacheIndex = tintIndex;
|
| 231 | this.tintCacheValue = tintColor;
|
| 232 | return tintColor;
|
| 233 | }
|
| 234 |
|
| 235 | private int computeTintColor(final BlockAndTintGetter level, final BlockState state, final BlockPos pos, final int tintIndex) {
|
| 236 | if (!this.tintSourcesInitialized) {
|
| 237 | this.configureTintCache(state);
|
| 238 | this.tintSourcesInitialized = true;
|
| 239 | }
|
| 240 |
|
| 241 | if (tintIndex >= this.tintSources.size()) {
|
| 242 | return -1;
|
| 243 | } else {
|
| 244 | BlockTintSource tintSource = this.tintSources.set(tintIndex, null);
|
| 245 | if (tintSource != null) {
|
| 246 | int computedTintValue = tintSource.colorInWorld(state, level, pos);
|
| 247 | this.computedTintValues.set(tintIndex, computedTintValue);
|
| 248 | return computedTintValue;
|
| 249 | } else {
|
| 250 | return this.computedTintValues.getInt(tintIndex);
|
| 251 | }
|
| 252 | }
|
| 253 | }
|
| 254 | }
|
| 255 |
|