strange shader issue [resolved]

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
Schwender.exe
Prole
Posts: 25
Joined: Mon Oct 16, 2017 6:07 pm
Location: the moon
Contact:

strange shader issue [resolved]

Post by Schwender.exe »

so I'm tinkering around with shaders and I finally got something nice looking, however, there's a really strange issue now
Image
for some strange reason, the player moves essentially opposite of the tiles
here's my shader code:

Code: Select all

local underwater_pixelcode = [[
  extern vec2 player;
  
  float length(float x, float y){ return sqrt(x*x+y*y); }
  
  vec4 effect(vec4 color, Image tex, vec2 texture_coords, vec2 screen_coords){
    vec4 pixel = Texel(tex, texture_coords);
    
    //wip code, just testing some things out, currently doesn't work
    if(length(screen_coords.x-player.x,screen_coords.y-player.y)==32){
      return vec4(0.5,0.5,0.5, 1.0) * pixel;
    }else{
      return vec4(1.0,1.0,1.0, 1.0) * pixel;
    }
  }
]]
local underwater_vertexcode = [[
  extern float time;
  vec4 position(mat4 transform_projection,vec4 vertex_position){
    vertex_position.y += sin(vertex_position.x + time*2)*4;
    return transform_projection * vertex_position;
  }
]]
and my love.draw() function:

Code: Select all

function love.draw()
  cam:draw(function(l,t,w,h)
    if not editor.on then love.graphics.setShader(UWShader) end
    UWShader:send("time",time)
    UWShader:send("player",{player.x,player.y})
    for layer=1,4 do
      if layer == 2 then player:draw() end
      drawTiles(layer)
    end
    love.graphics.setShader()
    editor:draw()
  end)
end
I'm using gamera for the camera and a level editor I made myself, hence the editor.on, I also draw the tiles based on layers (the stones are on layer -1 and the floor on layer 0)
Last edited by Schwender.exe on Sat Jun 09, 2018 1:19 am, edited 1 time in total.
User avatar
pgimeno
Party member
Posts: 3641
Joined: Sun Oct 18, 2015 2:58 pm

Re: strange shader issue

Post by pgimeno »

It's difficult to say with so little data and nothing that we can play with, but this bit looks suspicious:

Code: Select all

sin(vertex_position.x + time*2)
Unless you have a transform, vertex_position.x is going to increase in very large steps. I think it seems like it works, just because you're picking up a harmonic just by chance.

Try something like sin(vertex_position.x/100 + time*2), see if that helps.

Ultimately, it should depend on the screen resolution and your tile width.
User avatar
Schwender.exe
Prole
Posts: 25
Joined: Mon Oct 16, 2017 6:07 pm
Location: the moon
Contact:

Re: strange shader issue

Post by Schwender.exe »

pgimeno wrote: Fri Jun 08, 2018 11:40 pm It's difficult to say with so little data and nothing that we can play with, but this bit looks suspicious:

Code: Select all

sin(vertex_position.x + time*2)
Unless you have a transform, vertex_position.x is going to increase in very large steps. I think it seems like it works, just because you're picking up a harmonic just by chance.

Try something like sin(vertex_position.x/100 + time*2), see if that helps.

Ultimately, it should depend on the screen resolution and your tile width.
ah that worked! thanks! I'm still getting the ropes of shaders so I don't know 100% what I'm doing, but that seemed to fix it
User avatar
pgimeno
Party member
Posts: 3641
Joined: Sun Oct 18, 2015 2:58 pm

Re: strange shader issue

Post by pgimeno »

Schwender.exe wrote: Sat Jun 09, 2018 1:20 am ah that worked! thanks! I'm still getting the ropes of shaders so I don't know 100% what I'm doing, but that seemed to fix it
Glad that was it. The problem was that you were calculating the sine with an increment of 1 radian per pixel, that's about 1/3 turn per pixel, so you had a whole sine cycle every 3 pixels. You were lucky that it worked for your tiles, because resampling the sine at another frequency gives another sine. But if your player was not at the exact same position as the corner of a tile, chances were it looked desynced, and it did.

[Edit: Sorry, I got that wrong of course. 1 radian is about 1/6 turn per pixel, so a sine cycle happened every 6 pixels more or less (~6.2832 pixels).]

One bit of advice: keep the numbers low, e.g. if your time multiplier is 2 as it is now, don't let the time exceed math.pi*4. Otherwise, you will lose precision over time and the changes will look stepped. That's especially important when the numbers in the shader are single precision.
Post Reply

Who is online

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