xxxxxxxxxx
95
let noiseamp = 10;
let steps = 4000;
let noiseres = 0.008;
let trunkWidth = 40;
let offset;
let xStep = trunkWidth/steps;
function setup() {
createCanvas(400, 400);
background(220);
offset = {x:width/2 - trunkWidth/2,y:200}
createButton('new noise texture').mousePressed(() => {noiseSeed(random()*1000) }).position(10,460);
slider = createSlider(0, 0.1,0.02,0.001);
slider.position(10, 410);
slider.size(380);
slider2 = createSlider(0, 100,6);
slider2.position(10, 430);
slider2.size(380);
}
function generateField(){
// so here we wanna make it generate intereseting fields that are actually kinda tree like.
// visualize field:
background(255);
for(let x=10;x<width;x+=20){
for(let y=10;y<height;y+=20){
let dir = sampleField(x,y)
line(x,y,x+dir.x,y+dir.y);
}
}
}
function sampleField(x,y){
return {
x: (noise(x*noiseres, y*noiseres) - 0.5) * noiseamp,
y: (noise(x*noiseres+100, y*noiseres) - 0.5) * noiseamp
}
}
function growIt(){
background(255)
let coords = [{x:offset.x,y:offset.y}];
let tadjust = 0;
for(let t=1;t<steps;t++){
let noisevalx;
let noisevaly;
if(t%(50*PI)>25*PI && tadjust < t-1){
tadjust += 2
noisevalx = -1 * noise((coords[t-tadjust].x)*noiseres, coords[t-tadjust].y * noiseres) + 0.5;
noisevaly = -1 * noise((coords[t-tadjust].x)*noiseres+100, coords[t-tadjust].y * noiseres) + 0.5;
}
else{
tadjust = 0;
noisevalx = noise((coords[t-1].x)*noiseres,coords[t-1].y*noiseres) - 0.5;
noisevaly = noise((coords[t-1].x)*noiseres+100,coords[t-1].y*noiseres) - 0.5 ;
}
coords.push({
x: coords[t-1].x + xStep + abs(sin(0.02*t))**2 * noiseamp * noisevalx,
y: coords[t-1].y + 3 * cos(0.02*t) + abs(sin(0.02*t))**2 * noiseamp * noisevaly
})
// line(coords[t-1].x,coords[t-1].y,coords[t].x,coords[t].y);
point(coords[t].x,coords[t].y);
}
line (50,height/2,350,height/2);
}
function draw(){
noiseamp = slider2.value();
noiseres = slider.value();
// growIt();
generateField();
}