/* Cells ------- Generating a cell texture. Based on algorithm described by Ysaneya: http://www.gamedev.net/community/forums/mod/journal/journal.asp?jn=263350&cmonth=10&cyear=2008&cday=25 Author: Andreas Jonsson Created: October 31st, 2008 Texture Generator version 1.1.0 */ randomizer rnd; image @noiseTexture; void genNoiseTexture() { // Putting random values between 0 and 1 in the blue and green channels // These values represent a point within a grid square image @img = @image(8,8); for( int x = 0; x < 8; x++ ) { for( int y = 0; y < 8; y++ ) { img.Blue(x,y) = rnd.getNumber(); img.Green(x,y) = rnd.getNumber(); } } @noiseTexture = @img; } pixel getRandomPoint(int x, int y) { if( x < 0 ) x += noiseTexture.width; if( y < 0 ) y += noiseTexture.height; x = x % noiseTexture.width; y = y % noiseTexture.height; return noiseTexture.Pixel(x,y); } pixel getDistance(float x, float y) { // We'll return the two smallest distances to the points in the noiseTexture int xi = int(x); int yi = int(y); float xf = x - float(xi); float yf = y - float(yi); float dist1 = 10; float dist2 = 10; for( int xc = -1; xc < 2; xc++ ) { for( int yc = -1; yc < 2; yc++ ) { pixel p = getRandomPoint(xi+xc, yi+yc); float x1 = p.b + float(xc) - xf; float y1 = p.g + float(yc) - yf; float dist = x1*x1 + y1*y1; if( dist < dist1 ) { dist2 = dist1; dist1 = dist; } else if( dist < dist2 ) { dist2 = dist; } } } pixel p; p.b = sqrt(dist1); p.g = sqrt(dist2); return p; } void main() { genNoiseTexture(); image @img = @image(256, 256); for( int x = 0; x < 256; x++ ) { for( int y = 0; y < 256; y++ ) { pixel p = getDistance(noiseTexture.width*float(x)/256, noiseTexture.height*float(y)/256); img.Pixel(x,y) = pixel(p.g-p.b,0,0); } } img.Show(); }