Skip to content

Commit

Permalink
自定义泛光物品和基类
Browse files Browse the repository at this point in the history
  • Loading branch information
EDGtheXu committed Oct 20, 2024
1 parent 51dc70e commit 892fd0e
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 29 deletions.
3 changes: 3 additions & 0 deletions src/main/java/org/confluence/mod/client/post/PostUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import net.minecraft.client.Minecraft;
import org.confluence.mod.client.post.effect.Bloom;
import org.confluence.mod.client.post.effect.MotionBlur;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL30;

import java.util.*;
import java.util.function.Consumer;

Expand Down
57 changes: 34 additions & 23 deletions src/main/java/org/confluence/mod/client/post/effect/Bloom.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
package org.confluence.mod.client.post.effect;

import net.minecraft.client.Minecraft;
import net.minecraft.world.item.Item;
import org.confluence.mod.client.post.DIYBlitTarget;
import org.confluence.mod.client.post.PostEffect;
import org.confluence.mod.client.shader.ModRenderTypes;
import org.confluence.mod.item.curio.BaseCurioItem;
import org.confluence.mod.item.curio.CurioItems;
import org.confluence.mod.item.sword.LightSaber;
import org.confluence.mod.item.sword.Swords;

import java.util.HashMap;
import java.util.Map;

import static org.confluence.mod.client.post.PostUtil.postPass;
import static org.lwjgl.opengl.GL11C.*;
Expand All @@ -15,11 +23,20 @@ public class Bloom implements PostEffect {
public DIYBlitTarget glowTargetOri;
public DIYBlitTarget glowTargetH;
public DIYBlitTarget glowTargetV;
// 施加效果的物品:加入map则采样,selfGlow采样最大亮度,light修正亮度
public Map<Item,Tuple> itemMap = new HashMap<>();
public Map<Class<? extends Item>,Tuple> itemClassMap = new HashMap<>();

public Item mainhandItem;
public Item offhandItem;
public boolean shouldRender = false;
public record Tuple(boolean selfGlow, float light){}
public Bloom(){

}
// 辉光效果
public void apply() {
if(!shouldRender) return;

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Expand All @@ -33,40 +50,24 @@ public void apply() {
},
null
);

glDisable(GL_BLEND);
for(int i=0;i<20;i++){
int finalI = i;
var out =i%2==0? glowTargetV: glowTargetH;
var in =i%2==0? glowTargetH: glowTargetV;
postPass(
out, //输出FBO
in, //输入FBO
ModRenderTypes.Shaders.gaussian_blur, //shader定义实现的混合方式
out,
in,
ModRenderTypes.Shaders.gaussian_blur,
shader-> {
shader.setSampler("att", in); //采样当前帧缓冲
shader.setSampler("att", in);
},
um->{um.setUniform("hor", finalI %2==0?GL_TRUE:GL_FALSE);}
);
}

// glEnable(GL_BLEND);
// glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ZERO);
/*
// glBlendFunc(GL_ONE, GL_ONE);
postPass(
glowTargetH,
glowTargetOri,
ModRenderTypes.Shaders.diy_blit_gamma,
shader-> {
shader.setSampler("ori", glowTargetH);
shader.setSampler("att", glowTargetOri);
},
null
);
*/

// glEnable(GL_BLEND);
//
//glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ZERO);
//glBlendFunc(GL_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR);
glEnable(GL_BLEND);
Expand Down Expand Up @@ -98,14 +99,24 @@ public void init() {

glowTargetV = new DIYBlitTarget(width, height, false, true, ModRenderTypes.Shaders.diy_blit);
glowTargetV.setClearColor(0, 0, 0, 0);

// 注册辉光效果的物品类型
itemClassMap.put(LightSaber.class, new Tuple( true, 2.0f));
itemClassMap.put(BaseCurioItem.class, new Tuple( false, 1.0f));
itemMap.put(CurioItems.ANKH_SHIELD.get(),new Tuple( true, 1.0f));//覆盖类属性


// 注册辉光效果的物品 优先级更高
itemMap.put(Swords.VOLCANO.get(), new Tuple( true, 2.0f));
itemMap.put(Swords.DEVELOPER_SWORD.get(), new Tuple( false, 1.0f));

}

@Override
public void clear() {
if(glowTargetOri!=null) glowTargetOri.clear(true);
if(glowTargetH !=null) glowTargetH.clear(true);
if(glowTargetV !=null) glowTargetV.clear(true);

shouldRender = false;
}

}
53 changes: 48 additions & 5 deletions src/main/java/org/confluence/mod/event/GuiRenderEvent.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
package org.confluence.mod.event;

import com.mojang.blaze3d.platform.GlStateManager;
import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.Minecraft;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.item.Item;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.client.event.RenderGuiEvent;
import net.minecraftforge.client.event.RenderHandEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import org.confluence.mod.client.post.PostUtil;
import org.confluence.mod.client.renderer.gui.ArrowInBowOverlay;
import org.confluence.mod.client.shader.ModRenderTypes;

import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;

import static org.confluence.mod.Confluence.MODID;
import static org.confluence.mod.client.post.PostUtil.*;

Expand All @@ -19,12 +25,49 @@ public class GuiRenderEvent {
@SubscribeEvent
public static void renderHandEvent(RenderHandEvent event) {
ArrowInBowOverlay.render(event);
if(bloom==null) return;
// 如果右手无,左手有,先处理右手缓冲
Minecraft.getInstance().gameRenderer.renderBuffers.bufferSource().endBatch();
if(event.getHand() == InteractionHand.MAIN_HAND) bloom.mainhandItem = event.getItemStack().getItem();
else bloom.offhandItem = event.getItemStack().getItem();

if(event.getHand()== InteractionHand.OFF_HAND){
PostUtil.bloom.glowTargetOri.bindWrite(true);
ModRenderTypes.Shaders.positionColorSampler.setMultiOutTarget(PostUtil.bloom.glowTargetH);
ModRenderTypes.Shaders.positionColorSampler.apply();

// 物品优先级大于类
AtomicBoolean isChild = new AtomicBoolean(false);
AtomicReference<Class<? extends Item>> clazz = new AtomicReference<>(Item.class);
if(!bloom.itemMap.containsKey(event.getItemStack().getItem())){
bloom.itemClassMap.forEach((itemClass, bloomItem) -> {
if(itemClass.isAssignableFrom(event.getItemStack().getItem().getClass())){
isChild.set(true);
clazz.set(itemClass);
}
});
}
if(bloom.itemMap.containsKey(event.getItemStack().getItem()) || isChild.get()){
bloom.shouldRender = true;
bloom.glowTargetOri.bindWrite(true);
ModRenderTypes.Shaders.positionColorSampler.setMultiOutTarget(bloom.glowTargetH);
ModRenderTypes.Shaders.positionColorSampler.apply();
var r = Minecraft.getInstance().gameRenderer.itemInHandRenderer;
int light;
if(isChild.get()) light = bloom.itemClassMap.get(clazz.get()).selfGlow()?0xa000a0:event.getPackedLight();
else light = bloom.itemMap.get(event.getItemStack().getItem()).selfGlow()?0xa000a0:event.getPackedLight();
r.renderArmWithItem(
Minecraft.getInstance().player,
event.getPartialTick(),
event.getInterpolatedPitch(),
event.getHand(),
event.getSwingProgress(),
event.getItemStack(),
event.getEquipProgress(),
event.getPoseStack(),
event.getMultiBufferSource(),
light);
// 及时处理缓冲
Minecraft.getInstance().gameRenderer.renderBuffers.bufferSource().endBatch();
Minecraft.getInstance().getMainRenderTarget().bindWrite(true);
GlStateManager._bindTexture(Minecraft.getInstance().getMainRenderTarget().getColorTextureId());
event.setCanceled(true);
}

}
Expand Down
5 changes: 4 additions & 1 deletion src/main/resources/META-INF/accesstransformer.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,7 @@ public net.minecraft.client.model.geom.ModelPart$Polygon
public net.minecraft.client.model.geom.ModelPart$Vertex
public net.minecraft.world.level.biome.MultiNoiseBiomeSourceParameterList$Preset$SourceProvider

public net.minecraft.client.renderer.ItemInHandRenderer m_109371_(Lnet/minecraft/client/player/AbstractClientPlayer;FFLnet/minecraft/world/InteractionHand;FLnet/minecraft/world/item/ItemStack;FLcom/mojang/blaze3d/vertex/PoseStack;Lnet/minecraft/client/renderer/MultiBufferSource;I)V # renderArmWithItem
public net.minecraft.client.renderer.ItemInHandRenderer m_109371_(Lnet/minecraft/client/player/AbstractClientPlayer;FFLnet/minecraft/world/InteractionHand;FLnet/minecraft/world/item/ItemStack;FLcom/mojang/blaze3d/vertex/PoseStack;Lnet/minecraft/client/renderer/MultiBufferSource;I)V # renderArmWithItem
public net.minecraft.client.renderer.GameRenderer f_109064_ # renderBuffers


0 comments on commit 892fd0e

Please sign in to comment.