Game Development Reference
In-Depth Information
Shaders
LÖVE has supported pixel shaders since 0.8.0; these are also called fragment shaders. These
compute the color and other attributes for each pixel. With shaders, you can generate a variety of
effects, including adding color to images.
The pixel effects are not programmed or described in Lua; they use OpenGL Shading Language
(GLSL) instead, which is a subset of functions used by the pixel shaders. Note the C or JavaScript
type syntax of GLSL in contrast to the Lua syntax.
Let's make a sample that colors an image black and white:
extern number value;
vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 pixel_coords)
{
vec4 pixel = Texel(texture, texture_coords);
float avg = max(0, ((pixel.r + pixel.g + pixel.b)/3) + value/10);
pixel.r = avg;
pixel.g = avg;
pixel.b = avg;
return pixel;
}
]]
value = 0
function effect.func()
effect.effect:send("value", value)
love.graphics.setPixelEffect(effect.effect)
love.graphics.draw(image)
end
function love.draw()
love.graphics.setColor(255,255,255)
effect.func()
love.graphics.setPixelEffect()
end
To invert the colors of the image, we can simply change the effect as follows:
effect.effect = love.graphics.newPixelEffect [[
vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 pixel_coords)
{
vec4 pixel = Texel(texture, texture_coords);
pixel.r = 1.0 - pixel.r;
pixel.g = 1.0 - pixel.g;
pixel.b = 1.0 - pixel.b;
 
Search WWH ::




Custom Search