[SOLVED] Problem with shaders (especially with texture_coords)

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
LuaIsLife
Prole
Posts: 12
Joined: Fri Nov 02, 2018 4:48 pm

[SOLVED] Problem with shaders (especially with texture_coords)

Post 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!!
Attachments
main.lua
(559 Bytes) Downloaded 180 times
Last edited by LuaIsLife on Sat Nov 03, 2018 9:40 am, edited 1 time in total.
grump
Party member
Posts: 947
Joined: Sat Jul 22, 2017 7:43 pm

Re: Problem with shaders (especially with texture_coords)

Post 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.
User avatar
pgimeno
Party member
Posts: 3673
Joined: Sun Oct 18, 2015 2:58 pm

Re: Problem with shaders (especially with texture_coords)

Post 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.
User avatar
zorg
Party member
Posts: 3468
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Problem with shaders (especially with texture_coords)

Post 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.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
veethree
Inner party member
Posts: 877
Joined: Sat Dec 10, 2011 7:18 pm

Re: Problem with shaders (especially with texture_coords)

Post 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.
LuaIsLife
Prole
Posts: 12
Joined: Fri Nov 02, 2018 4:48 pm

Re: Problem with shaders (especially with texture_coords)

Post 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)
User avatar
pgimeno
Party member
Posts: 3673
Joined: Sun Oct 18, 2015 2:58 pm

Re: Problem with shaders (especially with texture_coords)

Post 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)
LuaIsLife
Prole
Posts: 12
Joined: Fri Nov 02, 2018 4:48 pm

Re: Problem with shaders (especially with texture_coords)

Post 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.
Last edited by LuaIsLife on Sun Nov 04, 2018 5:55 am, edited 1 time in total.
grump
Party member
Posts: 947
Joined: Sat Jul 22, 2017 7:43 pm

Re: Problem with shaders (especially with texture_coords)

Post 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;
User avatar
pgimeno
Party member
Posts: 3673
Joined: Sun Oct 18, 2015 2:58 pm

Re: Problem with shaders (especially with texture_coords)

Post 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;
?
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 4 guests