Re: Share a Shader!
Posted: Mon Dec 03, 2012 4:41 pm
That's quite bad, you've got 2 FPS with a black screen ^^' I guess I messed up something since I don't know the language, my bad
Code: Select all
--
-- The original Light Scattering GLSL shader by Fabien Sanglard
-- http://fabiensanglard.net/lightScattering/index.php
--
-- Adapted to the LOVE PixelEffect by ioxu
-- http://blog.ioxu.com/?p=909
--
-- 'Luminosity' wrapper by mz1
-- www.mz1net.com
--
-- =================================================================================
-- EXAMPLE USAGE:
-- ---
--[[
function love.load()
windowSizeX, windowSizeY = 800, 600
l = Luminosity.new(windowSizeX, windowSizeY)
end
function love.draw()
-- Draw BG, ...
-- ...
-- When you want to draw with luminosity:
-- 1] Call 'receive'
l:receive()
-- 2] Now the luminosity instance will receive all your renders up until you
-- call 'process', so draw whatever you want to draw illuminated:
love.graphics.draw(sprite1, 0, 0, ...)
love.graphics.draw(sprite2, 0, 0, ...)
-- 3] Call 'process'
l:process()
-- 4] Now the luminosity instance contains everything that you have drawn in the
-- step #2, and it is illuminated. Draw it to the screen when convenient.
l:draw()
-- Done. Continue to render whatever else you want to render.
-- ...
end
-- Remember that you can set the light's position with 'setLightPosition', and also
-- tweak the shader values in the constructor.
]]
-- =================================================================================
-- =================================================================================
Luminosity = {}
Luminosity.__index = Luminosity
-- =================================================================================
function Luminosity.new (canvasSizeX, canvasSizeY)
local new = {}
setmetatable(new, Luminosity)
new.canvasSizeX = canvasSizeX
new.canvasSizeY = canvasSizeY
new.backCanvas = love.graphics.newCanvas( canvasSizeX, canvasSizeY )
new.raysCanvas = love.graphics.newCanvas( canvasSizeX, canvasSizeY )
new.FX = love.graphics.newPixelEffect[[
extern number exposure = 1.0;
extern number decay = 1.0;
extern number density = 1.0;
extern number weight = 1.0;
extern vec2 lightPositionOnScreen= vec2(0.0,0.0);
extern number NUM_SAMPLES = 100.0 ;
vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 pixel_coords)
{
vec2 deltaTextCoord = vec2( texture_coords - lightPositionOnScreen.xy );
vec2 textCoo = texture_coords.xy;
deltaTextCoord *= 1.0 / float(NUM_SAMPLES) * density;
float illuminationDecay = 1.0;
vec4 cc = vec4(0.0, 0.0, 0.0, 1.0);
for(int i=0; i < NUM_SAMPLES ; i++)
{
textCoo -= deltaTextCoord;
vec4 sample = Texel( texture, textCoo );
sample *= illuminationDecay * weight;
cc += sample;
illuminationDecay *= decay;
}
cc *= exposure;
return cc;
}
]]
new:setLightSource( 0.5, 0.5 )
new.FX:send( 'exposure', 0.2 )
new.FX:send( 'decay', 0.95 )
new.FX:send( 'density', 0.4 )
new.FX:send( 'weight', 0.3 )
new.FX:send( 'NUM_SAMPLES', 70 )
return new
end
-- =================================================================================
--
-- x, y = [0, 1], relative to the canvas size
--
function Luminosity:setLightSource (x, y)
self.FX:send( 'lightPositionOnScreen', { x, 1-y } )
end
-- =================================================================================
function Luminosity:receive()
--
self.backCanvas:clear()
self.raysCanvas:clear()
--
love.graphics.setCanvas( self.backCanvas )
end
-- =================================================================================
function Luminosity:process()
--
love.graphics.setColor( 255,255,255 )
love.graphics.setCanvas( self.raysCanvas )
love.graphics.setPixelEffect( self.FX )
--
love.graphics.draw( self.backCanvas, 0, 0, 0, 1, 1 )
--
love.graphics.setCanvas()
love.graphics.setPixelEffect()
end
-- =================================================================================
function Luminosity:draw (x, y)
--
love.graphics.setColor( 255,255,255 )
love.graphics.draw( self.backCanvas, x or 0, y or 0 )
--
local bm = love.graphics.getBlendMode()
love.graphics.setBlendMode( 'additive' )
love.graphics.draw( self.raysCanvas, x, y, 0, 1, 1 )
love.graphics.draw( self.raysCanvas, x, y, 0, 1, 1 )
-- Restore the previous blend mode
love.graphics.setBlendMode( bm )
end
That could be useful for doing "flakey signal" effects. Awesome.retrotails wrote:An accident.