2021-06-16 20:19:33 +02:00
package com.jozufozu.flywheel.mixin ;
2021-01-13 06:58:40 +01:00
2021-07-28 23:18:24 +02:00
import org.spongepowered.asm.mixin.Final ;
2021-02-18 19:43:22 +01:00
import org.spongepowered.asm.mixin.Mixin ;
import org.spongepowered.asm.mixin.Shadow ;
2022-01-11 00:08:02 +01:00
import org.spongepowered.asm.mixin.Unique ;
2021-02-18 19:43:22 +01:00
import org.spongepowered.asm.mixin.injection.At ;
2022-01-11 00:08:02 +01:00
import org.spongepowered.asm.mixin.injection.Group ;
2021-02-18 19:43:22 +01:00
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 ;
2022-01-08 23:51:55 +01:00
import com.jozufozu.flywheel.backend.gl.GlStateTracker ;
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 ;
2021-09-15 08:45:29 +02:00
import com.mojang.blaze3d.vertex.PoseStack ;
2021-11-18 23:59:39 +01:00
import com.mojang.math.Matrix4f ;
2021-02-18 19:43:22 +01:00
2021-09-15 08:45:29 +02:00
import net.minecraft.client.Camera ;
2021-11-18 23:59:39 +01:00
import net.minecraft.client.multiplayer.ClientLevel ;
2021-04-08 19:22:11 +02:00
import net.minecraft.client.renderer.GameRenderer ;
2021-11-18 23:59:39 +01:00
import net.minecraft.client.renderer.LevelRenderer ;
2021-04-08 19:22:11 +02:00
import net.minecraft.client.renderer.LightTexture ;
2021-09-15 08:45:29 +02:00
import net.minecraft.client.renderer.RenderBuffers ;
2021-11-18 23:59:39 +01:00
import net.minecraft.client.renderer.RenderType ;
2021-09-15 08:45:29 +02:00
import net.minecraft.client.renderer.culling.Frustum ;
import net.minecraft.core.BlockPos ;
2021-11-18 23:59:39 +01:00
import net.minecraft.world.level.block.state.BlockState ;
2021-09-15 08:45:29 +02:00
import net.minecraft.world.phys.Vec3 ;
2021-01-14 22:59:26 +01:00
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
2021-01-14 22:59:26 +01:00
@OnlyIn ( Dist . CLIENT )
2022-01-11 00:08:02 +01:00
@Mixin ( value = LevelRenderer . class , priority = 1001 ) // Higher priority to go after sodium
2022-01-06 07:01:03 +01:00
public class LevelRendererMixin {
2021-02-13 02:30:21 +01:00
2021-04-08 19:22:11 +02:00
@Shadow
2021-09-15 08:45:29 +02:00
private ClientLevel level ;
2021-04-08 19:22:11 +02:00
2021-07-28 23:18:24 +02:00
@Shadow
@Final
2021-09-15 08:45:29 +02:00
private RenderBuffers renderBuffers ;
2021-07-28 23:18:24 +02:00
2021-08-05 23:53:22 +02:00
@Inject ( at = @At ( " HEAD " ) , method = " setupRender " )
2021-12-07 06:29:21 +01:00
private void setupRender ( Camera camera , Frustum frustum , boolean queue , boolean isSpectator , CallbackInfo ci ) {
MinecraftForge . EVENT_BUS . post ( new BeginFrameEvent ( level , camera , frustum ) ) ;
2021-05-05 08:56:50 +02:00
}
2022-01-11 00:08:02 +01:00
@Unique
private boolean flywheel$LayerRendered ;
2021-04-08 19:22:11 +02:00
/ * *
2022-01-11 00:08:02 +01:00
* This only gets injected if renderChunkLayer is not Overwritten
2021-04-08 19:22:11 +02:00
* /
2022-01-11 00:08:02 +01:00
@Group ( name = " flywheel$renderLayer " , min = 1 , max = 2 )
2022-01-06 07:01:03 +01:00
@Inject ( at = @At ( value = " INVOKE " , target = " Lnet/minecraft/client/renderer/ShaderInstance;clear()V " ) , 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 ) {
2022-01-11 00:08:02 +01:00
flywheel$renderLayer ( type , stack , camX , camY , camZ ) ;
flywheel$LayerRendered = true ;
}
/ * *
* This always gets injected .
* /
@Group ( name = " flywheel$renderLayer " )
@Inject ( at = @At ( " TAIL " ) , method = " renderChunkLayer " )
private void renderLayerSodium ( RenderType type , PoseStack stack , double camX , double camY , double camZ , Matrix4f p_172999_ , CallbackInfo ci ) {
if ( ! flywheel$LayerRendered ) {
flywheel$renderLayer ( type , stack , camX , camY , camZ ) ;
}
flywheel$LayerRendered = false ;
}
2021-04-08 19:22:11 +02:00
2022-01-11 00:08:02 +01:00
@Unique
private void flywheel$renderLayer ( RenderType type , PoseStack stack , double camX , double camY , double camZ ) {
2021-09-15 08:45:29 +02:00
RenderBuffers renderBuffers = this . renderBuffers ;
2021-07-28 23:18:24 +02:00
2022-01-08 23:51:55 +01:00
GlStateTracker . State restoreState = GlStateTracker . getRestoreState ( ) ;
2021-07-28 23:18:24 +02:00
MinecraftForge . EVENT_BUS . post ( new RenderLayerEvent ( level , type , stack , renderBuffers , camX , camY , camZ ) ) ;
2022-01-08 23:51:55 +01:00
restoreState . restore ( ) ;
2021-04-08 19:22:11 +02:00
}
2021-07-15 20:51:57 +02:00
@Inject ( at = @At ( " TAIL " ) , method = " allChanged " )
2021-05-05 08:56:50 +02:00
private void refresh ( CallbackInfo ci ) {
2021-06-30 21:43:54 +02:00
Backend . getInstance ( )
. refresh ( ) ;
2021-05-05 08:56:50 +02:00
2021-07-15 20:51:57 +02:00
MinecraftForge . EVENT_BUS . post ( new ReloadRenderersEvent ( level ) ) ;
2021-04-08 19:22:11 +02:00
}
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 " )
2021-09-15 08:45:29 +02:00
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 ) {
2021-12-23 06:35:48 +01:00
if ( ! Backend . isOn ( ) ) return ;
2021-05-07 03:38:05 +02:00
2021-09-15 08:45:29 +02:00
Vec3 cameraPos = info . getPosition ( ) ;
2021-12-07 08:36:14 +01:00
2022-01-08 23:51:55 +01:00
GlStateTracker . State restoreState = GlStateTracker . getRestoreState ( ) ;
2021-12-07 08:36:14 +01:00
CrumblingRenderer . renderBreaking ( new RenderLayerEvent ( level , null , stack , null , cameraPos . x , cameraPos . y , cameraPos . z ) ) ;
2022-01-08 23:51:55 +01:00
restoreState . restore ( ) ;
2021-05-07 03:38:05 +02:00
}
2021-05-05 08:56:50 +02:00
// Instancing
2021-08-10 11:06:22 +02:00
/ * *
* 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 " )
2021-04-08 19:22:11 +02:00
private void checkUpdate ( BlockPos pos , BlockState lastState , BlockState newState , CallbackInfo ci ) {
2021-12-29 05:39:32 +01:00
if ( Backend . isOn ( ) ) {
2022-01-04 06:41:08 +01:00
InstancedRenderDispatcher . getBlockEntities ( level )
2021-12-29 05:39:32 +01:00
. update ( level . getBlockEntity ( pos ) ) ;
}
2021-04-08 19:22:11 +02:00
}
2021-01-13 06:58:40 +01:00
}