Thursday, March 3, 2011

Perlin Noise demo with Processing

#copyright @ http://scriptdemo.blogspot.com
// inspired by the Perlin Noise Particle System from Daan van Hasselt (http://www.openprocessing.org/visuals/?visualID=10475)
// I recoded it with more additional features. Also it may be easier to understand without the class.
// Online test  from my dropbox (my Dropbox reference link)
float myw=400.0;
float myh=350.0;
float x, y;
float u=0;
float v=0;
float x0, y0, dx, dy;
float coefx, coefy, coefz;
boolean isAlive=false;
boolean isColor=true;
boolean isSmooth=true;
boolean isLoop=true;

int NumP=400;
float Npi;
void setup()
{
  Npi=2.3; coefx=0.009; coefy=0.011; coefz=0.0013;
  size(floor(myw),floor(myh));
  background(255,255,255);
  if (isSmooth) smooth();
}

void draw()
{

  for (int n=0; n<=NumP; n++)
  {
      if (isAlive)
      {
        float nv=noise(x*coefx,y*coefy,frameCount*coefz);
        if (isColor)
        {
           float mysin=sin((nv-0.3)*TWO_PI*Npi);
           float mycos=cos((nv-0.3)*TWO_PI*Npi);
           if (mycos>=0 & mysin >=0) fill(floor(abs(mysin)*255),floor(abs(mycos)*255),0,10);
           if (mycos>=0 & mysin <0) fill(floor(abs(mysin)*255),0,floor(abs(mycos)*255),10);
           if (mycos<0 & mysin >=0) fill(0,floor(abs(mycos)*255),floor(abs(mysin)*255),10);
           if (mycos<0 & mysin <0) fill(floor(abs(mysin)*255),floor(abs(mycos)*255),0,10);
        } else {
           fill(10,10);
        }
         u=u+cos((nv-0.3)*TWO_PI*Npi);
         v=v+sin((nv-0.3)*TWO_PI*Npi);
         u=u/2;
         v=v/2;
         x=x+u;
         y=y+v;
      } else {
        x0=0.5*myw;
        y0=0.5*myh;
        x=x0; y=y0;
        dx=100*frameCount/2000+50;
        dy=dx;
        isAlive=true;
      }
      noStroke();
      ellipse(x,y,1,1);
  }
  if (x>width || y> height || x <0 || y< 0) isAlive=false;
}

void mouseClicked()
{
  isLoop=!isLoop;
  if (!isLoop)
  {
     noLoop();
  } else {
     loop();
  }
}
Here are some demo videos:

No comments:

ShowCalendar