rainbow

Coordinator
Apr 24, 2011 at 1:02 PM

This is the rainbow color version of Conway's natural rules.


static const int2 g_sumIndex[8] = {
    {-1, -1},
    { 0, -1},
    { 1, -1},
    {-1,  0},
    { 1,  0},
    {-1,  1},
    { 0,  1},
    { 1,  1},
};

static const float3 g_rainbow[7] = {
    {1.0, 0.0, 0.0},
    {1.0, 0.5, 0.0},
    {1.0, 1.0, 0.0},
    {0.0, 0.6, 0.0},
    {0.0, 0.0, 1.0},
    {0.0, 0.0, 0.8},
    {0.4, 0.0, 0.8},
};

cbuffer CBParams : register(b0)
{
	int width;
	int height;
	int r1;
	int r2;
	int r3;
	int r4;
};

struct Data {
	int alive;
	float3 color;
};

StructuredBuffer current : register(t0);
RWStructuredBuffer next : register(t1);

[numthreads(1, 1, 1)]
void main( uint3 tid : SV_DispatchThreadID )
{
	int cx = tid.x % width;
	int cy = tid.x / width;
	

	float halfWidth = width / 2.0;
	float halfHeight = height / 2.0;
	float nx = (cx - halfWidth) / halfWidth;
	float ny = (cy - halfHeight) / halfHeight;
	float d = saturate(distance(float3(nx, ny, 0.0),
	                   float3(0.0, 0.0, 0.0)));
	
	int sum = 0;
	for(int i = 0; i < 8; ++i) {
		int x = cx + g_sumIndex[i].x;
		int y = cy + g_sumIndex[i].y;
		if(0 <= x && x < width && 0 <= y && y < height) {
			int id = x + y * width;
			if(current[id].alive != 0) {
				sum++;
			}
		}
	}

	if(current[tid.x].alive == 0 && sum >= 3 && sum <= 3) {
		next[tid.x].alive = 1;
		next[tid.x].color = g_rainbow[int(d * 6)];
	}
	else if(current[tid.x].alive != 0 && (sum > 3 || sum < 2)) {
		next[tid.x].alive = 0;
		next[tid.x].color = 0.0f;
	}
	else {
		next[tid.x].alive = current[tid.x].alive;
		next[tid.x].color = g_rainbow[int(d * 6)];
	}
}