Shadertoy shader import to libgdx

Viewed 279

I have searched everywhere but got no reference. I want to use this shader from shadertoy to my libgdx project, so I tried to import simple shader first from: https://www.shadertoy.com/view/XsffRs

I modified it a bit like this but got no success:

/*
 * 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.);

}

Code for ShaderProgram

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

Shader is running without error but getting black screen.

Edit: It works by drawing dummy texture
Texture t = new Texture(new Pixmap(width,height, Pixmap.Format.RGB565)); with spritebatch, but don't know why is dummy texture required?

1 Answers

In order to see the shader in action you need to draw something, the code is only specifying that the shader is going to be used but nothing is being drawn with it

batch.begin();
batch.setShader(shader);
batch.draw(new Texture(new Pixmap(width,height, Pixmap.Format.RGB565),0,0);
batch.end();
Related