Shadertoy 着色器导入到 libgdx

Shu*_*wal 5 shader procedural-generation libgdx

我到处搜索,但没有参考。我想将这个着色器从 shadertoy 用于我的 libgdx 项目,所以我尝试首先从以下位置导入简单的着色器:https ://www.shadertoy.com/view/XsffRs

我像这样修改了它,但没有成功:

/*
 * Original shader from: https://www.shadertoy.com/view/XsffRs
 */

#ifdef GL_ES
precision mediump float;
#endif

uniform float time;
uniform vec2 resolution;

// shadertoy emulation
#define iTime time
#define iResolution resolution

// --------[ Original ShaderToy begins here ]---------- //
#define TAU 6.28318531
float C,S;

mat2 rot(float a){
    return mat2(C=cos(a),S=sin(a),-S,C);
}

float map(vec3 p) {
    p.yz*=rot(p.z*(.03*sin(iTime*3.)));
    p.xz*=rot(p.z*(.03*cos(iTime*3.)));
    float m=TAU/6.,
    l=length(p.xy),
    a=mod(atan(p.y,p.x)-p.z*.5+iTime*5.,m)-.5*m;
    return length(vec2(a*l,l-2.))-.8;
}


void main(void)
{
    vec2 uv = gl_FragCoord.xy / iResolution.xy;
    uv-=.5;
    uv.x*=iResolution.x/iResolution.y;
    vec3 ro=vec3(uv,-3.),rd=normalize(vec3(uv,1.)),mp=ro;
    float i=0.;
    for (int ii=0;ii<30;++ii) {
        i++;
        float md=map(mp);
        if (abs(md)<.001)break;
        mp+=rd*md;
    }
    float r=i/30.;
    float d=length(mp-ro)*.1;
    vec3 c=mix(vec3(.2,.5,.7)*d*d,vec3(.2,.4,.8)*r/d,r*r);
    c=sqrt(c);
    gl_FragColor = vec4(c,1.);

}

Run Code Online (Sandbox Code Playgroud)

着色器程序代码

    public void create () {

        width = Gdx.graphics.getWidth();
        height = Gdx.graphics.getHeight();

        batch = new SpriteBatch();
        ShaderProgram.pedantic = false;
        shader = new ShaderProgram(Gdx.files.internal("vert.vert"), Gdx.files.internal("frag.frag"));
        if(!shader.isCompiled())
            shader.getLog();
    }

    public void render () {
        time+=Gdx.graphics.getDeltaTime();

        shader.begin();
        shader.setUniformf("resolution", new Vector2(width, height));
        shader.setUniformf("time", time);
        shader.end();
        
        batch.begin();
        batch.setShader(shader);
        batch.end();
    }

Run Code Online (Sandbox Code Playgroud)

着色器运行没有错误,但黑屏。

编辑:它通过
Texture t = new Texture(new Pixmap(width,height, Pixmap.Format.RGB565)); 使用 spritebatch绘制虚拟纹理来工作,但不知道为什么需要虚拟纹理?

小智 0

为了查看着色器的运行情况,您需要绘制一些内容,代码仅指定将使用着色器,但不会用它绘制任何内容

batch.begin();
batch.setShader(shader);
batch.draw(new Texture(new Pixmap(width,height, Pixmap.Format.RGB565),0,0);
batch.end();
Run Code Online (Sandbox Code Playgroud)