2021-06-16 20:19:33 +02:00
package com.jozufozu.flywheel.mixin ;
2021-01-13 06:58:40 +01:00
2021-02-18 19:43:22 +01:00
import org.lwjgl.opengl.GL20 ;
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-07-15 01:52:35 +02:00
import com.jozufozu.flywheel.core.crumbling.CrumblingRenderer ;
2021-06-05 00:56:46 +02:00
import com.jozufozu.flywheel.backend.instancing.InstancedRenderDispatcher ;
import com.jozufozu.flywheel.event.BeginFrameEvent ;
import com.jozufozu.flywheel.event.ReloadRenderersEvent ;
import com.jozufozu.flywheel.event.RenderLayerEvent ;
2021-01-13 06:58:40 +01:00
import com.mojang.blaze3d.matrix.MatrixStack ;
2021-02-18 19:43:22 +01:00
2021-04-08 19:22:11 +02:00
import net.minecraft.block.BlockState ;
import net.minecraft.client.renderer.ActiveRenderInfo ;
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.WorldRenderer ;
2021-02-13 02:30:21 +01:00
import net.minecraft.client.world.ClientWorld ;
2021-04-08 19:22:11 +02:00
import net.minecraft.util.math.BlockPos ;
2021-02-19 00:50:05 +01:00
import net.minecraft.util.math.vector.Matrix4f ;
2021-05-07 03:38:05 +02:00
import net.minecraft.util.math.vector.Vector3d ;
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 )
2021-01-13 06:58:40 +01:00
@Mixin ( WorldRenderer . class )
2021-02-13 02:30:21 +01:00
public class RenderHooksMixin {
2021-04-08 19:22:11 +02:00
@Shadow
private ClientWorld world ;
2021-05-05 08:56:50 +02:00
@Inject ( at = @At ( value = " INVOKE " , target = " net.minecraft.client.renderer.WorldRenderer.updateChunks(J)V " ) , method = " render " )
2021-06-30 21:43:54 +02:00
private void setupFrame ( MatrixStack stack , float p_228426_2_ , long p_228426_3_ , boolean p_228426_5_ , ActiveRenderInfo info , GameRenderer gameRenderer , LightTexture lightTexture , Matrix4f p_228426_9_ , CallbackInfo ci ) {
2021-06-05 00:56:46 +02:00
MinecraftForge . EVENT_BUS . post ( new BeginFrameEvent ( world , stack , info , gameRenderer , lightTexture ) ) ;
2021-05-05 08:56:50 +02:00
}
2021-04-08 19:22:11 +02:00
/ * *
* 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 .
* /
@Inject ( at = @At ( " TAIL " ) , method = " renderLayer " )
2021-06-30 21:43:54 +02:00
private void renderLayer ( RenderType type , MatrixStack stack , double camX , double camY , double camZ , CallbackInfo ci ) {
2021-07-15 20:36:24 +02:00
Matrix4f view = stack . last ( )
. pose ( ) ;
2021-04-13 01:00:13 +02:00
Matrix4f viewProjection = view . copy ( ) ;
2021-06-30 21:43:54 +02:00
viewProjection . multiplyBackward ( Backend . getInstance ( )
. getProjectionMatrix ( ) ) ;
2021-04-08 19:22:11 +02:00
2021-06-05 00:56:46 +02:00
MinecraftForge . EVENT_BUS . post ( new RenderLayerEvent ( world , type , viewProjection , camX , camY , camZ ) ) ;
2021-06-30 22:03:02 +02:00
if ( ! OptifineHandler . usingShaders ( ) ) GL20 . glUseProgram ( 0 ) ;
2021-04-08 19:22:11 +02:00
}
2021-05-05 08:56:50 +02:00
@Inject ( at = @At ( " TAIL " ) , method = " loadRenderers " )
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-06-05 00:56:46 +02:00
MinecraftForge . EVENT_BUS . post ( new ReloadRenderersEvent ( world ) ) ;
2021-04-08 19:22:11 +02:00
}
2021-05-08 06:24:07 +02:00
2021-06-30 21:43:54 +02:00
@Inject ( at = @At ( value = " INVOKE " , target = " Lnet/minecraft/client/renderer/WorldRenderer;checkEmpty(Lcom/mojang/blaze3d/matrix/MatrixStack;)V " , ordinal = 2 // after the game renders the breaking overlay normally
) , method = " render " )
private void renderBlockBreaking ( MatrixStack stack , float p_228426_2_ , long p_228426_3_ , boolean p_228426_5_ , ActiveRenderInfo info , GameRenderer gameRenderer , LightTexture lightTexture , Matrix4f p_228426_9_ , CallbackInfo ci ) {
if ( ! Backend . getInstance ( )
. available ( ) ) return ;
2021-05-07 03:38:05 +02:00
2021-07-15 20:36:24 +02:00
Matrix4f view = stack . last ( )
. pose ( ) ;
2021-05-07 03:38:05 +02:00
Matrix4f viewProjection = view . copy ( ) ;
2021-06-30 21:43:54 +02:00
viewProjection . multiplyBackward ( Backend . getInstance ( )
. getProjectionMatrix ( ) ) ;
2021-05-07 03:38:05 +02:00
2021-07-15 20:36:24 +02:00
Vector3d cameraPos = info . getPosition ( ) ;
2021-07-13 01:42:43 +02:00
CrumblingRenderer . renderBreaking ( world , viewProjection , cameraPos . x , cameraPos . y , cameraPos . z ) ;
2021-06-30 21:43:54 +02:00
2021-06-30 22:03:02 +02:00
if ( ! OptifineHandler . usingShaders ( ) ) GL20 . glUseProgram ( 0 ) ;
2021-05-07 03:38:05 +02:00
}
2021-05-05 08:56:50 +02:00
// Instancing
2021-04-08 19:22:11 +02:00
@Inject ( at = @At ( " TAIL " ) , method = " scheduleBlockRerenderIfNeeded " )
private void checkUpdate ( BlockPos pos , BlockState lastState , BlockState newState , CallbackInfo ci ) {
2021-06-07 11:43:36 +02:00
InstancedRenderDispatcher . getTiles ( world )
2021-07-15 20:36:24 +02:00
. update ( world . getBlockEntity ( pos ) ) ;
2021-04-08 19:22:11 +02:00
}
2021-01-13 06:58:40 +01:00
}