Reaction diffusion (type of cellular automaton)

Showcase your libraries, tools and other projects that help your fellow love users.
tourgen
Citizen
Posts: 53
Joined: Sat Mar 18, 2023 12:45 am

Re: Reaction diffusion (type of cellular automaton)

Post by tourgen »

darkfrei wrote: Tue Sep 05, 2023 8:06 pm
tourgen wrote: Wed Mar 22, 2023 5:42 pm
darkfrei wrote: Sun Mar 19, 2023 6:27 pm Is it possible to make a shader for it?
Absolutely. 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=xh61Ol2X4GA
Is it rigth?

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;
}
]]
The main.lua (it works wrong, please help me):

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
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.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 53 guests