| 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.VertexConsumer;
|
| 6 | import java.util.List;
|
| 7 | import net.fabricmc.api.EnvType;
|
| 8 | import net.fabricmc.api.Environment;
|
| 9 | import net.minecraft.client.renderer.block.BlockQuadOutput;
|
| 10 | import net.minecraft.client.renderer.block.ModelBlockRenderer;
|
| 11 | import net.minecraft.client.renderer.block.MovingBlockRenderState;
|
| 12 | import net.minecraft.client.renderer.block.dispatch.BlockStateModel;
|
| 13 | import net.minecraft.client.renderer.chunk.ChunkSectionLayer;
|
| 14 | import net.minecraft.client.renderer.feature.submit.TranslucentSubmit;
|
| 15 | import net.minecraft.client.renderer.rendertype.RenderType;
|
| 16 | import net.minecraft.client.renderer.rendertype.RenderTypes;
|
| 17 | import net.minecraft.client.resources.model.geometry.BakedQuad;
|
| 18 | import net.minecraft.world.level.block.state.BlockState;
|
| 19 | import org.joml.Matrix4fc;
|
| 20 |
|
| 21 | @Environment(EnvType.CLIENT)
|
| 22 | public class MovingBlockFeatureRenderer extends RenderTypeFeatureRenderer<MovingBlockFeatureRenderer.Submit> {
|
| 23 | public static final FeatureRendererType<MovingBlockFeatureRenderer.Submit> TYPE = FeatureRendererType.create("Moving Block");
|
| 24 | private final PoseStack poseStack = new PoseStack();
|
| 25 |
|
| 26 | @Override
|
| 27 | protected void buildGroup(final FeatureFrameContext context, final List<MovingBlockFeatureRenderer.Submit> submits) {
|
| 28 | boolean ambientOcclusion = context.options().ambientOcclusion;
|
| 29 | boolean cutoutLeaves = context.options().cutoutLeaves;
|
| 30 | ModelBlockRenderer blockRenderer = new ModelBlockRenderer(ambientOcclusion, false, context.blockColors());
|
| 31 |
|
| 32 | for (MovingBlockFeatureRenderer.Submit submit : submits) {
|
| 33 | MovingBlockRenderState movingBlockRenderState = submit.movingBlockRenderState();
|
| 34 | BlockState blockState = movingBlockRenderState.blockState;
|
| 35 | BlockStateModel model = context.blockStateModelSet().get(blockState);
|
| 36 | this.poseStack.setIdentity();
|
| 37 | this.poseStack.mulPose(submit.pose());
|
| 38 | BlockQuadOutput quadOutput = (x, y, z, quad, instance) -> this.putBakedQuad(
|
| 39 | this.poseStack, x, y, z, quad, instance, submit.forceTranslucent() ? ChunkSectionLayer.TRANSLUCENT : quad.materialInfo().layer(), submit.outlineColor()
|
| 40 | );
|
| 41 | BlockQuadOutput solidQuadOutput = (x, y, z, quad, instance) -> this.putBakedQuad(
|
| 42 | this.poseStack, x, y, z, quad, instance, submit.forceTranslucent() ? ChunkSectionLayer.TRANSLUCENT : ChunkSectionLayer.SOLID, submit.outlineColor()
|
| 43 | );
|
| 44 | BlockQuadOutput blockOutput = ModelBlockRenderer.forceOpaque(cutoutLeaves, blockState) ? solidQuadOutput : quadOutput;
|
| 45 | long blockSeed = blockState.getSeed(movingBlockRenderState.randomSeedPos);
|
| 46 | blockRenderer.tesselateBlock(blockOutput, 0.0F, 0.0F, 0.0F, movingBlockRenderState, movingBlockRenderState.blockPos, blockState, model, blockSeed);
|
| 47 | }
|
| 48 | }
|
| 49 |
|
| 50 | private void putBakedQuad(
|
| 51 | final PoseStack poseStack,
|
| 52 | final float x,
|
| 53 | final float y,
|
| 54 | final float z,
|
| 55 | final BakedQuad quad,
|
| 56 | final QuadInstance instance,
|
| 57 | final ChunkSectionLayer layer,
|
| 58 | final int outlineColor
|
| 59 | ) {
|
| 60 | poseStack.pushPose();
|
| 61 | poseStack.translate(x, y, z);
|
| 62 |
|
| 63 | RenderType renderType = switch (layer) {
|
| 64 | case SOLID -> RenderTypes.solidMovingBlock();
|
| 65 | case CUTOUT -> RenderTypes.cutoutMovingBlock();
|
| 66 | case TRANSLUCENT -> RenderTypes.translucentMovingBlock();
|
| 67 | };
|
| 68 | VertexConsumer buffer;
|
| 69 | if (outlineColor != 0 && renderType.outline().isPresent()) {
|
| 70 | instance.setColor(outlineColor);
|
| 71 | buffer = this.getVertexBuilder(renderType.outline().get());
|
| 72 | } else {
|
| 73 | buffer = this.getVertexBuilder(renderType);
|
| 74 | }
|
| 75 |
|
| 76 | buffer.putBakedQuad(poseStack.last(), quad, instance);
|
| 77 | poseStack.popPose();
|
| 78 | }
|
| 79 |
|
| 80 | @Environment(EnvType.CLIENT)
|
| 81 | public record Submit(Matrix4fc pose, MovingBlockRenderState movingBlockRenderState, int outlineColor, boolean forceTranslucent) implements TranslucentSubmit {
|
| 82 | @Override
|
| 83 | public float distanceToCameraSq() {
|
| 84 | return TranslucentSubmit.computeDistanceToCameraSq(this.pose, 0.5F, 0.5F, 0.5F);
|
| 85 | }
|
| 86 |
|
| 87 | @Override
|
| 88 | public FeatureRendererType<MovingBlockFeatureRenderer.Submit> featureType() {
|
| 89 | return MovingBlockFeatureRenderer.TYPE;
|
| 90 | }
|
| 91 | }
|
| 92 | }
|
| 93 |
|