Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use simple injection to get translucent BE's #117

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
org.gradle.daemon=false
org.gradle.jvmargs=-Xmx2g

mc_version=1.20.1
mod_version=3.2.1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.mojang.blaze3d.vertex.DefaultVertexFormat;
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.VertexFormat;
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
import net.covers1624.quack.util.CrashLock;
import net.minecraft.client.Camera;
import net.minecraft.client.Minecraft;
Expand Down Expand Up @@ -67,6 +68,8 @@ public class BCClientEventHandler {
private static LinkedList<ResourceKey<Level>> sortingOrder = new LinkedList<>();
public static int elapsedTicks = 0;

private static final Set<BlockEntity> translucentBlockEntities = new ObjectOpenHashSet<>();

private static Comparator<ResourceKey<Level>> sorter = (value, compare) -> {
long totalValue = 0;
for (Integer time : dimTickTimes.get(value)) {
Expand Down Expand Up @@ -97,6 +100,10 @@ public static void disconnectEvent(ClientPlayerNetworkEvent.LoggingOut event) {
}
}

public static <T extends BlockEntity> void addTranslucentBlockEntity(T blockEntity) {
translucentBlockEntities.add(blockEntity);
}

@SubscribeEvent
public void tickEnd(TickEvent.ClientTickEvent event) {
if (event.phase != TickEvent.Phase.END) {
Expand Down Expand Up @@ -166,42 +173,22 @@ public void renderLevelStage(RenderLevelStageEvent event) {

BlockEntityRenderDispatcher tileRenderDispatcher = Minecraft.getInstance().getBlockEntityRenderDispatcher();
MultiBufferSource buffers = Minecraft.getInstance().renderBuffers().bufferSource();
LevelRenderer levelRenderer = event.getLevelRenderer();
PoseStack poseStack = event.getPoseStack();
Camera camera = event.getCamera();
Vec3 vec3 = event.getCamera().getPosition();
double camX = vec3.x();
double camY = vec3.y();
double camZ = vec3.z();

for (LevelRenderer.RenderChunkInfo renderChunkInfo : levelRenderer.renderChunksInFrustum) {
List<BlockEntity> list = renderChunkInfo.chunk.getCompiledChunk().getRenderableBlockEntities();
for (BlockEntity tile : list) {
if (!event.getFrustum().isVisible(tile.getRenderBoundingBox())) continue;
BlockEntityRenderer<BlockEntity> renderer = tileRenderDispatcher.getRenderer(tile);
if (renderer instanceof BlockEntityRendererTransparent<BlockEntity> rendererTransparent) {
BlockPos pos = tile.getBlockPos();
poseStack.pushPose();
poseStack.translate((double) pos.getX() - camX, (double) pos.getY() - camY, (double) pos.getZ() - camZ);
renderTransparent(camera, rendererTransparent, tile, event.getPartialTick(), poseStack, buffers);
poseStack.popPose();
}
}
for (BlockEntity tile : translucentBlockEntities) {
BlockPos pos = tile.getBlockPos();
poseStack.pushPose();
poseStack.translate((double) pos.getX() - camX, (double) pos.getY() - camY, (double) pos.getZ() - camZ);
renderTransparent(camera, (BlockEntityRendererTransparent<BlockEntity>) tileRenderDispatcher.getRenderer(tile), tile, event.getPartialTick(), poseStack, buffers);
poseStack.popPose();
}

synchronized (levelRenderer.globalBlockEntities) {
for (BlockEntity tile : levelRenderer.globalBlockEntities) {
if (!event.getFrustum().isVisible(tile.getRenderBoundingBox())) continue;
BlockEntityRenderer<BlockEntity> renderer = tileRenderDispatcher.getRenderer(tile);
if (renderer instanceof BlockEntityRendererTransparent<BlockEntity> rendererTransparent) {
BlockPos blockpos3 = tile.getBlockPos();
poseStack.pushPose();
poseStack.translate((double) blockpos3.getX() - camX, (double) blockpos3.getY() - camY, (double) blockpos3.getZ() - camZ);
renderTransparent(camera, rendererTransparent, tile, event.getPartialTick(), poseStack, buffers);
poseStack.popPose();
}
}
}
translucentBlockEntities.clear();
}

public <E extends BlockEntity> void renderTransparent(Camera camera, BlockEntityRendererTransparent<E> renderer, E tile, float partialTicks, PoseStack poseStack, MultiBufferSource buffers) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.brandon3055.brandonscore.mixin;

import com.brandon3055.brandonscore.client.BCClientEventHandler;
import com.brandon3055.brandonscore.client.render.BlockEntityRendererTransparent;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.blockentity.BlockEntityRenderDispatcher;
import net.minecraft.client.renderer.blockentity.BlockEntityRenderer;
import net.minecraft.world.level.block.entity.BlockEntity;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(BlockEntityRenderDispatcher.class)
public class BlockEntityRenderDispatcherMixin {
@Inject(method = "setupAndRender", at = @At("RETURN"))
private static <T extends BlockEntity> void bc$addBEToList(BlockEntityRenderer<T> p_112285_, T p_112286_, float p_112287_, PoseStack p_112288_, MultiBufferSource p_112289_, CallbackInfo ci) {
if (p_112285_ instanceof BlockEntityRendererTransparent) {
BCClientEventHandler.addTranslucentBlockEntity(p_112286_);
}
}
}
2 changes: 1 addition & 1 deletion src/main/resources/mixins.brandonscore.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
"compatibilityLevel": "JAVA_17",
"mixins": ["EntityAccessor", "LivingEntityMixin", "PlayerMixin"],
"client": [
"ElytraLayerMixin", "LocalPlayerMixin"
"BlockEntityRenderDispatcherMixin", "ElytraLayerMixin", "LocalPlayerMixin"
]
}