Skip to content

Instantly share code, notes, and snippets.

@manoloide
Last active February 7, 2019 03:58
Show Gist options
  • Save manoloide/4dbbb4f8f0e299f853bd6adc5b528e87 to your computer and use it in GitHub Desktop.
Save manoloide/4dbbb4f8f0e299f853bd6adc5b528e87 to your computer and use it in GitHub Desktop.
int seed = int(random(999999));
float nwidth = 960;
float nheight = 960;
float swidth = 960;
float sheight = 960;
float scale = 1;
boolean export = false;
void settings() {
scale = nwidth/swidth;
size(int(swidth*scale), int(sheight*scale), P3D);
smooth(8);
pixelDensity(2);
}
void setup() {
generate();
if (export) {
saveImage();
exit();
}
}
void draw() {
}
void keyPressed() {
if (key == 's') saveImage();
else {
seed = int(random(999999));
generate();
}
}
void generate() {
randomSeed(seed);
noiseSeed(seed);
//background(lerpColor(rcol(), color(0), random(40)));
background(250);
DoubleNoise dn = new DoubleNoise();
dn.noiseDetail(9);
float des = random(1000);
float det = random(0.0006, 0.0008)*0.8;
float amp = random(16, 20)*random(0.5, 1);
float desLar = random(10000);
float detLar = random(0.001);
noFill();
for (int i = 0; i < 20000; i++) {
double x = width*random(0.05, 0.95);
double y = height*random(0.05, 0.95);
fill(0, random(100));
ellipse((float)x, (float)y, 2, 2);
noFill();
beginShape();
float ic = random(TAU);
float dc = random(0.1)*random(1);
float lar = 80*noise(desLar+(float)x*detLar, desLar+(float)y*detLar)-20;
for (int j = 0; j < lar; j++) {
stroke(80, 50+cos(ic+j*dc)*40);
double a = dn.noise(dn.noise(des+x*det, des+y*det)*amp*3)*amp;
x += Math.cos(a);
y += Math.sin(a);
vertex((float)x, (float)y);
}
double lx = x;
double ly = y;
stroke(random(255)*random(1), 20);
float lar2 = random(26, 32);
for (int j = 0; j < lar2; j++) {
x = lx;
y = ly;
float da = random(TAU);
float v1 = random(0.5, 1.5);
float lar3 = random(4, 12);
for (int k = 0; k < lar3; k++) {
double a = dn.noise(dn.noise(des+x*det*10, des+y*det*10)*amp*3)*amp+da;
x += Math.cos(a)*v1;
y += Math.sin(a)*v1;
vertex((float)x, (float)y);
}
}
endShape();
}
}
void saveImage() {
String timestamp = year() + nf(month(), 2) + nf(day(), 2) + "-" + nf(hour(), 2) + nf(minute(), 2) + nf(second(), 2);
saveFrame(timestamp+"-"+seed+".png");
}
//int colors[] = {#121B4B, #028594, #016C40, #FBAF34, #CF3B13, #E55E7F, #F0D5CA};
//int colors[] = {#ffffff, #B0E7FF, #143585, #5ACAA2, #D08714, #F98FC0};
//int colors[] = {#77ABC1, #669977, #DD9931, #AA3320, #33221F, #CE7353, #BC6657, #97AD67, #CC3211, #9D6A7F};
//int colors[] = {#043387, #0199DC, #BAD474, #FBE710, #FFE032, #EB8066, #E7748C, #DF438A, #D9007E, #6A0E80, #242527, #FCFCFA};
int colors[] = {#043387, #0199DC, #BAD474, #FBE710, #FFE032, #EB8066, #E7748C, #DF438A, #D9007E, #6A0E80};
//int colors[] = {#687FA1, #AFE0CD, #FDECB4, #F63A49, #FE8141};
int rcol() {
return colors[int(random(colors.length))];
}
int getColor() {
return getColor(random(colors.length));
}
int getColor(float v) {
v = abs(v);
v = v%(colors.length);
int c1 = colors[int(v%colors.length)];
int c2 = colors[int((v+1)%colors.length)];
return lerpColor(c1, c2, pow(v%1, random(1.4, 2.4)));//2.4)));
}
//https://github.com/postspectacular/toxiclibs/blob/master/src.core/toxi/math/noise/PerlinNoise.java
import java.util.Random;
class DoubleNoise {
static final int PERLIN_YWRAPB = 4;
static final int PERLIN_YWRAP = 1<<PERLIN_YWRAPB;
static final int PERLIN_ZWRAPB = 8;
static final int PERLIN_ZWRAP = 1<<PERLIN_ZWRAPB;
static final int PERLIN_SIZE = 4095;
int perlin_octaves = 4; // default to medium smooth
float perlin_amp_falloff = 0.5f; // 50% reduction/octave
// [toxi 031112]
// new vars needed due to recent change of cos table in PGraphics
int perlin_TWOPI, perlin_PI;
float[] perlin_cosTable;
float[] perlin;
float sinLUT[];
float cosLUT[];
float SINCOS_PRECISION=0.5;
int SINCOS_LENGTH= int((360.0/SINCOS_PRECISION));
Random perlinRandom;
DoubleNoise() {
// Fill the tables
sinLUT=new float[SINCOS_LENGTH];
cosLUT=new float[SINCOS_LENGTH];
for (int i = 0; i < SINCOS_LENGTH; i++) {
sinLUT[i]= (float)Math.sin(i*DEG_TO_RAD*SINCOS_PRECISION);
cosLUT[i]= (float)Math.cos(i*DEG_TO_RAD*SINCOS_PRECISION);
}
}
public double noise(double x) {
// is this legit? it's a dumb way to do it (but repair it later)
return noise(x, 0f, 0f);
}
public double noise(double x, double y) {
return noise(x, y, 0f);
}
public double noise(double x, double y, double z) {
if (perlin == null) {
if (perlinRandom == null) {
perlinRandom = new Random();
}
perlin = new float[PERLIN_SIZE + 1];
for (int i = 0; i < PERLIN_SIZE + 1; i++) {
perlin[i] = perlinRandom.nextFloat(); //(float)Math.random();
}
// [toxi 031112]
// noise broke due to recent change of cos table in PGraphics
// this will take care of it
perlin_cosTable = cosLUT;
perlin_TWOPI = perlin_PI = SINCOS_LENGTH;
perlin_PI >>= 1;
}
if (x<0) x=-x;
if (y<0) y=-y;
if (z<0) z=-z;
int xi=(int)x, yi=(int)y, zi=(int)z;
double xf = x - xi;
double yf = y - yi;
double zf = z - zi;
double rxf, ryf;
double r=0;
double ampl=0.5f;
double n1, n2, n3;
for (int i=0; i<perlin_octaves; i++) {
int of=xi+(yi<<PERLIN_YWRAPB)+(zi<<PERLIN_ZWRAPB);
rxf=noise_fsc(xf);
ryf=noise_fsc(yf);
n1 = perlin[of&PERLIN_SIZE];
n1 += rxf*(perlin[(of+1)&PERLIN_SIZE]-n1);
n2 = perlin[(of+PERLIN_YWRAP)&PERLIN_SIZE];
n2 += rxf*(perlin[(of+PERLIN_YWRAP+1)&PERLIN_SIZE]-n2);
n1 += ryf*(n2-n1);
of += PERLIN_ZWRAP;
n2 = perlin[of&PERLIN_SIZE];
n2 += rxf*(perlin[(of+1)&PERLIN_SIZE]-n2);
n3 = perlin[(of+PERLIN_YWRAP)&PERLIN_SIZE];
n3 += rxf*(perlin[(of+PERLIN_YWRAP+1)&PERLIN_SIZE]-n3);
n2 += ryf*(n3-n2);
n1 += noise_fsc(zf)*(n2-n1);
r += n1*ampl;
ampl *= perlin_amp_falloff;
xi<<=1;
xf*=2;
yi<<=1;
yf*=2;
zi<<=1;
zf*=2;
if (xf>=1.0f) {
xi++;
xf--;
}
if (yf>=1.0f) {
yi++;
yf--;
}
if (zf>=1.0f) {
zi++;
zf--;
}
}
return r;
}
// [toxi 031112]
// now adjusts to the size of the cosLUT used via
// the new variables, defined above
private double noise_fsc(double i) {
// using bagel's cosine table instead
return 0.5*(1.0f-perlin_cosTable[(int)(i*perlin_PI)%perlin_TWOPI]);
}
public void noiseDetail(int lod) {
if (lod>0) perlin_octaves=lod;
}
public void noiseDetail(int lod, float falloff) {
if (lod>0) perlin_octaves=lod;
if (falloff>0) perlin_amp_falloff=falloff;
}
public void noiseSeed(long seed) {
if (perlinRandom == null) perlinRandom = new Random();
perlinRandom.setSeed(seed);
// force table reset after changing the random number seed [0122]
perlin = null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment