I haven’t made up my mind yet about whether I want to make this an open source project, but since I’m in need of help, I guess this shader will at least be open source
.
// 3DPerlinNoise fragment shader
//
// This shader is a complete implementation
// of Perlin’s improved 3D noise algorithm.
//
// See http://mrl.nyu.edu/~perlin/noise/ for details
//
// Created by Wade Lutgen June 8, 2003
//
!!ARBfp1.0
OPTION ARB_precision_hint_fastest;
ATTRIB position = fragment.texcoord[0];
ATTRIB color = fragment.color;
PARAM const = { 1.0, 0.0, 0.0, 0.0 };
PARAM texFactor = 0.00390625;
TEMP uvw;
TEMP t0, t1;
TEMP A, AA, BB;
TEMP p;
TEMP gradx, gradxm1;
TEMP frac, int, dir;
// Find unit cube that contains point
FLR int, position;
// Scale for OGL texture lookup
MUL int, int, texFactor.x;
// Relative xyz of point in cube
FRC frac, position;
// Perform lookups into “permutation” texture
// Actually performs 2 lookups at once, one for p[x]
// which goes into the r component of output and one
// for p[x+1] which goes into the g component
// A.x = p[x] A.y = p[x+1]
TEX A, int.x, texture[0], 1D;
// A.x = p[x] + y, A.y = p[x+1]+y
ADD A, A, int.y;
// AA.x = p[A.x], AA.y = p[A.x+1]
TEX AA, A.x, texture[0], 1D;
// AA.x = p[A.x]+z, AA.y = p[A.x+1]+z
ADD AA, AA, int.z;
// BB.x = p[A.y], BB.y = p[A.y+1]
TEX BB, A.y, texture[0], 1D;
// BB.x = p[A.y]+z, BB.y = p[A.y+1]+z
ADD BB, BB, int.z;
// 8 lookups into RGBA gradient texture
// This particular algorithm has a pre-built in lookup into
// the p texture as well, so instead of g(x), this is actually
// g(p(x)). Saves four texture lookups.
// After lookup, rescale gradient and dot with current location
TEX p, AA.x, texture[1], 1D;
MAD p, p, 2.0, -1.0;
DP3 gradx.x, frac, p;
TEX p, BB.x, texture[1], 1D;
MAD p, p, 2.0, -1.0;
ADD dir, frac, -const.xwww;
DP3 gradxm1.x, dir, p;
TEX p, AA.y, texture[1], 1D;
MAD p, p, 2.0, -1.0;
ADD dir, frac, -const.wxww;
DP3 gradx.y, dir, p;
TEX p, BB.y, texture[1], 1D;
MAD p, p, 2.0, -1.0;
ADD dir, frac, -const.xxww;
DP3 gradxm1.y, dir, p;
// Add ‘1’ to each texture coordinate
// for the next 4 lookups
ADD AA, AA, texFactor;
ADD BB, BB, texFactor;
TEX p, AA.x, texture[1], 1D;
MAD p, p, 2.0, -1.0;
ADD dir, frac, -const.wwxw;
DP3 gradx.z, dir, p;
TEX p, BB.x, texture[1], 1D;
MAD p, p, 2.0, -1.0;
ADD dir, frac, -const.xwxw;
DP3 gradxm1.z, dir, p;
TEX p, AA.y, texture[1], 1D;
MAD p, p, 2.0, -1.0;
ADD dir, frac, -const.wxxw;
DP3 gradx.w, dir, p;
TEX p, BB.y, texture[1], 1D;
MAD p, p, 2.0, -1.0;
ADD dir, frac, -const.xxxw;
DP3 gradxm1.w, dir, p;
// Compute fade curve s(t) = (6t^5 - 15t^4 + 10*t^3)
MAD t0, frac, 6.0, -15.0;
MAD t1, t0, frac, 10.0;
MUL t0, t1, frac;
MUL t1, t0, frac;
MUL uvw, t1, frac;
// linear interpolate result
LRP t0, uvw.x, gradxm1, gradx;
LRP t1.x, uvw.y, t0.y, t0.x;
LRP t1.y, uvw.y, t0.w, t0.z;
LRP t0, uvw.z, t1.y, t1.x;
MUL result.color, t0, color;
END
I’d also appreciate any optimization tips anyone might have.