topical media & game development

talk show tell print

#javascript-processing-example-basic-math-noise3d.htm / htm



  <!DOCTYPE html>
  <html><head>
  <script src="javascript-processing-example-processing.js"></script>
  <script src="javascript-processing-example-init.js"></script>
  <link rel="stylesheet" href="javascript-processing-example-style.css">
  </head><body><h1><a href="http://ejohn.org/blog/processingjs/">Processing.js</a></h1>
  <h2>Noise3D</h2>
  
  <p>Using 3D noise to create simple animated texture. 
  Here, the third dimension ('z') is treated as time.</p>
  
  <p><a href="http://processing.org/learning/basics/noise3d.html"><b>Original Processing.org Example:</b> Noise3D</a><br>
  <script type="application/processing">
  float increment = 0.01;
  // The noise function's 3rd argument, a global variable that increments once per cycle
  float zoff = 0.0;  
  // We will increment zoff differently than xoff and yoff
  float zincrement = 0.02; 
  
  void setup() {
    size(50,50);
    frameRate(30);
  }
  
  void draw() {
    background(0);
    
    // Optional: adjust noise detail here
    // noiseDetail(8,0.65f);
    
    loadPixels();
  
    float xoff = 0.0; // Start xoff at 0
    
    // For every x,y coordinate in a 2D space, calculate a noise value and produce a brightness value
    for (int x = 0; x < width; x++) {
      xoff += increment;   // Increment xoff 
      float yoff = 0.0f;   // For every xoff, start yoff at 0
      for (int y = 0; y < height; y++) {
        yoff += increment; // Increment yoff
        
        // Calculate noise and scale by 255
        //float bright = noise(xoff,yoff,zoff)*255;
  
        // Try using this line instead
        float bright = random(0,255);
        
        // Set each pixel onscreen to a grayscale value
        pixels[x+y*width] = color(bright,bright,bright);
      }
    }
    updatePixels();
    
    zoff += zincrement; // Increment zoff
    
    
  }
  </script><canvas width="50" height="50"></canvas></p>
  <div style="overflow: hidden; height: 0px; width: 0px;"></div>
  
  <pre><b>// All Examples Written by <a href="http://reas.com/">Casey Reas</a> and <a href="http://benfry.com/">Ben Fry</a>
  // unless otherwise stated.</b>
  float increment = 0.01;
  // The noise function's 3rd argument, a global variable that increments once per cycle
  float zoff = 0.0;  
  // We will increment zoff differently than xoff and yoff
  float zincrement = 0.02; 
  
  void setup() {
    size(200,200);
    frameRate(30);
  }
  
  void draw() {
    background(0);
    
    // Optional: adjust noise detail here
    // noiseDetail(8,0.65f);
    
    loadPixels();
  
    float xoff = 0.0; // Start xoff at 0
    
    // For every x,y coordinate in a 2D space, calculate a noise value and produce a brightness value
    for (int x = 0; x &lt; width; x++) {
      xoff += increment;   // Increment xoff 
      float yoff = 0.0f;   // For every xoff, start yoff at 0
      for (int y = 0; y &lt; height; y++) {
        yoff += increment; // Increment yoff
        
        // Calculate noise and scale by 255
        float bright = noise(xoff,yoff,zoff)*255;
  
        // Try using this line instead
        //float bright = random(0,255);
        
        // Set each pixel onscreen to a grayscale value
        pixels[x+y*width] = color(bright,bright,bright);
      }
    }
    updatePixels();
    
    zoff += zincrement; // Increment zoff
    
    
  }</pre>
  </body></html>
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.