Skip to content

Commit

Permalink
Attempt to improve anomaly performance
Browse files Browse the repository at this point in the history
  • Loading branch information
Adubbz committed Jan 14, 2024
1 parent 0fe33bd commit be80ee8
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 41 deletions.
10 changes: 9 additions & 1 deletion common/src/main/java/biomesoplenty/block/AnomalyBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Block> 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<BlockState> possibleStates = renderBlock.getStateDefinition().getPossibleStates();
renderState = possibleStates.get(random.nextInt(possibleStates.size()));
index = (index + 1) % blockRegistry.size();
}

lastState = renderState;
lastTime = time;

return renderState;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,59 +28,24 @@

import java.util.Map;

public class AnomalyRenderer implements BlockEntityRenderer<AnomalyBlockEntity>
{
public class AnomalyRenderer implements BlockEntityRenderer<AnomalyBlockEntity> {
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<Block> 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<BlockState> possibleStates = renderBlock.getStateDefinition().getPossibleStates();
renderState = possibleStates.get(random.nextInt(possibleStates.size()));
index = (index + 1) % blockRegistry.size();
}

return renderState;
}
}

0 comments on commit be80ee8

Please sign in to comment.