I'll take a very close look at this when I've had some sleep. been up for > 30 hours. anyone that is into feedback shaders and cellular automata is a friend of mine. give me a few days.darkfrei wrote: ↑Tue Sep 05, 2023 8:06 pmIs it rigth?tourgen wrote: ↑Wed Mar 22, 2023 5:42 pmAbsolutely. This guy has excellent shader videos, and in fact did Game of Life in a shader. It uses a buffer texture to record state (of the previous evolution step). Game of Life is one of the original cellular automata algorithms. Apply different rules to produce different results: for instance, modelling the slime mold behavior.
https://www.youtube.com/watch?v=xh61Ol2X4GAThe main.lua (it works wrong, please help me):Code: Select all
local shader = love.graphics.newShader [[ vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 screen_coords) { // Calculate the texture coordinate offsets for neighboring pixels vec2 offsetH = vec2(1.0 / love_ScreenSize.x, 0.0); vec2 offsetV = vec2(0.0, 1.0 / love_ScreenSize.y); // Sample the colors of neighboring pixels 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); // Laplace sum the colors of all neighboring pixels, including diagonals vec4 sumColor = - 1.0 * colorCenter + 0.14645 * (colorLeft + colorRight + colorUp + colorDown) + 0.10355 * (colorUpLeft + colorUpRight + colorDownLeft + colorDownRight); sumColor.a = 1.0; return sumColor; } ]]
Code: Select all
local canvasWidth, canvasHeight = 600, 600 local shader = love.graphics.newShader [[ vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 screen_coords) { // Calculate the texture coordinate offsets for neighboring pixels vec2 offsetH = vec2(1.0 / love_ScreenSize.x, 0.0); vec2 offsetV = vec2(0.0, 1.0 / love_ScreenSize.y); // Sample the colors of neighboring pixels 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); // Laplace sum the colors of all neighboring pixels, including diagonals vec4 sumColor = - 1.0 * colorCenter + 0.14645 * (colorLeft + colorRight + colorUp + colorDown) + 0.10355 * (colorUpLeft + colorUpRight + colorDownLeft + colorDownRight); sumColor.a = 1.0; return sumColor; } ]] 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,1,0) for i = 1, 10 do love.graphics.rectangle ('fill', math.random (canvasWidth-10), math.random (canvasHeight-10), 10, 10) end love.graphics.setCanvas(canvases[2]) love.graphics.setColor (0,1,1) for i = 1, 10 do love.graphics.rectangle ('fill', math.random (canvasWidth-10), math.random (canvasHeight-10), 10, 10) 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.draw (canvases[iPrevCanvas]) love.graphics.setShader() love.graphics.setCanvas() end function love.draw() love.graphics.draw (canvases[iCanvas]) end function love.keypressed (k, s) if k == 'escape' then love.event.quit () end print (k) end
Reaction diffusion (type of cellular automaton)
Re: Reaction diffusion (type of cellular automaton)
Who is online
Users browsing this forum: No registered users and 1 guest