[SOLVED] Gradient shader getting rotated

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
piotrek75
Prole
Posts: 9
Joined: Tue Oct 18, 2016 7:43 pm

[SOLVED] Gradient shader getting rotated

Post 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
Last edited by piotrek75 on Sun Feb 12, 2017 11:03 am, edited 1 time in total.
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Gradient shader getting rotated

Post 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.
User avatar
piotrek75
Prole
Posts: 9
Joined: Tue Oct 18, 2016 7:43 pm

Re: Gradient shader getting rotated

Post 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;
    }
  ]]
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 5 guests