Hi all!
I really need your help! I am making the shader for System Reaction-Diffusion, but something goes wrong:
Code: Select all
local canvasWidth, canvasHeight = 600, 600
local shaderCode = [[
float feed_rate = 0.162; // Feed rate (F)
float kill_rate = 0.155; // Kill rate (K)
float diffusion_rate_red = 0.06; // Diffusion rate for U
float diffusion_rate_green = 0.025; // Diffusion rate for V
float dt = 0.05; // Time step (dt)
vec4 calculateLaplacian(Image texture, vec2 texture_coords) {
vec2 offsetH = vec2(1.0 / love_ScreenSize.x, 0.0);
vec2 offsetV = vec2(0.0, 1.0 / love_ScreenSize.y);
vec4 colorCenter = Texel(texture, texture_coords);
vec4 colorLeft = Texel(texture, texture_coords - offsetH);
vec4 colorRight = Texel(texture, texture_coords + offsetH);
vec4 colorUp = Texel(texture, texture_coords - offsetV);
vec4 colorDown = Texel(texture, texture_coords + offsetV);
vec4 colorUpLeft = Texel(texture, texture_coords - offsetV - offsetH);
vec4 colorUpRight = Texel(texture, texture_coords - offsetV + offsetH);
vec4 colorDownLeft = Texel(texture, texture_coords + offsetV - offsetH);
vec4 colorDownRight = Texel(texture, texture_coords + offsetV + offsetH);
vec4 laplacian = - 1.0 * colorCenter +
// 0.14645 * (colorLeft + colorRight + colorUp + colorDown) +
// 0.10355 * (colorUpLeft + colorUpRight + colorDownLeft + colorDownRight);
0.2 * (colorLeft + colorRight + colorUp + colorDown) +
0.05 * (colorUpLeft + colorUpRight + colorDownLeft + colorDownRight);
return laplacian;
}
vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 screen_coords) {
vec4 current = Texel(texture, texture_coords);
vec4 laplacian = calculateLaplacian(texture, texture_coords);
float triangleR = laplacian.r;
float triangleG = laplacian.g;
float cR = current.r; // current U
float cG = current.g;
float newU = cR + (diffusion_rate_red * triangleR - cR * cG * cG + feed_rate * (1.0 - cR)) * dt;
float newV = cG + (diffusion_rate_green * triangleG + cR * cG * cG - (kill_rate + feed_rate) * cG) * dt;
vec4 new = vec4 (newU, newV, 0, 1);
return new;
}
]]
shader = love.graphics.newShader(shaderCode)
local canvases = {
love.graphics.newCanvas(canvasWidth, canvasHeight),
love.graphics.newCanvas(canvasWidth, canvasHeight)
}
local iCanvas = 1
function love.load()
love.window.setTitle("Reaction Diffusion Simulation")
love.window.setMode(canvasWidth, canvasHeight)
love.graphics.setCanvas(canvases[1])
love.graphics.setColor (1,0,0)
for i = 1, 10 do
love.graphics.rectangle ('fill', math.random (canvasWidth-50), math.random (canvasHeight-50), 50, 50)
end
-- love.graphics.setCanvas(canvases[2])
love.graphics.setColor (0,1,0)
for i = 1, 10 do
love.graphics.rectangle ('fill', math.random (canvasWidth-50), math.random (canvasHeight-50), 50, 50)
end
love.graphics.setColor (1,1,0)
for i = 1, 10 do
love.graphics.rectangle ('fill', math.random (canvasWidth-40), math.random (canvasHeight-40), 40, 40)
end
love.graphics.setCanvas()
end
function love.update(dt)
local iPrevCanvas = iCanvas
iCanvas = iCanvas %2 +1
love.window.setTitle (iCanvas)
love.graphics.setCanvas(canvases[iCanvas])
love.graphics.setShader(shader)
love.graphics.setColor (1,1,1)
love.graphics.draw (canvases[iPrevCanvas])
love.graphics.setShader()
love.graphics.setCanvas()
end
function love.draw()
love.graphics.setColor (1,1,1)
love.graphics.draw (canvases[iCanvas])
end