xxxxxxxxxx
199
let brush;
let generationVectors;
let genArr;
let genArrSizeX = 32;
let genArrSizeY = 32;
let toggleView = true;
let thresholdColors;
function setup() {
createCanvas(600, 600);
InitButtons();
InitGenerationVectors();
InitGenerationArray();
InitColors();
}
// these are the generation-setting-vectors
// the first entry is the scale of the warping
// the second entry is a offset, which is applied after
function InitGenerationVectors(){
generationVectors = [];
generationVectors[0] = [1, -0.35]; // scale 1, offset -0.25
generationVectors[1] = [2, -0.1]; // scale 2, offset 0
generationVectors[2] = [3, +0.0]; // scale 3, offset 0.1
generationVectors[3] = [4.5, +0.25]; // scale 1.5, offset 0.25
}
function Generate(){
noiseSeed(random()*100000); // change the seed so we get some different pictures
loadPixels();
let r = width/genArrSizeX; // scaling factor
let x, y; // coordinates of generation vector we want
let xFloor, yFloor; // coordinates at the topleft corner of the cell
let xp, yp; // coordinates at the bottomright corner of the cell
for(let i = 0; i < width; i++){
for(let j = 0; j < height; j++){
// mapping pixel space to the space where generation vectors live
x = i / r;
y = j / r;
xFloor = floor(x);
yFloor = floor(y);
xp = xFloor+1;
yp = yFloor+1;
// try getting the surrounding 4 gen-vectors at the corners of the cell
/*
g0 ----- g1
| |
| (x,y) |
| |
g2 ----- g3
*/
let g0 = generationVectors[genArr[xFloor][yFloor]];
let g1;
let g1CanExist = xp < genArrSizeX;
let g2;
let g2CanExist = yp < genArrSizeY;
let g3;
let g3CanExist = g1CanExist && g2CanExist;
if(g1CanExist)
g1 = generationVectors[genArr[xp][yFloor]];
if(g2CanExist)
g2 = generationVectors[genArr[xFloor][yp]];
if(g3CanExist)
g3 = generationVectors[genArr[xp][yp]];
// lerp between g0 and g1
if(g1CanExist)
g0 = lerpArrays(g0, g1, x - xFloor);
// lerp between g2 and g3
if(g3CanExist)
g2 = lerpArrays(g2, g3, x - xFloor);
// lerp between the results of the previous lerps
if(g2CanExist)
g0 = lerpArrays(g0, g2, y - yFloor);
// use the lerped vector as input to the noise function
set(i, j, ThresholdColor(myNoise(i, j, g0)));
}
}
updatePixels();
toggleView = false;
}
function myNoise(x, y, g){
let scale = g[0];
let offset = g[1];
x = x * 0.02;
y = y * 0.02;
return offset + noise(
10 + x + scale * noise(x + 10, y + 15),
15 + y + scale * noise(y + 10, x + 15));
}
function draw() {
if(toggleView){
DrawGenerationVectors();
}
if(mouseIsPressed){
let r = width/genArrSizeX;
let x = floor(mouseX / r);
let y = floor(mouseY / r);
if(x >= 0 && x < genArrSizeX && y >= 0 && y < genArrSizeY)
genArr[x][y] = brush;
}
}
function DrawGenerationVectors(){
let scale = width/genArrSizeX;
noStroke();
for(let i = 0; i < genArrSizeX; i++){
for(let j = 0; j < genArrSizeY; j++){
fill(map(genArr[i][j], 0, generationVectors.length, 0, 255));
square(i * scale, j * scale, scale);
}
}
let r = width/genArrSizeX;
let x = floor(mouseX / r);
let y = floor(mouseY / r);
fill(map(brush, 0, generationVectors.length, 0, 255));
stroke(255);
if(x >= 0 && x < genArrSizeX && y >= 0 && y < genArrSizeY)
square(x*scale, y*scale, scale);
}
function lerpArrays(arrA, arrB, val){
let ret = [];
for(let i = 0; i < arrA.length; i++){
ret[i] = lerp(arrA[i], arrB[i], val);
}
return ret;
}
function InitGenerationArray(){
genArr = [];
for(let i = 0; i < genArrSizeX; i++){
genArr[i] = [];
for(let j = 0; j < genArrSizeY; j++){
let dx = i - genArrSizeX/2;
let dy = j - genArrSizeY/2;
let d = sqrt(dx*dx+dy*dy);
let index = floor(map(d, 0, genArrSizeX/2*sqrt(2), 0, generationVectors.length));
index = constrain(index, 0, generationVectors.length - 1);
genArr[i][j] = index;
}
}
}
function InitButtons(){
brush = 0;
let btn0 = createButton("Brush 0");
btn0.mousePressed(()=>{brush = 0; toggleView = true;});
let btn1 = createButton("Brush 1");
btn1.mousePressed(()=>{brush = 1; toggleView = true;});
let btn2 = createButton("Brush 2");
btn2.mousePressed(()=>{brush = 2; toggleView = true;});
let btn3 = createButton("Brush 3");
btn3.mousePressed(()=>{brush = 3; toggleView = true;});
let btnGenerate = createButton("Generate");
btnGenerate.position(530, 600);
btnGenerate.mousePressed(()=>{Generate();});
}
function ThresholdColor(thresholdValue) {
for (let k = 0; k < thresholdColors.length; k++)
if (thresholdValue < thresholdColors[k].val)
return thresholdColors[k].col;
}
function InitColors(){
thresholdColors = [
{ val: 0.05, col: color(9, 15, 32) },
{ val: 0.13, col: color(14, 27, 65) },
{ val: 0.2, col: color(20, 40, 90) },
{ val: 0.3, col: color(35, 89, 164) },
{ val: 0.35, col: color(208, 218, 145) },
{ val: 0.45, col: color(117, 167, 67) },
{ val: 0.55, col: color(46, 101, 44) },
{ val: 0.65, col: color(46, 90, 44) },
{ val: 0.7, col: color(129, 151, 150) },
{ val: 0.75, col: color(189, 191, 190) },
{ val: Infinity, col: color(255, 255, 255) }];
}