I have a hard time understanding how to use canvases and shaders together. In my example, I have this code for a class :
Code: Select all
local Class = require 'libs.hump.class'
local Entity = require 'entities.Entity'
local DeathLine = Class{
__includes = Entity -- DeathLine class inherits our Entity class
}
function DeathLine:init(world, x, y, w, h)
Entity.init(self, x, y, w, h)
self.world = world
self.time = 0
self.body = love.physics.newBody(self.world, x+w/2, y+h/2, 'kinematic')
self.shape = love.physics.newRectangleShape(w, h)
self.fixture = love.physics.newFixture(self.body, self.shape)
self.fixture:setCategory(8)
self.fixture:setSensor(true)
plasLine = love.graphics.newShader[[
uniform vec2 iResolution;
uniform float iTime;
float random( vec2 p ) {
vec2 K1 = vec2(
23.14069263277926, // e^pi (Gelfond's constant)
2.665144142690225 // 2^sqrt(2) (Gelfond–Schneider constant)
);
return fract( cos( dot(p,K1) ) * 12345.6789 );
}
vec4 effect( vec4 color, Image texture, vec2 texture_coords, vec2 screen_coords ){
vec2 uv = screen_coords.xy / iResolution.xy;
vec2 p = uv*2.0 - 1.0;
p *= 15.0;
vec2 limit = vec2(1., 7.);
vec2 sfunc = vec2(p.x, p.y + 2.0*sin(uv.x*10.0-iTime*10 + cos(iTime*random(limit)) )+2.0*cos(uv.x*25.+iTime*random(limit)));
sfunc.y *= uv.x*2.0+0.05;
sfunc.y *= 2.0 - uv.x*2.0+0.05;
sfunc.y /= 0.1; // Thickness fix
vec3 c = vec3(abs(sfunc.y));
c = pow(c, vec3(-0.5));
c *= vec3(1,0.,0.);
return vec4(c,1.0);
}
]]
plasLine:send('iResolution', {love.graphics.getWidth(), love.graphics.getHeight()})
--plasLine:send('iTime', self.time)
self.can = love.graphics.newCanvas(love.graphics.getWidth(), 100)
love.graphics.setCanvas(self.can)
love.graphics.clear()
love.graphics.setBlendMode("alpha")
love.graphics.setShader(plasLine)
love.graphics.rectangle('line', 0, 0, love.graphics.getWidth(), love.graphics.getHeight())
love.graphics.setShader()
love.graphics.setCanvas()
end
function DeathLine:update(dt)
self.time = self.time+dt
plasLine:send('iTime', os.clock())
end
function DeathLine:draw(dt)
local x, y = self.body:getWorldPoints(self.shape:getPoints())
love.graphics.draw(self.can, x, y)
end
return DeathLine
the only important thing in the picture is the red line, it's my not working shader.
The shader in itself works, because if I comment the line "love.graphics.setShader()", it works fine and it's animated as intended :
The problem here is it take the whole screen and not just the size of my canvas.
How can I make this work as intended ?
Thank you