xxxxxxxxxx
/******************
Code by Vamoss
Original code link:
https://openprocessing.org/sketch/1688782
Author links:
http://vamoss.com.br
http://twitter.com/vamoss
http://instagram.com/vamoss
http://github.com/vamoss
******************/
// p5js version is based on https://johanneshoff.com/physarum/
// original implementation src code credit to Sage Jensen
//latter addapted by Chaski on https://editor.p5js.org/chaski/sketches/M2deZHzm8
var W;
var H;
var physarum;
var font;
var textureText;
const msg = "NEURON";
function preload(){
font = loadFont("Ubuntu-Bold.ttf");
}
function setup() {
createCanvas(windowWidth,windowHeight);
pixelDensity(1);
background(0);
iniGui();
//find max font size
textFont(font);
var fontSize = 155;
do{
fontSize -= 5;
textSize(fontSize);
}while(textWidth(msg) > width-100);
//create texture
textureText = createGraphics(floor(textWidth(msg)+10), fontSize+10);
textureText.fill(255);
textureText.noStroke();
textureText.textFont(font);
textureText.textSize(fontSize);
textureText.text(msg, 5, fontSize);
//convert pixels to float array
textureText.loadPixels();
var textureArr = new Float32Array(textureText.width * textureText.height);
for(var y = 0; y < textureText.height; y++){
for(var x = 0; x < textureText.width; x++){
const index = x + y * textureText.width;
textureArr[index] = textureText.pixels[index * 4];
}
}
W = width;
H = height;
physarum = new Physarum(W, H, textureArr, textureText.width, textureText.height);
}
function draw() {
/*
loadPixels();
for (let i = 0; i < pixels.length; i+=4) {
pixels[i+3] *= 0.9;
}
updatePixels();
/**/
//*
fill(0, 10);
noStroke();
rect(0, 0, width, height);
/**/
if(frameCount < 200){
regenerate();
}
if (mouseIsPressed) {
for(var i = 0; i < 40; i++){
physarum.addAgent(
W * mouseX / width,
H * mouseY / height,
random(TWO_PI)
);
}
}
physarum.update();
physarum.draw();
//image(textureText, 0, 0);
}
// dat gui
let config = {
sensor_distance : 9,//15
sensor_angle : 40/180*Math.PI, // radians
turning_speed : 0.1, // radians
speed : 1.0,
decay_factor : 0.95,
deposit_amount : 0.6,
num_agents : 3000,
start_in_circle : true, // otherwise start randomly
//highlight_agents: false,
random_turning : false, // randomly turn within the limits of turning_speed
wrap_around : true
}
function iniGui(){
let gui = new dat.GUI();
gui.add(config, 'sensor_distance', 1, 100);
gui.add(config, 'sensor_angle', 0, PI);
gui.add(config, 'turning_speed', 0, PI);
gui.add(config, 'speed', 0, 40);
gui.add(config, 'decay_factor', 0, 1);
gui.add(config, 'deposit_amount', 0, 1);
gui.add(config, 'num_agents', 1, 20000);
gui.add(config, 'start_in_circle');
//gui.add(config, 'highlight_agents');
gui.add(config, 'random_turning');
gui.add(config, 'wrap_around');
let obj = { reset_sim: function () {
frameCount = 0;
physarum.reset();
regenerate();
}}
gui.add(obj, 'reset_sim');
}
function regenerate() {
const stepPerFrame = 200;
//random agents
for (let i=0; i<config.num_agents/stepPerFrame; ++i) {
physarum.addAgent(
random(W),
random(H),
random(TWO_PI)
);
}
//text agents
for (let i=0; i<config.num_agents/stepPerFrame; ++i) {
physarum.addAgent(
random(50, W-50),
random(H*0.4, H*0.6),
random(TWO_PI)
);
}
//circular agents
const radius = min(W, H) * 0.3;
for (let i=0; i<config.num_agents/stepPerFrame; ++i) {
const angle = random(TWO_PI);
physarum.addAgent(
W * 0.5 + cos(angle) * radius,
H * 0.5 + sin(angle) * radius,
angle+HALF_PI
);
}
}