libgdx中的着色器无效[桌面]

时间:2016-01-29 00:15:36

标签: java libgdx glsl shader

这主要是一个普遍的问题,因为我根本无法使用任何着色器。通常的精灵和纹理渲染得很好,它只是没有与着色器发生任何事情。没有从着色器日志中收到任何错误消息。据我所知,对于下面类型的过滤器,只需要将着色器设置为批处理,如batch.setShader(着色器),并设置任何制服,批处理将负责其余部分。如果我错了,请告诉我我的错误。

片段着色器,应该模糊

//"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();



}

1 个答案:

答案 0 :(得分:1)

感谢Tenfour04我有一个很好的scanlines着色器可以工作(使用上面的另一个进行测试):

    @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有更多的例子。