gpt4 book ai didi

java - libgdx 中的着色器没有效果 [桌面]

转载 作者:行者123 更新时间:2023-11-30 03:04:44 24 4
gpt4 key购买 nike

这主要是一个一般性问题,因为我根本无法让任何着色器工作。通常的 Sprite 和纹理渲染得很好,只是着色器不会发生任何事情。也没有从着色器日志中收到任何错误消息。据我了解,对于以下类型的过滤器,只需将着色器设置为批处理,例如batch.setShader(shader),并设置任何制服,批处理将处理其余的事情。如果我错了,请告诉我我的错误。

片段着色器,应该模糊

//"in" attributes from our vertex shader
varying vec2 v_texCoord0;

//declare uniforms
uniform sampler2D uImage0;
uniform vec2 uResolution;

uniform float radius;
uniform float dirx;
uniform float diry;

void main()
{
//this will be our RGBA sum
vec4 sum = vec4(0.0);

//our original texcoord for this fragment
vec2 tc = v_texCoord0;

//the amount to blur, i.e. how far off center to sample from
//1.0 -> blur by one pixel
//2.0 -> blur by two pixels, etc.
float blur = radius / uResolution.x;

//the direction of our blur
//(1.0, 0.0) -> x-axis blur
//(0.0, 1.0) -> y-axis blur
float hstep = dirx;
float vstep = diry;

//apply blurring, using a 9-tap filter with predefined gaussian weights

sum += texture2D(uImage0, vec2(tc.x - 4.0*blur*hstep, tc.y - 4.0*blur*vstep)) * 0.0162162162;
sum += texture2D(uImage0, vec2(tc.x - 3.0*blur*hstep, tc.y - 3.0*blur*vstep)) * 0.0540540541;
sum += texture2D(uImage0, vec2(tc.x - 2.0*blur*hstep, tc.y - 2.0*blur*vstep)) * 0.1216216216;
sum += texture2D(uImage0, vec2(tc.x - 1.0*blur*hstep, tc.y - 1.0*blur*vstep)) * 0.1945945946;

sum += texture2D(uImage0, vec2(tc.x, tc.y)) * 0.2270270270;

sum += texture2D(uImage0, vec2(tc.x + 1.0*blur*hstep, tc.y + 1.0*blur*vstep)) * 0.1945945946;
sum += texture2D(uImage0, vec2(tc.x + 2.0*blur*hstep, tc.y + 2.0*blur*vstep)) * 0.1216216216;
sum += texture2D(uImage0, vec2(tc.x + 3.0*blur*hstep, tc.y + 3.0*blur*vstep)) * 0.0540540541;
sum += texture2D(uImage0, vec2(tc.x + 4.0*blur*hstep, tc.y + 4.0*blur*vstep)) * 0.0162162162;

//discard alpha for our simple demo, multiply by vertex color and return
gl_FragColor = vec4(sum.rgb, 1.0);
}

顶点着色器

attribute vec4 a_color;     
attribute vec2 a_texCoord0;
attribute vec3 a_position;

uniform mat4 u_projTrans;

varying vec4 v_color;
varying vec2 v_texCoord0;

void main(){
v_color = a_color;
v_texCoord0 = a_texCoord0;
gl_Position = u_projTrans * vec4(a_position,1.0) ;
}

设置着色器。在这里尝试了不同的值

    public void setupShader(){
ShaderProgram.pedantic=true;
shader = new ShaderProgram(Gdx.files.internal("shaders/pass.vert"),Gdx.files.internal("shaders/scanlines.frag"));
shader.begin();
shader.setUniformf("radius", 5f);
shader.setUniformf("dirx", 5f);
shader.setUniformf("diry", 5f);
shader.end();

if(shader.isCompiled())
batch.setShader(shader);
else
Settings.log(shader.getLog());
}

渲染方法。我没有在这里放置任何有关着色器的内容。

    @Override
public void render(float delta) {
Settings.clearScreen(); //usual clear screen calls from here
batch.setProjectionMatrix(cam.combined);
cam.update();

detectClicks();
checkBallScreenEdges();

batch.begin();
draw(delta);
batch.end();



}

最佳答案

感谢 Tenfour04,我得到了一个很好的扫描线着色器(使用除上述之外的其他着色器进行了测试):

    @Override
public void render(float delta) {
Settings.clearScreen();
batch.setProjectionMatrix(cam.combined);
cam.update();

batch.setShader(SpriteBatch.createDefaultShader());
main.buffer.begin();
batch.begin();
draw(delta);
batch.end();
main.buffer.end();

//POST PROCESSING
Texture bufferedTexture = main.buffer.getColorBufferTexture();
batch.setShader(main.shader);
batch.begin();
batch.draw(bufferedTexture, 0, 0, Settings.WIDTH, Settings.HEIGHT, 0, 0, Settings.WIDTH, Settings.HEIGHT, false, true); //need to flip texture
batch.end();
}

并设置着色器:

    buffer = new FrameBuffer(Pixmap.Format.RGBA8888,Settings.WIDTH,Settings.HEIGHT,false);

ShaderProgram.pedantic=false;
shader = new ShaderProgram(Gdx.files.internal("shaders/pass.vert"),Gdx.files.internal("shaders/scanlines.frag"));
shader.begin();
shader.setUniformf("uResolution",(float)Settings.WIDTH,(float)Settings.HEIGHT);
shader.end();

希望 libgdx wiki 有更多示例。

关于java - libgdx 中的着色器没有效果 [桌面],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35074698/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com