Flywheel/src/main/java/com/jozufozu/flywheel/mixin/RenderHooksMixin.java

107 lines
4.2 KiB
Java
Raw Normal View History

2021-06-16 20:19:33 +02:00
package com.jozufozu.flywheel.mixin;
2021-01-13 06:58:40 +01:00
import org.lwjgl.opengl.GL20;
2021-07-28 23:18:24 +02:00
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
2021-05-02 01:32:09 +02:00
import com.jozufozu.flywheel.backend.Backend;
2021-06-30 22:03:02 +02:00
import com.jozufozu.flywheel.backend.OptifineHandler;
2021-06-05 00:56:46 +02:00
import com.jozufozu.flywheel.backend.instancing.InstancedRenderDispatcher;
2021-07-24 08:46:30 +02:00
import com.jozufozu.flywheel.core.crumbling.CrumblingRenderer;
2021-06-05 00:56:46 +02:00
import com.jozufozu.flywheel.event.BeginFrameEvent;
import com.jozufozu.flywheel.event.ReloadRenderersEvent;
import com.jozufozu.flywheel.event.RenderLayerEvent;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.client.Camera;
import net.minecraft.client.renderer.GameRenderer;
import net.minecraft.client.renderer.LightTexture;
2021-01-13 06:58:40 +01:00
import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.renderer.RenderBuffers;
import net.minecraft.client.renderer.LevelRenderer;
import net.minecraft.client.renderer.culling.Frustum;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.core.BlockPos;
import com.mojang.math.Matrix4f;
import net.minecraft.world.phys.Vec3;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
2021-06-05 00:56:46 +02:00
import net.minecraftforge.common.MinecraftForge;
2021-02-13 02:30:21 +01:00
@OnlyIn(Dist.CLIENT)
@Mixin(LevelRenderer.class)
2021-02-13 02:30:21 +01:00
public class RenderHooksMixin {
@Shadow
private ClientLevel level;
2021-07-28 23:18:24 +02:00
@Shadow
@Final
private RenderBuffers renderBuffers;
2021-07-28 23:18:24 +02:00
@Inject(at = @At("HEAD"), method = "setupRender")
private void setupRender(Camera info, Frustum clippingHelper, boolean p_228437_3_, int frameCount, boolean isSpectator, CallbackInfo ci) {
MinecraftForge.EVENT_BUS.post(new BeginFrameEvent(level, info, clippingHelper));
}
/**
* JUSTIFICATION: This method is called once per layer per frame. It allows us to perform
* layer-correct custom rendering. RenderWorldLast is not refined enough for rendering world objects.
* This should probably be a forge event.
*/
2021-07-15 20:51:57 +02:00
@Inject(at = @At("TAIL"), method = "renderChunkLayer")
2021-09-15 09:26:51 +02:00
private void renderLayer(RenderType type, PoseStack stack, double camX, double camY, double camZ, Matrix4f p_172999_, CallbackInfo ci) {
RenderBuffers renderBuffers = this.renderBuffers;
2021-07-28 23:18:24 +02:00
MinecraftForge.EVENT_BUS.post(new RenderLayerEvent(level, type, stack, renderBuffers, camX, camY, camZ));
2021-06-30 22:03:02 +02:00
if (!OptifineHandler.usingShaders()) GL20.glUseProgram(0);
2021-07-28 23:18:24 +02:00
renderBuffers.bufferSource().endBatch(type);
}
2021-07-15 20:51:57 +02:00
@Inject(at = @At("TAIL"), method = "allChanged")
private void refresh(CallbackInfo ci) {
Backend.getInstance()
.refresh();
2021-07-15 20:51:57 +02:00
MinecraftForge.EVENT_BUS.post(new ReloadRenderersEvent(level));
}
2021-05-08 06:24:07 +02:00
2021-09-15 09:26:51 +02:00
@Inject(at = @At(value = "INVOKE", target = "Lnet/minecraft/client/renderer/LevelRenderer;checkPoseStack(Lcom/mojang/blaze3d/vertex/PoseStack;)V", ordinal = 2 // after the game renders the breaking overlay normally
2021-07-15 20:51:57 +02:00
), method = "renderLevel")
private void renderBlockBreaking(PoseStack stack, float p_228426_2_, long p_228426_3_, boolean p_228426_5_, Camera info, GameRenderer gameRenderer, LightTexture lightTexture, Matrix4f p_228426_9_, CallbackInfo ci) {
if (!Backend.getInstance()
.available()) return;
2021-07-15 20:36:24 +02:00
Matrix4f view = stack.last()
.pose();
Matrix4f viewProjection = view.copy();
viewProjection.multiplyBackward(RenderSystem.getProjectionMatrix());
Vec3 cameraPos = info.getPosition();
2021-07-15 20:51:57 +02:00
CrumblingRenderer.renderBreaking(level, viewProjection, cameraPos.x, cameraPos.y, cameraPos.z);
2021-06-30 22:03:02 +02:00
if (!OptifineHandler.usingShaders()) GL20.glUseProgram(0);
}
// Instancing
/**
* This gets called when a block is marked for rerender by vanilla.
*/
2021-09-15 09:26:51 +02:00
@Inject(at = @At("TAIL"), method = "setBlockDirty(Lnet/minecraft/core/BlockPos;Lnet/minecraft/world/level/block/state/BlockState;Lnet/minecraft/world/level/block/state/BlockState;)V")
private void checkUpdate(BlockPos pos, BlockState lastState, BlockState newState, CallbackInfo ci) {
2021-07-15 20:51:57 +02:00
InstancedRenderDispatcher.getTiles(level)
.update(level.getBlockEntity(pos));
}
2021-01-13 06:58:40 +01:00
}