From be80ee82392554ad25dc6a6cc78be520f7930504 Mon Sep 17 00:00:00 2001 From: Adubbz Date: Sun, 14 Jan 2024 14:34:30 +1100 Subject: [PATCH] Attempt to improve anomaly performance --- .../biomesoplenty/block/AnomalyBlock.java | 10 +++- .../block/entity/AnomalyBlockEntity.java | 58 +++++++++++++++++++ .../client/renderer/AnomalyRenderer.java | 45 ++------------ 3 files changed, 72 insertions(+), 41 deletions(-) diff --git a/common/src/main/java/biomesoplenty/block/AnomalyBlock.java b/common/src/main/java/biomesoplenty/block/AnomalyBlock.java index 9ac4e848b..a1a070302 100644 --- a/common/src/main/java/biomesoplenty/block/AnomalyBlock.java +++ b/common/src/main/java/biomesoplenty/block/AnomalyBlock.java @@ -46,7 +46,15 @@ public RenderShape getRenderShape(BlockState state) @Override public VoxelShape getOcclusionShape(BlockState state, BlockGetter getter, BlockPos pos) { - return state.getValue(ANOMALY_TYPE) == AnomalyType.STABLE ? Shapes.block() : Shapes.empty(); + if (state.getValue(ANOMALY_TYPE) == AnomalyType.STABLE) return Shapes.block(); + + AnomalyBlockEntity blockEntity = (AnomalyBlockEntity)getter.getBlockEntity(pos); + + if (blockEntity != null) + { + return blockEntity.getRenderState().getOcclusionShape(getter, pos); + } + else return Shapes.empty(); } @Override diff --git a/common/src/main/java/biomesoplenty/block/entity/AnomalyBlockEntity.java b/common/src/main/java/biomesoplenty/block/entity/AnomalyBlockEntity.java index 51bd1f712..e9fc89ca6 100644 --- a/common/src/main/java/biomesoplenty/block/entity/AnomalyBlockEntity.java +++ b/common/src/main/java/biomesoplenty/block/entity/AnomalyBlockEntity.java @@ -5,13 +5,71 @@ package biomesoplenty.block.entity; import biomesoplenty.api.block.BOPBlockEntities; +import biomesoplenty.block.AnomalyBlock; +import com.google.common.collect.ImmutableList; import net.minecraft.core.BlockPos; +import net.minecraft.core.Registry; +import net.minecraft.core.registries.Registries; +import net.minecraft.util.Mth; +import net.minecraft.util.RandomSource; +import net.minecraft.world.level.Level; +import net.minecraft.world.level.block.Block; +import net.minecraft.world.level.block.Blocks; +import net.minecraft.world.level.block.RenderShape; import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraft.world.level.block.state.BlockState; +import java.util.Map; + public class AnomalyBlockEntity extends BlockEntity { + private long lastTime = -1; + private BlockState lastState = null; + public AnomalyBlockEntity(BlockPos pos, BlockState state) { super(BOPBlockEntities.ANOMALY, pos, state); } + + public BlockState getRenderState() + { + Level level = this.getLevel(); + final long time = level.getGameTime(); + + if (lastTime == time && lastState != null) + return lastState; + + RandomSource random = RandomSource.create(Mth.getSeed(this.getBlockPos())); + BlockState state = this.getBlockState(); + Registry blockRegistry = level.registryAccess().registryOrThrow(Registries.BLOCK); + int index = random.nextInt(blockRegistry.size()); + + switch (state.getValue(AnomalyBlock.ANOMALY_TYPE)) + { + case VOLATILE -> index *= (int) (time / 2L); + case QUIRKY -> index += (int) (time / 10L); + case UNSTABLE -> { + // Changes slowly most of the time, but has sudden bursts of rapid changes + final float slowWeight = 0.98F; + int mode = (Mth.sign(Mth.sin((float)time / 20.0F) + slowWeight) + 1) / 2; + if (mode > 0) index += (int)(time/ 100L); + else index += (int) time; + } + } + + index %= blockRegistry.size(); + BlockState renderState = Blocks.AIR.defaultBlockState(); + + while (renderState.getRenderShape() != RenderShape.MODEL) + { + Block renderBlock = blockRegistry.entrySet().stream().skip(index).map(Map.Entry::getValue).findFirst().orElseThrow(); + ImmutableList possibleStates = renderBlock.getStateDefinition().getPossibleStates(); + renderState = possibleStates.get(random.nextInt(possibleStates.size())); + index = (index + 1) % blockRegistry.size(); + } + + lastState = renderState; + lastTime = time; + + return renderState; + } } diff --git a/common/src/main/java/biomesoplenty/client/renderer/AnomalyRenderer.java b/common/src/main/java/biomesoplenty/client/renderer/AnomalyRenderer.java index eabeadc9b..6968201f1 100644 --- a/common/src/main/java/biomesoplenty/client/renderer/AnomalyRenderer.java +++ b/common/src/main/java/biomesoplenty/client/renderer/AnomalyRenderer.java @@ -28,59 +28,24 @@ import java.util.Map; -public class AnomalyRenderer implements BlockEntityRenderer -{ +public class AnomalyRenderer implements BlockEntityRenderer { private final BlockRenderDispatcher dispatcher; - public AnomalyRenderer(BlockEntityRendererProvider.Context context) - { + public AnomalyRenderer(BlockEntityRendererProvider.Context context) { this.dispatcher = context.getBlockRenderDispatcher(); } @Override - public void render(AnomalyBlockEntity blockEntity, float partialTick, PoseStack poseStack, MultiBufferSource buffer, int packedLight, int packedOverlay) - { + public void render(AnomalyBlockEntity blockEntity, float partialTick, PoseStack poseStack, MultiBufferSource buffer, int packedLight, int packedOverlay) { // Do regular model rendering for stable anomalies if (blockEntity.getBlockState().getValue(AnomalyBlock.ANOMALY_TYPE) == AnomalyBlock.AnomalyType.STABLE) return; Level level = blockEntity.getLevel(); BlockPos pos = blockEntity.getBlockPos(); - BlockState renderState = getRenderState(RandomSource.create(Mth.getSeed(pos)), blockEntity); + BlockState renderState = blockEntity.getRenderState(); this.dispatcher.getModelRenderer().tesselateBlock(level, this.dispatcher.getBlockModel(renderState), renderState, pos, poseStack, buffer.getBuffer(ItemBlockRenderTypes.getRenderType(renderState, true)), false, RandomSource.create(), renderState.getSeed(pos), OverlayTexture.NO_OVERLAY); } +} - private BlockState getRenderState(RandomSource random, AnomalyBlockEntity blockEntity) - { - Level level = blockEntity.getLevel(); - BlockState state = blockEntity.getBlockState(); - Registry blockRegistry = level.registryAccess().registryOrThrow(Registries.BLOCK); - int index = random.nextInt(blockRegistry.size()); - - switch (state.getValue(AnomalyBlock.ANOMALY_TYPE)) - { - case VOLATILE -> index *= level.getGameTime() / 2L; - case QUIRKY -> index += level.getGameTime() / 10L; - case UNSTABLE -> { - // Changes slowly most of the time, but has sudden bursts of rapid changes - final float slowWeight = 0.98F; - int mode = (Mth.sign(Mth.sin(level.getGameTime() / 20L) + slowWeight) + 1) / 2; - if (mode > 0) index += (int)(level.getGameTime() / 100L); - else index += level.getGameTime(); - } - } - - index %= blockRegistry.size(); - BlockState renderState = Blocks.AIR.defaultBlockState(); - - while (renderState.getRenderShape() != RenderShape.MODEL) - { - Block renderBlock = blockRegistry.entrySet().stream().skip(index).map(Map.Entry::getValue).findFirst().orElseThrow(); - ImmutableList possibleStates = renderBlock.getStateDefinition().getPossibleStates(); - renderState = possibleStates.get(random.nextInt(possibleStates.size())); - index = (index + 1) % blockRegistry.size(); - } - return renderState; - } -}