!!ARBfp1.0 # 2D noise # includes code both to compute and look up in texture # switched based on 'frame' input parameter (bound the 5/6 keys in test app) # uses texture for frame<.01; computed for frame >= .01 ATTRIB frame = fragment.texcoord[0]; ATTRIB noiseInput = fragment.texcoord[1]; # base for blum blum shub should be product of two primes # but I couldn't find any product that looked good and didn't # result in huge texture sizes. 61 is a single prime that seemed # to produce reasonably good results PARAM base = {61., 0.0163934}; # base, 1/base ############################## # texture-based noise TEMP ntex; MUL ntex, noiseInput, base.y; TEX ntex.r, ntex, texture[0], 2D; ############################## # computed noise # integer and fractional components of position TEMP intArg, fracArg; FLR intArg.xy, noiseInput; FRC fracArg.xy, noiseInput; # compute fade values from fractional location in grid cell # fade(t) = t*t*t*(t*(6*t-15)+10) TEMP fade; MAD fade.xy,fracArg,-2,3; # old fade function t*t*(-2*t+3) MUL fade.xy,fade,fracArg; MUL fade.xy,fade,fracArg; # hash will hold hash values for x,y; x+1,y; x,y+1 and x+1,y+1 TEMP hash, htmp; # start with y & y+1 terms and run through hash function # I scaled by a number relatively prime to base to bootstrap it into # the more interesting part of the hash function a better choice may # have been to just apply the hash multiple times. Note that # this is JUST for hash purposes, it does not scale the frequency of # the noise MAD hash.xy,intArg.y,7,{0,7}; # 7*{y, y+1} MUL htmp.xy,hash,base.y; # mod base FLR htmp.xy,htmp; MAD hash.xy,htmp,-base.x,hash; MUL hash.xy,hash,hash; # squared MUL htmp.xy,hash,base.y; # mod base FLR htmp.xy,htmp; MAD hash.xy,htmp,-base.x,hash; # add x & x+1 terms and run through hash again ADD hash,intArg.x,hash.xxyy; # + {x, x+1} ADD hash,hash,{0,1,0,1}; MUL hash,hash,hash; # squared MUL htmp,hash,base.y; # mod base FLR htmp,htmp; MAD hash,htmp,-base.x,hash; # extract +/- gradient bits each axis TEMP xs, ys, zs, ws; MUL xs,hash,.5; FRC xs,xs; SGE xs,xs,.49999; MUL ys,hash,.25; FRC ys,ys; SGE ys,ys,.49999; # construct full x & y gradients TEMP g, gx, gy; ADD g, fracArg.xxyy, {0,-1,0,-1}; LRP gx, xs, -g.xyxy, g.xyxy; LRP gy, ys, -g.zzww, g.zzww; ADD g, gx, gy; # blend x/y noise in x then y TEMP ncomp; LRP ncomp.xy, fade.x, g.ywyw, g.xzxz; LRP ncomp.x, fade.y, ncomp.y, ncomp.x; # scale into final color TEMP noise; MAD noise.x, ncomp.x, .5, .5; ############################## # select and combine with diffuse lighting TEMP cmp; SUB cmp.x, frame.z, .01; # frame.z<.01 ? ntex : ncomp CMP noise, cmp.x, ntex.x, noise.x; MUL result.color.rgb,noise,fragment.color; MOV result.color.a,1; END