Oct
26
2012

Simplex Noise

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 noisescale:float=0.04;
private  var texture:Texture2D;
function Start ()
{
permutation = [
151, 160, 137, 91, 90, 15, 131, 13, 201, 95, 96, 53, 194, 233, 7, 225, 140, 36, 103, 30, 69, 142, 8, 99,
37, 240, 21, 10, 23, 190, 6, 148, 247, 120, 234, 75, 0, 26, 197, 62, 94, 252, 219, 203, 117, 35, 11, 32,
57, 177, 33, 88, 237, 149, 56, 87, 174, 20, 125, 136, 171, 168, 68, 175, 74, 165, 71, 134, 139, 48, 27, 166,
77, 146, 158, 231, 83, 111, 229, 122, 60, 211, 133, 230, 220, 105, 92, 41, 55, 46, 245, 40, 244, 102, 143,
54, 65, 25, 63, 161, 1, 216, 80, 73, 209, 76, 132, 187, 208, 89, 18, 169, 200, 196, 135, 130, 116, 188, 159,
86, 164, 100, 109, 198, 173, 186, 3, 64, 52, 217, 226, 250, 124, 123, 5, 202, 38, 147, 118, 126, 255, 82,
85, 212, 207, 206, 59, 227, 47, 16, 58, 17, 182, 189, 28, 42, 223, 183, 170, 213, 119, 248, 152, 2, 44,
154, 163, 70, 221, 153, 101, 155, 167, 43, 172, 9, 129, 22, 39, 253, 19, 98, 108, 110, 79, 113, 224, 232,
178, 185, 112, 104, 218, 246, 97, 228, 251, 34, 242, 193, 238, 210, 144, 12, 191, 179, 162, 241, 81, 51,
145, 235, 249, 14, 239, 107, 49, 192, 214, 31, 181, 199, 106, 157, 184, 84, 204, 176, 115, 121, 50, 45,
127, 4, 150, 254, 138, 236, 205, 93, 222, 114, 67, 29, 24, 72, 243, 141, 128, 195, 78, 66, 215, 61, 156, 180
];

p = new int[size];

for (var i = 0; i < 256; i++) { //p[256 + i] = p[i] = permutation[i]; p[i] = permutation[i]; p[256 + i] = permutation[i]; } // create texture image texture = new Texture2D (size, size); renderer.material.mainTexture = texture; var stopwatch:System.Diagnostics.Stopwatch  = new System.Diagnostics.Stopwatch(); stopwatch.Start(); // your function here.. // draw heighmap for (var y:int=0;y0.55)
{
texture.SetPixel(x,y,new Color(1,1,1,1));
}else{
texture.SetPixel(x,y,new Color(0,0,0,1));
}
}
}
texture.Apply();
}

}

function fade(t:float)
{
return t * t * t * (t * (t * 6 – 15) + 10);
}

function lerp(t:float, a:float, b:float)
{
return a + t * (b – a);
}

function grad(hash:float, x:float, y:float, z:float)
{
// Convert lo 4 bits of hash code into 12 gradient directions.
var h:int = parseInt(hash) & 15;
var u:float = h < 8 ? x : y; var v:float = h < 4 ? y : h == 12 || h == 14 ? x : z; return ((h & 1) == 0 ? u : -u) + ((h & 2) == 0 ? v : -v); } function scale(n:float) { return (1 + n) / 2; } /** Returns a number between 0 and 1. */ function noise3d(x:float, y:float, z:float) { // Find unit cube that contains point. var X:int = parseInt(Mathf.Floor(x)) & 255; var Y:int = parseInt(Mathf.Floor(y)) & 255; var Z:int = parseInt(Mathf.Floor(z)) & 255; //    var X:int = x & 255; //    var Y:int = y & 255; //    var Z:int = z & 255; // Find relative x,y,z of point in cube. x -= Mathf.Floor(x); y -= Mathf.Floor(y); z -= Mathf.Floor(z); // Compute fade curves for each of x,y,z. var u:float = fade(x); var v:float = fade(y); var w:float = fade(z); // Hash coordinates of the corners. var A:int = p[X    ] + Y; var AA:int = p[A] + Z; var AB:int = p[A + 1] + Z; var B:int = p[X + 1] + Y; var BA:int = p[B] + Z; var BB:int = p[B + 1] + Z; // Add blended results from 8 corners of cube. return scale( lerp( w, lerp( v, lerp ( u, grad(p[AA], x, y, z), grad(p[BA], x - 1, y, z) ), lerp ( u, grad(p[AB], x, y - 1, z), grad(p[BB], x - 1, y - 1, z) ) ), lerp ( v, lerp ( u, grad(p[AA + 1], x, y, z - 1), grad(p[BA + 1], x - 1, y, z - 1) ), lerp( u, grad(p[AB + 1], x, y - 1, z - 1), grad(p[BB + 1], x - 1, y - 1, z - 1) ) ) ) ); } /* // Returns a number between 0 and 1. function noise2d(x, y) { // Find unit square that contains point. var X = Math.floor(x) & 255; var Y = Math.floor(y) & 255; // Find relative x,y of point in square. x -= Math.floor(x); y -= Math.floor(y); // Compute fade curves for each of x,y. var u = fade(x); var v = fade(y); // Hash coordinates of the corners. var A = p[X    ] + Y, AA = p[A], AB = p[A + 1]; var B = p[X + 1] + Y, BA = p[B], BB = p[B + 1]; // Add blended results from the corners. return scale( lerp( v, lerp( u, grad(p[AA], x, y, 0), grad(p[BA], x - 1, y, 0) ), lerp( u, grad(p[AB], x, y - 1, 0), grad(p[BB], x - 1, y - 1, 0) ) ) ); } */ [/javascript]


9 Comments + Add Comment

  • Help. I installed the toolbar but i cant download anything.
    Everytime i try to download it bring me to the toolbar download site

    Browser: Chrome

    i posted it here because i think you dont read the comments to posts from 2010 or anything.

    sorry when my english is not the best

    mfg MexMaster

    • Try disabling adblock, that should help.

      • i dont have adblocker or is the adblocker automaticle installed in chrome because i dont installed anything. i dont know anything about an adblocker?

        • if its installed, its visible in Settings / Extensions. (or maybe some other adblock software..)

          Or if it doesnt work, then have to use other browser for downloads.. (some other users have had problems with chrome also..)

  • just working on noise things at the moment, the libnoise conversion in the unity forum is excellent, although accessing it is a bit awkward, but it does generate normal maps and colour terrain maps, you choose between 5 noise types and sends them to a texture you can access and write to whatever, after that my personal tip is to square and cube root and perform audio wave shaping tricks on the noise to make different styles.
    the built-in unity Perlin noise can actually work wonders and make thorough 2-D maps, it is even as versatile than the nice implementation because it’s so simple and you can perform a lots of postprocessing on it. it helps to do a lot of audio work on ways because you learn how to shape similar patterns into varying patterns in many ways, and in ways that are interpretable to the human mind, because repetitive noise in 3d has to be modulated by many other patterns to save from being awful.

  • simplex noise c# (+procedural wood texture)
    http://stephencarmody.wikispaces.com/Simplex+Noise

  • When will the source be available??? I could really use a UnityScript version of simplex :O

Leave a comment

Connect

Twitter View LinkedIn profile Youtube Github Join Discord Twitch Instagram BlueSky

UnityLauncherPro

Get UnityLauncherPRO and work faster with Unity Projects!
*free unity hub alternative

@unitycoder_com

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.