From 16cc91724bb50797bc7439c20a8275cdef616740 Mon Sep 17 00:00:00 2001 From: Marcus Ihde Date: Fri, 28 Mar 2014 03:32:32 +0100 Subject: [PATCH] Add several post shader effects. --- gfx/light.png | Bin 0 -> 193 bytes light.lua | 18 +++++------ main.lua | 47 +++++++++++++++++++++++----- postshader.lua | 66 ++++++++++++++++++++++++++++++---------- shader/four_colors.glsl | 8 +++++ shader/monochrom.glsl | 11 +++++++ shader/poly_shadow.glsl | 4 +-- shader/refraction.glsl | 4 +-- shader/scanlines.glsl | 31 +++++++++++++++++++ shader/tilt_shift.glsl | 12 ++++++++ 10 files changed, 164 insertions(+), 37 deletions(-) create mode 100644 gfx/light.png create mode 100644 shader/four_colors.glsl create mode 100644 shader/monochrom.glsl create mode 100644 shader/scanlines.glsl create mode 100644 shader/tilt_shift.glsl diff --git a/gfx/light.png b/gfx/light.png new file mode 100644 index 0000000000000000000000000000000000000000..e142986d30f56f964ae90571fa94b8469a522579 GIT binary patch literal 193 zcmeAS@N?(olHy`uVBq!ia0vp^oFL4>1|%O$WD@{VEa{HEjtmSN`?>!lvVtTFJR*x3 z82Ao=Fk{xWPLK}C64!{5;QX|b^2DN4hJeJ(yb?V>*ARs=V?9$n3v=s+SfC<1PZ!4! zjo{>jhKBzEQtH3|?|0_;8qm7qhQQ= 1.0 then + love.graphics.setColor(0, 255, 0) + love.graphics.print("F10: Effects (" .. effectOn .. ")", 4 + 152 * 4, 4 + 20 * 1) + else + love.graphics.setColor(255, 0, 0) + love.graphics.print("F10: Effects (off)", 4 + 152 * 4, 4 + 20 * 1) + end love.graphics.setColor(255, 0, 255) love.graphics.print("F11: Clear obj.", 4 + 152 * 4, 4 + 20 * 2) love.graphics.print("F12: Clear lights", 4 + 152 * 4, 4 + 20 * 3) @@ -407,6 +420,17 @@ function love.draw() love.postshader.addEffect("bloom", 2.0, bloomOn) end + if effectOn == 1.0 then + love.postshader.addEffect("4colors", {15, 56, 15}, {48, 98, 48}, {139, 172, 15}, {155, 188, 15}) + --love.postshader.addEffect("4colors", {108, 108, 78}, {142, 139, 87}, {195, 196, 165}, {227, 230, 201}) + elseif effectOn == 2.0 then + love.postshader.addEffect("monochrom") + elseif effectOn == 3.0 then + love.postshader.addEffect("scanlines") + elseif effectOn == 4.0 then + love.postshader.addEffect("tiltshift", 4.0) + end + love.postshader.draw() end @@ -427,6 +451,7 @@ function love.mousepressed(x, y, c) light.setGlowStrength(0.3) elseif c == "l" then -- add rectangle + math.randomseed(love.timer.getTime()) phyCnt = phyCnt + 1 phyLight[phyCnt] = lightWorld.newPolygon() phyBody[phyCnt] = love.physics.newBody(physicWorld, x, y, "dynamic") @@ -435,6 +460,7 @@ function love.mousepressed(x, y, c) phyFixture[phyCnt]:setRestitution(0.5) elseif c == "r" then -- add circle + math.randomseed(love.timer.getTime()) cRadius = math.random(8, 32) phyCnt = phyCnt + 1 phyLight[phyCnt] = lightWorld.newCircle(x, y, cRadius) @@ -487,6 +513,11 @@ function love.keypressed(k, u) glowBlur = 0.0 end lightWorld.setGlowStrength(glowBlur) + elseif k == "f10" then + effectOn = effectOn + 1.0 + if effectOn > 4.0 then + effectOn = 0.0 + end elseif k == "f11" then physicWorld:destroy() lightWorld.clearBodys() diff --git a/postshader.lua b/postshader.lua index 81361c3..2a07d97 100644 --- a/postshader.lua +++ b/postshader.lua @@ -6,9 +6,14 @@ LOVE_POSTSHADER_BLURV = love.graphics.newShader("shader/blurv.glsl") LOVE_POSTSHADER_BLURH = love.graphics.newShader("shader/blurh.glsl") LOVE_POSTSHADER_CONTRAST = love.graphics.newShader("shader/contrast.glsl") LOVE_POSTSHADER_CHROMATIC_ABERRATION = love.graphics.newShader("shader/chromatic_aberration.glsl") +LOVE_POSTSHADER_FOUR_COLOR = love.graphics.newShader("shader/four_colors.glsl") +LOVE_POSTSHADER_MONOCHROM = love.graphics.newShader("shader/monochrom.glsl") +LOVE_POSTSHADER_SCANLINES = love.graphics.newShader("shader/scanlines.glsl") +LOVE_POSTSHADER_TILT_SHIFT = love.graphics.newShader("shader/tilt_shift.glsl") LOVE_POSTSHADER_BLURV:send("screen", {love.window.getWidth(), love.window.getHeight()}) LOVE_POSTSHADER_BLURH:send("screen", {love.window.getWidth(), love.window.getHeight()}) +LOVE_POSTSHADER_SCANLINES:send("screenHeight", {love.window.getWidth(), love.window.getHeight()}) love.postshader = {} @@ -25,12 +30,13 @@ love.postshader.addEffect = function(shader, ...) args = {...} LOVE_POSTSHADER_LAST_BUFFER = love.graphics.getCanvas() + love.graphics.setCanvas(LOVE_POSTSHADER_BUFFER_BACK) + love.graphics.setBlendMode("alpha") + if shader == "bloom" then -- Bloom Shader LOVE_POSTSHADER_BLURV:send("steps", args[1] or 2.0) LOVE_POSTSHADER_BLURH:send("steps", args[1] or 2.0) - love.graphics.setCanvas(LOVE_POSTSHADER_BUFFER_BACK) - love.graphics.setBlendMode("alpha") love.graphics.setShader(LOVE_POSTSHADER_BLURV) love.graphics.draw(LOVE_POSTSHADER_BUFFER_RENDER) @@ -53,8 +59,44 @@ love.postshader.addEffect = function(shader, ...) -- Blur Shader LOVE_POSTSHADER_BLURV:send("steps", args[1] or 2.0) LOVE_POSTSHADER_BLURH:send("steps", args[2] or 2.0) + + love.graphics.setShader(LOVE_POSTSHADER_BLURV) + love.graphics.draw(LOVE_POSTSHADER_BUFFER_RENDER) + + love.graphics.setShader(LOVE_POSTSHADER_BLURH) + love.graphics.draw(LOVE_POSTSHADER_BUFFER_BACK) + elseif shader == "chromatic" then + -- Chromatic Shader + LOVE_POSTSHADER_CHROMATIC_ABERRATION:send("redStrength", {args[1] or 0.0, args[2] or 0.0}) + LOVE_POSTSHADER_CHROMATIC_ABERRATION:send("greenStrength", {args[3] or 0.0, args[4] or 0.0}) + LOVE_POSTSHADER_CHROMATIC_ABERRATION:send("blueStrength", {args[5] or 0.0, args[6] or 0.0}) love.graphics.setCanvas(LOVE_POSTSHADER_BUFFER_BACK) - love.graphics.setBlendMode("alpha") + love.graphics.setShader(LOVE_POSTSHADER_CHROMATIC_ABERRATION) + love.graphics.draw(LOVE_POSTSHADER_BUFFER_RENDER) + elseif shader == "4colors" then + -- 4 Color Shader + for i = 1, 4 do + for k = 1, 3 do + args[i][k] = args[i][k] / 255.0 + end + end + LOVE_POSTSHADER_FOUR_COLOR:send("palette", args[1], args[2], args[3], args[4]) + love.graphics.setShader(LOVE_POSTSHADER_FOUR_COLOR) + love.graphics.draw(LOVE_POSTSHADER_BUFFER_RENDER) + elseif shader == "monochrom" then + -- Monochrom Shader + LOVE_POSTSHADER_MONOCHROM:send("time", love.timer.getTime()) + love.graphics.setShader(LOVE_POSTSHADER_MONOCHROM) + love.graphics.draw(LOVE_POSTSHADER_BUFFER_RENDER) + elseif shader == "scanlines" then + -- Scanlines Shader + LOVE_POSTSHADER_SCANLINES:send("time", love.timer.getTime()) + love.graphics.setShader(LOVE_POSTSHADER_SCANLINES) + love.graphics.draw(LOVE_POSTSHADER_BUFFER_RENDER) + elseif shader == "tiltshift" then + -- Blur Shader + LOVE_POSTSHADER_BLURV:send("steps", args[1] or 2.0) + LOVE_POSTSHADER_BLURH:send("steps", args[1] or 2.0) love.graphics.setShader(LOVE_POSTSHADER_BLURV) love.graphics.draw(LOVE_POSTSHADER_BUFFER_RENDER) @@ -62,21 +104,13 @@ love.postshader.addEffect = function(shader, ...) love.graphics.setShader(LOVE_POSTSHADER_BLURH) love.graphics.draw(LOVE_POSTSHADER_BUFFER_BACK) - love.graphics.setCanvas(LOVE_POSTSHADER_LAST_BUFFER) - love.graphics.setShader() - love.graphics.setColor(255, 255, 255) + LOVE_POSTSHADER_TILT_SHIFT:send("buffer", LOVE_POSTSHADER_BUFFER_RENDER) + love.graphics.setShader(LOVE_POSTSHADER_TILT_SHIFT) love.graphics.draw(LOVE_POSTSHADER_BUFFER_BACK) - elseif shader == "chromatic" then - -- Blur Shader - LOVE_POSTSHADER_CHROMATIC_ABERRATION:send("redStrength", {args[1] or 0.0, args[2] or 0.0}) - LOVE_POSTSHADER_CHROMATIC_ABERRATION:send("greenStrength", {args[3] or 0.0, args[4] or 0.0}) - LOVE_POSTSHADER_CHROMATIC_ABERRATION:send("blueStrength", {args[5] or 0.0, args[6] or 0.0}) - love.graphics.setCanvas(LOVE_POSTSHADER_BUFFER_BACK) + end + + if shader ~= "bloom" then love.graphics.setBlendMode("alpha") - - love.graphics.setShader(LOVE_POSTSHADER_CHROMATIC_ABERRATION) - love.graphics.draw(LOVE_POSTSHADER_BUFFER_RENDER) - love.graphics.setCanvas(LOVE_POSTSHADER_LAST_BUFFER) love.graphics.setShader() love.graphics.setColor(255, 255, 255) diff --git a/shader/four_colors.glsl b/shader/four_colors.glsl new file mode 100644 index 0000000..c68b1cf --- /dev/null +++ b/shader/four_colors.glsl @@ -0,0 +1,8 @@ +extern vec3 palette[4]; + +vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 pixel_coords){ + vec4 pixel = Texel(texture, texture_coords); + int index = int(min(0.9999, max(0.0001,(pixel.r + pixel.g + pixel.b) / 3.0)) * 4); + + return vec4(palette[index], 1.0); +} \ No newline at end of file diff --git a/shader/monochrom.glsl b/shader/monochrom.glsl new file mode 100644 index 0000000..43297bd --- /dev/null +++ b/shader/monochrom.glsl @@ -0,0 +1,11 @@ +extern float time = 0.0; + +float rand(vec2 position, float seed) { + return fract(sin(dot(position.xy,vec2(12.9898, 78.233))) * seed); +} + +vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 pixel_coords){ + vec4 pixel = Texel(texture, texture_coords); + + return vec4(vec3((pixel.r + pixel.g + pixel.b) / 3.0) + (rand(texture_coords, time) - 0.5) * 0.1, 1.0); +} \ No newline at end of file diff --git a/shader/poly_shadow.glsl b/shader/poly_shadow.glsl index eb0947e..ba8ae77 100644 --- a/shader/poly_shadow.glsl +++ b/shader/poly_shadow.glsl @@ -1,6 +1,6 @@ #define PI 3.1415926535897932384626433832795 -extern vec2 lightPosition; +extern vec3 lightPosition; extern vec3 lightColor; extern float lightRange; extern float lightSmooth; @@ -10,7 +10,7 @@ extern float lightAngle; vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 pixel_coords){ vec4 pixel = Texel(texture, texture_coords); - vec2 lightToPixel = pixel_coords - lightPosition; + vec3 lightToPixel = vec3(pixel_coords.x, pixel_coords.y, 0.0) - lightPosition; float distance = length(lightToPixel); float att = 1 - distance / lightRange; diff --git a/shader/refraction.glsl b/shader/refraction.glsl index 7dc534f..d9b3d23 100644 --- a/shader/refraction.glsl +++ b/shader/refraction.glsl @@ -6,9 +6,9 @@ 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); vec4 normal = Texel(texture, texture_coords); - if(normal.a > 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)); - if(normalOffset.a > 0.0) { + if(normalOffset.b > 0.0) { return Texel(backBuffer, vec2(texture_coords.x + (normal.x - 0.5) * pSize.x * refractionStrength, texture_coords.y + (normal.y - 0.5) * pSize.y * refractionStrength)); } else { return Texel(backBuffer, texture_coords); diff --git a/shader/scanlines.glsl b/shader/scanlines.glsl new file mode 100644 index 0000000..9fa370c --- /dev/null +++ b/shader/scanlines.glsl @@ -0,0 +1,31 @@ +extern vec2 screenHeight = vec2(800.0, 600.0); +extern float time = 0.0; + +vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 pixel_coords){ + vec2 pSize = 1.0 / screenHeight; + float brightness = 1.0; + float offsetX = sin(texture_coords.y * 10.0 + time * 2.0) * pSize.x; + + if(texture_coords.x < 0.5) { + if(texture_coords.y < 0.5) { + brightness = min(texture_coords.x * texture_coords.y * 100.0, 1.0); + } else { + brightness = min(texture_coords.x * (1.0 - texture_coords.y) * 100.0, 1.0); + } + } else { + if(texture_coords.y < 0.5) { + brightness = min((1.0 - texture_coords.x) * texture_coords.y * 100.0, 1.0); + } else { + brightness = min((1.0 - texture_coords.x) * (1.0 - texture_coords.y) * 100.0, 1.0); + } + } + float red = Texel(texture, vec2(texture_coords.x + offsetX, texture_coords.y + pSize.y * 0.5)).r; + float green = Texel(texture, vec2(texture_coords.x + offsetX, texture_coords.y - pSize.y * 0.5)).g; + float blue = Texel(texture, vec2(texture_coords.x + offsetX, texture_coords.y)).b; + + if(mod(texture_coords.y * screenHeight.y, 2.0) > 0.5) { + return vec4(vec3(red, green, blue) * brightness, 1.0); + } else { + return vec4(vec3(red * 0.75, green * 0.75, blue * 0.75) * brightness, 1.0); + } +} \ No newline at end of file diff --git a/shader/tilt_shift.glsl b/shader/tilt_shift.glsl new file mode 100644 index 0000000..d0df84f --- /dev/null +++ b/shader/tilt_shift.glsl @@ -0,0 +1,12 @@ +extern Image buffer; + +vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 pixel_coords){ + vec4 pixel = Texel(texture, texture_coords); + vec4 pixel2 = Texel(buffer, texture_coords); + + if(texture_coords.y > 0.5) { + return vec4(pixel.rgb * (texture_coords.y - 0.5) * 2.0 + pixel2.rgb * (1.0 - texture_coords.y) * 2.0, 1.0); + } else { + return vec4(pixel.rgb * (0.5 - texture_coords.y) * 2.0 + pixel2.rgb * texture_coords.y * 2.0, 1.0); + } +} \ No newline at end of file