Skip to content

Commit

Permalink
gui bloom
Browse files Browse the repository at this point in the history
  • Loading branch information
EDGtheXu committed Oct 19, 2024
1 parent ec35242 commit 1ca0080
Show file tree
Hide file tree
Showing 33 changed files with 685 additions and 145 deletions.
13 changes: 13 additions & 0 deletions src/main/java/org/confluence/mod/client/post/DIYBlitTarget.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import net.minecraft.client.renderer.ShaderInstance;
import org.confluence.mod.client.shader.ModRenderTypes;
import org.joml.Matrix4f;
import org.lwjgl.opengl.GL11;

import java.util.function.Consumer;

Expand All @@ -25,6 +26,7 @@ public DIYBlitTarget(int pWidth, int pHeight, boolean pUseDepth, boolean pClearE
super(pWidth, pHeight, pUseDepth, pClearError);
this.blitShader = blitShader;
}
private boolean autoClear = true;
private Consumer<DIYShaderInstance> createSampler;
private DIYShaderInstance blitShader;
public void blitToScreen(int pWidth, int pHeight, boolean pDisableBlend) {
Expand Down Expand Up @@ -88,7 +90,9 @@ private void _blitToScreen(int pWidth, int pHeight, boolean pDisableBlend) {
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, 0);


GlStateManager._depthMask(true);

GlStateManager._colorMask(true, true, true, true);
}

Expand All @@ -105,4 +109,13 @@ public void setUniforms(Consumer<UniformsMap> consumer) {
public void setCreateSampler(Consumer<DIYShaderInstance> blitShader) {
this.createSampler = blitShader;
}
public void setAutoClear(boolean autoClear) {
this.autoClear = autoClear;
}
public void clear(boolean error){
super.clear(error);
if( autoClear && (this.viewHeight!=Minecraft.getInstance().getWindow().getHeight() || this.viewWidth!=Minecraft.getInstance().getWindow().getWidth())){
this.resize(Minecraft.getInstance().getWindow().getWidth(), Minecraft.getInstance().getWindow().getHeight(),error);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,14 @@ public DIYShaderInstance(ResourceProvider resourceProvider, ResourceLocation sha

public void apply(){
super.apply();
if(createUniforms!= null &&um.programId== this.getId()) createUniforms.accept(um);
//um.createUniform("samcolor");
if(applyUniforms!= null && um.programId == this.getId()) applyUniforms.accept(um);
//um.setUniform("samcolor", new Vector4f(0,1,0,1));

if(createUniforms!= null &&um.programId== this.getId() && !um.isReady) createUniforms.accept(um);
if(applyUniforms!= null && um.programId == this.getId() && um.isReady) applyUniforms.accept(um);

if(multiOut || bind) {
multiOut = false;
//glActiveTexture(GL_TEXTURE0+SAMPLE_CHANNELS);
glBindTexture(GL_TEXTURE_2D, textureId);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, Minecraft.getInstance().getMainRenderTarget().width,Minecraft.getInstance().getMainRenderTarget().height, 0, GL_RGBA, GL_UNSIGNED_BYTE, (ByteBuffer) null);
// glBindTexture(GL_TEXTURE_2D, textureId);
// glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, Minecraft.getInstance().getMainRenderTarget().width,Minecraft.getInstance().getMainRenderTarget().height, 0, GL_RGBA, GL_UNSIGNED_BYTE, (ByteBuffer) null);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0+SAMPLE_CHANNELS, GL_TEXTURE_2D, textureId, 0);
int[] textures = new int[]{GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT0+SAMPLE_CHANNELS};
glDrawBuffers(textures);
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/org/confluence/mod/client/post/PostEffect.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.confluence.mod.client.post;

public interface PostEffect {

void init();
void clear();
void apply();

}
128 changes: 39 additions & 89 deletions src/main/java/org/confluence/mod/client/post/PostUtil.java
Original file line number Diff line number Diff line change
@@ -1,116 +1,72 @@
package org.confluence.mod.client.post;


import com.mojang.blaze3d.pipeline.MainTarget;
import com.mojang.blaze3d.pipeline.RenderTarget;
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.stats.Stat;
import net.minecraft.world.entity.LivingEntity;
import org.confluence.mod.client.renderer.entity.boss.CthulhuEyeRenderer;
import org.confluence.mod.client.post.effect.Bloom;
import org.confluence.mod.client.post.effect.MotionBlur;
import org.confluence.mod.client.shader.ModRenderTypes;
import org.confluence.mod.item.sword.Swords;
import org.joml.Vector2f;

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.lwjgl.opengl.GL11C.GL_TEXTURE_2D;
import static org.lwjgl.opengl.GL11C.glBindTexture;
import static com.mojang.blaze3d.platform.GlStateManager.glBlendFuncSeparate;
import static org.lwjgl.opengl.GL11C.*;
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 List<PostEffect> effects = new ArrayList<>();

public static Bloom bloom;
public static MotionBlur motionBlur;

// 运动模糊帧缓冲
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(){
// 触发条件
blurList.forEach((k,v)->{
if(v.dirty){

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

// 混合方式
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() {
if(!isFirstFrame) {
renderGlow();
renderCthBlur();
//renderGlow();
motionBlur.apply();

backUp();
}
}

public static void backUp(){
Minecraft.getInstance().getMainRenderTarget().bindWrite(true);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, Minecraft.getInstance().getMainRenderTarget().getColorTextureId());
}

public static boolean isFirstFrame = true; // 必须初始化一次
// 添加特效
public static void init() {
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);
bloom = new Bloom();
motionBlur = new MotionBlur();
effects.add(bloom);
effects.add(motionBlur);



effects.forEach(PostEffect::init);
}
}

public static boolean isFirstFrame = true; // 必须初始化一次

public static void addEffect(PostEffect effect){
effects.add(effect);
}

public static void backUp(){
Minecraft.getInstance().getMainRenderTarget().bindWrite(true);
GlStateManager.glActiveTexture(GL_TEXTURE0);
GlStateManager._bindTexture(Minecraft.getInstance().getMainRenderTarget().getColorTextureId());
}

// 单个特效渲染
public static void postPass(DIYBlitTarget in, RenderTarget out, DIYShaderInstance shader, Consumer<DIYShaderInstance> samplers,Consumer<UniformsMap> uniforms){
public static void postPass( RenderTarget out, DIYBlitTarget in,DIYShaderInstance shader, Consumer<DIYShaderInstance> samplers,Consumer<UniformsMap> uniforms){
out.bindWrite(true);
GlStateManager.glActiveTexture(GL_TEXTURE0);
GlStateManager._bindTexture(out.getColorTextureId());
Expand All @@ -124,29 +80,23 @@ 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){
public static void postPass(RenderTarget out,DIYBlitTarget in, DIYShaderInstance shader, Consumer<DIYShaderInstance> samplers,Consumer<UniformsMap> uniforms,float scale){
out.bindWrite(true);
GlStateManager.glActiveTexture(GL_TEXTURE0);
GlStateManager._bindTexture(out.getColorTextureId());
in.setBlitShader(shader);
in.setUniforms(uniforms);
in.setCreateSampler(samplers);
in.blitToScreen(
Minecraft.getInstance().getWindow().getWidth()/scale,
Minecraft.getInstance().getWindow().getHeight()/scale,false
(int) (Minecraft.getInstance().getWindow().getWidth()*scale),
(int) (Minecraft.getInstance().getWindow().getHeight()*scale),false
);
}

public static void clear(){
if(glowTarget!=null) glowTarget.clear(true);
if(cthBlurTarget!=null) cthBlurTarget.clear(true);

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

});
}

public static void clear(){
effects.forEach(PostEffect::clear);
}
}
11 changes: 9 additions & 2 deletions src/main/java/org/confluence/mod/client/post/UniformsMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class UniformsMap {

public int programId;
private Map<String, Integer> uniforms;
public boolean isReady = false;

public UniformsMap(int programId) {
this.programId = programId;
Expand All @@ -23,10 +24,14 @@ public UniformsMap(int programId) {
public void createUniform(String uniformName) {
int uniformLocation = glGetUniformLocation(programId, uniformName);
if (uniformLocation < 0) {
throw new RuntimeException("Could not find uniform [" + uniformName + "] in shader program [" +

System.out.println("Could not find uniform [" + uniformName + "] in shader program [" +
programId + "]");

}else{
uniforms.put(uniformName, uniformLocation);
isReady = true;
}
uniforms.put(uniformName, uniformLocation);
}

private int getUniformLocation(String uniformName) {
Expand All @@ -37,6 +42,8 @@ private int getUniformLocation(String uniformName) {
return location.intValue();
}



public void setUniform(String uniformName, int value) {
glUniform1i(getUniformLocation(uniformName), value);
}
Expand Down
Loading

0 comments on commit 1ca0080

Please sign in to comment.