Skip to content

Instantly share code, notes, and snippets.

@murilopolese
Last active February 6, 2017 14:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save murilopolese/3d7de2f5ef30d36737273567ff96795e to your computer and use it in GitHub Desktop.
Save murilopolese/3d7de2f5ef30d36737273567ff96795e to your computer and use it in GitHub Desktop.
SeriousBusiness RFF
import controlP5.*;
ControlP5 cp5;
// Control visuals
int controlHeight = 20;
int controlWidth = 200;
int controlPadding = 10;
int sMaxParticles = 10000;
int sMinParticles = 1000;
int sInitialParticles = 5000;
int sParticles = sInitialParticles;
int sMaxNoiseScale = 10000;
int sMinNoiseScale = 100;
int sInitialNoiseScale = 5000;
int sNoiseScale = sInitialNoiseScale;
float sMinNoiseStrength = 1;
float sMaxNoiseStrength = 10;
float sInitialNoiseStrength = 5;
float sNoiseStrength = sInitialNoiseStrength; // Turbulence
float sMaxLength = 15;
float sMinLength = .5;
float sInitialLength = 5;
float sLength = sInitialLength;
float sMinThickness = 0.5; // minimum thicknes
float sMaxThickness = 2; // maximum thickness
float sMinThicknessScale = 0.2;
float sMaxThicknessScale = 4;
float sInitialThicknessScale = 1;
float sThicknessScale = sInitialThicknessScale;
int sInitialWidth = 877;
int sWidth = sInitialWidth;
int sInitialHeight = 1241;
int sHeight = sInitialHeight;
Scene scene;
boolean saving = false;
void setup() {
size( 1024, 768 );
noStroke();
hint(DISABLE_ASYNC_SAVEFRAME);
//Controllers
cp5 = new ControlP5( this );
cp5.addSlider( "sParticles" )
.setPosition( 10, 0 )
.setHeight( 20 )
.setWidth( 200 )
.setRange( sMinParticles, sMaxParticles )
.setValue( sInitialParticles )
;
cp5.addSlider("sNoiseScale")
.setPosition( 10, 30 )
.setHeight( 20 )
.setWidth( 200 )
.setRange( sMinNoiseScale, sMaxNoiseScale )
.setValue( sInitialNoiseScale )
;
cp5.addSlider("sNoiseStrength")
.setPosition( 10, 60 )
.setHeight( 20 )
.setWidth( 200 )
.setRange( sMinNoiseStrength, sMaxNoiseStrength )
.setValue( sInitialNoiseStrength )
;
cp5.addSlider("sLength")
.setPosition( 10, 90 )
.setHeight( 20 )
.setWidth( 200 )
.setRange( sMinLength, sMaxLength )
.setValue( sInitialLength )
;
cp5.addSlider("sThicknessScale")
.setPosition( 10, 120 )
.setHeight( 20 )
.setWidth( 200 )
.setRange( sMinThicknessScale, sMaxThicknessScale )
.setValue( sInitialThicknessScale )
;
cp5.addNumberbox("sWidth")
.setPosition( 10, 150 )
.setHeight( 20 )
.setWidth( 200 )
.setValue( sInitialWidth )
;
cp5.addNumberbox("sHeight")
.setPosition( 10, 180 )
.setHeight( 20 )
.setWidth( 200 )
.setValue( sInitialHeight )
;
cp5.addBang("sReset")
.setPosition( 10, 210 )
.setSize( 20, 20 )
;
scene = new Scene( sWidth, sHeight, sParticles );
}
void draw() {
background( 0 );
scene.run();
imageMode( CENTER );
// If canvas is bigger than screen
if(
scene.resolution.x > width
|| scene.resolution.y > height
) {
if( scene.resolution.x > scene.resolution.y ) {
float screenRatio = width / scene.resolution.x;
image(
scene.canvas,
width/2, height/2,
width, scene.resolution.y*screenRatio
);
} else {
float screenRatio = height / scene.resolution.y;
image(
scene.canvas,
width/2, height/2,
scene.resolution.x*screenRatio, height
);
}
} else {
// If canvas is smaller than screen
image( scene.canvas, width/2, height/2 );
}
}
void keyPressed() {
}
void sParticles( int n ) {
sParticles = n;
scene.nParticles = n;
}
void sNoiseScale( int n ) {
sNoiseScale = n;
scene.noiseScale = n;
}
void sNoiseStrength( float n ) {
sNoiseStrength = n;
scene.noiseStrength = n;
}
void sLength( float n ) {
sLength = n;
scene.dragLength = n;
}
void sThicknessScale( float n ) {
sThicknessScale = n;
scene.thicknessScale = n;
}
void sReset() {
scene = new Scene( sWidth, sHeight, sParticles );
scene.loadP5Values();
}
class Particle {
PVector loc, dir, vel;
float speed;
int d = 1; // direction change
color col;
Particle(PVector _loc, PVector _dir, float _speed) {
loc = _loc;
dir = _dir;
speed = _speed;
}
void move() {
// Default values
this.move( 5000, 5, frameCount );
}
void move( int noiseScale, float noiseStrength, int time ) {
float angle = noise(
loc.x/noiseScale,
loc.y/noiseScale,
time/noiseScale
) * TWO_PI*noiseStrength;
dir.x = cos(angle);
dir.y = sin(angle);
vel = dir.get();
vel.mult(speed*d);
loc.add(vel);
}
}
class Scene {
PGraphics canvas;
ArrayList<Particle> particles;
PVector resolution;
float ratio = 1;
int time = (int)random(0,100000);
int nParticles = 5000; // Number of particles
int noiseScale = 5000;
float noiseStrength = 5;
float maxDragLength = sMaxLength;
float dragLength = 5;
float minThickness = 0.5;
float maxThickness = 2;
float minSpeed = 0.5;
float maxSpeed = 2;
float thicknessScale = 1;
Scene( int width, int height, int nParticles ) {
this.resolution = new PVector( width, height );
this.ratio = width / height;
this.canvas = createGraphics(
(int) this.resolution.x,
(int) this.resolution.y
);
this.nParticles = nParticles;
this.particles = new ArrayList();
this.generateParticles();
}
void loadP5Values() {
this.thicknessScale = sThicknessScale;
this.dragLength = sLength;
this.noiseScale = sNoiseScale;
this.noiseStrength = sNoiseStrength;
}
void generateParticles() {
this.particles.clear();
for( int i = 0; i < this.nParticles; i++ ) {
// Position
PVector loc = new PVector(
random( this.resolution.x ),
random( this.resolution.y ),
random( this.minThickness, this.maxThickness ) // Thickness
);
// Direction/Velocity
float angle = random( TWO_PI );
PVector dir = new PVector( cos( angle ), sin( angle ) );
float speed = random( this.minSpeed, this.maxSpeed );
this.particles.add( new Particle( loc, dir, speed ) );
}
}
void checkEdges( Particle p ) {
if (p.loc.x<0 || p.loc.x>this.resolution.x || p.loc.y<0 || p.loc.y>this.resolution.y) {
p.loc.x = random(this.resolution.x*1.2);
p.loc.y = random(this.resolution.y);
}
}
void run() {
this.time++;
this.canvas.beginDraw();
this.canvas.noStroke();
this.canvas.fill( 0, this.maxDragLength - this.dragLength );
this.canvas.rect( 0, 0, this.resolution.x, this.resolution.y );
this.canvas.fill(255);
for( int i = 0; i < this.particles.size(); i++ ) {
if( i < this.nParticles ) {
Particle p = (Particle) this.particles.get( i );
p.move( this.noiseScale, this.noiseStrength, this.time );
checkEdges( p );
this.canvas.ellipse(
p.loc.x, p.loc.y, // Position
p.loc.z*this.thicknessScale, p.loc.z*this.thicknessScale // Size
);
}
}
this.canvas.endDraw();
if( keyPressed == true && key == 's' ) {
// if( saving == false ) {
saving = true;
println("Saving image export-"+frameCount+".png");
println(scene.canvas);
PImage img = scene.canvas.get();
img.save("export-"+frameCount+".png");
// }
// } else {
// saving = false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment