| 1 | package net.minecraft.client.renderer.block;
|
| 2 |
|
| 3 | import com.mojang.blaze3d.vertex.PoseStack;
|
| 4 | import com.mojang.math.MatrixUtil;
|
| 5 | import it.unimi.dsi.fastutil.ints.IntArrayList;
|
| 6 | import it.unimi.dsi.fastutil.ints.IntList;
|
| 7 | import it.unimi.dsi.fastutil.objects.ObjectArrayList;
|
| 8 | import java.util.List;
|
| 9 | import net.fabricmc.api.EnvType;
|
| 10 | import net.fabricmc.api.Environment;
|
| 11 | import net.fabricmc.fabric.api.client.renderer.v1.render.FabricBlockModelRenderState;
|
| 12 | import net.fabricmc.fabric.api.client.rendering.v1.FabricRenderState;
|
| 13 | import net.minecraft.client.renderer.Sheets;
|
| 14 | import net.minecraft.client.renderer.SubmitNodeCollector;
|
| 15 | import net.minecraft.client.renderer.block.dispatch.BlockStateModelPart;
|
| 16 | import net.minecraft.client.renderer.rendertype.RenderType;
|
| 17 | import net.minecraft.client.renderer.rendertype.RenderTypes;
|
| 18 | import net.minecraft.client.renderer.special.SpecialModelRenderer;
|
| 19 | import net.minecraft.client.renderer.texture.TextureAtlas;
|
| 20 | import net.minecraft.util.LightCoordsUtil;
|
| 21 | import net.minecraft.util.RandomSource;
|
| 22 | import org.joml.Matrix4fc;
|
| 23 | import org.jspecify.annotations.Nullable;
|
| 24 |
|
| 25 | @Environment(EnvType.CLIENT)
|
| 26 | public class BlockModelRenderState implements FabricBlockModelRenderState, FabricRenderState {
|
| 27 | public static final int[] EMPTY_TINTS = new int[0];
|
| 28 | private @Nullable List<BlockStateModelPart> modelParts;
|
| 29 | private @Nullable Matrix4fc transformation;
|
| 30 | private @Nullable RenderType renderType;
|
| 31 | private @Nullable SpecialModelRenderer<?> specialRenderer;
|
| 32 | private @Nullable Matrix4fc specialRendererTransformation;
|
| 33 | private @Nullable IntList tintLayers;
|
| 34 | public int blockLightCoords;
|
| 35 | private @Nullable RandomSource randomSource;
|
| 36 |
|
| 37 | public void clear() {
|
| 38 | this.modelParts = null;
|
| 39 | this.transformation = null;
|
| 40 | this.renderType = null;
|
| 41 | this.specialRenderer = null;
|
| 42 | this.specialRendererTransformation = null;
|
| 43 | this.blockLightCoords = 0;
|
| 44 | if (this.tintLayers != null) {
|
| 45 | this.tintLayers.clear();
|
| 46 | }
|
| 47 | }
|
| 48 |
|
| 49 | public IntList tintLayers() {
|
| 50 | if (this.tintLayers == null) {
|
| 51 | this.tintLayers = new IntArrayList();
|
| 52 | }
|
| 53 |
|
| 54 | return this.tintLayers;
|
| 55 | }
|
| 56 |
|
| 57 | public <T> void setupSpecialModel(final SpecialModelRenderer<T> renderer, final Matrix4fc transformation) {
|
| 58 | this.specialRenderer = renderer;
|
| 59 | this.specialRendererTransformation = identityToNull(transformation);
|
| 60 | }
|
| 61 |
|
| 62 | public List<BlockStateModelPart> setupModel(final Matrix4fc transformation, final boolean hasTranslucency) {
|
| 63 | this.transformation = identityToNull(transformation);
|
| 64 | this.renderType = hasTranslucency ? Sheets.translucentBlockItemSheet() : Sheets.cutoutBlockItemSheet();
|
| 65 | if (this.modelParts == null) {
|
| 66 | this.modelParts = new ObjectArrayList<>();
|
| 67 | } else {
|
| 68 | this.modelParts.clear();
|
| 69 | }
|
| 70 |
|
| 71 | return this.modelParts;
|
| 72 | }
|
| 73 |
|
| 74 | public void submit(
|
| 75 | final PoseStack poseStack, final SubmitNodeCollector submitNodeCollector, final int externalLightCoords, final int overlayCoords, final int outlineColor
|
| 76 | ) {
|
| 77 | this.submitModel(this.renderType, poseStack, submitNodeCollector, externalLightCoords, overlayCoords, outlineColor);
|
| 78 | if (this.specialRenderer != null) {
|
| 79 | int lightCoords = LightCoordsUtil.max(externalLightCoords, this.blockLightCoords);
|
| 80 | if (this.specialRendererTransformation != null) {
|
| 81 | poseStack.pushPose();
|
| 82 | poseStack.mulPose(this.specialRendererTransformation);
|
| 83 | submitSpecialRenderer(this.specialRenderer, poseStack, submitNodeCollector, lightCoords, overlayCoords, outlineColor);
|
| 84 | poseStack.popPose();
|
| 85 | } else {
|
| 86 | submitSpecialRenderer(this.specialRenderer, poseStack, submitNodeCollector, lightCoords, overlayCoords, outlineColor);
|
| 87 | }
|
| 88 | }
|
| 89 | }
|
| 90 |
|
| 91 | private static @Nullable Matrix4fc identityToNull(final Matrix4fc transformation) {
|
| 92 | return MatrixUtil.checkPropertyRaw(transformation, 4) ? null : transformation;
|
| 93 | }
|
| 94 |
|
| 95 | private void submitModel(
|
| 96 | final RenderType renderType,
|
| 97 | final PoseStack poseStack,
|
| 98 | final SubmitNodeCollector submitNodeCollector,
|
| 99 | final int externalLightCoords,
|
| 100 | final int overlayCoords,
|
| 101 | final int outlineColor
|
| 102 | ) {
|
| 103 | if (this.modelParts != null && !this.modelParts.isEmpty()) {
|
| 104 | List<BlockStateModelPart> modelPartsCopy = new ObjectArrayList<>(this.modelParts);
|
| 105 | int[] tints = this.tintLayers != null ? this.tintLayers.toArray(EMPTY_TINTS) : EMPTY_TINTS;
|
| 106 | int lightCoords = LightCoordsUtil.max(externalLightCoords, this.blockLightCoords);
|
| 107 | if (this.transformation != null) {
|
| 108 | poseStack.pushPose();
|
| 109 | poseStack.mulPose(this.transformation);
|
| 110 | submitNodeCollector.submitBlockModel(poseStack, renderType, modelPartsCopy, tints, lightCoords, overlayCoords, outlineColor);
|
| 111 | poseStack.popPose();
|
| 112 | } else {
|
| 113 | submitNodeCollector.submitBlockModel(poseStack, renderType, modelPartsCopy, tints, lightCoords, overlayCoords, outlineColor);
|
| 114 | }
|
| 115 | }
|
| 116 | }
|
| 117 |
|
| 118 | private static void submitSpecialRenderer(
|
| 119 | final SpecialModelRenderer<?> renderer,
|
| 120 | final PoseStack poseStack,
|
| 121 | final SubmitNodeCollector submitNodeCollector,
|
| 122 | final int lightCoords,
|
| 123 | final int overlayCoords,
|
| 124 | final int outlineColor
|
| 125 | ) {
|
| 126 | renderer.submit(null, poseStack, submitNodeCollector, lightCoords, overlayCoords, false, outlineColor);
|
| 127 | }
|
| 128 |
|
| 129 | public void submitOnlyOutline(
|
| 130 | final PoseStack poseStack, final SubmitNodeCollector submitNodeCollector, final int lightCoords, final int overlayCoords, final int outlineColor
|
| 131 | ) {
|
| 132 | this.submitModel(RenderTypes.outline(TextureAtlas.LOCATION_BLOCKS), poseStack, submitNodeCollector, lightCoords, overlayCoords, outlineColor);
|
| 133 | }
|
| 134 |
|
| 135 | public void submitWithZOffset(
|
| 136 | final PoseStack poseStack, final SubmitNodeCollector submitNodeCollector, final int lightCoords, final int overlayCoords, final int outlineColor
|
| 137 | ) {
|
| 138 | this.submitModel(
|
| 139 | RenderTypes.entitySolidZOffsetForward(TextureAtlas.LOCATION_BLOCKS), poseStack, submitNodeCollector, lightCoords, overlayCoords, outlineColor
|
| 140 | );
|
| 141 | }
|
| 142 |
|
| 143 | public boolean isEmpty() {
|
| 144 | return this.modelParts == null && this.specialRenderer == null;
|
| 145 | }
|
| 146 |
|
| 147 | public RandomSource scratchRandomSource(final long seed) {
|
| 148 | if (this.randomSource == null) {
|
| 149 | this.randomSource = RandomSource.create(seed);
|
| 150 | } else {
|
| 151 | this.randomSource.setSeed(seed);
|
| 152 | }
|
| 153 |
|
| 154 | return this.randomSource;
|
| 155 | }
|
| 156 | }
|
| 157 |
|