Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
local framebuffer1 = love.graphics.newCanvas();
local framebuffer2 = love.graphics.newCanvas();
local framebuffer3 = love.graphics.newCanvas();
local pos = {x = 20,y = 20}
local drawLines = true
local effect = fishEye
local barrel
local blur
local fishEye
function love.load()
barrel = love.graphics.newPixelEffect [[
vec2 distort(vec2 p)
{
const float barrelPower = 1.05;
float theta = atan(p.y,p.x);
float radius = length(p);
radius = pow(radius,barrelPower);
p.x = radius * cos(theta);
p.y = radius * sin(theta);
return 0.5 * (p + 1.0);
}
vec2 distort2(vec2 p)
{
const float ax = 0.01;
const float ay = 0.01;
float len = length(p);
float len2 = len*len;
float xx = len/(1.0 - ax * len2);
float yy = len/(1.0 - ay * len2);
return ( p / vec2(1.0-ax*xx*xx,1.0-ay*yy*yy)+1) * 0.5;
}
vec4 effect(vec4 color, Image tex,vec2 tc,vec2 pc)
{
mat3 forwardTransform = mat3( 2.0,0.0,-1.0,
0.0,2.0,-1.0,
0.0,0.0, 0.0);
mat3 backwardTransform = mat3( 0.5,0.0,1.0,
0.0,0.5,1.0,
0.0,0.0,0.0);
vec2 coord = (vec3(tc,1.0) * forwardTransform).xy;
coord = distort(coord);
//coord = (backwardTransform * vec3(coord,1.0)).xy;
return texture2D(tex,coord);
//return texture2D(texture,clamp(distort(tc),0.0,1.0));
}
]]
blur = love.graphics.newPixelEffect [[
extern float blurSize;
extern float fade;
vec4 effect(vec4 color,Image tex,vec2 tc,vec2 pc)
{
vec4 sum = vec4(0.0);
sum += (texture2D(tex,vec2(tc.x - 4.0*blurSize,tc.y)) * 0.05 ) - fade;
sum += (texture2D(tex,vec2(tc.x - 3.0*blurSize,tc.y)) * 0.09 ) - fade;
sum += (texture2D(tex,vec2(tc.x - 2.0*blurSize,tc.y)) * 0.12 ) - fade;
sum += (texture2D(tex,vec2(tc.x - 1.0*blurSize,tc.y)) * 0.15 ) - fade;
sum += (texture2D(tex,vec2(tc.x,tc.y)) * 0.181 ) - fade;
sum += (texture2D(tex,vec2(tc.x + 4.0*blurSize,tc.y)) * 0.05 ) - fade;
sum += (texture2D(tex,vec2(tc.x + 3.0*blurSize,tc.y)) * 0.09 ) - fade;
sum += (texture2D(tex,vec2(tc.x + 2.0*blurSize,tc.y)) * 0.12 ) - fade;
sum += (texture2D(tex,vec2(tc.x + 1.0*blurSize,tc.y)) * 0.15 ) - fade;
sum.a = 1.0;
return sum * color;// + vec4(0.1,0.1,0.1,0.1);
}
]]
fishEye = love.graphics.newPixelEffect [[
float aperture = 180.0;
const float PI = 3.1415926535;
vec4 effect(vec4 color,Image texture,vec2 tc,vec2 sc)
{
//Pass throught what ever the current texel is
//return texture2D(texture,tc);
float apertureHalf = 0.5 * aperture * (PI/180);
float maxFactor = sin(apertureHalf);
vec2 pos = 2.0 * tc.st - 1.0;
float len = length(pos);
if(len > 1.0) {
return vec4(0.0,0.4,0.0,1.0);
} else {
float x = maxFactor * pos.x;
float y = maxFactor * pos.y;
float n = length(vec2(x,y));
float z = sqrt(1.0 - n * n);
float r = atan(n,z) / PI;
float phi = atan(y,x);
float u = r * cos(phi) + 0.5;
float v = r * sin(phi) + 0.5;
return texture2D(texture,vec2(u,v));
}
}
]]
effect = barrel
end
function love.draw()
love.graphics.setPixelEffect()
love.graphics.setCanvas(framebuffer1)
love.graphics.draw(framebuffer2)
----------------- Draw Stuff --------------------
love.graphics.push();
love.graphics.translate(pos.x,pos.y)
love.graphics.setColor(0,255,0)
--Draw a triangle
love.graphics.line(0,0,10,0)
love.graphics.line(10,0,5,20)
love.graphics.line(5,20,0,0)
love.graphics.pop();
love.graphics.print("Hello world",100,200)
----------------- Draw some lines ---------------
if drawLines then
for var=0,600,50 do
love.graphics.push()
love.graphics.translate(0,var)
love.graphics.line(0,0,800,0);
love.graphics.pop()
end
for var=0,800,50 do
love.graphics.push()
love.graphics.translate(var,0)
love.graphics.line(0,0,0,600);
love.graphics.pop()
end
end
-------------------------------------------------
love.graphics.setCanvas(framebuffer2)
love.graphics.setPixelEffect(blur)
blur:send('fade',0.01);
blur:send('blurSize',1/4096);
love.graphics.draw(framebuffer1)
love.graphics.setCanvas()
love.graphics.setPixelEffect(blur)
blur:send('fade',0.0);
blur:send('blurSize',1/4096);
love.graphics.draw(framebuffer2)
------- Third Rendering Pass for Ctr Effect------
love.graphics.setCanvas(framebuffer3)
love.graphics.setPixelEffect(effect)
love.graphics.draw(framebuffer2)
-------------------------------------------------
-- FINALLY draw to the real screen
love.graphics.setCanvas()
love.graphics.draw(framebuffer3)
end
function love.update(dt)
pos["x"] = pos["x"]+(dt*10)
pos["y"] = pos["y"]+(dt*10)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment