diff --git a/lib/init.lua b/lib/init.lua index 384072a..c20de2e 100644 --- a/lib/init.lua +++ b/lib/init.lua @@ -31,8 +31,7 @@ local PostShader = require(_PACKAGE..'postshader') local light_world = class() -light_world.blurv = love.graphics.newShader(_PACKAGE.."shaders/blurv.glsl") -light_world.blurh = love.graphics.newShader(_PACKAGE.."shaders/blurh.glsl") +light_world.shadowShader = love.graphics.newShader(_PACKAGE.."/shaders/shadow.glsl") light_world.refractionShader = love.graphics.newShader(_PACKAGE.."shaders/refraction.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.render_buffer = love.graphics.newCanvas(w, h) - self.normal = love.graphics.newCanvas(w, h) - self.normal2 = love.graphics.newCanvas(w, h) + self.shadow_buffer = love.graphics.newCanvas(w, h) self.normalMap = love.graphics.newCanvas(w, h) self.shadowMap = 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.refractionMap2 = 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) end @@ -110,17 +96,6 @@ function light_world:draw(cb) end) self.post_shader:drawWith(self.render_buffer, self.l, self.t, self.s) 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 function light_world:drawNormalShading(l,t,w,h,s) @@ -134,34 +109,54 @@ function light_world:drawNormalShading(l,t,w,h,s) end end) - self.normal2:clear() + self.shadow_buffer:clear() for i = 1, #self.lights do + local light = self.lights[i] if self.lights[i]:inRange(l,t,w,h,s) then -- create shadow map for this light self.shadowMap:clear() 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 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]) 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) -- 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 -- add in ambient color - self.normal:clear(255, 255, 255) - util.drawCanvasToCanvas(self.normal2, self.normal, {blendmode = "alpha"}) - util.drawto(self.normal, l, t, s, function() + util.drawto(self.shadow_buffer, l, t, s, function() love.graphics.setBlendMode("additive") love.graphics.setColor({self.ambient[1], self.ambient[2], self.ambient[3]}) love.graphics.rectangle("fill", -l/s, -t/s, w/s,h/s) end) - light_world:drawBlur("alpha", self.shadowBlur, self.normal, self.normal2, l, t, w, h, s) - util.drawCanvasToCanvas(self.normal, self.render_buffer, {blendmode = "multiplicative"}) + self.post_shader:drawBlur(self.shadow_buffer, {self.shadowBlur}) + util.drawCanvasToCanvas(self.shadow_buffer, self.render_buffer, {blendmode = "multiplicative"}) end -- draw material @@ -198,7 +193,7 @@ function light_world:drawGlow(l,t,w,h,s) end) 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"}) end end @@ -214,8 +209,7 @@ function light_world:drawRefraction(l,t,w,h,s) end end) - util.drawCanvasToCanvas(self.render_buffer, self.refractionMap2) - self.refractionShader:send("backBuffer", self.refractionMap2) + self.refractionShader:send("backBuffer", self.render_buffer) self.refractionShader:send("refractionStrength", self.refractionStrength) util.drawCanvasToCanvas(self.refractionMap, self.render_buffer, {shader = self.refractionShader}) end @@ -232,8 +226,7 @@ function light_world:drawReflection(l,t,w,h,s) end end) - util.drawCanvasToCanvas(self.render_buffer, self.reflectionMap2) - self.reflectionShader:send("backBuffer", self.reflectionMap2) + self.reflectionShader:send("backBuffer", self.render_buffer) self.reflectionShader:send("reflectionStrength", self.reflectionStrength) self.reflectionShader:send("reflectionVisibility", self.reflectionVisibility) util.drawCanvasToCanvas(self.reflectionMap, self.render_buffer, {shader = self.reflectionShader}) diff --git a/lib/light.lua b/lib/light.lua index f6e6f6d..f331700 100644 --- a/lib/light.lua +++ b/lib/light.lua @@ -4,8 +4,6 @@ local util = require(_PACKAGE..'/util') local light = class() -light.shadowShader = love.graphics.newShader(_PACKAGE.."/shaders/shadow.glsl") - function light:init(x, y, r, g, b, range) self.direction = 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.glowStrength = 0.0 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 -- 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 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) self.visible = visible end diff --git a/lib/postshader.lua b/lib/postshader.lua index 7db59a4..ffa3b35 100644 --- a/lib/postshader.lua +++ b/lib/postshader.lua @@ -26,10 +26,6 @@ local class = require(_PACKAGE..'/class') local util = require(_PACKAGE..'/util') 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 shaders = {} @@ -56,19 +52,8 @@ end function post_shader:refreshScreenSize(w, h) 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) - 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.h = h end @@ -105,30 +90,29 @@ function post_shader:drawWith(canvas) end function post_shader:drawBloom(canvas, args) - post_shader.blurv:send("steps", args[1] or 2.0) - post_shader.blurh:send("steps", args[1] or 2.0) - util.drawCanvasToCanvas(canvas, self.back_buffer, {shader = post_shader.blurv}) - util.drawCanvasToCanvas(self.back_buffer, self.back_buffer, {shader = post_shader.blurh}) - util.drawCanvasToCanvas(self.back_buffer, self.back_buffer, {shader = post_shader.contrast}) - util.drawCanvasToCanvas(canvas, canvas, {shader = post_shader.contrast}) + shaders['blurv'][1]:send("steps", args[1] or 2.0) + shaders['blurh'][1]:send("steps", args[1] or 2.0) + util.drawCanvasToCanvas(canvas, self.back_buffer, {shader = shaders['blurv'][1]}) + util.process(self.back_buffer, {shader = shaders['blurh'][1]}) + util.process(self.back_buffer, {shader = shaders['contrast'][1]}) + 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}}) end function post_shader:drawBlur(canvas, args) - post_shader.blurv:send("steps", args[1] or 2.0) - post_shader.blurh:send("steps", args[2] or 2.0) - util.drawCanvasToCanvas(canvas, self.back_buffer, {shader = post_shader.blurv}) - util.drawCanvasToCanvas(self.back_buffer, self.back_buffer, {shader = post_shader.blurh}) - util.drawCanvasToCanvas(self.back_buffer, canvas) + shaders['blurv'][1]:send("steps", args[1] or 0.0) + shaders['blurh'][1]:send("steps", args[2] or args[1] or 0.0) + util.process(canvas, {shader = shaders['blurv'][1], blendmode = "alpha"}) + util.process(canvas, {shader = shaders['blurh'][1], blendmode = "alpha"}) end function post_shader:drawTiltShift(canvas, args) - post_shader.blurv:send("steps", args[1] or 2.0) - post_shader.blurh:send("steps", args[2] or 2.0) - util.drawCanvasToCanvas(canvas, self.back_buffer, {shader = post_shader.blurv}) - util.drawCanvasToCanvas(self.back_buffer, self.back_buffer, {shader = post_shader.blurh}) - post_shader.tilt_shift:send("imgBuffer", canvas) - util.drawCanvasToCanvas(self.back_buffer, canvas, {shader = post_shader.tilt_shift}) + shaders['blurv'][1]:send("steps", args[1] or 2.0) + shaders['blurh'][1]:send("steps", args[2] or 2.0) + util.drawCanvasToCanvas(canvas, self.back_buffer, {shader = shaders['blurv'][1]}) + util.process(self.back_buffer, {shader = shaders['blurh'][1]}) + shaders['tilt_shift'][1]:send("imgBuffer", canvas) + util.drawCanvasToCanvas(self.back_buffer, canvas, {shader = shaders['tilt_shift'][1]}) end function post_shader:drawShader(shaderName, canvas, args) @@ -156,7 +140,7 @@ function post_shader:drawShader(shaderName, canvas, args) current_arg = current_arg + 3 elseif def == "imgBuffer" then effect[1]:send("imgBuffer", canvas) - elseif def ~= "screen" and def ~= "textureSize" and def ~= "inputSize" and def ~= "outputSize" then + else local value = args[current_arg] if value ~= nil then effect[1]:send(def, value) diff --git a/lib/shaders/postshaders/black_and_white.glsl b/lib/shaders/postshaders/black_and_white.glsl index 57093f6..cf90ee8 100644 --- a/lib/shaders/postshaders/black_and_white.glsl +++ b/lib/shaders/postshaders/black_and_white.glsl @@ -1,24 +1,13 @@ extern float exposure = 0.7; extern float brightness = 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); -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); - - //exposure knee input0 *= (exp2(input0)*vec4(exposure)); - vec4 lumacomponents = vec4(lumcoeff * lumacomponents, 0.0 ); - float luminance = dot(input0,lumacomponents); - vec4 luma = vec4(luminance); - return vec4(luma.rgb * brightness, 1.0); } diff --git a/lib/shaders/blurh.glsl b/lib/shaders/postshaders/blurh.glsl similarity index 82% rename from lib/shaders/blurh.glsl rename to lib/shaders/postshaders/blurh.glsl index 747f383..ae58eff 100644 --- a/lib/shaders/blurh.glsl +++ b/lib/shaders/postshaders/blurh.glsl @@ -1,8 +1,7 @@ -extern vec2 screen = vec2(800.0, 600.0); extern float steps = 2.0; 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); for(int i = 1; i <= steps; i++) { col = col + Texel(texture, vec2(texture_coords.x, texture_coords.y - pSize.y * i)); @@ -10,4 +9,4 @@ vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 pixel_coords) { } col = col / (steps * 2.0 + 1.0); return vec4(col.r, col.g, col.b, 1.0); -} \ No newline at end of file +} diff --git a/lib/shaders/blurv.glsl b/lib/shaders/postshaders/blurv.glsl similarity index 82% rename from lib/shaders/blurv.glsl rename to lib/shaders/postshaders/blurv.glsl index f92b51a..00b929e 100644 --- a/lib/shaders/blurv.glsl +++ b/lib/shaders/postshaders/blurv.glsl @@ -1,8 +1,7 @@ -extern vec2 screen = vec2(800.0, 600.0); extern float steps = 2.0; 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); for(int i = 1; i <= steps; i++) { col = col + Texel(texture, vec2(texture_coords.x - pSize.x * i, texture_coords.y)); @@ -10,4 +9,4 @@ vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 pixel_coords) { } col = col / (steps * 2.0 + 1.0); return vec4(col.r, col.g, col.b, 1.0); -} \ No newline at end of file +} diff --git a/lib/shaders/postshaders/chromatic_aberration.glsl b/lib/shaders/postshaders/chromatic_aberration.glsl index 6d6909c..a86ade2 100644 --- a/lib/shaders/postshaders/chromatic_aberration.glsl +++ b/lib/shaders/postshaders/chromatic_aberration.glsl @@ -1,13 +1,12 @@ -extern vec2 screen = vec2(800.0, 600.0); extern vec2 redStrength = vec2(4.0, 3.0); extern vec2 greenStrength = vec2(-2.0, -1.0); extern vec2 blueStrength = vec2(1.0, -3.0); 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 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; return vec4(colRed, colGreen, colBlue, 1.0); -} \ No newline at end of file +} diff --git a/lib/shaders/postshaders/contrast.glsl b/lib/shaders/postshaders/contrast.glsl index 066a794..dea0d10 100644 --- a/lib/shaders/postshaders/contrast.glsl +++ b/lib/shaders/postshaders/contrast.glsl @@ -1,6 +1,5 @@ -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; col *= col; return vec4(col, 1.0); -} \ No newline at end of file +} diff --git a/lib/shaders/postshaders/curvature.glsl b/lib/shaders/postshaders/curvature.glsl index 8f9a942..79f0e30 100644 --- a/lib/shaders/postshaders/curvature.glsl +++ b/lib/shaders/postshaders/curvature.glsl @@ -1,88 +1,18 @@ -extern vec2 inputSize; -extern vec2 textureSize; - - #define distortion 0.2 - -/* -#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; - float dist = dot(cc, cc) * distortion; - vec2 result = 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); - - 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; +vec2 radialDistortion(vec2 coord) { + vec2 cc = coord - 0.5; + float dist = dot(cc, cc) * distortion; + return coord + cc * (1.0 + dist) * dist; } -/* -vec4 checkTexelBounds(Image texture, vec2 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; - - return texcolor; +vec4 checkTexelBounds(Image texture, vec2 coords) { + vec2 ss = step(coords, vec2(1.0, 1.0)) * step(vec2(0.0, 0.0), coords); + return Texel(texture, coords) * ss.x * ss.y; } +vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 pixel_coords) { + vec2 coords = radialDistortion(texture_coords); + vec4 texcolor = checkTexelBounds(texture, coords); + texcolor.a = 1.0; + return texcolor; +} diff --git a/lib/shaders/postshaders/edges.glsl b/lib/shaders/postshaders/edges.glsl index 3fcf360..c8add09 100644 --- a/lib/shaders/postshaders/edges.glsl +++ b/lib/shaders/postshaders/edges.glsl @@ -6,9 +6,6 @@ modified by slime73 for use with love2d and mari0 */ - -extern vec2 textureSize; - vec3 grayscale(vec3 color) { 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); - float x = 0.5 / textureSize.x; - float y = 0.5 / textureSize.y; + float x = 0.5 / love_ScreenSize.x; + float y = 0.5 / love_ScreenSize.y; vec2 dg1 = 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 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 second = mix(c02, c22, fract(tex.x * texsize.x + 0.5)); diff --git a/lib/shaders/postshaders/hdr_tv.glsl b/lib/shaders/postshaders/hdr_tv.glsl index 0bfcc90..ba5e30b 100644 --- a/lib/shaders/postshaders/hdr_tv.glsl +++ b/lib/shaders/postshaders/hdr_tv.glsl @@ -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 intens = smoothstep(0.2,0.8,rgb) + normalize(vec4(rgb.xyz, 1.0)); diff --git a/lib/shaders/postshaders/phosphor.glsl b/lib/shaders/postshaders/phosphor.glsl index f505d27..cf58451 100644 --- a/lib/shaders/postshaders/phosphor.glsl +++ b/lib/shaders/postshaders/phosphor.glsl @@ -18,8 +18,6 @@ ) */ -extern vec2 textureSize; - // 0.5 = the spot stays inside the original 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 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) ) return texture2D(_tex0_, coords); else @@ -93,16 +91,16 @@ vec4 grid_color( vec2 coords ) #endif // DEBUG -vec2 onex = vec2( 1.0/textureSize.x, 0.0 ); -vec2 oney = vec2( 0.0, 1.0/textureSize.y ); +vec2 onex = vec2( 1.0/love_ScreenSize.x, 0.0 ); +vec2 oney = vec2( 0.0, 1.0/love_ScreenSize.y ); 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); coords -= pixel_start; 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 pixel; diff --git a/lib/shaders/postshaders/phosphorish.glsl b/lib/shaders/postshaders/phosphorish.glsl index 719c5cb..5449cbd 100644 --- a/lib/shaders/postshaders/phosphorish.glsl +++ b/lib/shaders/postshaders/phosphorish.glsl @@ -5,9 +5,6 @@ */ // modified by slime73 for use with love pixeleffects - -extern vec2 textureSize; - vec3 to_focus(float pixel) { 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) { - 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); - 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_prev = Texel(texture, texture_coords - 1.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_prev = to_focus(pixel_x - 1.0); diff --git a/lib/shaders/postshaders/pip.glsl b/lib/shaders/postshaders/pip.glsl index 42ab20b..56e9b7e 100644 --- a/lib/shaders/postshaders/pip.glsl +++ b/lib/shaders/postshaders/pip.glsl @@ -1,9 +1,5 @@ #define glarebasesize 0.896 #define power 0.50 - -extern vec2 textureSize; -extern vec2 outputSize; - extern float time; 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 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 i; diff --git a/lib/shaders/postshaders/pixellate.glsl b/lib/shaders/postshaders/pixellate.glsl index 40f78e0..6a7935e 100644 --- a/lib/shaders/postshaders/pixellate.glsl +++ b/lib/shaders/postshaders/pixellate.glsl @@ -1,13 +1,10 @@ - -extern vec2 textureSize; - const float pixel_w = 2.0; const float pixel_h = 2.0; vec4 effect(vec4 vcolor, Image texture, vec2 uv, vec2 pixel_coords) { - float dx = pixel_w*(1.0/textureSize.x); - float dy = pixel_h*(1.0/textureSize.y); + float dx = pixel_w*(1.0/love_ScreenSize.x); + float dy = pixel_h*(1.0/love_ScreenSize.y); vec2 coord = vec2(dx*floor(uv.x/dx), dy*floor(uv.y/dy)); return Texel(texture, coord); } diff --git a/lib/shaders/postshaders/radialblur.glsl b/lib/shaders/postshaders/radialblur.glsl index d47766e..5c2a032 100644 --- a/lib/shaders/postshaders/radialblur.glsl +++ b/lib/shaders/postshaders/radialblur.glsl @@ -1,5 +1,4 @@ #define nsamples 5 - extern number blurstart = 1.0; // 0 to 1 extern number blurwidth = -0.02; // -1 to 1 diff --git a/lib/shaders/postshaders/scanlines.glsl b/lib/shaders/postshaders/scanlines.glsl index 2e4582a..1e460d4 100644 --- a/lib/shaders/postshaders/scanlines.glsl +++ b/lib/shaders/postshaders/scanlines.glsl @@ -1,9 +1,8 @@ -extern vec2 screen = vec2(800.0, 600.0); extern float strength = 2.0; extern float time = 0.0; 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 offsetX = sin(texture_coords.y * 10.0 + time * strength) * pSize.x; float corner = 500.0; diff --git a/lib/shaders/postshaders/waterpaint.glsl b/lib/shaders/postshaders/waterpaint.glsl index 5461eb8..8c44c92 100644 --- a/lib/shaders/postshaders/waterpaint.glsl +++ b/lib/shaders/postshaders/waterpaint.glsl @@ -17,12 +17,10 @@ vec4 compress(vec4 in_color, float threshold, float ratio) return in_color - (diff * (1.0 - 1.0/ratio)); } -extern vec2 textureSize; - vec4 effect(vec4 vcolor, Image texture, vec2 tex, vec2 pixel_coords) { - float x = 0.5 * (1.0 / textureSize.x); - float y = 0.5 * (1.0 / textureSize.y); + float x = 0.5 * (1.0 / love_ScreenSize.x); + float y = 0.5 * (1.0 / love_ScreenSize.y); vec2 dg1 = 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 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 second = mix(c02, c22, fract(tex.x * texsize.x + 0.5)); diff --git a/lib/shaders/reflection.glsl b/lib/shaders/reflection.glsl index edf662f..16c14e9 100644 --- a/lib/shaders/reflection.glsl +++ b/lib/shaders/reflection.glsl @@ -1,11 +1,10 @@ extern Image backBuffer; -extern vec2 screen = vec2(800.0, 600.0); extern float reflectionStrength; extern float reflectionVisibility; 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); if(normal.a > 0.0 && normal.r > 0.0) { vec3 pColor = Texel(backBuffer, texture_coords).rgb; @@ -21,4 +20,4 @@ vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 pixel_coords) { } else { return vec4(0.0); } -} \ No newline at end of file +} diff --git a/lib/shaders/refraction.glsl b/lib/shaders/refraction.glsl index d9b3d23..8ded775 100644 --- a/lib/shaders/refraction.glsl +++ b/lib/shaders/refraction.glsl @@ -1,10 +1,9 @@ extern Image backBuffer; -extern vec2 screen = vec2(800.0, 600.0); extern float refractionStrength = 1.0; 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); 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)); @@ -16,4 +15,4 @@ vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 pixel_coords) { } else { return vec4(0.0); } -} \ No newline at end of file +} diff --git a/lib/shaders/shadow.glsl b/lib/shaders/shadow.glsl index 6c1da45..312a887 100644 --- a/lib/shaders/shadow.glsl +++ b/lib/shaders/shadow.glsl @@ -4,26 +4,14 @@ */ #define PI 3.1415926535897932384626433832795 -extern vec2 screenResolution; //size of the screen extern Image shadowMap; //a canvas containing shadow data only extern vec3 lightPosition; //the light position on the screen(not global) extern vec3 lightColor; //the rgb color of the light extern float lightRange; //the range of the light extern float lightSmooth; //smoothing of the lights attenuation 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 -//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 pixelColor = Texel(texture, 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 return vec4(0.0, 0.0, 0.0, 1.0); }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; if(pixelColor.a > 0.0) { //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; } else { //on the normal map, draw normal shadows - vec3 dir = vec3((lightPosition.xy - pixel_coords.xy) / screenResolution.xy, lightPosition.z); - dir.x *= screenResolution.x / screenResolution.y; + vec3 dir = vec3((lightPosition.xy - pixel_coords.xy) / love_ScreenSize.xy, lightPosition.z); + dir.x *= love_ScreenSize.x / love_ScreenSize.y; vec3 diff = lightColor * max(dot(normalize(normal), normalize(dir)), 0.0); //return the light that is effected by the normal and attenuation return vec4(diff * att, 1.0); diff --git a/lib/util.lua b/lib/util.lua index 79f6595..48b71da 100644 --- a/lib/util.lua +++ b/lib/util.lua @@ -1,31 +1,44 @@ local util = {} +function util.process(canvas, options) + util.drawCanvasToCanvas(canvas, canvas, options) +end + function util.drawCanvasToCanvas(canvas, other_canvas, options) options = options or {} - local last_buffer = love.graphics.getCanvas() - love.graphics.push() - love.graphics.origin() - love.graphics.setCanvas(other_canvas) - if options["blendmode"] then - love.graphics.setBlendMode(options["blendmode"]) - end - if options["shader"] then - love.graphics.setShader(options["shader"]) - end - if options["color"] then - love.graphics.setColor(unpack(options["color"])) - end + util.drawto(other_canvas, 0, 0, 1, function() + if options["blendmode"] then + love.graphics.setBlendMode(options["blendmode"]) + end + if options["shader"] then + love.graphics.setShader(options["shader"]) + 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 + love.graphics.setColor(unpack(options["color"])) + else love.graphics.setColor(255,255,255) - love.graphics.draw(canvas,0,0) - love.graphics.setCanvas(last_buffer) + end + love.graphics.draw(canvas,0,0) if options["blendmode"] then love.graphics.setBlendMode("alpha") end if options["shader"] then love.graphics.setShader() end - love.graphics.pop() + if options["stencil"] then + love.graphics.setStencil() + end + if options["istencil"] then + love.graphics.setInvertedStencil() + end + end) end function util.drawto(canvas, x, y, scale, cb)