| 1 | package net.minecraft.client.renderer.feature;
|
| 2 |
|
| 3 | import com.mojang.blaze3d.vertex.PoseStack;
|
| 4 | import com.mojang.blaze3d.vertex.QuadInstance;
|
| 5 | import com.mojang.blaze3d.vertex.SheetedDecalTextureGenerator;
|
| 6 | import com.mojang.blaze3d.vertex.VertexConsumer;
|
| 7 | import java.util.List;
|
| 8 | import net.fabricmc.api.EnvType;
|
| 9 | import net.fabricmc.api.Environment;
|
| 10 | import net.minecraft.client.renderer.block.dispatch.BlockStateModelPart;
|
| 11 | import net.minecraft.client.renderer.feature.submit.TranslucentSubmit;
|
| 12 | import net.minecraft.client.renderer.rendertype.RenderType;
|
| 13 | import net.minecraft.client.resources.model.geometry.BakedQuad;
|
| 14 | import net.minecraft.core.Direction;
|
| 15 | import net.minecraft.util.ARGB;
|
| 16 | import org.jspecify.annotations.Nullable;
|
| 17 |
|
| 18 | @Environment(EnvType.CLIENT)
|
| 19 | public class BlockModelFeatureRenderer extends RenderTypeFeatureRenderer<BlockModelFeatureRenderer.Submit> {
|
| 20 | public static final FeatureRendererType<BlockModelFeatureRenderer.Submit> TYPE = FeatureRendererType.create("Block Model");
|
| 21 | private static final Direction[] DIRECTIONS = Direction.values();
|
| 22 | private final QuadInstance quadInstance = new QuadInstance();
|
| 23 |
|
| 24 | @Override
|
| 25 | protected void buildGroup(final FeatureFrameContext context, final List<BlockModelFeatureRenderer.Submit> submits) {
|
| 26 | for (BlockModelFeatureRenderer.Submit submit : submits) {
|
| 27 | VertexConsumer buffer = this.getVertexBuilder(submit.renderType());
|
| 28 | VertexConsumer wrappedBuffer = submit.sheetedDecalPose() != null ? new SheetedDecalTextureGenerator(buffer, submit.sheetedDecalPose(), 1.0F) : buffer;
|
| 29 | this.quadInstance.setLightCoords(submit.lightCoords());
|
| 30 | this.quadInstance.setOverlayCoords(submit.overlayCoords());
|
| 31 |
|
| 32 | for (BlockStateModelPart part : submit.modelParts()) {
|
| 33 | putPartQuads(part, submit.pose(), this.quadInstance, submit.tintColor(), submit.tintLayers(), wrappedBuffer);
|
| 34 | }
|
| 35 | }
|
| 36 | }
|
| 37 |
|
| 38 | private static void putPartQuads(
|
| 39 | final BlockStateModelPart part,
|
| 40 | final PoseStack.Pose pose,
|
| 41 | final QuadInstance quadInstance,
|
| 42 | final int baseTintColor,
|
| 43 | final int[] tintLayers,
|
| 44 | final VertexConsumer buffer
|
| 45 | ) {
|
| 46 | for (Direction direction : DIRECTIONS) {
|
| 47 | for (BakedQuad quad : part.getQuads(direction)) {
|
| 48 | putQuad(pose, quad, quadInstance, baseTintColor, tintLayers, buffer);
|
| 49 | }
|
| 50 | }
|
| 51 |
|
| 52 | for (BakedQuad quad : part.getQuads(null)) {
|
| 53 | putQuad(pose, quad, quadInstance, baseTintColor, tintLayers, buffer);
|
| 54 | }
|
| 55 | }
|
| 56 |
|
| 57 | private static void putQuad(
|
| 58 | final PoseStack.Pose pose, final BakedQuad quad, final QuadInstance instance, final int baseTintColor, final int[] tintLayers, final VertexConsumer buffer
|
| 59 | ) {
|
| 60 | int tintIndex = quad.materialInfo().tintIndex();
|
| 61 | boolean useTintLayer = tintIndex != -1 && tintIndex < tintLayers.length;
|
| 62 | instance.setColor(useTintLayer ? ARGB.multiply(baseTintColor, tintLayers[tintIndex]) : baseTintColor);
|
| 63 | buffer.putBakedQuad(pose, quad, instance);
|
| 64 | }
|
| 65 |
|
| 66 | @Environment(EnvType.CLIENT)
|
| 67 | public record Submit(
|
| 68 | PoseStack.Pose pose,
|
| 69 | RenderType renderType,
|
| 70 | List<BlockStateModelPart> modelParts,
|
| 71 | int[] tintLayers,
|
| 72 | int lightCoords,
|
| 73 | int overlayCoords,
|
| 74 | int tintColor,
|
| 75 | PoseStack.@Nullable Pose sheetedDecalPose
|
| 76 | ) implements TranslucentSubmit {
|
| 77 | @Override
|
| 78 | public float distanceToCameraSq() {
|
| 79 | return TranslucentSubmit.computeDistanceToCameraSq(this.pose.pose(), 0.5F, 0.5F, 0.5F);
|
| 80 | }
|
| 81 |
|
| 82 | @Override
|
| 83 | public FeatureRendererType<BlockModelFeatureRenderer.Submit> featureType() {
|
| 84 | return BlockModelFeatureRenderer.TYPE;
|
| 85 | }
|
| 86 | }
|
| 87 | }
|
| 88 |
|