What I need is a single function that I can give a vector location and it will return a float or int of the height that the Z axis will be. (no caves yet)
Short code from the Perlin wiki (Link):
Code: Select all
final function vector DotGridGradient(int iX, int iY, float X, float Y)
{
local vector v1,v2;
local float dX,dY;
dX=X-iX; dY=Y-iY;
v1.X=iY; v1.Y=iX; v1.Z=0;
v2.X=iY; v2.Y=iX; v2.Z=1;
return (dX*v1+dY*v2);
}
final function float Perlin(float X, float Y)
{
local vector n0,n1;
local float ix0, ix1;
local int x0,x1,y0,y1;
local float sX,sY;
x0=int(X); x1=x0+1; y0=int(Y); y1=y0+1; sX=X-x0; sY=Y-y0;
n0=DotGridGradient(x0, y0, X, Y); n1=DotGridGradient(x1, y0, X, Y);
ix0=lerp(n0.X, n1.Y, sX);
n0=DotGridGradient(x0, y1, X, Y); n1=DotGridGradient(x1, y1, X, Y);
ix1=lerp(n0.X, n1.Y, sX);
return lerp(ix0, ix1, sY);
}
Long code from some other place([URL=http://www.scratchapixel.com/code.php?id=57&origin=/lessons/procedural-generation-virtual-worlds/perlin-noise-part-2]Link[/url]):
Code: Select all
function float GetMePerlin(vector p, optional out vector derivs)
{
local int xi0,yi0,zi0, xi1,yi1,zi1;
local float tx,ty,tz;
local float u,v,w;
local float x0,y0,z0,x1,y1,z1;
local float a,b,c,d,e,f,g,h;
local float du,dv,dw;
local float k0,k1,k2,k3,k4,k5,k6,k7;
xi0 = int(p.x) & 254;
yi0 = int(p.y) & 254;
zi0 = int(p.z) & 254;
xi1 = (xi0+1) & 254;
yi1 = (yi0+1) & 254;
zi1 = (zi0+1) & 254;
tx = p.x - int(p.x);
ty = p.y - int(p.y);
tz = p.z - int(p.z);
u = quintic(tx);
v = quintic(ty);
w = quintic(tz);
// generate vectors going from the grid points to p
x0 = tx; x1 = tx - 1;
y0 = ty; y1 = ty - 1;
z0 = tz; z1 = tz - 1;
a = gradientDotV(byte(xi0+yi0+zi0), x0, y0, z0);
b = gradientDotV(byte(xi1+yi0+zi0), x1, y0, z0);
c = gradientDotV(byte(xi0+yi1+zi0), x0, y1, z0);
d = gradientDotV(byte(xi1+yi1+zi0), x1, y1, z0);
e = gradientDotV(byte(xi0+yi0+zi1), x0, y0, z1);
f = gradientDotV(byte(xi1+yi0+zi1), x1, y0, z1);
g = gradientDotV(byte(xi0+yi1+zi1), x0, y1, z1);
h = gradientDotV(byte(xi1+yi1+zi1), x1, y1, z1);
du = quinticDeriv(tx);
dv = quinticDeriv(ty);
dw = quinticDeriv(tz);
k0 = a;
k1 = (b - a);
k2 = (c - a);
k3 = (e - a);
k4 = (a + d - b - c);
k5 = (a + f - b - e);
k6 = (a + g - c - e);
k7 = (b + c + e + h - a - d - f - g);
derivs.x = du *(k1 + k4 * v + k5 * w + k7 * v * w);
derivs.y = dv *(k2 + k4 * u + k6 * w + k7 * v * w);
derivs.z = dw *(k3 + k5 * u + k6 * v + k7 * v * w);
return k0 + k1 * u + k2 * v + k3 * w + k4 * u * v + k5 * u * w + k6 * v * w + k7 * u * v * w;
}
function float quinticDeriv(float t)
{
return 30 * t * t * (t * (t - 2) + 1);
}
function float quintic(float t)
{
return t * t * t * (t * (t * 6 - 15) + 10);
}
function float gradientDotV(byte perm, float x, float y, float z)
{
switch (perm & 15)
{
case 0: return x + y; // (1,1,0)
case 1: return -x + y; // (-1,1,0)
case 2: return x - y; // (1,-1,0)
case 3: return -x - y; // (-1,-1,0)
case 4: return x + z; // (1,0,1)
case 5: return -x + z; // (-1,0,1)
case 6: return x - z; // (1,0,-1)
case 7: return -x - z; // (-1,0,-1)
case 8: return y + z; // (0,1,1),
case 9: return -y + z; // (0,-1,1),
case 10: return y - z; // (0,1,-1),
case 11: return -y - z; // (0,-1,-1)
case 12: return y + x; // (1,1,0)
case 13: return -x + y; // (-1,1,0)
case 14: return -y + z; // (0,-1,1)
case 15: return -y - z; // (0,-1,-1)
}
}
Obviously I have no idea what I'm doing and apart from being able to (hopefully) convert it correctly I am clueless as to what this thing actually does. So who's got the spare time to help me figure this one out?