Page 1 of 1

[SOLVED] Gradient shader getting rotated

Posted: Sat Feb 11, 2017 6:32 pm
by piotrek75
Hello,
I build this shader for shadow creation purposes. I would like to get it drawn from top to bottom of the texture.
But during the love.graphics.draw() the strite is getting rotated and so the shader result :/

Could you help me to resolve this problem, please?
I would like with any rotated love.graphics.draw() call, a shadow being driven from top to bottom independent from any angle.

Code: Select all

function love.load()
  myShader = love.graphics.newShader[[   
    vec4 effect( vec4 color, Image texture, vec2 texture_coords, vec2 screen_coords ){
      vec4 pixel = Texel(texture, texture_coords);//This is the current pixel color
      pixel.r = pixel.r * texture_coords.y;
      pixel.g = pixel.g * texture_coords.y;
      pixel.b = pixel.b * texture_coords.y;
      return pixel;
    }
  ]]
  
  img = love.graphics.newImage("asteroid_1.png")
  angle = 0
end

function love.update(dt)
  angle = angle + 0.1 * dt * 5
end

function love.draw()
  local imgW, imgH = img:getDimensions()
  love.graphics.setShader(myShader) --draw something here
  love.graphics.draw(img, love.graphics.getWidth()/2, love.graphics.getHeight()/2, angle, 1, 1, imgW/2, imgH/2)
  love.graphics.setShader()
end

Re: Gradient shader getting rotated

Posted: Sat Feb 11, 2017 6:39 pm
by raidho36
Well yeah you use texture coordinate Y and you sample by texture coordinate Y (and X), so of course each part of the sprite will be shaded the same way regardless of orientation, position, scaling, etc. You need to compensate against the graphical transformation inside the shader to get it to work this way. But you can probably get away with using screen coordinates, which are not transformed. Keep in mind though that screen coordinates are in pixels instead of percent.

Re: Gradient shader getting rotated

Posted: Sun Feb 12, 2017 11:03 am
by piotrek75
Brillant ! Thank you for your help!

Here's the working shader updated

Code: Select all

myShader = love.graphics.newShader[[
    number y;
    vec4 effect( vec4 color, Image texture, vec2 texture_coords, vec2 screen_coords ){
      vec4 pixel = Texel(texture, texture_coords);//This is the current pixel color
      y = screen_coords.y / love_ScreenSize.y;
      pixel.r = pixel.r * y;
      pixel.g = pixel.g * y;
      pixel.b = pixel.b * y;
      return pixel;
    }
  ]]