libgdx中的着色器无效[桌面] [英] Shaders in libgdx have no effect [Desktop]

查看:212
本文介绍了libgdx中的着色器无效[桌面]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

This is mostly a general question, since I can't get any shader to work at all. The usual sprites and textures render just fine, it just doesn't happen anything with the shaders. Not getting any error messages from the shader log either. As far as I understand, for a filter of the type below, one only need to set the shader to the batch, like batch.setShader(shader), and set any uniforms, and the batch will take care of the rest. If I am wrong, please tell me my errors.

片段着色器,应该模糊

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

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

Setting up the shader. Tried different values here

    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());
}

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

The render method. I've not put anything concerning shaders here.

    @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我有一个很好的scanlines着色器工作(用上面的另一个测试):

Thanks to Tenfour04 I got a nice scanlines shader to work (tested with another than the one above):

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

Wished the libgdx wiki had more examples.

这篇关于libgdx中的着色器无效[桌面]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆