[SOLVED] Shader + Warps UVs with normal map

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.
Post Reply
User avatar
var77
Citizen
Posts: 52
Joined: Wed May 02, 2018 1:57 pm

[SOLVED] Shader + Warps UVs with normal map

Post by var77 »

Hi every one.

Well i was searching on the forum for something very interesting that i found viewtopic.php?f=4&t=3733&start=150 but unfortunatly looks a bit old for the actual version of love2d 11.1.

So after few correction to get it fit with the actual one it seems that i still got an issue with the shader:

Code: Select all

  function love.load()
    --love.keyboard.setKeyRepeat(.1, .05)
    fac = 1
    canvas = love.graphics.newCanvas()
    tex = love.graphics.newImage('bg.png')
    map = love.graphics.newImage('map.png')
    shader = love.graphics.newShader[[
        extern Image map;
        extern number fac = 1.0;
        extern number mousex = 1.0;
        extern number mousey = 1.0;
        uniform sampler2D tex0;
        vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 pixel_coords)
        {
            vec2 uv2         = vec2(
                    texture_coords.x,
                    1 - texture_coords.y
            );
            vec4 mapcol = Texel(map, uv2);
            vec2 uv         = vec2(
                    texture_coords.x - (.5 - mapcol.r)/(2/fac)*ceil(mapcol.g), // - mapcol.b/(16/fac),
                    texture_coords.y - (.5 - mapcol.g)/(2/fac)*ceil(mapcol.g)  // - mapcol.b/(16/fac)
            );

            vec3 col = vec3(
                    texture2D(tex0,uv).x,
                    texture2D(tex0,uv).y,
                    texture2D(tex0,uv).z
            );
            col += (1 - clamp(sqrt(pow(texture_coords.x*4 - mousex*4 - .5, 2) + pow(texture_coords.y*4 - mousey*4 + .5, 2)), 0, 1) - .2)*ceil(mapcol.g)*fac;
         return vec4(col, 1);
        }
    ]]

end

function love.update(dt)
    --exit
    if love.keyboard.isDown('escape') then love.event.quit() end
    shader:send('map', canvas)
    shader:send('fac', fac)
    shader:send('mousex', love.mouse.getX()/800)
    shader:send('mousey', love.mouse.getY()/600)
end

function love.draw()
    love.graphics.setCanvas(canvas)
    love.graphics.setColor(0, 0, 0)
    love.graphics.rectangle('fill', 0, 0, 800, 600)
    love.graphics.setColor(1, 1, 1)
    love.graphics.draw(map, love.mouse.getX(), love.mouse.getY(), 0, 1, 1, 128, 128)
    love.graphics.setCanvas()

    love.graphics.newShader(shader)
    love.graphics.draw(tex, 0, 0)
    love.graphics.newShader()
    love.graphics.print(fac, 0, 0)
end
It's actually return me this :

Code: Select all

Error

main.lua:54: bad argument #1 to 'newShader' (string expected, got userdata)


Traceback

[C]: in function 'newShader'
main.lua:54: in function 'draw'
[C]: in function 'xpcall'
I'm not good at shader so hope someone could explain me why it's not returning the expected string value ?

Thanks in advance ^^
Last edited by var77 on Wed May 16, 2018 3:42 pm, edited 1 time in total.
User avatar
zorg
Party member
Posts: 3441
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Shader + Warps UVs with normal map

Post by zorg »

Simple, you wrote love.graphics.newShader in love.draw instead of love.graphics.setShader (twice, might i add); you already created the shader in love.load, you want to make it active. Small mistake.
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
var77
Citizen
Posts: 52
Joined: Wed May 02, 2018 1:57 pm

Re: Shader + Warps UVs with normal map

Post by var77 »

Yes thanks zorg for the miss, i should watch it more carefully next time.
Unfortunatly i still got a mistake with the final result that not seems to be like it should be expected.

Expected:
edf.png
edf.png (528.67 KiB) Viewed 2949 times
Actual result:
result.png
result.png (10.99 KiB) Viewed 2949 times
Any ideas ?
User avatar
zorg
Party member
Posts: 3441
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Shader + Warps UVs with normal map

Post by zorg »

No idea from me, sorry;it does look like the shader itself works, just the image is what's not showing up?
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
pgimeno
Party member
Posts: 3548
Joined: Sun Oct 18, 2015 2:58 pm

Re: Shader + Warps UVs with normal map

Post by pgimeno »

Maybe include a self-contained .love file?

Edit: Never mind. I'm still scratching my head at why it worked at all in 0.8 through 0.10. It turns out that it depends on the tex0 extern to be set to the source texture. Adding this makes it work: shader:send('tex0', tex)

Also, this part is only required for 0.8.0:

Code: Select all

1 - texture_coords.y
In 0.9+ it should be:

Code: Select all

texture_coords.y
which of course enables a lot of simplifications (you can get rid of uv2 entirely).
User avatar
var77
Citizen
Posts: 52
Joined: Wed May 02, 2018 1:57 pm

Re: Shader + Warps UVs with normal map

Post by var77 »

Thanks pal for your answers.
As last time pgimeno you saved my day.

Here is the updated version for love 2d 11.1 of the post from retrotails:
main.zip
(621.87 KiB) Downloaded 178 times
Issue solved !
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 156 guests