{"id":15777,"date":"2012-05-07T04:44:05","date_gmt":"2012-05-07T09:44:05","guid":{"rendered":"http:\/\/www.shamusyoung.com\/twentysidedtale\/?p=15777"},"modified":"2012-05-07T08:10:43","modified_gmt":"2012-05-07T13:10:43","slug":"project-octant-part-4-the-beautiful-noise","status":"publish","type":"post","link":"https:\/\/www.shamusyoung.com\/twentysidedtale\/?p=15777","title":{"rendered":"Project Octant Part 4: The Beautiful Noise"},"content":{"rendered":"<p>Perlin Noise is a technique for quickly generating a metric crapload of really interesting pseudo-randomness. &#8220;Interesting&#8221; in that it forms nice organic patterns instead of pure random noise.  &#8220;Pseudo Random&#8221; in that you can give it the same input and get the same output.  &#8220;Crapload&#8221; means that you can make a final data set thousands of times larger than the noise you start with.<\/p>\n<p>Note that in the context of this project I&#8217;m going to <em>discuss<\/em> Perlin in terms of 2D images, but I&#8217;m <em>using<\/em> it in 3D.  It&#8217;s just easier to show you what we&#8217;re doing in 2D.<\/p>\n<p>We begin with a basic image of really random noise, which I will depict as a 2D greyscale image.  The more random the better. We want areas of light, dark, and medium brightness. We want it to be really diverse overall, but have small local clusters of bright or darkness. We don&#8217;t want large areas to be homogeneous, and we don&#8217;t want the small areas just just be a scatter of white and black pixels. We can accomplish this a lot of ways.  I could churn out a bunch of values in a random number generator, for example. Or, we can just open up a new image in your Photoshop of choice, crank up the noise filter on a blank image, and hit save.<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/octant4_1.jpg' class='insetimage'   alt='octant4_1.jpg' title='octant4_1.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>Awesome, right? <\/p>\n<p><!--more-->No. Obviously pure noise is boring. <\/p>\n<p>To make Perlin noise&#8230;<\/p>\n<div class=\"dmnotes\">Stop!<\/p>\n<p>Okay. According to <a href=\"http:\/\/www.shamusyoung.com\/twentysidedtale\/?p=15777&#038;cpage=1#comment-277055\">Chris Serson below<\/a>, this is not Perlin noise. This is&#8230; It&#8217;s complicated. The point is, when I Google&#8217;d for Perlin noise I ran into multiple pages that made the same error I made here. To avoid perpetuating this mis-naming of noise systems, I&#8217;m adding this note here.  <\/p>\n<p>I will say that it is hard to find a proper implementation of either Perlin Noise or the (sometimes preferred) Simplex noise.  Most of the stuff I find is:<\/p>\n<ol>\n<li>Impenetrable jargon.\n<\/li>\n<li>Pages of indecipherable, Greek-laden maths.\n<\/li>\n<li>Example images of what the system can do, provided you can figure it out.\n<\/li>\n<li>A history of all the noise systems Ken Perlin has made, how he used them, and how people keep mis-attributing other noise systems to him.\n<\/li>\n<\/ol>\n<p>So&#8230; not much in the way of code out there. In the end, what I have here works well enough for my purposes.  If I need something more robust I can go on the quest for the One True Noise Algorithm at another time.  Let&#8217;s just get on with this.<\/p><\/div>\n<p>&#8230;we have to sample this image at different resolutions.  Massive resolutions.  Bigger than we would want to bother keeping in memory, really. What we do is take this noise and kind of zoom in on it. For example, here are the four pixels in the upper-left of our noise image.<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/octant4_6.jpg' class='insetimage'   alt='octant4_6.jpg' title='octant4_6.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>Blending between these four values, we can come up with values in between.  If it asks for the value 20% of the way across and 80% of the way down, we get this:<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/octant4_7.jpg' class='insetimage'   alt='octant4_7.jpg' title='octant4_7.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>To be clear: We&#8217;re not calculating ALL of these values.  It only creates the one pixel that was requested. I&#8217;m just showing you how it arrives at that value.<\/p>\n<p>The neat thing is, I already had 90% of the code needed for Perlin Noise. For those of you reading through Project Frontier, check out <code>MathInterpolateQuad ()<\/code>.  It has everything you need for doing the above. <\/p>\n<p>The terrain generator is feeding us coordinates.  We&#8217;re dividing those values by a thousand, so that it would require a thousand samples (a thousand meters of scenery) to cover the gradient from one edge of this four-pixel square to the other.  Of course, if we stopped here we would have some really boring, super-flat scenery. <\/p>\n<p>So we take another sample.  Instead of <sup>1<\/sup>\/<sub>1,000<\/sub>, this one is taken at <sup>1<\/sup>\/<sub>500<\/sub>.  Then we take another sample at <sup>1<\/sup>\/<sub>250<\/sub>, and another at <sup>1<\/sup>\/<sub>125<\/sub>.  Each level of noise is at twice the frequency of the one before. Then we add all of these samples together, <em>giving them all equal weight<\/em>.  We average them.<\/p>\n<p>The upshot of all of this is that we get this large pattern of interesting noise. <\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/octant4_2.jpg' class='insetimage'   alt='octant4_2.jpg' title='octant4_2.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>This might not look terribly useful, but we can use this to generate interesting topography.  Obviously we could use this to make hills where brighter = taller.  But we can also use it to generate underground scenery. Let&#8217;s say we set a threshold.  Everything above a certain brightness will be hollow, and everything below that threshold will be solid rock.<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/octant4_3.jpg' class='insetimage'   alt='octant4_3.jpg' title='octant4_3.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>Which gives us a system of caves.  If we set the threshold higher, then instead of large caves we&#8217;ll get little pockets, like Swiss Cheese. <\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/octant4_4.jpg' class='insetimage'   alt='octant4_4.jpg' title='octant4_4.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>If we take a narrow range of values above one point and below another, then we get a bunch of passages, all twisty-like. <\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/octant4_5.jpg' class='insetimage'   alt='octant4_5.jpg' title='octant4_5.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>So let&#8217;s take this noise and feed it into our cube-world and see how it looks:<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/octant4_8.jpg' class='insetimage'   alt='octant4_8.jpg' title='octant4_8.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>This is just raw Perlin output.  I&#8217;m not doing anything fancy with it at this point. Eventually it might be worthwhile to combine different sets of noise. (Perhaps one to make hills, and another to bore tunnels in the hills.) Or to set up different ways of deciding what&#8217;s solid and what&#8217;s empty. (Mess with the thresholds.)  Maybe I&#8217;ll stretch noise along one axis to make tall or long caves.  Or flatten out the bottoms to make Star-Trek style caves with lumpy walls and floors level enough for championship billiards. (A classic trope. Actually, is it a trope? I&#8217;ve never seen it <a href=\"http:\/\/tvtropes.org\/pmwiki\/search_result.php?cx=partner-pub-6610802604051523%3Aamzitfn8e7v&#038;cof=FORID%3A10&#038;ie=ISO-8859-1&#038;q=cave+level&#038;siteurl=tvtropes.org%2Fpmwiki%2Fpmwiki.php%2FMain%2FPlatonicCave&#038;ref=www.google.com%252Furl%253Fsa%253Dt%2526rct%253Dj%2526q%253D%2526esrc%253Ds%2526source%253Dweb%2526cd%253D1%2526ved%253D0CH0QFjAA%2526url%253Dhttp%253A%252F%252Ftvtropes.org%252F%2526ei%253DElSnT4r6J-eg6QGl3sG9BA%2526usg%253DAFQjCNFXyqvffDkbQu7TCFs8F8Ymx_WHng\">officially listed<\/a>.) <\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/octant4_9.jpg' class='insetimage'   alt='octant4_9.jpg' title='octant4_9.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>One of the things I wanted for this project was to experiment with noise like this, and see what other sorts of shapes and places one can make. <\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/octant4_10.jpg' class='insetimage'   alt='octant4_10.jpg' title='octant4_10.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>So now we have a foundation for making scenery. I&#8217;m sure we&#8217;ll come back to this when it&#8217;s time to work on the output. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Perlin Noise is a technique for quickly generating a metric crapload of really interesting pseudo-randomness. &#8220;Interesting&#8221; in that it forms nice organic patterns instead of pure random noise. &#8220;Pseudo Random&#8221; in that you can give it the same input and get the same output. &#8220;Crapload&#8221; means that you can make a final data set thousands [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[66],"tags":[233],"class_list":["post-15777","post","type-post","status-publish","format-standard","hentry","category-programming","tag-octant"],"_links":{"self":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts\/15777","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=15777"}],"version-history":[{"count":0,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts\/15777\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=15777"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=15777"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=15777"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}