kernel Automata2D < namespace : "Ian Reichert-Watts"; vendor : "www.shadowhelm.com"; version : 1; description : "2D Cellular Automata"; > { input image4 src; output pixel4 dst; parameter int over ; parameter int under ; parameter int live ; parameter int width < minValue:2; maxValue:4096; defaultValue:512; >; parameter int height < minValue:2; maxValue:4096; defaultValue:512; >; void evaluatePixel() { dst = sampleNearest(src,outCoord()); //make sure we aren't on the edges if(outCoord()[1] > 0.0 && outCoord()[0] < float(height) && outCoord()[0] > 0.0 && outCoord()[0] < float(width)){ //find live neighbors int neighbors = 0; //top-left pixel4 samp = sampleNearest(src,outCoord()+float2(-1,-1)); if(samp.r < 0.5 && samp.g < 0.5 && samp.b < 0.5){ neighbors++; } //top-center samp = sampleNearest(src,outCoord()+float2(0,-1)); if(samp.r < 0.5 && samp.g < 0.5 && samp.b < 0.5){ neighbors++; } //top-right samp = sampleNearest(src,outCoord()+float2(1,-1)); if(samp.r < 0.5 && samp.g < 0.5 && samp.b < 0.5){ neighbors++; } //left samp = sampleNearest(src,outCoord()+float2(-1,0)); if(samp.r < 0.5 && samp.g < 0.5 && samp.b < 0.5){ neighbors++; } //right samp = sampleNearest(src,outCoord()+float2(1,0)); if(samp.r < 0.5 && samp.g < 0.5 && samp.b < 0.5){ neighbors++; } //bottom-left samp = sampleNearest(src,outCoord()+float2(-1,1)); if(samp.r < 0.5 && samp.g < 0.5 && samp.b < 0.5){ neighbors++; } //bottom-center samp = sampleNearest(src,outCoord()+float2(0,1)); if(samp.r < 0.5 && samp.g < 0.5 && samp.b < 0.5){ neighbors++; } //bottom-right samp = sampleNearest(src,outCoord()+float2(1,1)); if(samp.r < 0.5 && samp.g < 0.5 && samp.b < 0.5){ neighbors++; } //solve for rules - Conway's Game of Life samp = sampleNearest(src,outCoord()); //is alive? if(samp.r < 0.5 && samp.g < 0.5 && samp.b < 0.5){ //if less than 'under' neighbors, die by underpopulation //if more than 'over' neighbors, die by overcrowding if(neighbors < under || neighbors > over){ pixel4 state = pixel4(1.0,1.0,1.0,1.0); dst = state; } }else { //if exactly 'live' neighbors, reborn if dead if(neighbors == live){ pixel4 state = pixel4(0.0,0.0,0.0,1.0); dst = state; } } } } }