{"id":1350,"date":"2012-10-26T22:35:23","date_gmt":"2012-10-26T19:35:23","guid":{"rendered":"http:\/\/unitycoder.com\/blog\/?p=1350"},"modified":"2015-01-22T19:06:27","modified_gmt":"2015-01-22T16:06:27","slug":"simplex-noise","status":"publish","type":"post","link":"https:\/\/unitycoder.com\/blog\/2012\/10\/26\/simplex-noise\/","title":{"rendered":"Simplex Noise"},"content":{"rendered":"<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"1351\" data-permalink=\"https:\/\/unitycoder.com\/blog\/2012\/10\/26\/simplex-noise\/simplex_noise_unity3d\/\" data-orig-file=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2012\/10\/simplex_noise_unity3d.jpg?fit=680%2C560&amp;ssl=1\" data-orig-size=\"680,560\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;}\" data-image-title=\"simplex_noise_unity3d\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2012\/10\/simplex_noise_unity3d.jpg?fit=680%2C560&amp;ssl=1\" class=\"alignnone size-full wp-image-1351\" title=\"simplex_noise_unity3d\" src=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2012\/10\/simplex_noise_unity3d.jpg?resize=680%2C560\" alt=\"\" width=\"680\" height=\"560\" srcset=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2012\/10\/simplex_noise_unity3d.jpg?w=680&amp;ssl=1 680w, https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2012\/10\/simplex_noise_unity3d.jpg?resize=300%2C247&amp;ssl=1 300w\" sizes=\"auto, (max-width: 680px) 100vw, 680px\" \/><\/p>\n<p>Converted <a title=\"http:\/\/stackoverflow.com\/questions\/8405526\/javascript-simplex-perlin-noise\/12964906#12964906\" href=\"http:\/\/stackoverflow.com\/questions\/8405526\/javascript-simplex-perlin-noise\/12964906#12964906\" target=\"_blank\">this javascript noise<\/a> to Unity js.<\/p>\n<p>It feels quite slow.. maybe trying to convert <a title=\"https:\/\/gist.github.com\/304522\" href=\"https:\/\/gist.github.com\/304522\" target=\"_blank\">these noise generators<\/a> next.<\/p>\n<p><strong>Webplayer:<\/strong><br \/>\nnothing to see..<\/p>\n<p><strong>Source: (SimplexNoise.js)<\/strong><\/p>\n<p>#pragma strict<\/p>\n<p>\/\/ http:\/\/stackoverflow.com\/questions\/8405526\/javascript-simplex-perlin-noise<\/p>\n<p>private var size:int = 512;<br \/>\nprivate var p:int[];\/\/ = new Array(size);<\/p>\n<p>private var permutation:int[];<br \/>\nprivate var noisescale:float=0.04;<br \/>\nprivate\u00a0 var texture:Texture2D;<br \/>\nfunction Start ()<br \/>\n{<br \/>\npermutation = [<br \/>\n151, 160, 137, 91, 90, 15, 131, 13, 201, 95, 96, 53, 194, 233, 7, 225, 140, 36, 103, 30, 69, 142, 8, 99,<br \/>\n37, 240, 21, 10, 23, 190, 6, 148, 247, 120, 234, 75, 0, 26, 197, 62, 94, 252, 219, 203, 117, 35, 11, 32,<br \/>\n57, 177, 33, 88, 237, 149, 56, 87, 174, 20, 125, 136, 171, 168, 68, 175, 74, 165, 71, 134, 139, 48, 27, 166,<br \/>\n77, 146, 158, 231, 83, 111, 229, 122, 60, 211, 133, 230, 220, 105, 92, 41, 55, 46, 245, 40, 244, 102, 143,<br \/>\n54, 65, 25, 63, 161, 1, 216, 80, 73, 209, 76, 132, 187, 208, 89, 18, 169, 200, 196, 135, 130, 116, 188, 159,<br \/>\n86, 164, 100, 109, 198, 173, 186, 3, 64, 52, 217, 226, 250, 124, 123, 5, 202, 38, 147, 118, 126, 255, 82,<br \/>\n85, 212, 207, 206, 59, 227, 47, 16, 58, 17, 182, 189, 28, 42, 223, 183, 170, 213, 119, 248, 152, 2, 44,<br \/>\n154, 163, 70, 221, 153, 101, 155, 167, 43, 172, 9, 129, 22, 39, 253, 19, 98, 108, 110, 79, 113, 224, 232,<br \/>\n178, 185, 112, 104, 218, 246, 97, 228, 251, 34, 242, 193, 238, 210, 144, 12, 191, 179, 162, 241, 81, 51,<br \/>\n145, 235, 249, 14, 239, 107, 49, 192, 214, 31, 181, 199, 106, 157, 184, 84, 204, 176, 115, 121, 50, 45,<br \/>\n127, 4, 150, 254, 138, 236, 205, 93, 222, 114, 67, 29, 24, 72, 243, 141, 128, 195, 78, 66, 215, 61, 156, 180<br \/>\n];<\/p>\n<p>p = new int[size];<\/p>\n<p>for (var i = 0; i < 256; i++)\n{\n\/\/p[256 + i] = p[i] = permutation[i];\np[i] = permutation[i];\np[256 + i] = permutation[i];\n}\n\n\/\/ create texture image\ntexture = new Texture2D (size, size);\nrenderer.material.mainTexture = texture;\n\n\nvar stopwatch:System.Diagnostics.Stopwatch\u00a0 = new System.Diagnostics.Stopwatch();\nstopwatch.Start();\n\n\/\/ your function here..\n\n\n\/\/ draw heighmap\nfor (var y:int=0;y<size;y++)\n{\nfor (var x:int=0;x<size;x++)\n{\nvar value:float = noise3d(x*(noisescale*y*0.001),y*(noisescale*y*0.001),0)*0.7;\nvalue+=noise3d(x*(noisescale*4),y*(noisescale*4),1)*0.3;\n\ntexture.SetPixel(x,y,new Color(value,value,value,1));\n\n\/*\nif (value<0.55)\n{\ntexture.SetPixel(x,y,new Color(1,1,1,1));\n}else{\ntexture.SetPixel(x,y,new Color(0,0,0,1));\n}*\/\n}\n}\n\nstopwatch.Stop();\n\/\/Debug.Log(\"Timer: \" + stopwatch.Elapsed);\nDebug.Log(\"Timer: \" + stopwatch.ElapsedMilliseconds); \/\/ this one gives you the time in ms\nstopwatch.Reset();\n\n\n\ntexture.Apply();\n}\n\n\nfunction Update()\n{\n\nif (Input.GetMouseButtonDown(0))\n{\n\/\/ draw heighmap\nfor (var y:int=0;y<size;y++)\n{\nfor (var x:int=0;x<size;x++)\n{\nvar value:float = noise3d(x*(noisescale*y*0.001),y*(noisescale*y*0.001),0)*0.8;\nvalue+=noise3d(x*(noisescale*0.5),y*(noisescale*0.5),10)*0.4;\n\n\/\/texture.SetPixel(x,y,new Color(value,value,value,1));\n\n\nif (value>0.55)<br \/>\n{<br \/>\ntexture.SetPixel(x,y,new Color(1,1,1,1));<br \/>\n}else{<br \/>\ntexture.SetPixel(x,y,new Color(0,0,0,1));<br \/>\n}<br \/>\n}<br \/>\n}<br \/>\ntexture.Apply();<br \/>\n}<\/p>\n<p>}<\/p>\n<p>function fade(t:float)<br \/>\n{<br \/>\nreturn t * t * t * (t * (t * 6 &#8211; 15) + 10);<br \/>\n}<\/p>\n<p>function lerp(t:float, a:float, b:float)<br \/>\n{<br \/>\nreturn a + t * (b &#8211; a);<br \/>\n}<\/p>\n<p>function grad(hash:float, x:float, y:float, z:float)<br \/>\n{<br \/>\n\/\/ Convert lo 4 bits of hash code into 12 gradient directions.<br \/>\nvar h:int = parseInt(hash) &#038; 15;<br \/>\nvar u:float = h < 8 ? x : y;\nvar v:float = h < 4 ? y : h == 12 || h == 14 ? x : z;\nreturn ((h &#038; 1) == 0 ? u : -u) + ((h &#038; 2) == 0 ? v : -v);\n}\n\nfunction scale(n:float)\n{\nreturn (1 + n) \/ 2;\n}\n\n\n\n\/** Returns a number between 0 and 1. *\/\nfunction noise3d(x:float, y:float, z:float)\n{\n\/\/ Find unit cube that contains point.\nvar X:int = parseInt(Mathf.Floor(x)) &#038; 255;\nvar Y:int = parseInt(Mathf.Floor(y)) &#038; 255;\nvar Z:int = parseInt(Mathf.Floor(z)) &#038; 255;\n\/\/\u00a0\u00a0 \u00a0var X:int = x &#038; 255;\n\/\/\u00a0\u00a0 \u00a0var Y:int = y &#038; 255;\n\/\/\u00a0\u00a0 \u00a0var Z:int = z &#038; 255;\n\/\/ Find relative x,y,z of point in cube.\nx -= Mathf.Floor(x);\ny -= Mathf.Floor(y);\nz -= Mathf.Floor(z);\n\/\/ Compute fade curves for each of x,y,z.\nvar u:float = fade(x);\nvar v:float = fade(y);\nvar w:float = fade(z);\n\/\/ Hash coordinates of the corners.\nvar A:int = p[X\u00a0\u00a0\u00a0 ] + Y;\nvar AA:int = p[A] + Z;\nvar AB:int = p[A + 1] + Z;\nvar B:int = p[X + 1] + Y;\nvar BA:int = p[B] + Z;\nvar BB:int = p[B + 1] + Z;\n\n\/\/ Add blended results from 8 corners of cube.\n\nreturn scale(\nlerp(\nw,\nlerp(\nv,\nlerp\n(\nu,\ngrad(p[AA], x, y, z),\ngrad(p[BA], x - 1, y, z)\n),\nlerp\n(\nu,\ngrad(p[AB], x, y - 1, z),\ngrad(p[BB], x - 1, y - 1, z)\n)\n),\nlerp\n(\nv,\nlerp\n(\nu,\ngrad(p[AA + 1], x, y, z - 1),\ngrad(p[BA + 1], x - 1, y, z - 1)\n),\nlerp(\nu,\ngrad(p[AB + 1], x, y - 1, z - 1),\ngrad(p[BB + 1], x - 1, y - 1, z - 1)\n)\n)\n)\n);\n}\n\n\/*\n\/\/ Returns a number between 0 and 1.\nfunction noise2d(x, y)\n{\n\/\/ Find unit square that contains point.\nvar X = Math.floor(x) &#038; 255;\nvar Y = Math.floor(y) &#038; 255;\n\/\/ Find relative x,y of point in square.\nx -= Math.floor(x);\ny -= Math.floor(y);\n\/\/ Compute fade curves for each of x,y.\nvar u = fade(x);\nvar v = fade(y);\n\/\/ Hash coordinates of the corners.\nvar A = p[X\u00a0\u00a0\u00a0 ] + Y, AA = p[A], AB = p[A + 1];\nvar B = p[X + 1] + Y, BA = p[B], BB = p[B + 1];\n\n\/\/ Add blended results from the corners.\nreturn scale(\nlerp(\nv,\nlerp(\nu,\ngrad(p[AA], x, y, 0),\ngrad(p[BA], x - 1, y, 0)\n),\nlerp(\nu,\ngrad(p[AB], x, y - 1, 0),\ngrad(p[BB], x - 1, y - 1, 0)\n)\n)\n);\n\n}\n*\/\n\n[\/javascript]\n\n<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Converted this javascript noise to Unity js. It feels quite slow.. maybe trying to convert these noise generators next. Webplayer: nothing to see.. Source: (SimplexNoise.js) #pragma strict \/\/ http:\/\/stackoverflow.com\/questions\/8405526\/javascript-simplex-perlin-noise private var size:int = 512; private var p:int[];\/\/ = new Array(size); private var permutation:int[]; private var [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1351,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[4,3],"tags":[223,260,253,352],"class_list":["post-1350","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-demos","category-unity3d","tag-generator","tag-noise","tag-perlin","tag-simplex-noise"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2012\/10\/simplex_noise_unity3d.jpg?fit=680%2C560&ssl=1","jetpack_shortlink":"https:\/\/wp.me\/p1KTaT-lM","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/posts\/1350","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/comments?post=1350"}],"version-history":[{"count":4,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/posts\/1350\/revisions"}],"predecessor-version":[{"id":3594,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/posts\/1350\/revisions\/3594"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/media\/1351"}],"wp:attachment":[{"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/media?parent=1350"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/categories?post=1350"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/tags?post=1350"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}