Perlin noise: Difference between revisions
Content deleted Content added
No edit summary |
|||
Line 1,074: | Line 1,074: | ||
Public: |
Public: |
||
function noise(x, y, z) { |
function noise(x, y, z) { |
||
p=lambda a=.permutation (it)-> { |
|||
if it < 256 then =a#val(it) else =a#val(it - 256) |
|||
} |
|||
} |
|||
def fade(t)= t * t * t * (t * (t * 6 - 15) + 10) |
|||
def lerp(t, a, b) = a + t * (b - a) |
|||
function grad(hash, x, y, z) { |
function grad(hash, x, y, z) { |
||
// Convert low 4 bits of hash code into 12 gradient directions |
// Convert low 4 bits of hash code into 12 gradient directions |
||
Line 1,086: | Line 1,086: | ||
=if(binary.and(h,1) = 0-> u, -u) + if(binary.and(h, 2) = 0-> v, -v) |
=if(binary.and(h,1) = 0-> u, -u) + if(binary.and(h, 2) = 0-> v, -v) |
||
} |
} |
||
// Find unit cube that contains point |
|||
xi = binary.and(floor(x), 255) |
|||
yi = binary.and(floor(y), 255) |
|||
zi = binary.and(floor(z), 255) |
|||
// Find relative x, y, z of point in cube |
|||
xx = x - floor(x) |
|||
yy = y - floor(y) |
|||
zz = z - floor(z) |
|||
// Compute fade curves for each of xx, yy, zz |
|||
u = fade(xx) |
|||
v = fade(yy) |
|||
w = fade(zz) |
|||
// Hash co-ordinates of the 8 cube corners |
|||
// and add blended results from 8 corners of cube |
|||
a = p(xi) + yi |
|||
aa = p(a) + zi |
|||
ab = p(a + 1) + zi |
|||
b = p(xi + 1) + yi |
|||
ba = p(b) + zi |
|||
bb = p(b + 1) + zi |
|||
=lerp(w, lerp(v, lerp(u, grad(p(aa), xx, yy, zz), grad(p(ba), xx - 1, yy, zz)), lerp(u, grad(p(ab), xx, yy - 1, zz), grad(p(bb), xx - 1, yy - 1, zz))), lerp(v, lerp(u, grad(p(aa + 1), xx, yy, zz - 1), grad(p(ba + 1), xx - 1, yy, zz - 1)), lerp(u, grad(p(ab + 1), xx, yy - 1, zz - 1), grad(p(bb + 1), xx - 1, yy - 1, zz - 1)))) |
|||
} |
|||
} |
|||
} |
} |
||
print Perlin.noise(3.14, 42.0, 7.0) |
print Perlin.noise(3.14, 42.0, 7.0) |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,120: | Line 1,119: | ||
0.136919958784 |
0.136919958784 |
||
</pre> |
</pre> |
||
=={{header|Lua}}== |
=={{header|Lua}}== |