{"id":708,"date":"2014-09-16T11:03:04","date_gmt":"2014-09-16T18:03:04","guid":{"rendered":"http:\/\/www.imaginary-institute.com\/blog\/?p=708"},"modified":"2014-09-16T11:06:34","modified_gmt":"2014-09-16T18:06:34","slug":"noise-blending-with-the-au-library","status":"publish","type":"post","link":"https:\/\/www.imaginary-institute.com\/blog\/2014\/09\/16\/noise-blending-with-the-au-library\/","title":{"rendered":"Noise Blending with the AU Library"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" class=\"alignleft  wp-image-722\" src=\"http:\/\/www.imaginary-institute.com\/blog\/wp-content\/uploads\/2014\/09\/AUNoiseField2.gif\" alt=\"New noise field\" width=\"128\" height=\"128\" \/>Now that my AU Library is out (in beta, anyway) I thought I&#8217;d show how to write the noise blending code from an <a href=\"http:\/\/www.imaginary-institute.com\/blog\/2014\/08\/29\/noise-blending-implementation\/#more-588\" target=\"_blank\">earlier post<\/a>, but this time using the AU Library. Although I do a little more work in <code>setup()<\/code>, the <code>draw()<\/code> is much simpler, and the results are much nicer.<!--more--><\/p>\n<p>Here&#8217;s what&#8217;s changed:<\/p>\n<ul>\n<li>I replaced the 3D array of floats with a single <code>AUMultiField<\/code> object.<\/li>\n<li>I replaced the first two lines of <code>draw()<\/code> (which were pretty messy) with simple calls to an <code>AUStepper<\/code> object.<\/li>\n<li>I replaced the call to <code>curve()<\/code> with a call to a per-pixel <code>AUCurve<\/code> object. This means we move through the curves at a smooth, constant pace.<\/li>\n<li>These changes mean that the noise fields and a couple of other variables no longer need to be global. The only globals are the <code>AUStepper<\/code> and the <code>AUCurve<\/code> objects.<\/li>\n<\/ul>\n<p>I could scrunch the code down even more by doing away with the <code>AUMultiField<\/code> that holds the noise. After all, there&#8217;s really no reason to compute it and save it since it&#8217;s only used once. The expressions to get the noise could be rolled right into each <code>AUCurve<\/code> constructor. But I think this is a little easier to follow, and it demonstrates how to use <code>AUMultiField<\/code> to hold this kind of data, so I left it this way.<\/p>\n<pre>import AULibrary.*;\r\n\r\n\/\/ A little sketch to demonstrate the multiple noise field interpolation technique.\r\n\/\/ No guarantee or warranty of any kind. You may use and distribute this freely.\r\n\/\/ Andrew Glassner, September 16, 2014\r\n\r\nAUStepper Stepper;  \/\/ tracks our progress through the fields\r\nAUCurve[][] NoiseCurves;  \/\/ one curve of noise per pixel\r\n\r\nvoid setup() {\r\n  size(500, 500);\r\n  \r\n  AUMultiField noiseArrays;       \/\/ list of noise fields\r\n  int numFields = 4;              \/\/ how many fields you want to use. Must be &gt; 0\r\n  int cycleLength = 25;           \/\/ number of frames to get from one field to another\r\n\r\n  Stepper = new AUStepper(numFields, cycleLength);    \/\/ create the stepper \r\n  Stepper.setAllEases(AULibrary.EASE_LINEAR);\r\n  \r\n  noiseArrays = new AUMultiField(this, numFields, width, height);\r\n  for (int n=0; n&lt;numFields; n++) { \/\/ for each field n,\r\n    for (int y=0; y&lt;height; y++) {  \/\/ look at all the pixels\r\n      for (int x=0; x&lt;width; x++) {\r\n        float noiseX = (x*.02) + (n*width);  \/\/ .02 was chosen by eye; it's\r\n        float noiseY = (y*.02) + (n*height); \/\/ not too simple or busy\r\n        noiseArrays.fields[n].z[y][x] = noise(noiseY, noiseX); \/\/ save this value\r\n      }\r\n    }\r\n  }\r\n\r\n  NoiseCurves = new AUCurve[height][width];\r\n  for (int y=0; y&lt;height; y++) { \/\/ build a curve for each pixel\r\n    for (int x=0; x&lt;width; x++) {\r\n      float[][] knots = new float[numFields][1];  \/\/ the data for this curve\r\n      for (int n=0; n&lt;numFields; n++) { \/\/ fill in with this curve's data\r\n        knots[n][0] = noiseArrays.fields[n].z[y][x];\r\n      }\r\n      NoiseCurves[y][x] = new AUCurve(knots, 1, true); \/\/ 1D geometry, closed curve\r\n    }\r\n  }\r\n}\r\n\r\nvoid draw() {\r\n  Stepper.step();\r\n  float alfa = Stepper.getFullAlfa();  \/\/ how far we are into the curves\r\n  loadPixels();\r\n  for (int y=0; y&lt;height; y++) {\r\n    for (int x=0; x&lt;width; x++) {\r\n      float v = NoiseCurves[y][x].getX(alfa);  \/\/ get value from this curve\r\n      pixels[(y*width)+x] = color(255 * v); \/\/ save noise value as a shade of gray\r\n    }\r\n  }\r\n  updatePixels();\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Now that my AU Library is out (in beta, anyway) I thought I&#8217;d show how to write the noise blending code from an earlier post, but this time using the AU Library. Although I do a little more work in setup(), the draw() is much simpler, and the results are much nicer.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-708","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.imaginary-institute.com\/blog\/wp-json\/wp\/v2\/posts\/708","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.imaginary-institute.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.imaginary-institute.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.imaginary-institute.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.imaginary-institute.com\/blog\/wp-json\/wp\/v2\/comments?post=708"}],"version-history":[{"count":21,"href":"https:\/\/www.imaginary-institute.com\/blog\/wp-json\/wp\/v2\/posts\/708\/revisions"}],"predecessor-version":[{"id":731,"href":"https:\/\/www.imaginary-institute.com\/blog\/wp-json\/wp\/v2\/posts\/708\/revisions\/731"}],"wp:attachment":[{"href":"https:\/\/www.imaginary-institute.com\/blog\/wp-json\/wp\/v2\/media?parent=708"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.imaginary-institute.com\/blog\/wp-json\/wp\/v2\/categories?post=708"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.imaginary-institute.com\/blog\/wp-json\/wp\/v2\/tags?post=708"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}