optimizations to say the least

-cut out complicated light angle calulations and instead put in a arc
stencil
-stenciled the range of each light to optimize the shader drawing
-refactors postshaders to user proper love variables
-minimized amount of canvases
-added better functionality to my canvas util
-refactored blurring to be in one place
This commit is contained in:
Tim Anema 2014-12-19 22:50:42 -05:00
parent 6c87407a6f
commit 7fe549a01a
22 changed files with 126 additions and 283 deletions

View File

@ -31,8 +31,7 @@ local PostShader = require(_PACKAGE..'postshader')
local light_world = class() local light_world = class()
light_world.blurv = love.graphics.newShader(_PACKAGE.."shaders/blurv.glsl") light_world.shadowShader = love.graphics.newShader(_PACKAGE.."/shaders/shadow.glsl")
light_world.blurh = love.graphics.newShader(_PACKAGE.."shaders/blurh.glsl")
light_world.refractionShader = love.graphics.newShader(_PACKAGE.."shaders/refraction.glsl") light_world.refractionShader = love.graphics.newShader(_PACKAGE.."shaders/refraction.glsl")
light_world.reflectionShader = love.graphics.newShader(_PACKAGE.."shaders/reflection.glsl") light_world.reflectionShader = love.graphics.newShader(_PACKAGE.."shaders/reflection.glsl")
@ -67,25 +66,12 @@ function light_world:refreshScreenSize(w, h)
self.w, self.h = w, h self.w, self.h = w, h
self.render_buffer = love.graphics.newCanvas(w, h) self.render_buffer = love.graphics.newCanvas(w, h)
self.normal = love.graphics.newCanvas(w, h) self.shadow_buffer = love.graphics.newCanvas(w, h)
self.normal2 = love.graphics.newCanvas(w, h)
self.normalMap = love.graphics.newCanvas(w, h) self.normalMap = love.graphics.newCanvas(w, h)
self.shadowMap = love.graphics.newCanvas(w, h) self.shadowMap = love.graphics.newCanvas(w, h)
self.glowMap = love.graphics.newCanvas(w, h) self.glowMap = love.graphics.newCanvas(w, h)
self.glowMap2 = love.graphics.newCanvas(w, h)
self.refractionMap = love.graphics.newCanvas(w, h) self.refractionMap = love.graphics.newCanvas(w, h)
self.refractionMap2 = love.graphics.newCanvas(w, h)
self.reflectionMap = love.graphics.newCanvas(w, h) self.reflectionMap = love.graphics.newCanvas(w, h)
self.reflectionMap2 = love.graphics.newCanvas(w, h)
self.blurv:send("screen", {w, h})
self.blurh:send("screen", {w, h})
self.refractionShader:send("screen", {w, h})
self.reflectionShader:send("screen", {w, h})
for i = 1, #self.lights do
self.lights[i]:refresh(w, h)
end
self.post_shader:refreshScreenSize(w, h) self.post_shader:refreshScreenSize(w, h)
end end
@ -111,17 +97,6 @@ function light_world:draw(cb)
self.post_shader:drawWith(self.render_buffer, self.l, self.t, self.s) self.post_shader:drawWith(self.render_buffer, self.l, self.t, self.s)
end end
function light_world:drawBlur(blendmode, blur, canvas, canvas2, l, t, w, h, s)
if blur <= 0 then
return
end
canvas2:clear()
self.blurv:send("steps", blur)
self.blurh:send("steps", blur)
util.drawCanvasToCanvas(canvas, canvas2, {shader = self.blurv, blendmode = blendmode})
util.drawCanvasToCanvas(canvas2, canvas, {shader = self.blurh, blendmode = blendmode})
end
-- draw normal shading -- draw normal shading
function light_world:drawNormalShading(l,t,w,h,s) function light_world:drawNormalShading(l,t,w,h,s)
-- create normal map -- create normal map
@ -134,34 +109,54 @@ function light_world:drawNormalShading(l,t,w,h,s)
end end
end) end)
self.normal2:clear() self.shadow_buffer:clear()
for i = 1, #self.lights do for i = 1, #self.lights do
local light = self.lights[i]
if self.lights[i]:inRange(l,t,w,h,s) then if self.lights[i]:inRange(l,t,w,h,s) then
-- create shadow map for this light -- create shadow map for this light
self.shadowMap:clear() self.shadowMap:clear()
util.drawto(self.shadowMap, l, t, s, function() util.drawto(self.shadowMap, l, t, s, function()
love.graphics.setStencil(function()
love.graphics.circle('fill', light.x, light.y, light.range)
end)
for k = 1, #self.body do for k = 1, #self.body do
if self.body[k]:isInLightRange(self.lights[i]) and self.body[k]:isInRange(-l,-t,w,h,s) then if self.body[k]:isInLightRange(self.lights[i]) and self.body[k]:isInRange(-l,-t,w,h,s) then
self.body[k]:drawShadow(self.lights[i]) self.body[k]:drawShadow(self.lights[i])
end end
end end
if light.angle > 0 then
local angle = math.pi - light.angle / 2.0
love.graphics.setColor(0, 0, 0)
love.graphics.arc("fill", light.x, light.y, light.range, light.direction - angle, light.direction + angle)
end
end) end)
-- draw scene for this light using normals and shadowmap -- draw scene for this light using normals and shadowmap
self.lights[i]:drawNormalShading(l,t,w,h,s, self.normalMap, self.shadowMap, self.normal2) self.shadowShader:send('shadowMap', self.shadowMap)
self.shadowShader:send('lightColor', {light.red / 255.0, light.green / 255.0, light.blue / 255.0})
self.shadowShader:send("lightPosition", {(light.x + l/s) * s, (h/s - (light.y + t/s)) * s, (light.z * 10) / 255.0})
self.shadowShader:send('lightRange',{light.range * s})
self.shadowShader:send("lightSmooth", light.smooth)
self.shadowShader:send("lightGlow", {1.0 - light.glowSize, light.glowStrength})
self.shadowShader:send("invert_normal", self.normalInvert == true)
util.drawCanvasToCanvas(self.normalMap, self.shadow_buffer, {
blendmode = 'additive',
shader = self.shadowShader,
stencil = function()
love.graphics.circle('fill', (light.x + l/s) * s, (light.y + t/s) * s, light.range)
end
})
end end
end end
-- add in ambient color -- add in ambient color
self.normal:clear(255, 255, 255) util.drawto(self.shadow_buffer, l, t, s, function()
util.drawCanvasToCanvas(self.normal2, self.normal, {blendmode = "alpha"})
util.drawto(self.normal, l, t, s, function()
love.graphics.setBlendMode("additive") love.graphics.setBlendMode("additive")
love.graphics.setColor({self.ambient[1], self.ambient[2], self.ambient[3]}) love.graphics.setColor({self.ambient[1], self.ambient[2], self.ambient[3]})
love.graphics.rectangle("fill", -l/s, -t/s, w/s,h/s) love.graphics.rectangle("fill", -l/s, -t/s, w/s,h/s)
end) end)
light_world:drawBlur("alpha", self.shadowBlur, self.normal, self.normal2, l, t, w, h, s) self.post_shader:drawBlur(self.shadow_buffer, {self.shadowBlur})
util.drawCanvasToCanvas(self.normal, self.render_buffer, {blendmode = "multiplicative"}) util.drawCanvasToCanvas(self.shadow_buffer, self.render_buffer, {blendmode = "multiplicative"})
end end
-- draw material -- draw material
@ -198,7 +193,7 @@ function light_world:drawGlow(l,t,w,h,s)
end) end)
if has_glow then if has_glow then
light_world:drawBlur("alpha", self.glowBlur, self.glowMap, self.glowMap2, l, t, w, h, s) self.post_shader:drawBlur(self.glowMap, {self.glowBlur})
util.drawCanvasToCanvas(self.glowMap, self.render_buffer, {blendmode = "additive"}) util.drawCanvasToCanvas(self.glowMap, self.render_buffer, {blendmode = "additive"})
end end
end end
@ -214,8 +209,7 @@ function light_world:drawRefraction(l,t,w,h,s)
end end
end) end)
util.drawCanvasToCanvas(self.render_buffer, self.refractionMap2) self.refractionShader:send("backBuffer", self.render_buffer)
self.refractionShader:send("backBuffer", self.refractionMap2)
self.refractionShader:send("refractionStrength", self.refractionStrength) self.refractionShader:send("refractionStrength", self.refractionStrength)
util.drawCanvasToCanvas(self.refractionMap, self.render_buffer, {shader = self.refractionShader}) util.drawCanvasToCanvas(self.refractionMap, self.render_buffer, {shader = self.refractionShader})
end end
@ -232,8 +226,7 @@ function light_world:drawReflection(l,t,w,h,s)
end end
end) end)
util.drawCanvasToCanvas(self.render_buffer, self.reflectionMap2) self.reflectionShader:send("backBuffer", self.render_buffer)
self.reflectionShader:send("backBuffer", self.reflectionMap2)
self.reflectionShader:send("reflectionStrength", self.reflectionStrength) self.reflectionShader:send("reflectionStrength", self.reflectionStrength)
self.reflectionShader:send("reflectionVisibility", self.reflectionVisibility) self.reflectionShader:send("reflectionVisibility", self.reflectionVisibility)
util.drawCanvasToCanvas(self.reflectionMap, self.render_buffer, {shader = self.reflectionShader}) util.drawCanvasToCanvas(self.reflectionMap, self.render_buffer, {shader = self.reflectionShader})

View File

@ -4,8 +4,6 @@ local util = require(_PACKAGE..'/util')
local light = class() local light = class()
light.shadowShader = love.graphics.newShader(_PACKAGE.."/shaders/shadow.glsl")
function light:init(x, y, r, g, b, range) function light:init(x, y, r, g, b, range)
self.direction = 0 self.direction = 0
self.angle = math.pi * 2.0 self.angle = math.pi * 2.0
@ -20,11 +18,6 @@ function light:init(x, y, r, g, b, range)
self.glowSize = 0.1 self.glowSize = 0.1
self.glowStrength = 0.0 self.glowStrength = 0.0
self.visible = true self.visible = true
self:refresh()
end
function light:refresh(w, h)
self.shadowShader:send('screenResolution', {w or love.window.getWidth(), h or love.window.getHeight()})
end end
-- set position -- set position
@ -116,22 +109,6 @@ function light:inRange(l,t,w,h,s)
return self.visible and (lx + rs) > 0 and (lx - rs) < w/s and (ly + rs) > 0 and (ly - rs) < h/s return self.visible and (lx + rs) > 0 and (lx - rs) < w/s and (ly + rs) > 0 and (ly - rs) < h/s
end end
function light:drawNormalShading(l,t,w,h,s, normalMap, shadowMap, canvas)
self.shadowShader:send('shadowMap', shadowMap)
self.shadowShader:send('lightColor', {self.red / 255.0, self.green / 255.0, self.blue / 255.0})
self.shadowShader:send("lightPosition", {(self.x + l/s) * s, (h/s - (self.y + t/s)) * s, (self.z * 10) / 255.0})
self.shadowShader:send('lightRange',{self.range * s})
self.shadowShader:send("lightSmooth", self.smooth)
self.shadowShader:send("lightGlow", {1.0 - self.glowSize, self.glowStrength})
self.shadowShader:send("lightAngle", math.pi - self.angle / 2.0)
self.shadowShader:send("lightDirection", self.direction)
self.shadowShader:send("invert_normal", self.normalInvert == true)
util.drawCanvasToCanvas(normalMap, canvas, {
blendmode = 'additive',
shader = self.shadowShader
})
end
function light:setVisible(visible) function light:setVisible(visible)
self.visible = visible self.visible = visible
end end

View File

@ -26,10 +26,6 @@ local class = require(_PACKAGE..'/class')
local util = require(_PACKAGE..'/util') local util = require(_PACKAGE..'/util')
local post_shader = class() local post_shader = class()
post_shader.blurv = love.graphics.newShader(_PACKAGE.."/shaders/blurv.glsl")
post_shader.blurh = love.graphics.newShader(_PACKAGE.."/shaders/blurh.glsl")
post_shader.contrast = love.graphics.newShader(_PACKAGE.."/shaders/postshaders/contrast.glsl")
post_shader.tilt_shift = love.graphics.newShader(_PACKAGE.."/shaders/postshaders/tilt_shift.glsl")
local files = love.filesystem.getDirectoryItems(_PACKAGE.."/shaders/postshaders") local files = love.filesystem.getDirectoryItems(_PACKAGE.."/shaders/postshaders")
local shaders = {} local shaders = {}
@ -56,19 +52,8 @@ end
function post_shader:refreshScreenSize(w, h) function post_shader:refreshScreenSize(w, h)
w, h = w or love.window.getWidth(), h or love.window.getHeight() w, h = w or love.window.getWidth(), h or love.window.getHeight()
self.render_buffer = love.graphics.newCanvas(w, h)
self.back_buffer = love.graphics.newCanvas(w, h) self.back_buffer = love.graphics.newCanvas(w, h)
post_shader.blurv:send("screen", {w, h})
post_shader.blurh:send("screen", {w, h})
for shaderName, v in pairs(shaders) do
for def in pairs(v[2]) do
if def == "screen" or def == "textureSize" or def == "inputSize" or def == "outputSize" then
v[1]:send(def, {w, h})
end
end
end
self.w = w self.w = w
self.h = h self.h = h
end end
@ -105,30 +90,29 @@ function post_shader:drawWith(canvas)
end end
function post_shader:drawBloom(canvas, args) function post_shader:drawBloom(canvas, args)
post_shader.blurv:send("steps", args[1] or 2.0) shaders['blurv'][1]:send("steps", args[1] or 2.0)
post_shader.blurh:send("steps", args[1] or 2.0) shaders['blurh'][1]:send("steps", args[1] or 2.0)
util.drawCanvasToCanvas(canvas, self.back_buffer, {shader = post_shader.blurv}) util.drawCanvasToCanvas(canvas, self.back_buffer, {shader = shaders['blurv'][1]})
util.drawCanvasToCanvas(self.back_buffer, self.back_buffer, {shader = post_shader.blurh}) util.process(self.back_buffer, {shader = shaders['blurh'][1]})
util.drawCanvasToCanvas(self.back_buffer, self.back_buffer, {shader = post_shader.contrast}) util.process(self.back_buffer, {shader = shaders['contrast'][1]})
util.drawCanvasToCanvas(canvas, canvas, {shader = post_shader.contrast}) util.process(canvas, {shader = shaders['contrast'][1]})
util.drawCanvasToCanvas(self.back_buffer, canvas, {blendmode = "additive", color = {255, 255, 255, (args[2] or 0.25) * 255}}) util.drawCanvasToCanvas(self.back_buffer, canvas, {blendmode = "additive", color = {255, 255, 255, (args[2] or 0.25) * 255}})
end end
function post_shader:drawBlur(canvas, args) function post_shader:drawBlur(canvas, args)
post_shader.blurv:send("steps", args[1] or 2.0) shaders['blurv'][1]:send("steps", args[1] or 0.0)
post_shader.blurh:send("steps", args[2] or 2.0) shaders['blurh'][1]:send("steps", args[2] or args[1] or 0.0)
util.drawCanvasToCanvas(canvas, self.back_buffer, {shader = post_shader.blurv}) util.process(canvas, {shader = shaders['blurv'][1], blendmode = "alpha"})
util.drawCanvasToCanvas(self.back_buffer, self.back_buffer, {shader = post_shader.blurh}) util.process(canvas, {shader = shaders['blurh'][1], blendmode = "alpha"})
util.drawCanvasToCanvas(self.back_buffer, canvas)
end end
function post_shader:drawTiltShift(canvas, args) function post_shader:drawTiltShift(canvas, args)
post_shader.blurv:send("steps", args[1] or 2.0) shaders['blurv'][1]:send("steps", args[1] or 2.0)
post_shader.blurh:send("steps", args[2] or 2.0) shaders['blurh'][1]:send("steps", args[2] or 2.0)
util.drawCanvasToCanvas(canvas, self.back_buffer, {shader = post_shader.blurv}) util.drawCanvasToCanvas(canvas, self.back_buffer, {shader = shaders['blurv'][1]})
util.drawCanvasToCanvas(self.back_buffer, self.back_buffer, {shader = post_shader.blurh}) util.process(self.back_buffer, {shader = shaders['blurh'][1]})
post_shader.tilt_shift:send("imgBuffer", canvas) shaders['tilt_shift'][1]:send("imgBuffer", canvas)
util.drawCanvasToCanvas(self.back_buffer, canvas, {shader = post_shader.tilt_shift}) util.drawCanvasToCanvas(self.back_buffer, canvas, {shader = shaders['tilt_shift'][1]})
end end
function post_shader:drawShader(shaderName, canvas, args) function post_shader:drawShader(shaderName, canvas, args)
@ -156,7 +140,7 @@ function post_shader:drawShader(shaderName, canvas, args)
current_arg = current_arg + 3 current_arg = current_arg + 3
elseif def == "imgBuffer" then elseif def == "imgBuffer" then
effect[1]:send("imgBuffer", canvas) effect[1]:send("imgBuffer", canvas)
elseif def ~= "screen" and def ~= "textureSize" and def ~= "inputSize" and def ~= "outputSize" then else
local value = args[current_arg] local value = args[current_arg]
if value ~= nil then if value ~= nil then
effect[1]:send(def, value) effect[1]:send(def, value)

View File

@ -1,24 +1,13 @@
extern float exposure = 0.7; extern float exposure = 0.7;
extern float brightness = 1.0; extern float brightness = 1.0;
extern vec3 lumacomponents = vec3(1.0, 1.0, 1.0); extern vec3 lumacomponents = vec3(1.0, 1.0, 1.0);
// luma
//const vec3 lumcoeff = vec3(0.299,0.587,0.114);
const vec3 lumcoeff = vec3(0.212671, 0.715160, 0.072169); const vec3 lumcoeff = vec3(0.212671, 0.715160, 0.072169);
vec4 effect(vec4 vcolor, Image texture, vec2 texcoord, vec2 pixel_coords) vec4 effect(vec4 vcolor, Image texture, vec2 texcoord, vec2 pixel_coords) {
{
vec4 input0 = Texel(texture, texcoord); vec4 input0 = Texel(texture, texcoord);
//exposure knee
input0 *= (exp2(input0)*vec4(exposure)); input0 *= (exp2(input0)*vec4(exposure));
vec4 lumacomponents = vec4(lumcoeff * lumacomponents, 0.0 ); vec4 lumacomponents = vec4(lumcoeff * lumacomponents, 0.0 );
float luminance = dot(input0,lumacomponents); float luminance = dot(input0,lumacomponents);
vec4 luma = vec4(luminance); vec4 luma = vec4(luminance);
return vec4(luma.rgb * brightness, 1.0); return vec4(luma.rgb * brightness, 1.0);
} }

View File

@ -1,8 +1,7 @@
extern vec2 screen = vec2(800.0, 600.0);
extern float steps = 2.0; extern float steps = 2.0;
vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 pixel_coords) { vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 pixel_coords) {
vec2 pSize = vec2(1.0 / screen.x, 1.0 / screen.y); vec2 pSize = vec2(1.0 / love_ScreenSize.x, 1.0 / love_ScreenSize.y);
vec4 col = Texel(texture, texture_coords); vec4 col = Texel(texture, texture_coords);
for(int i = 1; i <= steps; i++) { for(int i = 1; i <= steps; i++) {
col = col + Texel(texture, vec2(texture_coords.x, texture_coords.y - pSize.y * i)); col = col + Texel(texture, vec2(texture_coords.x, texture_coords.y - pSize.y * i));

View File

@ -1,8 +1,7 @@
extern vec2 screen = vec2(800.0, 600.0);
extern float steps = 2.0; extern float steps = 2.0;
vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 pixel_coords) { vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 pixel_coords) {
vec2 pSize = vec2(1.0 / screen.x, 1.0 / screen.y); vec2 pSize = vec2(1.0 / love_ScreenSize.x, 1.0 / love_ScreenSize.y);
vec4 col = Texel(texture, texture_coords); vec4 col = Texel(texture, texture_coords);
for(int i = 1; i <= steps; i++) { for(int i = 1; i <= steps; i++) {
col = col + Texel(texture, vec2(texture_coords.x - pSize.x * i, texture_coords.y)); col = col + Texel(texture, vec2(texture_coords.x - pSize.x * i, texture_coords.y));

View File

@ -1,10 +1,9 @@
extern vec2 screen = vec2(800.0, 600.0);
extern vec2 redStrength = vec2(4.0, 3.0); extern vec2 redStrength = vec2(4.0, 3.0);
extern vec2 greenStrength = vec2(-2.0, -1.0); extern vec2 greenStrength = vec2(-2.0, -1.0);
extern vec2 blueStrength = vec2(1.0, -3.0); extern vec2 blueStrength = vec2(1.0, -3.0);
vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 pixel_coords) { vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 pixel_coords) {
vec2 pSize = vec2(1.0 / screen.x, 1.0 / screen.y); vec2 pSize = vec2(1.0 / love_ScreenSize.x, 1.0 / love_ScreenSize.y);
float colRed = Texel(texture, vec2(texture_coords.x + pSize.x * redStrength.x, texture_coords.y - pSize.y * redStrength.y)).r; float colRed = Texel(texture, vec2(texture_coords.x + pSize.x * redStrength.x, texture_coords.y - pSize.y * redStrength.y)).r;
float colGreen = Texel(texture, vec2(texture_coords.x + pSize.x * greenStrength.x, texture_coords.y - pSize.y * greenStrength.y)).g; float colGreen = Texel(texture, vec2(texture_coords.x + pSize.x * greenStrength.x, texture_coords.y - pSize.y * greenStrength.y)).g;
float colBlue = Texel(texture, vec2(texture_coords.x + pSize.x * blueStrength.x, texture_coords.y - pSize.y * blueStrength.y)).b; float colBlue = Texel(texture, vec2(texture_coords.x + pSize.x * blueStrength.x, texture_coords.y - pSize.y * blueStrength.y)).b;

View File

@ -1,5 +1,4 @@
vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 pixel_coords) vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 pixel_coords) {
{
vec3 col = Texel(texture, texture_coords).rgb * 2.0; vec3 col = Texel(texture, texture_coords).rgb * 2.0;
col *= col; col *= col;
return vec4(col, 1.0); return vec4(col, 1.0);

View File

@ -1,88 +1,18 @@
extern vec2 inputSize;
extern vec2 textureSize;
#define distortion 0.2 #define distortion 0.2
vec2 radialDistortion(vec2 coord) {
/*
#define f 0.6
#define ox 0.5
#define oy 0.5
#define scale 0.8
#define k1 0.7
#define k2 -0.5
vec2 barrelDistort(vec2 coord)
{
vec2 xy = (coord - vec2(ox, oy))/vec2(f) * scale;
vec2 r = vec2(sqrt(dot(xy, xy)));
float r2 = float(r*r);
float r4 = r2*r2;
float coeff = (k1*r2 + k2*r4);
return ((xy+xy*coeff) * f) + vec2(ox, oy);
}
*/
vec2 radialDistortion(vec2 coord, const vec2 ratio)
{
float offsety = 1.0 - ratio.y;
coord.y -= offsety;
coord /= ratio;
vec2 cc = coord - 0.5; vec2 cc = coord - 0.5;
float dist = dot(cc, cc) * distortion; float dist = dot(cc, cc) * distortion;
vec2 result = coord + cc * (1.0 + dist) * dist; return coord + cc * (1.0 + dist) * dist;
result *= ratio;
result.y += offsety;
return result;
} }
/*
vec4 checkTexelBounds(Image texture, vec2 coords, vec2 bounds)
{
vec4 color = Texel(texture, coords) *
vec2 ss = step(coords, vec2(bounds.x, 1.0)) * step(vec2(0.0, bounds.y), coords); vec4 checkTexelBounds(Image texture, vec2 coords) {
vec2 ss = step(coords, vec2(1.0, 1.0)) * step(vec2(0.0, 0.0), coords);
color.rgb *= ss.x * ss.y;
color.a = step(color.a, ss.x * ss.y);
return color;
}*/
vec4 checkTexelBounds(Image texture, vec2 coords, vec2 bounds)
{
vec2 ss = step(coords, vec2(bounds.x, 1.0)) * step(vec2(0.0, bounds.y), coords);
return Texel(texture, coords) * ss.x * ss.y; return Texel(texture, coords) * ss.x * ss.y;
} }
/* vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 pixel_coords) {
vec4 checkTexelBounds(Image texture, vec2 coords) vec2 coords = radialDistortion(texture_coords);
{ vec4 texcolor = checkTexelBounds(texture, coords);
vec2 bounds = vec2(inputSize.x / textureSize.x, 1.0 - inputSize.y / textureSize.y);
vec4 color;
if (coords.x > bounds.x || coords.x < 0.0 || coords.y > 1.0 || coords.y < bounds.y)
color = vec4(0.0, 0.0, 0.0, 1.0);
else
color = Texel(texture, coords);
return color;
}
*/
vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 pixel_coords)
{
vec2 coords = radialDistortion(texture_coords, inputSize / textureSize);
vec4 texcolor = checkTexelBounds(texture, coords, vec2(inputSize.x / textureSize.x, 1.0 - inputSize.y / textureSize.y));
texcolor.a = 1.0; texcolor.a = 1.0;
return texcolor; return texcolor;
} }

View File

@ -6,9 +6,6 @@
modified by slime73 for use with love2d and mari0 modified by slime73 for use with love2d and mari0
*/ */
extern vec2 textureSize;
vec3 grayscale(vec3 color) vec3 grayscale(vec3 color)
{ {
return vec3(dot(color, vec3(0.3, 0.59, 0.11))); return vec3(dot(color, vec3(0.3, 0.59, 0.11)));
@ -18,8 +15,8 @@ vec4 effect(vec4 vcolor, Image texture, vec2 tex, vec2 pixel_coords)
{ {
vec4 texcolor = Texel(texture, tex); vec4 texcolor = Texel(texture, tex);
float x = 0.5 / textureSize.x; float x = 0.5 / love_ScreenSize.x;
float y = 0.5 / textureSize.y; float y = 0.5 / love_ScreenSize.y;
vec2 dg1 = vec2( x, y); vec2 dg1 = vec2( x, y);
vec2 dg2 = vec2(-x, y); vec2 dg2 = vec2(-x, y);
@ -29,7 +26,7 @@ vec4 effect(vec4 vcolor, Image texture, vec2 tex, vec2 pixel_coords)
vec3 c20 = Texel(texture, tex - dg2).xyz; vec3 c20 = Texel(texture, tex - dg2).xyz;
vec3 c22 = Texel(texture, tex + dg1).xyz; vec3 c22 = Texel(texture, tex + dg1).xyz;
vec2 texsize = textureSize; vec2 texsize = love_ScreenSize.xy;
vec3 first = mix(c00, c20, fract(tex.x * texsize.x + 0.5)); vec3 first = mix(c00, c20, fract(tex.x * texsize.x + 0.5));
vec3 second = mix(c02, c22, fract(tex.x * texsize.x + 0.5)); vec3 second = mix(c02, c22, fract(tex.x * texsize.x + 0.5));

View File

@ -1,5 +1,4 @@
vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 pixel_coords) vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 pixel_coords) {
{
vec4 rgb = Texel(texture, texture_coords); vec4 rgb = Texel(texture, texture_coords);
vec4 intens = smoothstep(0.2,0.8,rgb) + normalize(vec4(rgb.xyz, 1.0)); vec4 intens = smoothstep(0.2,0.8,rgb) + normalize(vec4(rgb.xyz, 1.0));

View File

@ -18,8 +18,6 @@
) )
*/ */
extern vec2 textureSize;
// 0.5 = the spot stays inside the original pixel // 0.5 = the spot stays inside the original pixel
// 1.0 = the spot bleeds up to the center of next pixel // 1.0 = the spot bleeds up to the center of next pixel
@ -80,7 +78,7 @@ vec4 B_IN = vec4(1.0) - A_IN;
#ifdef DEBUG #ifdef DEBUG
vec4 grid_color( vec2 coords ) vec4 grid_color( vec2 coords )
{ {
vec2 snes = floor( coords * textureSize ); vec2 snes = floor( coords * love_ScreenSize );
if ( (mod(snes.x, 3.0) == 0.0) && (mod(snes.y, 3.0) == 0.0) ) if ( (mod(snes.x, 3.0) == 0.0) && (mod(snes.y, 3.0) == 0.0) )
return texture2D(_tex0_, coords); return texture2D(_tex0_, coords);
else else
@ -93,16 +91,16 @@ vec4 grid_color( vec2 coords )
#endif // DEBUG #endif // DEBUG
vec2 onex = vec2( 1.0/textureSize.x, 0.0 ); vec2 onex = vec2( 1.0/love_ScreenSize.x, 0.0 );
vec2 oney = vec2( 0.0, 1.0/textureSize.y ); vec2 oney = vec2( 0.0, 1.0/love_ScreenSize.y );
vec4 effect(vec4 vcolor, Image texture, vec2 texCoord, vec2 pixel_coords) vec4 effect(vec4 vcolor, Image texture, vec2 texCoord, vec2 pixel_coords)
{ {
vec2 coords = (texCoord * textureSize); vec2 coords = (texCoord * love_ScreenSize.xy);
vec2 pixel_start = floor(coords); vec2 pixel_start = floor(coords);
coords -= pixel_start; coords -= pixel_start;
vec2 pixel_center = pixel_start + vec2(0.5); vec2 pixel_center = pixel_start + vec2(0.5);
vec2 texture_coords = pixel_center / textureSize; vec2 texture_coords = pixel_center / love_ScreenSize.xy;
vec4 color = vec4(0.0); vec4 color = vec4(0.0);
vec4 pixel; vec4 pixel;

View File

@ -5,9 +5,6 @@
*/ */
// modified by slime73 for use with love pixeleffects // modified by slime73 for use with love pixeleffects
extern vec2 textureSize;
vec3 to_focus(float pixel) vec3 to_focus(float pixel)
{ {
pixel = mod(pixel + 3.0, 3.0); pixel = mod(pixel + 3.0, 3.0);
@ -21,16 +18,16 @@ vec3 to_focus(float pixel)
vec4 effect(vec4 vcolor, Image texture, vec2 texture_coords, vec2 pixel_coords) vec4 effect(vec4 vcolor, Image texture, vec2 texture_coords, vec2 pixel_coords)
{ {
float y = mod(texture_coords.y * textureSize.y, 1.0); float y = mod(texture_coords.y * love_ScreenSize.y, 1.0);
float intensity = exp(-0.2 * y); float intensity = exp(-0.2 * y);
vec2 one_x = vec2(1.0 / (3.0 * textureSize.x), 0.0); vec2 one_x = vec2(1.0 / (3.0 * love_ScreenSize.x), 0.0);
vec3 color = Texel(texture, texture_coords - 0.0 * one_x).rgb; vec3 color = Texel(texture, texture_coords - 0.0 * one_x).rgb;
vec3 color_prev = Texel(texture, texture_coords - 1.0 * one_x).rgb; vec3 color_prev = Texel(texture, texture_coords - 1.0 * one_x).rgb;
vec3 color_prev_prev = Texel(texture, texture_coords - 2.0 * one_x).rgb; vec3 color_prev_prev = Texel(texture, texture_coords - 2.0 * one_x).rgb;
float pixel_x = 3.0 * texture_coords.x * textureSize.x; float pixel_x = 3.0 * texture_coords.x * love_ScreenSize.x;
vec3 focus = to_focus(pixel_x - 0.0); vec3 focus = to_focus(pixel_x - 0.0);
vec3 focus_prev = to_focus(pixel_x - 1.0); vec3 focus_prev = to_focus(pixel_x - 1.0);

View File

@ -1,9 +1,5 @@
#define glarebasesize 0.896 #define glarebasesize 0.896
#define power 0.50 #define power 0.50
extern vec2 textureSize;
extern vec2 outputSize;
extern float time; extern float time;
const vec3 green = vec3(0.17, 0.62, 0.25); const vec3 green = vec3(0.17, 0.62, 0.25);
@ -27,9 +23,9 @@ vec4 effect(vec4 vcolor, Image texture, vec2 texcoord, vec2 pixel_coords)
vec4 sum = vec4(0.0); vec4 sum = vec4(0.0);
vec4 bum = vec4(0.0); vec4 bum = vec4(0.0);
vec2 glaresize = vec2(glarebasesize) / textureSize; vec2 glaresize = vec2(glarebasesize) / love_ScreenSize.xy;
float y_one = 1.0 / outputSize.y; float y_one = 1.0 / love_ScreenSize.y;
int j; int j;
int i; int i;

View File

@ -1,13 +1,10 @@
extern vec2 textureSize;
const float pixel_w = 2.0; const float pixel_w = 2.0;
const float pixel_h = 2.0; const float pixel_h = 2.0;
vec4 effect(vec4 vcolor, Image texture, vec2 uv, vec2 pixel_coords) vec4 effect(vec4 vcolor, Image texture, vec2 uv, vec2 pixel_coords)
{ {
float dx = pixel_w*(1.0/textureSize.x); float dx = pixel_w*(1.0/love_ScreenSize.x);
float dy = pixel_h*(1.0/textureSize.y); float dy = pixel_h*(1.0/love_ScreenSize.y);
vec2 coord = vec2(dx*floor(uv.x/dx), dy*floor(uv.y/dy)); vec2 coord = vec2(dx*floor(uv.x/dx), dy*floor(uv.y/dy));
return Texel(texture, coord); return Texel(texture, coord);
} }

View File

@ -1,5 +1,4 @@
#define nsamples 5 #define nsamples 5
extern number blurstart = 1.0; // 0 to 1 extern number blurstart = 1.0; // 0 to 1
extern number blurwidth = -0.02; // -1 to 1 extern number blurwidth = -0.02; // -1 to 1

View File

@ -1,9 +1,8 @@
extern vec2 screen = vec2(800.0, 600.0);
extern float strength = 2.0; extern float strength = 2.0;
extern float time = 0.0; extern float time = 0.0;
vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 pixel_coords){ vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 pixel_coords){
vec2 pSize = 1.0 / screen; vec2 pSize = 1.0 / love_ScreenSize.xy;
float brightness = 1.0; float brightness = 1.0;
float offsetX = sin(texture_coords.y * 10.0 + time * strength) * pSize.x; float offsetX = sin(texture_coords.y * 10.0 + time * strength) * pSize.x;
float corner = 500.0; float corner = 500.0;

View File

@ -17,12 +17,10 @@ vec4 compress(vec4 in_color, float threshold, float ratio)
return in_color - (diff * (1.0 - 1.0/ratio)); return in_color - (diff * (1.0 - 1.0/ratio));
} }
extern vec2 textureSize;
vec4 effect(vec4 vcolor, Image texture, vec2 tex, vec2 pixel_coords) vec4 effect(vec4 vcolor, Image texture, vec2 tex, vec2 pixel_coords)
{ {
float x = 0.5 * (1.0 / textureSize.x); float x = 0.5 * (1.0 / love_ScreenSize.x);
float y = 0.5 * (1.0 / textureSize.y); float y = 0.5 * (1.0 / love_ScreenSize.y);
vec2 dg1 = vec2( x, y); vec2 dg1 = vec2( x, y);
vec2 dg2 = vec2(-x, y); vec2 dg2 = vec2(-x, y);
@ -39,7 +37,7 @@ vec4 effect(vec4 vcolor, Image texture, vec2 tex, vec2 pixel_coords)
vec3 c21 = Texel(texture, tex + dx).xyz; vec3 c21 = Texel(texture, tex + dx).xyz;
vec3 c22 = Texel(texture, tex + dg1).xyz; vec3 c22 = Texel(texture, tex + dg1).xyz;
vec2 texsize = textureSize; vec2 texsize = love_ScreenSize.xy;
vec3 first = mix(c00, c20, fract(tex.x * texsize.x + 0.5)); vec3 first = mix(c00, c20, fract(tex.x * texsize.x + 0.5));
vec3 second = mix(c02, c22, fract(tex.x * texsize.x + 0.5)); vec3 second = mix(c02, c22, fract(tex.x * texsize.x + 0.5));

View File

@ -1,11 +1,10 @@
extern Image backBuffer; extern Image backBuffer;
extern vec2 screen = vec2(800.0, 600.0);
extern float reflectionStrength; extern float reflectionStrength;
extern float reflectionVisibility; extern float reflectionVisibility;
vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 pixel_coords) { vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 pixel_coords) {
vec2 pSize = vec2(1.0 / screen.x, 1.0 / screen.y); vec2 pSize = vec2(1.0 / love_ScreenSize.x, 1.0 / love_ScreenSize.y);
vec4 normal = Texel(texture, texture_coords); vec4 normal = Texel(texture, texture_coords);
if(normal.a > 0.0 && normal.r > 0.0) { if(normal.a > 0.0 && normal.r > 0.0) {
vec3 pColor = Texel(backBuffer, texture_coords).rgb; vec3 pColor = Texel(backBuffer, texture_coords).rgb;

View File

@ -1,10 +1,9 @@
extern Image backBuffer; extern Image backBuffer;
extern vec2 screen = vec2(800.0, 600.0);
extern float refractionStrength = 1.0; extern float refractionStrength = 1.0;
vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 pixel_coords) { vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 pixel_coords) {
vec2 pSize = vec2(1.0 / screen.x, 1.0 / screen.y); vec2 pSize = vec2(1.0 / love_ScreenSize.x, 1.0 / love_ScreenSize.y);
vec4 normal = Texel(texture, texture_coords); vec4 normal = Texel(texture, texture_coords);
if(normal.b > 0.0) { if(normal.b > 0.0) {
vec4 normalOffset = Texel(texture, vec2(texture_coords.x + (normal.x - 0.5) * pSize.x * refractionStrength, texture_coords.y + (normal.y - 0.5) * pSize.y * refractionStrength)); vec4 normalOffset = Texel(texture, vec2(texture_coords.x + (normal.x - 0.5) * pSize.x * refractionStrength, texture_coords.y + (normal.y - 0.5) * pSize.y * refractionStrength));

View File

@ -4,26 +4,14 @@
*/ */
#define PI 3.1415926535897932384626433832795 #define PI 3.1415926535897932384626433832795
extern vec2 screenResolution; //size of the screen
extern Image shadowMap; //a canvas containing shadow data only extern Image shadowMap; //a canvas containing shadow data only
extern vec3 lightPosition; //the light position on the screen(not global) extern vec3 lightPosition; //the light position on the screen(not global)
extern vec3 lightColor; //the rgb color of the light extern vec3 lightColor; //the rgb color of the light
extern float lightRange; //the range of the light extern float lightRange; //the range of the light
extern float lightSmooth; //smoothing of the lights attenuation extern float lightSmooth; //smoothing of the lights attenuation
extern vec2 lightGlow; //how brightly the light bulb part glows extern vec2 lightGlow; //how brightly the light bulb part glows
extern float lightAngle; //if set, the light becomes directional to a slice lightAngle degrees wide
extern float lightDirection; //which direction to shine the light in if directional in degrees
extern bool invert_normal; //if the light should invert normals extern bool invert_normal; //if the light should invert normals
//calculate if a pixel is within the light slice
bool not_in_slice(vec2 pixel_coords){
float angle = atan(lightPosition.x - pixel_coords.x, pixel_coords.y - lightPosition.y) + PI;
bool pastRightSide = angle < mod(lightDirection + lightAngle, PI * 2);
bool pastLeftSide = angle > mod(lightDirection - lightAngle, PI * 2);
bool lightUp = lightDirection - lightAngle > 0 && lightDirection + lightAngle < PI * 2;
return (lightUp && (pastRightSide && pastLeftSide)) || (!lightUp && (pastRightSide || pastLeftSide));
}
vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 pixel_coords) { vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 pixel_coords) {
vec4 pixelColor = Texel(texture, texture_coords); vec4 pixelColor = Texel(texture, texture_coords);
vec4 shadowColor = Texel(shadowMap, texture_coords); vec4 shadowColor = Texel(shadowMap, texture_coords);
@ -34,11 +22,6 @@ vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 pixel_coords) {
//not in range draw in shadows //not in range draw in shadows
return vec4(0.0, 0.0, 0.0, 1.0); return vec4(0.0, 0.0, 0.0, 1.0);
}else{ }else{
//if the light is a slice and the pixel is not inside
if(lightAngle > 0.0 && not_in_slice(pixel_coords)) {
return vec4(0.0, 0.0, 0.0, 1.0);
}
vec3 normal; vec3 normal;
if(pixelColor.a > 0.0) { if(pixelColor.a > 0.0) {
//if on the normal map ie there is normal map data //if on the normal map ie there is normal map data
@ -70,8 +53,8 @@ vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 pixel_coords) {
return pixel; return pixel;
} else { } else {
//on the normal map, draw normal shadows //on the normal map, draw normal shadows
vec3 dir = vec3((lightPosition.xy - pixel_coords.xy) / screenResolution.xy, lightPosition.z); vec3 dir = vec3((lightPosition.xy - pixel_coords.xy) / love_ScreenSize.xy, lightPosition.z);
dir.x *= screenResolution.x / screenResolution.y; dir.x *= love_ScreenSize.x / love_ScreenSize.y;
vec3 diff = lightColor * max(dot(normalize(normal), normalize(dir)), 0.0); vec3 diff = lightColor * max(dot(normalize(normal), normalize(dir)), 0.0);
//return the light that is effected by the normal and attenuation //return the light that is effected by the normal and attenuation
return vec4(diff * att, 1.0); return vec4(diff * att, 1.0);

View File

@ -1,31 +1,44 @@
local util = {} local util = {}
function util.process(canvas, options)
util.drawCanvasToCanvas(canvas, canvas, options)
end
function util.drawCanvasToCanvas(canvas, other_canvas, options) function util.drawCanvasToCanvas(canvas, other_canvas, options)
options = options or {} options = options or {}
local last_buffer = love.graphics.getCanvas() util.drawto(other_canvas, 0, 0, 1, function()
love.graphics.push()
love.graphics.origin()
love.graphics.setCanvas(other_canvas)
if options["blendmode"] then if options["blendmode"] then
love.graphics.setBlendMode(options["blendmode"]) love.graphics.setBlendMode(options["blendmode"])
end end
if options["shader"] then if options["shader"] then
love.graphics.setShader(options["shader"]) love.graphics.setShader(options["shader"])
end end
if options["stencil"] then
love.graphics.setStencil(options["stencil"])
end
if options["istencil"] then
love.graphics.setInvertedStencil(options["istencil"])
end
if options["color"] then if options["color"] then
love.graphics.setColor(unpack(options["color"])) love.graphics.setColor(unpack(options["color"]))
end else
love.graphics.setColor(255,255,255) love.graphics.setColor(255,255,255)
end
love.graphics.draw(canvas,0,0) love.graphics.draw(canvas,0,0)
love.graphics.setCanvas(last_buffer)
if options["blendmode"] then if options["blendmode"] then
love.graphics.setBlendMode("alpha") love.graphics.setBlendMode("alpha")
end end
if options["shader"] then if options["shader"] then
love.graphics.setShader() love.graphics.setShader()
end end
love.graphics.pop() if options["stencil"] then
love.graphics.setStencil()
end
if options["istencil"] then
love.graphics.setInvertedStencil()
end
end)
end end
function util.drawto(canvas, x, y, scale, cb) function util.drawto(canvas, x, y, scale, cb)