Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· openaiHugging Face Suzhen/CodeChat

写一个Foege MOD,监听RenderWorldLastEvent方法在世界原点处渲染两张垂直关系的透明图片,要求在各个角度他们都不受遮挡,请提供关键代码

  1. User

    写一个Foege MOD,监听RenderWorldLastEvent方法在世界原点处渲染两张垂直关系的透明图片,要求在各个角度他们都不受遮挡,请提供关键代码

  2. Assistant

    要实现一个Foege MOD,监听RenderWorldLastEvent方法在世界原点处渲染两张垂直关系的透明图片,并使它们在各个角度都不受遮挡,可以按照以下方式编写代码。

    首先,创建一个类来实现IRenderHandler接口,并实现其中的方法。

    import net.minecraftforge.client.event.RenderWorldLastEvent;
    import net.minecraftforge.client.IRenderHandler;
    import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
    import org.lwjgl.opengl.GL11;
    
    public class MyRenderHandler implements IRenderHandler {
    
        @Override
        public void render(float partialTicks, WorldClient world, Minecraft mc) {
            // 在此处渲染两张垂直关系的透明图片
    
            // 获取玩家的位置
            double playerX = mc.renderViewEntity.lastTickPosX + (mc.renderViewEntity.posX - mc.renderViewEntity.lastTickPosX) * partialTicks;
            double playerY = mc.renderViewEntity.lastTickPosY + (mc.renderViewEntity.posY - mc.renderViewEntity.lastTickPosY) * partialTicks;
            double playerZ = mc.renderViewEntity.lastTickPosZ + (mc.renderViewEntity.posZ - mc.renderViewEntity.lastTickPosZ) * partialTicks;
    
            // 设置渲染模式为透明渲染
            GL11.glEnable(GL11.GL_BLEND);
            GL11.glDepthMask(false);
            GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
            GL11.glDisable(GL11.GL_ALPHA_TEST);
    
            // 在世界原点处渲染两张垂直关系的图片
            GL11.glPushMatrix();
            {
                // 移动到世界原点
                GL11.glTranslated(-playerX, -playerY, -playerZ);
    
                // 渲染第一张图片
                // ...
                // 使用TextureManager和Tessellator渲染图片
    
                // 渲染第二张图片
                // ...
                // 使用TextureManager和Tessellator渲染图片
            }
            GL11.glPopMatrix();
    
            // 恢复渲染模式
            GL11.glDisable(GL11.GL_BLEND);
            GL11.glDepthMask(true);
            GL11.glEnable(GL11.GL_ALPHA_TEST);
        }
    
        @SubscribeEvent
        public void onRenderWorldLast(RenderWorldLastEvent event) {
            // 渲染事件发生时的处理
            Minecraft mc = Minecraft.getMinecraft();
            WorldClient world = mc.world;
    
            // 调用自定义渲染方法
            render(event.getPartialTicks(), world, mc);
        }
    }
    

    然后,创建一个主类来注册MOD,并添加对渲染事件的监听。

    import net.minecraft.client.Minecraft;
    import net.minecraftforge.client.ClientCommandHandler;
    import net.minecraftforge.common.MinecraftForge;
    import net.minecraftforge.fml.common.Mod;
    import net.minecraftforge.fml.common.Mod.EventHandler;
    import net.minecraftforge.fml.common.event.FMLInitializationEvent;
    import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
    import net.minecraftforge.fml.common.eventhandler.EventBus;
    
    @Mod(modid = "my_mod_id", name = "My Mod", version = "1.0")
    public class MyMod {
    
        @EventHandler
        public void preInit(FMLPreInitializationEvent event) {
            // 注册渲染事件监听器
            MinecraftForge.EVENT_BUS.register(new MyRenderHandler());
        }
    
        @EventHandler
        public void init(FMLInitializationEvent event) {
            // 在此处进行其他初始化操作
        }
    }
    

    请注意,需要将render方法中的图片渲染代码替换为实际的渲染代码,例如使用TextureManager和Tessellator来渲染图片。

    最后,在主类的@Mod注释中指定MOD的ID(modid)、名称(name)和版本(version)。

    完成上述代码后,打包并在Minecraft中安装该MOD后,它将在世界原点处垂直关系地渲染两张透明图片,并且它们将在各个角度都不受遮挡。