• fullscreen
  • classSilkCloth.pde
  • keyboard.pde
  • n01_web.pde
  • class SilkCloth
    {
      int X, Y, Z, totalSize, resolution, a;
      float spacing, nx, ny, wx, wy, WX, WY, m;
    
      PVector [] [] cloth;
    
      boolean standartMode = false, altMode1 = false, altMode2 = false, altMode3 = false, altMode4 = false, altMode5 = false, altMode6 = true, altMode7 = false, altMode8 = false, altMode9 = false;
      boolean wireFrame = false;
    
      color standart, highlight;
    
      SilkCloth(int x_, int y_, int z_, int totalSize_, int resolution_, float nx_, float ny_, float wx_, float wy_, float m_)
      {
        X = x_;
        Y = y_;
        Z = z_;
        totalSize = totalSize_;
        resolution = resolution_;
        spacing = (float) totalSize / (float) resolution;
        nx = nx_;
        ny = ny_;
        wx = wx_;
        wy = wy_;
        WX = wx;
        WY = wy;
        m = m_;
    
        noiseDetail ((int) spacing,nx);
    
        a = 255;
        standart = color (180,a);
        highlight = color (13, 116, 162,a);
    
        setupCloth();
        createCloth ();
      }
    
      void display()
      {
        if (!wireFrame) {
          noStroke();
          // fill (124,11,103,255);
          fill (standart);
        }
    
        else {
          noLights();
          noFill();
          stroke (0,50);
        }
    
        for (int i = 0; i < resolution; i++)
        { 
    
          if (!wireFrame && altMode2) {
            if (i >= 25 && i < 27) fill (highlight);
            else fill (standart);
          }
    
          if (!wireFrame && altMode4) {
            if (i >= 11 && i < 13) fill (highlight);
            else fill (standart);
          } 
    
          if (!wireFrame && altMode8) {
            if (i >= 5 && i < 7) fill (highlight);
            else fill (standart);
          } 
    
          if (!wireFrame && altMode9) {
            if (i >= resolution - 12 && i < resolution - 10) fill (highlight);
            else fill (standart);
          } 
    
          for (int j = 0; j < resolution; j++)
          {
    
            if (!wireFrame && altMode1) {
              if (i >= resolution-10 && j >  resolution-8 && j <  resolution-5) fill (highlight);
              else fill (standart);
            }
    
            if (!wireFrame && altMode3) {
              if (i > 80 && j >  resolution-8 && j <  resolution-5) fill (highlight);
              else fill (standart);
            }
    
            if (!wireFrame && altMode6) {
              if (j >  resolution-25 && j <  resolution-22) fill (highlight);
              else fill (standart);
            }
    
    
            PVector p1 = cloth [i] [j];
            PVector p2 = cloth [i+1] [j];
            PVector p3 = cloth [i+1] [j+1];
            PVector p4 = cloth [i] [j+1];
    
            beginShape(TRIANGLE_STRIP);
            vertex (p1.x,p1.y,p1.z * m);
            vertex (p4.x,p4.y,p4.z * m);
            vertex (p2.x,p2.y,p2.z * m);
            vertex (p3.x,p3.y,p3.z * m);
            endShape ();
          }
        }
      }
    
      void setWind (int frameCounter)
      {
    
        wx += WX;
        wy += WY;
        updateCloth ();
      }
    
      void updateCloth ()
      {
        float angle = 0.0;
        float steps = TWO_PI/totalSize;
        if (altMode6) steps = TWO_PI/(resolution+1.2);
        float x = 0, y = 0, z = 0;
        float maxD = dist (0,0,(resolution+1)/2, (resolution+1)/2);
    
    
        float timeX = wx;
        for (int i = 0; i < resolution+1; i++)
        {
    
          float timeY = wy;
    
          for (int j = 0; j < resolution+1; j++)
          {
            float nV = noise (timeX, timeY, timeX+timeY); 
    
            if(standartMode) {
              x = X+i*spacing-totalSize/2;
              y = Y+j*spacing-totalSize/2;
              z = nV;
            }
    
            if(altMode1 ) {
              x = cos (angle) * (totalSize*(j+1)/180+1);
              y = sin (angle) * (totalSize*(j+1)/180+1);
              z = nV*(i+1)/40;
            }
    
            if(altMode2) {
              x = cos (angle) * (totalSize*(i+1)/180+1);
              y = sin (angle) * (totalSize*(i+1)/180+1);
              z = nV*(i+1)/30;
            }
    
            if(altMode3) {
              x = cos (angle) * (totalSize*(j+1)/180+1);
              y = sin (angle) * (totalSize*(i+1)/180+1);
              z = nV*(j+i+1)/60;
            }
    
            if(altMode4) {
              x = ( ( cos (angle) * (totalSize/2) ) + ( X+i*spacing-totalSize/2 ) ) / 2;
              y = ( ( sin (angle) * (totalSize/2) ) + ( Y+j*spacing-totalSize/2 ) ) / 2;
              z = nV*2*sin (nV+(i+1)/10)*cos (nV+(j+1)/10);
            }
    
            if(altMode5) {
              x = cos (angle) * (totalSize*nV);
              y = sin (angle) * (totalSize*nV);
              z = nV*(j+i)/50;
            }
    
            if(altMode6) {
              x = cos (angle) * (totalSize/10+i)*(nV)*7;
              y = sin (angle) * (totalSize/10+i)*(nV)*7;
              z =  nV*2;
            }
    
            if(altMode7) {
              x = cos (angle+angle) * (totalSize/2+nV+cos(angle)*20);
              y = sin (angle+angle) * (totalSize/2+nV+sin(angle)*20);
              z = nV*2;
    
              //x = cos (angle+angle) * (totalSize*i+nV+cos(angle)*16)/20;
              //y = sin (angle+angle) * (totalSize*i+nV+sin(angle*16))/20;
    
              //x = cos (angle+angle) * (totalSize+i+nV+cos(angle)*16)/j;
              //y = sin (angle+angle) * (totalSize+j+nV+sin(angle*16))/j;
            }
    
            if(altMode8) {
              float d = dist (i,j, (resolution+1)/2,(resolution+1)/2); 
              x = X+i*spacing-totalSize/2;
              y = Y+j*spacing-totalSize/2;
              z = 1-map (d, 0, maxD, 0, 1.0)+nV;
            }
    
            if(altMode9) {
              x = X+nV*(i+1)*spacing-totalSize/2+pow (cos (angle), nV) * totalSize/2;
              y = Y+nV*(j+1)*spacing-totalSize/2+pow (sin (angle), nV) * totalSize/2;
              z = nV*2;
            }
    
            cloth [i] [j] = new PVector (x,y,z);
    
            timeY += ny;
    
            if(altMode1 || altMode2 || altMode3 || altMode4 || altMode5 || altMode6 || altMode7 || altMode9 ) angle += steps;
          }
          timeX += nx;
        }
      }
    
      void setupCloth()
      {
        cloth = new PVector [resolution+1] [resolution+1];
      }
    
      void createCloth ()
      {
        float x = 0, y = 0, z = 0;
    
        float timeX = wx;
        for (int i = 0; i < resolution+1; i++)
        {
    
          float timeY = wy;
    
          for (int j = 0; j < resolution+1; j++)
          {
            float nV = noise (timeX, timeY, timeX+timeY); 
    
            x = X+i*spacing-totalSize/2;
            y = Y+j*spacing-totalSize/2;
            z = nV;
    
            cloth [i] [j] = new PVector (x,y,z);
    
            timeY += ny;
          }
          timeX += nx;
        }
      }
    
      void updateColor (int a_)
      {
        a = a_;
        standart = color (180,a);
        highlight = color (13, 116, 162,a);
      }
    }
    
    
    void keyPressed ()
    {
      if (keyCode == KeyEvent.VK_1)
      {
        silkCloth.standartMode = true;
        silkCloth.altMode1 = false;
        silkCloth.altMode2 = false;
        silkCloth.altMode3 = false;
        silkCloth.altMode4 = false;
        silkCloth.altMode5 = false;
        silkCloth.altMode6 = false;
        silkCloth.altMode7 = false;
        silkCloth.altMode8 = false;
        silkCloth.altMode9 = false;
    
        cam.setRotations (-radians (80), -radians (45), radians(5));
        cam.lookAt(0, -35, 11);
        cam.setDistance(850);
      }
    
      if (keyCode == KeyEvent.VK_2)
      {
        silkCloth.standartMode = false;
        silkCloth.altMode1 = true;
        silkCloth.altMode2 = false;
        silkCloth.altMode3 = false;
        silkCloth.altMode4 = false;
        silkCloth.altMode5 = false;
        silkCloth.altMode6 = false;
        silkCloth.altMode7 = false;
        silkCloth.altMode8 = false;
        silkCloth.altMode9 = false;
    
        cam.setRotations (-radians (45), 0, 0);
        cam.lookAt(0, -50, -50);
        cam.setDistance(1180);
      }
    
      if (keyCode == KeyEvent.VK_3)
      {
        silkCloth.standartMode = false;
        silkCloth.altMode1 = false;
        silkCloth.altMode2 = true;
        silkCloth.altMode3 = false;
        silkCloth.altMode4 = false;
        silkCloth.altMode5 = false;
        silkCloth.altMode6 = false;
        silkCloth.altMode7 = false;
        silkCloth.altMode8 = false;
        silkCloth.altMode9 = false;
    
        cam.setRotations (-radians (45), -radians (5), 0);
        cam.lookAt(-15, -30, 150);
        cam.setDistance(665);
      }
    
      if (keyCode == KeyEvent.VK_4)
      {
        silkCloth.standartMode = false;
        silkCloth.altMode1 = false;
        silkCloth.altMode2 = false;
        silkCloth.altMode3 = true;
        silkCloth.altMode4 = false;
        silkCloth.altMode5 = false;
        silkCloth.altMode6 = false;
        silkCloth.altMode7 = false;
        silkCloth.altMode8 = false;
        silkCloth.altMode9 = false;
    
        cam.setRotations (-radians (180), -radians (0), -radians (90));
        cam.lookAt(-5, -40, 165);
        cam.setDistance(1090);
      }
    
      if (keyCode == KeyEvent.VK_5)
      {
        silkCloth.standartMode = false;
        silkCloth.altMode1 = false;
        silkCloth.altMode2 = false;
        silkCloth.altMode3 = false;
        silkCloth.altMode4 = true;
        silkCloth.altMode5 = false;
        silkCloth.altMode6 = false;
        silkCloth.altMode7 = false;
        silkCloth.altMode8 = false;
        silkCloth.altMode9 = false;
    
        cam.setRotations (radians (135), radians (30), -radians (50));
        cam.lookAt(65, 10, 85);
        cam.setDistance(1045);
      }
    
      if (keyCode == KeyEvent.VK_6)
      {
        silkCloth.standartMode = false;
        silkCloth.altMode1 = false;
        silkCloth.altMode2 = false;
        silkCloth.altMode3 = false;
        silkCloth.altMode4 = false;
        silkCloth.altMode5 = true;
        silkCloth.altMode6 = false;
        silkCloth.altMode7 = false;
        silkCloth.altMode8 = false;
        silkCloth.altMode9 = false;
    
        cam.setRotations (-radians (35), -radians (50), radians (15));
        cam.lookAt(130, -103, 150);
        cam.setDistance(1270);
      }
    
      if (keyCode == KeyEvent.VK_7)
      {
        silkCloth.standartMode = false;
        silkCloth.altMode1 = false;
        silkCloth.altMode2 = false;
        silkCloth.altMode3 = false;
        silkCloth.altMode4 = false;
        silkCloth.altMode5 = false;
        silkCloth.altMode6 = true;
        silkCloth.altMode7 = false;
        silkCloth.altMode8 = false;
        silkCloth.altMode9 = false;
    
        cam.setRotations (-radians (0), -radians (0), -radians (45));
        cam.lookAt(0, 0, 140);
        cam.setDistance(960);
      }
    
      if (keyCode == KeyEvent.VK_8)
      {
        silkCloth.standartMode = false;
        silkCloth.altMode1 = false;
        silkCloth.altMode2 = false;
        silkCloth.altMode3 = false;
        silkCloth.altMode4 = false;
        silkCloth.altMode5 = false;
        silkCloth.altMode6 = false;
        silkCloth.altMode7 = true;
        silkCloth.altMode8 = false;
        silkCloth.altMode9 = false;
    
        cam.setRotations (-radians (90), -radians (25), radians (5));
        cam.lookAt(30, -30, 220);
        cam.setDistance(850);
      }
    
      if (keyCode == KeyEvent.VK_9)
      {
        silkCloth.standartMode = false;
        silkCloth.altMode1 = false;
        silkCloth.altMode2 = false;
        silkCloth.altMode3 = false;
        silkCloth.altMode4 = false;
        silkCloth.altMode5 = false;
        silkCloth.altMode6 = false;
        silkCloth.altMode7 = false;
        silkCloth.altMode8 = true;
        silkCloth.altMode9 = false;
    
        cam.setRotations (-radians (90), -radians (45), -radians (0));
        cam.lookAt(-30, -40, 100);
        cam.setDistance(890);
      }
    
      if (keyCode == KeyEvent.VK_0)
      {
        silkCloth.standartMode = false;
        silkCloth.altMode1 = false;
        silkCloth.altMode2 = false;
        silkCloth.altMode3 = false;
        silkCloth.altMode4 = false;
        silkCloth.altMode5 = false;
        silkCloth.altMode6 = false;
        silkCloth.altMode7 = false;
        silkCloth.altMode8 = false;
        silkCloth.altMode9 = true;
    
        cam.setRotations (-radians (5), -radians (5), -radians (20));
        cam.lookAt(-35, -30, 10);
        cam.setDistance(950);
      }
    
      if (keyCode == KeyEvent.VK_RIGHT) {
        int a = silkCloth.a + 10;
        a = constrain (a, 0,255);
        silkCloth.updateColor (a);
      }
      if (keyCode == KeyEvent.VK_LEFT) {
        int a = silkCloth.a - 10;
        a = constrain (a, 0,255);
        silkCloth.updateColor (a);
      }
    
      if (keyCode == KeyEvent.VK_N) saveFrame ("export/" + timestamp () + ".png");
      if (keyCode == KeyEvent.VK_W) silkCloth.wireFrame = !silkCloth.wireFrame;
      if (keyCode == KeyEvent.VK_P) pause = !pause ;
    
      if (keyCode == KeyEvent.VK_I)
      {
        float[] rotations = cam.getRotations(); 
        println ("rotation: " + degrees (rotations[0]) + ", " + degrees (rotations[1]) + ", " + degrees (rotations[2]));
        float[] position = cam.getPosition();
        println ("position: " + position[0] + ", " + position[1] + ", " + position[2]);
        float[] lookAt = cam.getLookAt();
        println ("lookAt: " + lookAt[0] + ", " + lookAt[1] + ", " + lookAt[2]);
        println ("distance: " + cam.getDistance());
      }
    }
    
    String timestamp()
    {
      String time, year, month, day, hour, minute, second;
    
      year = nf( year(), 4 );
      month = nf( month(), 2 );
      day = nf( day(), 2 );
      hour = nf( hour(), 2 );
      minute = nf( minute(), 2 );
      second = nf( second(), 2 );
    
      time = year + "_" + month + "_" + day + "_" + hour + "_" + minute + "_" + second;
    
      return time;
    }
    
    
    import processing.opengl.*;
    import peasy.test.*;
    import peasy.org.apache.commons.math.*;
    import peasy.*;
    import peasy.org.apache.commons.math.geometry.*;
    
    PeasyCam cam;
    SilkCloth silkCloth;
    
    PFont font;
    
    boolean pause = false;
    
    void setup()
    {
      size (1000, 600, P3D);
      frameRate (30);
    
      //setHintsForOPENGL ();
    
      silkCloth = new SilkCloth (0, 0, 0, 1000, 100, 0.017, 0.016, 0.008, 0.0075, 400);
    
      font = createFont ("Arial", 12);
    
      cam = new PeasyCam (this, 0, 0, 0, 1000);
      cam.setRotations (-radians (0), -radians (0), -radians (45));
      cam.lookAt(0, 0, 140);
      cam.setDistance(960);
    }
    
    void draw()
    {
      cam.beginHUD();
      background (247);
      lights();
      directionalLight(50, 50, 50, 0, 1, 0);
      lightSpecular(200, 200, 200);
      emissive (50,50,50);
      specular (12.0);
    
      cam.endHUD();
    
    
      //setHintsForOPENGL ();
    
      if (!pause) silkCloth.setWind(frameCount);
      silkCloth.display();
    
      //  cam.beginHUD();
      //  fill (120,0,0);
      //  textFont(font);
      //  text (frameRate,20, height-20);
      //  cam.endHUD();
    }
    
    
    
    void setHintsForOPENGL ()
    {
      //hint(DISABLE_DEPTH_TEST);
      hint(ENABLE_OPENGL_4X_SMOOTH);
      hint(DISABLE_OPENGL_2X_SMOOTH);
      hint(DISABLE_OPENGL_ERROR_REPORT);
    }
    
    

    code

    tweaks (0)

    about this sketch

    This sketch is running as Java applet, exported from Processing.

    license

    advertisement

    Diana Lange

    Cloth, Objects & Noise

    Add to Faves Me Likey@! 19
    You must login/register to add this sketch to your favorites.

    I've just played along with the values of my 'cloth' and suddenly these came out.

    Of course, looks much better and works much faster with OpenGL.

    Controls:
    PeasyCam
    0 - 9: toggle between the objects
    w = wireframe on /off
    p = pause on /off
    left / right arrow = reduce / increase alpha

    Watch the video: http://vimeo.com/20367690

    ale plus+
    24 Sep 2011
    You have a really nice collection of sketches!
    Congrats!
    ^-^
    ale plus+
    25 Sep 2011
    I was wondering... what is all that VK stuff?? It's the first time I see it... Is it part of a library, part of the p5 core?? (I don't see any specific call to it in the sketch)
    You might use instead (key-48) and create and array for all that mode booleans...
    Anyway, good work!
    ^-^
    rajn
    31 Dec 2012
    As far as I am concerned you are a genius.
    Great works, all of them!
    Nico Volatil
    9 May 2014
    nice one!
    can you tell me how you do the render?
    i´m trying many ways ...move maker class in 1.5 and the tool in 2.1 but can´t get the right one, having problems with the speed or framerate.
    You need to login/register to comment.