GMLscripts.com

Discuss and collaborate on GML scripts
Invert

You are not logged in.

#1 2013-09-30 10:01:05

xot
Administrator
Registered: 2007-08-18
Posts: 1,239

Perlin's Improved Noise

Perlin's improved noise ported to GML. Based on Ken Perlin's Java reference implementation.

Must be initialized before first use with noise_init(). Also requires support functions fade(), grad(), and lerp().

NOTE: It expects lerp() to be available in GM:Studio. For other versions, a lerp script can be found in the parametric functions section of the main site. However, at the time of this writing, the argument order of the script is different than that of GM:Studio. If using the script, alter the argument order from (t,a,b) to (a,b,t).

A demonstration of one use for it can be found here:
http://www.gmlscripts.com/forums/viewtopic.php?id=1960

noise()

Expand//  noise(x,y,z)
{
    var X,Y,Z,u,v,w,A,AA,AB,B,BA,BB;
    X = floor(argument0) & 255;
    Y = floor(argument1) & 255;
    Z = floor(argument2) & 255;
    argument0 -= floor(argument0);
    argument1 -= floor(argument1);
    argument2 -= floor(argument2);
    u = fade(argument0);
    v = fade(argument1);
    w = fade(argument2);
    A  = noiseP[X  ]+Y;
    AA = noiseP[A  ]+Z;
    AB = noiseP[A+1]+Z;
    B  = noiseP[X+1]+Y;
    BA = noiseP[B  ]+Z;
    BB = noiseP[B+1]+Z;
    return lerp(lerp(lerp(grad(noiseP[AA  ], argument0,     argument1,     argument2),
                          grad(noiseP[BA  ], argument0 - 1, argument1,     argument2),
                          u),
                     lerp(grad(noiseP[AB  ], argument0,     argument1 - 1, argument2),
                          grad(noiseP[BB  ], argument0 - 1, argument1 - 1, argument2),
                          u),
                     v),
                lerp(lerp(grad(noiseP[AA+1], argument0,     argument1,     argument2 - 1),
                          grad(noiseP[BA+1], argument0 - 1, argument1,     argument2 - 1),
                          u),
                     lerp(grad(noiseP[AB+1], argument0,     argument1 - 1, argument2 - 1),
                          grad(noiseP[BB+1], argument0 - 1, argument1 - 1, argument2 - 1),
                          u),
                    v),
                w);
}

noise_init()

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

fade()

Expand{
    return argument0 * argument0 * argument0 * (argument0 * (argument0 * 6 - 15) + 10);
}

grad()

Expand//  grad(hash, x, y, z):
{
    var u,v;
    argument0 = argument0 & 15;
    if (argument0 < 8)
        u = argument1;
    else
        u = argument2;
    if (argument0 < 4)
        v = argument2;
    else if (argument0 == 12 or argument0 == 14)
        v = argument1;
    else
        v = argument3;
    if (argument0 & 1 != 0)
        u = -u;
    if (argument0 & 2 != 0)
        v = -v;
    return u + v;
}

Abusing forum power since 1986.

Offline

#2 2013-09-30 10:07:14

xot
Administrator
Registered: 2007-08-18
Posts: 1,239

Re: Perlin's Improved Noise

A fractal noise function that can be used in conjunction with the above.

Expand//  fbm(x,y,z,H,lacunarity,octaves)
{
    var px,py,pz,H,lacunarity,octaves,value;
    px = argument0;
    py = argument1;
    pz = argument2;
    H = argument3;
    lacunarity = argument4;
    octaves = argument5;
    value = 0.0;
    
    var frequency,i;
    frequency = 1.0;
    for (i=0; i<octaves; i+=1)
    {
        value += noise(px,py,pz) * power(frequency, -H);
        px *= lacunarity;
        py *= lacunarity;
        pz *= lacunarity;
        frequency *= lacunarity;
    }    
    return value;
}

Abusing forum power since 1986.

Offline

Board footer

Powered by FluxBB