/* Cracked ------- This script generates a tilable texture with thin cracks all over it. Author : Andreas Jonsson Created: April 25th, 2003 Updated: July 28th, 2005 Texture Generator version 1.1.0 */ // Change these values to define the output const int numberOfCracks = 100; const int wantedSize = 256; const int superSampling = 3; const float crackVariation = 0.25; const float crackLength = 256.0; randomizer rnd; // Add a crack to the heightmap void crack(image @img) { int w = img.width; int h = img.height; float x, y, a; int count; x = rnd.getNumber()*float(w); y = rnd.getNumber()*float(h); a = 2.0*3.141592*rnd.getNumber(); count = int(rnd.getNumber()*crackLength); while( --count >= 0 ) { int ix, iy; ix = int(x)%w; iy = int(y)%h; img.Alpha(ix, iy) = 1.0; x = x + cos(a); y = y + sin(a); if( x < 0.0 ) x = x + float(w); if( y < 0.0 ) y = y + float(h); a = a + crackVariation*(2.0*rnd.getNumber()-1.0); } } // Takes a height map with height values in the alpha channel image @normalMap(image @hmap) { int w = hmap.width; int h = hmap.height; image @nmap = @image(w, h); int x, y = 0; while( y < h ) { x = 0; while( x < w ) { // Get two vectors passing through pixel float hxp = hmap.Alpha((x + 1)%w, y); float hyp = hmap.Alpha(x, (y + 1)%h); float hxm = hmap.Alpha((x + w - 1)%w, y); float hym = hmap.Alpha(x, (y + h - 1)%h); // Compute the cross product of the two vectors float nx = -2.0*(hyp - hym); float ny = -(hxp - hxm)*2.0; float nz = 2.0*2.0; // Normalize float norm = 1.0/sqrt(nx*nx + ny*ny + nz*nz); nmap.Pixel(x, y) = pixel(nx*norm, ny*norm, nz*norm); x++; } y++; } return @nmap; } void shadeNormal(image @nmap) { int w = nmap.width; int h = nmap.height; int x, y; y = 0; while( y < w ) { x = 0; while( x < h ) { // Dot product pixel with light vector float nx = nmap.Red(x, y); float ny = nmap.Green(x, y); float nz = nmap.Blue(x, y); float i = 0.5*nx + 0.5*ny + 0.5*nz; nmap.Pixel(x, y) = pixel(i, i, i); x++; } y++; } } void main() { image @img = @image(wantedSize*superSampling, wantedSize*superSampling); // Add some cracks to heightmap int n = 0; while( n++ < numberOfCracks ) { crack(@img); } // Compute normal map image @nmap = @normalMap(@img); // Light the normal map shadeNormal(@nmap); nmap.Resize(wantedSize,wantedSize); nmap.Show(); }