Page 1 of 2

[SOLVED] Problem with shaders (especially with texture_coords)

Posted: Fri Nov 02, 2018 4:56 pm
by LuaIsLife
Hello,

I'm playing around with shaders... the most basic stuff.

I followed
http://blogs.love2d.org/content/beginners-guide-shaders

but the part where the rectangle should be half blue/red (using texture_coords) never worked out..
(my attached example is with a circle, but it doesn't matter, rectangle didn't work out too).

Code: Select all

  function love.load()
      
    local shader_code = [[
    vec4 effect( vec4 color, Image texture, vec2 texture_coords, vec2 screen_coords ){
    if(texture_coords.x > 0.5) {
      return vec4(1.0,0.0,0.0,1.0);//red
    }
    else
    {
      return vec4(0.0,0.0,1.0,1.0);//blue
    }
    }
    ]]

    shader = love.graphics.newShader(shader_code)
  end
  
  function love.draw()
    love.graphics.setShader(shader)
    love.graphics.setColor(0,0.3,0)
    love.graphics.circle("fill", 500, 10, 200)
    love.graphics.setShader()
  end
If I execute my code, the circle is blue or - if I change "> 0.5" to "< 0.5" - red, but never I see a cleanly divided cirlce,
one half red the other blue.

But the part in the tutorial with the screen_coords worked fine.. it gets another color in the defined screen_coords...

Any idea why? Thanks!!

Re: Problem with shaders (especially with texture_coords)

Posted: Fri Nov 02, 2018 7:08 pm
by grump
That tutorial's code says "draw something here", when it actually should say "draw something with a texture here". Circles, lines, rectangles etc. can't have shaders applied to them. Try to draw an image instead.

Re: Problem with shaders (especially with texture_coords)

Posted: Fri Nov 02, 2018 8:27 pm
by pgimeno
grump wrote: Fri Nov 02, 2018 7:08 pm Circles, lines, rectangles etc. can't have shaders applied to them.
Well, they can have shaders applied, it's just that the texture_coords parameter won't work on them.

Re: Problem with shaders (especially with texture_coords)

Posted: Sat Nov 03, 2018 6:55 am
by zorg
pgimeno wrote: Fri Nov 02, 2018 8:27 pm
grump wrote: Fri Nov 02, 2018 7:08 pm Circles, lines, rectangles etc. can't have shaders applied to them.
Well, they can have shaders applied, it's just that the texture_coords parameter won't work on them.
For the reason that they aren't textured, so there's nothing to color.

Re: Problem with shaders (especially with texture_coords)

Posted: Sat Nov 03, 2018 7:31 am
by veethree
To apply shaders to circles, lines, rectangles etc. you can to draw them to a canvas, then apply the shader to the canvas.

Re: Problem with shaders (especially with texture_coords)

Posted: Sat Nov 03, 2018 9:40 am
by LuaIsLife
Thanks!!

I guess that's the reason my other shader (black&white) doesn't work too (at least not for the parts of my program where I just draw circles and stuff) or at least not as expected (every colored circle/rectangle and such is just converted to plain white)

Re: Problem with shaders (especially with texture_coords)

Posted: Sat Nov 03, 2018 10:28 am
by pgimeno
zorg wrote: Sat Nov 03, 2018 6:55 am For the reason that they aren't textured, so there's nothing to color.
ISTR the texture was a dummy 1x1px white texture, with all UVs set to (0,0). From that, it's clear that texture_coords will always be (0,0) as well. So you can't obtain any useful texture coordinates, but you certainly can colour it. In fact, the code in the OP does exactly that (just not multicoloured as intended)

Re: Problem with shaders (especially with texture_coords)

Posted: Sun Nov 04, 2018 5:33 am
by LuaIsLife
pgimeno wrote: Sat Nov 03, 2018 10:28 am
zorg wrote: Sat Nov 03, 2018 6:55 am For the reason that they aren't textured, so there's nothing to color.
ISTR the texture was a dummy 1x1px white texture, with all UVs set to (0,0). From that, it's clear that texture_coords will always be (0,0) as well. So you can't obtain any useful texture coordinates, but you certainly can colour it. In fact, the code in the OP does exactly that (just not multicoloured as intended)
Well, the shader code for the b/w conversion looks like this:

Code: Select all

vec4 effect(vec4 color, Image image, vec2 uvs, vec2 screen_coords) {


  // This one converts to (roughly) black and white (simle desaturation to be more precise) but keeps the alpha (important for the "sprites")
  vec4 pixel = Texel(image, uvs);
  float av = (pixel.r + pixel.g + pixel.b) / 3.0;
  return vec4(av, av, av, pixel[3]);
}
I have some lines/cirles drawn to the screen which are changing their sizes over time.. so the solution I've seen where these
basic shapes are drawn only one time during love.load to a canvas where a shader could be applied with texture_coords and stuff
doesn't work for me here..

It's not that important, just tinkering around.. but I would like to understand what I could do in such a case when I'm "mixing" normal images
and the texture-less basic shapes but want to apply a shader to the whole scene without having to worry about some elements not behaving
like the rest shader-wise...

EDIT: Okay.. so I guess I have them (the "basic shapes") to be drawn to a seperate canvas... which kind of works, at least they are on the screen like before now (after some fiddling around), but my b/w shader still has the same problem.... maybe the shader has to be applied for the canvas in a seperate call.. I'll look into that.

Re: Problem with shaders (especially with texture_coords)

Posted: Sun Nov 04, 2018 5:51 am
by grump
LuaIsLife wrote: Sun Nov 04, 2018 5:33 am

Code: Select all

  float av = (pixel.r + pixel.g + pixel.b) / 3.0;
Just a quick heads-up: you'll get better results for this conversion if you take spectral weighting into account:

Code: Select all

float av = pixel.r * .2125 + pixel.g * .7154 + pixel.b * .0721;

Re: Problem with shaders (especially with texture_coords)

Posted: Sun Nov 04, 2018 11:21 am
by pgimeno
LuaIsLife wrote: Sun Nov 04, 2018 5:33 am

Code: Select all

  vec4 pixel = Texel(image, uvs);
This will return always white. Maybe you meant

Code: Select all

  vec4 pixel = Texel(image, uvs) * color;
?