Skip to content

Commit

Permalink
克眼残影
Browse files Browse the repository at this point in the history
  • Loading branch information
EDGtheXu committed Oct 18, 2024
1 parent ebdb9cf commit dbaecad
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,9 @@
import static org.lwjgl.opengl.GL30C.*;

public class DIYBlitTarget extends TextureTarget {
public DIYBlitTarget(int pWidth, int pHeight, boolean pUseDepth, boolean pClearError, DIYShaderInstance blitShader,
Consumer<DIYShaderInstance> createSampler) {
public DIYBlitTarget(int pWidth, int pHeight, boolean pUseDepth, boolean pClearError, DIYShaderInstance blitShader) {
super(pWidth, pHeight, pUseDepth, pClearError);
this.blitShader = blitShader;
this.createSampler = createSampler;

}
private Consumer<DIYShaderInstance> createSampler;
private DIYShaderInstance blitShader;
Expand Down Expand Up @@ -54,7 +51,7 @@ private void _blitToScreen(int pWidth, int pHeight, boolean pDisableBlend) {

Matrix4f matrix4f = (new Matrix4f()).setOrtho(0.0F, (float)pWidth, (float)pHeight, 0.0F, 1000.0F, 3000.0F);
RenderSystem.setProjectionMatrix(matrix4f, VertexSorting.ORTHOGRAPHIC_Z);
createSampler.accept(blitShader);
if(createSampler!= null) createSampler.accept(blitShader);
// blitShader.setSampler("ori", Minecraft.getInstance().getMainRenderTarget().getColorTextureId());
// blitShader.setSampler("att", this.colorTextureId);

Expand Down
119 changes: 73 additions & 46 deletions src/main/java/org/confluence/mod/client/post/PostUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,68 +5,90 @@
import com.mojang.blaze3d.platform.GlStateManager;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.ShaderInstance;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
import org.confluence.mod.client.renderer.entity.boss.CthulhuEyeRenderer;
import org.confluence.mod.client.shader.ModRenderTypes;
import org.confluence.mod.item.sword.Swords;
import org.joml.Vector2f;

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

import static com.mojang.blaze3d.platform.GlStateManager.glActiveTexture;
import static org.confluence.mod.client.renderer.entity.boss.CthulhuEyeRenderer.tempBlurTarget;
//import static org.confluence.mod.client.renderer.entity.boss.CthulhuEyeRenderer.tempBlurTarget;
import static org.lwjgl.opengl.GL11C.GL_TEXTURE_2D;
import static org.lwjgl.opengl.GL11C.glBindTexture;
import static org.lwjgl.opengl.GL13C.GL_TEXTURE0;

public class PostUtil {
// 辉光帧缓冲
public static DIYBlitTarget glowTarget;
// 辉光效果
public static void renderGlow() {
// Minecraft.getInstance().player.getMainHandItem().is(Swords.DEVELOPER_SWORD.get())


public static DIYBlitTarget cthBlurTarget;
public static List<blurTuple> blurList = new ArrayList<>();
public record blurTuple(DIYBlitTarget fbo, float distance, Vector2f dir){}


}


public static void renderGlow() {
// 运动模糊帧缓冲
public static DIYBlitTarget cthBlurTarget;
public static Map<Object,blurTuple> blurList = new LinkedHashMap<>();
public static class blurTuple{
public DIYBlitTarget fbo;
public float distance;
public Vector2f dir;
public boolean dirty;
public blurTuple(DIYBlitTarget fbo, float distance, Vector2f dir, boolean dirty){
this.dir = dir;
this.distance = distance;
this.fbo = fbo;
this.dirty = dirty;
}
}

// 运动模糊效果
public static void renderCthBlur(){
// 触发条件
if(ModRenderTypes.Shaders.cthSampler.isMultiOut()){
// 迭代模糊
blurList.forEach((k,v)->{
if(v.dirty){

// 迭代模糊
// for(int i=1;i<=5;i++) {
postPass(tempBlurTarget, //输入FBO
tempBlurTarget, //输出FBO
ModRenderTypes.Shaders.conv, //使用shader
shader-> shader.setSampler("att", tempBlurTarget), //采样
postPass(v.fbo, //输入FBO
v.fbo, //输出FBO
ModRenderTypes.Shaders.conv, //使用shader
shader-> shader.setSampler("att", v.fbo), //采样
um-> {
um.setUniform("dir", new Vector2f(10,10));
um.setUniform("dist", 10.0f); //uniform
um.setUniform("dir", v.dir);
um.setUniform("dist", v.distance); //uniform
}
);
// }

// 混合方式
postPass(tempBlurTarget, //输入FBO
Minecraft.getInstance().getMainRenderTarget(), //输出FBO
ModRenderTypes.Shaders.diy_blit, //shader定义实现的混合方式
shader-> {
shader.setSampler("ori",Minecraft.getInstance().getMainRenderTarget()); //采样主屏幕
shader.setSampler("att", tempBlurTarget); //采样当前帧缓冲
},
null
//2 //视图缩放倍数
);
}
// 混合方式
postPass(v.fbo, //输入FBO
Minecraft.getInstance().getMainRenderTarget(), //输出FBO
ModRenderTypes.Shaders.diy_blit, //shader定义实现的混合方式
shader-> {
shader.setSampler("ori",Minecraft.getInstance().getMainRenderTarget()); //采样主屏幕
shader.setSampler("att", v.fbo); //采样当前帧缓冲
},
null
//2 //视图缩放倍数
);
}
});
}

// 咖式管线
public static void postProcess() {
renderGlow();
renderCthBlur();
if(!isFirstFrame) {
renderGlow();
renderCthBlur();

backUp();
backUp();
}
}

public static void backUp(){
Expand All @@ -75,20 +97,19 @@ public static void backUp(){
glBindTexture(GL_TEXTURE_2D, Minecraft.getInstance().getMainRenderTarget().getColorTextureId());
}

public static boolean isFirstFrame = true;
public static boolean isFirstFrame = true; // 必须初始化一次
public static void init() {
int width = Minecraft.getInstance().getWindow().getWidth();
int height = Minecraft.getInstance().getWindow().getHeight();
glowTarget = new DIYBlitTarget(width, height,false,true,ModRenderTypes.Shaders.diy_blit,shader->{
});
cthBlurTarget = new DIYBlitTarget(width, height,false,true,ModRenderTypes.Shaders.conv,shader->{
// shader.setSampler("ori", Minecraft.getInstance().getMainRenderTarget());
// shader.setSampler("att", cthBlurTarget);
});
cthBlurTarget.setClearColor(0,0,0,0);

if(isFirstFrame) {
isFirstFrame = false;
int width = Minecraft.getInstance().getWindow().getWidth();
int height = Minecraft.getInstance().getWindow().getHeight();
glowTarget = new DIYBlitTarget(width, height, false, true, ModRenderTypes.Shaders.diy_blit);
cthBlurTarget = new DIYBlitTarget(width, height, false, true, ModRenderTypes.Shaders.conv);
cthBlurTarget.setClearColor(0, 0, 0, 0);
}
}

// 单个特效渲染
public static void postPass(DIYBlitTarget in, RenderTarget out, DIYShaderInstance shader, Consumer<DIYShaderInstance> samplers,Consumer<UniformsMap> uniforms){
out.bindWrite(true);
GlStateManager.glActiveTexture(GL_TEXTURE0);
Expand All @@ -102,6 +123,7 @@ public static void postPass(DIYBlitTarget in, RenderTarget out, DIYShaderInstanc
);
}

// 重载用于缩放
public static void postPass(DIYBlitTarget in, RenderTarget out, DIYShaderInstance shader, Consumer<DIYShaderInstance> samplers,Consumer<UniformsMap> uniforms,int scale){
out.bindWrite(true);
GlStateManager.glActiveTexture(GL_TEXTURE0);
Expand All @@ -118,8 +140,13 @@ public static void postPass(DIYBlitTarget in, RenderTarget out, DIYShaderInstanc
public static void clear(){
if(glowTarget!=null) glowTarget.clear(true);
if(cthBlurTarget!=null) cthBlurTarget.clear(true);
if(CthulhuEyeRenderer.tempBlurTarget!=null) CthulhuEyeRenderer.tempBlurTarget.clear(true);
blurList.clear();

blurList.forEach((k,v)->{
v.dirty = false;
v.fbo.clear(true);
if(k instanceof LivingEntity && !((LivingEntity) k).isAlive()) blurList.remove(v);

});
}

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package org.confluence.mod.client.renderer.entity.boss;

import com.mojang.blaze3d.pipeline.TextureTarget;
import com.mojang.blaze3d.platform.GlStateManager;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.math.Axis;
import net.minecraft.client.Minecraft;
Expand All @@ -12,21 +9,21 @@
import net.minecraft.resources.ResourceLocation;
import org.confluence.mod.client.model.entity.CthulhuEyeModel;
import org.confluence.mod.client.post.DIYBlitTarget;
import org.confluence.mod.client.post.DIYShaderInstance;
import org.confluence.mod.client.post.PostUtil;
import org.confluence.mod.client.shader.ModRenderTypes;
import org.confluence.mod.entity.boss.geoEntity.CthulhuEye;
import org.joml.Vector4f;
import org.joml.Matrix4f;
import org.joml.Vector2f;
import org.joml.Vector3f;
import software.bernie.geckolib.renderer.GeoEntityRenderer;

import javax.annotation.Nullable;

import static com.mojang.blaze3d.platform.GlStateManager.glActiveTexture;
import static org.lwjgl.opengl.GL11C.GL_TEXTURE_2D;
import static org.lwjgl.opengl.GL11C.glBindTexture;
import static org.lwjgl.opengl.GL13C.GL_TEXTURE0;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

public class CthulhuEyeRenderer extends GeoEntityRenderer<CthulhuEye> {
public static DIYBlitTarget tempBlurTarget;
//public static DIYBlitTarget tempBlurTarget;
public CthulhuEyeRenderer(EntityRendererProvider.Context renderManager) {
super(renderManager, new CthulhuEyeModel());

Expand All @@ -48,36 +45,33 @@ protected float getDeathMaxRotation(CthulhuEye animatable){

@Override
public void render(CthulhuEye entity, float entityYaw, float partialTick, PoseStack poseStack, MultiBufferSource bufferSource, int packedLight) {
// ModRenderTypes.Shaders.cthSampler.setUniforms(ump -> {
// ump.setUniform("samcolor", new Vector4f(0,1,0,1));
// });
if(tempBlurTarget==null) {
tempBlurTarget=new DIYBlitTarget(Minecraft.getInstance().getWindow().getWidth(), Minecraft.getInstance().getWindow().getHeight(),
false,true,ModRenderTypes.Shaders.conv,shader->{
shader.setSampler("att", tempBlurTarget.getColorTextureId());
});
// tempBlurTarget=new TextureTarget(Minecraft.getInstance().getWindow().getWidth(), Minecraft.getInstance().getWindow().getHeight(),false,true);
tempBlurTarget.setClearColor(0,0,0,0);
}


ModRenderTypes.Shaders.cthSampler.setMultiOutTarget(tempBlurTarget);
if(entity.stage>1){
Object obj = entity.getUUID();
AtomicBoolean exisit = new AtomicBoolean(false);
PostUtil.blurList.forEach((k,v)->{ if(k==obj) exisit.set(true);});
DIYBlitTarget target;
Vector3f dir = entity.getDeltaMovement().toVector3f();
new Matrix4f().rotate(Minecraft.getInstance().gameRenderer.getMainCamera().rotation().conjugate().normalize()).transformPosition(dir);
float distance = Minecraft.getInstance().player.distanceTo(entity);
distance /= Math.min(entity.getDeltaMovement().length(), 1);
if(!exisit.get()){
target = new DIYBlitTarget(Minecraft.getInstance().getWindow().getWidth(), Minecraft.getInstance().getWindow().getHeight(),
false,true,ModRenderTypes.Shaders.conv);
target.setClearColor(0,0,0,0);

PostUtil.blurList.put(obj,new PostUtil.blurTuple(target,distance,new Vector2f(dir.x,dir.y),true));
}else{
target = PostUtil.blurList.get(obj).fbo;

PostUtil.blurList.get(obj).dir = new Vector2f(dir.x,dir.y);
PostUtil.blurList.get(obj).distance = distance;
PostUtil.blurList.get(obj).dirty = true;
}
ModRenderTypes.Shaders.cthSampler.setMultiOutTarget(target);
}
super.render(entity, entityYaw, partialTick, poseStack, bufferSource, packedLight);


/*
Minecraft.getInstance().getMainRenderTarget().unbindWrite();
tempBlurTarget.bindWrite(true);
tempBlurTarget.setBlitShader(ModRenderTypes.Shaders.conv);
tempBlurTarget.getBlitShader().setUniforms(ump -> {ump.setUniform("offs", 10);});
tempBlurTarget.blitToScreen(Minecraft.getInstance().getWindow().getWidth(),
Minecraft.getInstance().getWindow().getHeight(),false);
Minecraft.getInstance().getMainRenderTarget().bindWrite(true);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, Minecraft.getInstance().getMainRenderTarget().getColorTextureId());
*/
}

public RenderType getRenderType(CthulhuEye animatable, ResourceLocation texture,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@
import org.confluence.mod.client.post.PostUtil;
import org.confluence.mod.item.sword.Swords;

import static com.mojang.blaze3d.platform.GlStateManager.glActiveTexture;
import static org.confluence.mod.client.post.PostUtil.cthBlurTarget;
import static org.confluence.mod.client.renderer.entity.boss.CthulhuEyeRenderer.tempBlurTarget;


import static org.confluence.mod.effect.beneficial.helper.SpelunkerHelper.renderLevel;

import static org.lwjgl.opengl.GL30C.*;
Expand All @@ -24,7 +23,7 @@ public final class RenderEvents {
public static void renderLevelStage(RenderLevelStageEvent event) {
renderLevel(event); //洞探
if(event.getStage()== RenderLevelStageEvent.Stage.AFTER_LEVEL
&& Minecraft.getInstance().player.getMainHandItem().is(Swords.DEVELOPER_SWORD.get())
// && Minecraft.getInstance().player.getMainHandItem().is(Swords.DEVELOPER_SWORD.get())
){
PostUtil.postProcess();
/*
Expand Down Expand Up @@ -106,6 +105,8 @@ public static void renderLevelStage(RenderLevelStageEvent event) {

}
}


@SubscribeEvent
public static void onRenderTickEnd(TickEvent.RenderTickEvent event){
/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ protected void addTranslations() {

add("bossevent.confluence.cthulhu_eye.generate", "克苏鲁之眼已经苏醒!");
add("bossevent.confluence.cthulhu_eye.death", "克苏鲁之眼已被击败!");
add("bossevent.confluence.cthulhu_eye.leave", "克苏鲁之眼离开了!");

add("entity.confluence.ice_slime", "冰冻史莱姆");
add("entity.confluence.blue_slime", "蓝色史莱姆");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ protected void addTranslations() {

add("bossevent.confluence.cthulhu_eye.generate", "The CthulhuEye has awakened!");
add("bossevent.confluence.cthulhu_eye.death", "The CthulhuEye been defeated!");
add("bossevent.confluence.cthulhu_eye.leave", "The CthulhuEye leaved!");

add("curios.tooltip.speed_boots", "The wearer can run super fast");
add("curios.tooltip.may_fly", "Allows flight");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class CthulhuEye extends TerraBossBase implements DeathAnimOptions, GeoEn
private final float stage2SpeedFactor = 1.5f; //二阶段加速加成
private final float minDashDistanceSqr = 20;
private int difficultyIdx;
private int stage = 1; //阶段
public int stage = 1; //阶段

//定义技能参数
private int summonCDAll = 15; //仆从召唤cd
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ void main(){
// fragColor = texture(ori, texCoord) + color1;
// }
*/

vec2 dir1 = vec2(dir.x, -dir.y);
vec2 oneTexel = vec2(0.3/dist, 0.3/dist);
vec3 color = vec3(0.0);
for(int i = -5; i <= 5; i++){
color += texture(att, texCoord + i*oneTexel*normalize(dir)).rgb;
for(int i = -6; i <= 2; i++){
color += texture(att, texCoord + i*oneTexel*normalize(dir1)).rgb;
}
fragColor = vec4(color/10.0,texture(att,texCoord).a);

Expand Down

0 comments on commit dbaecad

Please sign in to comment.