Created
May 19, 2013 05:43
-
-
Save acceptable-security/5606812 to your computer and use it in GitHub Desktop.
Rust-Perlin-Noise
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn floor(a: float) -> float { | |
let mut b = a as f64; | |
b = float::floor(b); | |
let c = b as float; | |
return c; | |
} | |
fn permentation() -> ~[int] { | |
let list = ~[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]; | |
return vec::concat([copy list, copy list]); | |
} | |
fn lerp(t: float, a: float, b: float) -> float { | |
return a + t * (b - a); | |
} | |
fn fade(t: float) -> float { | |
return t * t * t * (t * (t * 6.0 - 15.0) + 10.0); | |
} | |
fn grad(hash: int, x: float, y: float, z: float) -> float { | |
let hi = hash as int; | |
let h = hi & 15; | |
let u = if h < 8 { x } else { y }; | |
let v = if h < 4 { y } else { if h == 12 || h == 14 { x } else { z } }; | |
return (if h & 1 == 0 { u } else { -u }) + (if h & 2 == 0 { v } else { -v }); | |
} | |
fn snoise(nx: float, ny: float, nz: float, p: ~[int]) -> float { | |
let nxi = nx as int; | |
let nyi = ny as int; | |
let nzi = nz as int; | |
let floopXi = nxi & 255; | |
let floopYi = nyi & 255; | |
let floopZi = nzi & 255; | |
let x = floor(nx)-nx; | |
let y = floor(ny)-ny; | |
let z = floor(nz)-nz; | |
let u = fade(x); | |
let v = fade(y); | |
let w = fade(z); | |
let a = p[floopXi] + floopYi; | |
let ai = a as int; | |
let aa = p[ai] + floopZi; | |
let ab = p[ai+1] + floopZi; | |
let b = p[floopXi + 1] + floopYi; | |
let bi = b as int; | |
let ba = p[bi] + floopZi; | |
let bb = p[bi + 1] + floopZi; | |
let pAA = p[aa]; | |
let pAB = p[ab]; | |
let pBA = p[ba]; | |
let pBB = p[bb]; | |
let pAA1 = p[aa+1]; | |
let pAB1 = p[ab+1]; | |
let pBA1 = p[ba+1]; | |
let pBB1 = p[bb+1]; | |
let gradAA = grad(pAA, x, y, z); | |
let gradBA = grad(pBA, x-1.0, y, z); | |
let gradAB = grad(pAB, x, y-1.0, z); | |
let gradBB = grad(pBB,x-1.0,y-1.0,z); | |
let gradAA1 = grad(pAA1, x, y , z-1.0); | |
let gradBA1 = grad(pBA1,x-1.0,y,z-1.0); | |
let gradAB1 = grad(pAB1,x,y-1.0,z-1.0); | |
let gradBB1 = grad(pBB1,x-1.0,y-1.0,z-1.0); | |
return lerp(w, | |
lerp(v, lerp(u, gradAA, gradBA), lerp(u, gradAB, gradBB)), | |
lerp(v, lerp(u, gradAA1, gradBA1), lerp(u, gradAB1, gradBB1)) | |
); | |
} | |
fn main() { | |
let x = 11.0; | |
let y = 5.0; | |
let z = 1.0; | |
let max = 30; | |
let min = 10; | |
let seed = 0.12312313512341; | |
let quality = 128.0; | |
let delta = quality + seed; | |
let perm = permentation(); | |
let sn = snoise((x / seed) / delta, (y / seed) / delta, (z / seed) / delta,perm); | |
let sni = sn as int; | |
let noise = (sni + 1) / 2 * (max - min) + min; | |
io::println(fmt!("%?",noise)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment