2021-06-16 21:57:52 +02:00
package com.jozufozu.flywheel.mixin ;
import org.lwjgl.opengl.GL20 ;
2021-07-28 23:18:24 +02:00
import org.spongepowered.asm.mixin.Final ;
2021-06-16 21:57:52 +02:00
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 ;
import com.jozufozu.flywheel.backend.Backend ;
2021-06-30 22:03:02 +02:00
import com.jozufozu.flywheel.backend.OptifineHandler ;
2021-06-16 21:57:52 +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-16 21:57:52 +02:00
import com.jozufozu.flywheel.event.BeginFrameEvent ;
import com.jozufozu.flywheel.event.ReloadRenderersEvent ;
import com.jozufozu.flywheel.event.RenderLayerEvent ;
import com.mojang.blaze3d.matrix.MatrixStack ;
import net.minecraft.block.BlockState ;
import net.minecraft.client.renderer.ActiveRenderInfo ;
import net.minecraft.client.renderer.GameRenderer ;
import net.minecraft.client.renderer.LightTexture ;
import net.minecraft.client.renderer.RenderType ;
2021-07-28 23:18:24 +02:00
import net.minecraft.client.renderer.RenderTypeBuffers ;
2021-06-16 21:57:52 +02:00
import net.minecraft.client.renderer.WorldRenderer ;
2021-08-05 23:53:22 +02:00
import net.minecraft.client.renderer.culling.ClippingHelper ;
2021-06-16 21:57:52 +02:00
import net.minecraft.client.world.ClientWorld ;
import net.minecraft.util.math.BlockPos ;
import net.minecraft.util.math.vector.Matrix4f ;
import net.minecraft.util.math.vector.Vector3d ;
import net.minecraftforge.api.distmarker.Dist ;
import net.minecraftforge.api.distmarker.OnlyIn ;
import net.minecraftforge.common.MinecraftForge ;
@OnlyIn ( Dist . CLIENT )
@Mixin ( WorldRenderer . class )
public class RenderHooksMixin {
@Shadow
2021-07-15 20:51:57 +02:00
private ClientWorld level ;
2021-06-16 21:57:52 +02:00
2021-07-28 23:18:24 +02:00
@Shadow
@Final
private RenderTypeBuffers renderBuffers ;
2021-08-05 23:53:22 +02:00
@Inject ( at = @At ( " HEAD " ) , method = " setupRender " )
private void setupRender ( ActiveRenderInfo info , ClippingHelper clippingHelper , boolean p_228437_3_ , int frameCount , boolean isSpectator , CallbackInfo ci ) {
MinecraftForge . EVENT_BUS . post ( new BeginFrameEvent ( level , info , clippingHelper ) ) ;
2021-06-16 21:57:52 +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 .
* /
2021-07-15 20:51:57 +02:00
@Inject ( at = @At ( " TAIL " ) , method = " renderChunkLayer " )
2021-06-30 21:43:54 +02:00
private void renderLayer ( RenderType type , MatrixStack stack , double camX , double camY , double camZ , CallbackInfo ci ) {
2021-06-16 21:57:52 +02:00
2021-07-28 23:18:24 +02:00
RenderTypeBuffers renderBuffers = this . renderBuffers ;
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-06-16 21:57:52 +02:00
}
2021-07-15 20:51:57 +02:00
@Inject ( at = @At ( " TAIL " ) , method = " allChanged " )
2021-06-16 21:57:52 +02:00
private void refresh ( CallbackInfo ci ) {
2021-06-30 21:43:54 +02:00
Backend . getInstance ( )
. refresh ( ) ;
2021-06-16 21:57:52 +02:00
2021-07-15 20:51:57 +02:00
MinecraftForge . EVENT_BUS . post ( new ReloadRenderersEvent ( level ) ) ;
2021-06-16 21:57:52 +02:00
}
2021-07-15 20:51:57 +02:00
@Inject ( at = @At ( value = " INVOKE " , target = " Lnet/minecraft/client/renderer/WorldRenderer;checkPoseStack(Lcom/mojang/blaze3d/matrix/MatrixStack;)V " , ordinal = 2 // after the game renders the breaking overlay normally
) , method = " renderLevel " )
2021-06-30 21:43:54 +02:00
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-06-16 21:57:52 +02:00
2021-07-15 20:36:24 +02:00
Matrix4f view = stack . last ( )
. pose ( ) ;
2021-06-16 21:57:52 +02:00
Matrix4f viewProjection = view . copy ( ) ;
2021-06-30 21:43:54 +02:00
viewProjection . multiplyBackward ( Backend . getInstance ( )
. getProjectionMatrix ( ) ) ;
2021-06-16 21:57:52 +02:00
2021-07-15 20:36:24 +02:00
Vector3d cameraPos = info . getPosition ( ) ;
2021-07-15 20:51:57 +02:00
CrumblingRenderer . renderBreaking ( level , 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-06-16 21:57:52 +02:00
}
// Instancing
2021-07-15 20:51:57 +02:00
@Inject ( at = @At ( " TAIL " ) , method = " setBlockDirty(Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/block/BlockState;Lnet/minecraft/block/BlockState;)V " )
2021-06-16 21:57:52 +02:00
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-06-16 21:57:52 +02:00
}
}