xxxxxxxxxx
148
let raritySlider;
let clusterSizeSlider;
const noiseScale = 0.05;
const sz = 15;
const PerlinNoise = "PN";
const RandomWalk = "RW";
const Cluster = "CL";
let method = Cluster;
let debug = false;
let rarity = 0;
let clusterSize = 0;
function setup() {
createCanvas(600, 600);
raritySlider = createSlider(0.01, 1, 0.5, 0.01);
clusterSizeSlider = createSlider(1, 10, 5, 1);
noStroke();
}
function draw() {
if(rarity !== raritySlider.value() ||
clusterSize !== clusterSizeSlider.value()) {
rarity = raritySlider.value();
clusterSize = clusterSizeSlider.value();
if(method !== PerlinNoise) {
background(0);
}
for(let i = 0; i < width/sz; i ++) {
for(let j = 0; j < height/sz; j ++) {
if(method === RandomWalk) {
randomWalk(i, j);
} else if(method === PerlinNoise) {
perlinMethod(i, j);
} else if(method === Cluster) {
cluster(i, j);
}
}
}
}
}
function keyReleased() {
if(key === ' ') {
rarity = -1;
noiseSeed(millis());
} else if(key === 'p') {
method = PerlinNoise;
console.log(method);
rarity = -1;
} else if(key === 'r') {
method = RandomWalk;
console.log(method);
rarity = -1;
} else if(key === 'c') {
method = Cluster;
console.log(method);
rarity = -1;
} else {
rarity = -1;
debug = !debug;
}
}
function cluster(x, y) {
const seed = (int)(y) << 16 | (int)(x);
const rand = mulberry32(seed);
if(rand() < rarity/10) {
fill(255);
const cx = x * sz;
const cy = y * sz;
for(let i = 0; i < clusterSize; i ++) {
const a = rand() * TAU;
const r = rand() * sz;
const nx = cx + cos(a) * r;
const ny = cy + sin(a) * r;
const cr = rand() * 4 + 2;
circle(nx, ny, cr);
}
}
}
function randomWalk(x, y) {
const seed = (int)(y) << 16 | (int)(x);
const rand = mulberry32(seed);
fill(255);
if(rand() < rarity/100) {
// start walk
for(let i = 0; i < clusterSize; i ++) {
rect(x * sz, y * sz, sz, sz);
const dir = (int)(rand() * 4);
if(dir === 0) x += 1;
if(dir === 1) x -= 1;
if(dir === 2) y += 1;
if(dir === 3) y -= 1;
if(dir === 4) console.log("fuck");
}
}
}
function perlinMethod(x, y) {
if(debug) {
fill(noiseValue(x, y) * 255);
} else {
fill(0);
if(isOre(x, y)) {
fill(255);
}
}
rect(x * sz, y * sz, sz, sz);
}
function isOre(x, y) {
const n = noiseValue(x, y);
return n > (1 - rarity/2);
}
function noiseValue(x, y) {
const n = noise(x * noiseScale, y * noiseScale);
return n;
}
// rng
function mulberry32(a) {
return function() {
var t = a += 0x6D2B79F5;
t = Math.imul(t ^ t >>> 15, t | 1);
t ^= t + Math.imul(t ^ t >>> 7, t | 61);
return ((t ^ t >>> 14) >>> 0) / 4294967296;
}
}