5

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?

  • When you instantiate a ShaderProgram, check `shaderProgram.isCompiled()`. If it isn't compiled, log `ShaderProgram.getLog()` to see where the errors are in your shader. If a shader fails to compile, LibGDX won't complain when you try to use it. It will simply render black. – Tenfour04 Jul 22 '20 at 15:31
  • Are you remembering to set your uniforms on the Java side? – Tenfour04 Jul 22 '20 at 15:32
  • @Tenfour04 Not getting any log – Shubham Agrawal Jul 22 '20 at 15:39

1 Answers1

0

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();
Caolem
  • 124
  • 1
  • 4
  • Actually, my query is, Can we draw texture with shader instead of passing it one? I know it's a separate question? But why to pass a texture to shader if I am not using that.. – Shubham Agrawal Jul 25 '20 at 21:48
  • Imagine that the shader is paint and the texture is a canvas, you cannot paint in the air but on the canvas, it does not matter if the canvas has an image or is empty – Caolem Sep 09 '20 at 18:04