Skip to content

Commit

Permalink
Color according to generation
Browse files Browse the repository at this point in the history
  • Loading branch information
fornwall committed Jun 25, 2023
1 parent 30abb60 commit 77d56fb
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
11 changes: 8 additions & 3 deletions src/game-of-life.compute.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,22 @@ fn getCell(x: i32, y: i32) -> u32 {
return current[getIndex(x, y)];
}

fn isAlive(x: i32, y: i32) -> u32 {
return u32(current[getIndex(x, y)] > 0u);
}

fn countNeighbors(x: i32, y: i32) -> u32 {
return getCell(x - 1, y - 1) + getCell(x, y - 1) + getCell(x + 1, y - 1) + getCell(x - 1, y) + getCell(x + 1, y) + getCell(x - 1, y + 1) + getCell(x, y + 1) + getCell(x + 1, y + 1);
return isAlive(x - 1, y - 1) + isAlive(x, y - 1) + isAlive(x + 1, y - 1) + isAlive(x - 1, y) + isAlive(x + 1, y) + isAlive(x - 1, y + 1) + isAlive(x, y + 1) + isAlive(x + 1, y + 1);
}

@compute @workgroup_size(8, 8)
fn main(@builtin(global_invocation_id) grid: vec3<u32>) {
let x = i32(grid.x);
let y = i32(grid.y);
let n = countNeighbors(x, y);
let current_generation = getCell(x, y);
let cell_lives = current_generation >= 1u;
let will_be_born = u32(((1 << n) & rule.x) > 0);
let will_survive = u32(((1 << n) & rule.y) > 0);
let cell_lives = getCell(x, y) == 1u;
let will_survive = u32(((1 << n) & rule.y) > 0) * (1u + current_generation);
next[getIndex(x, y)] = select(will_be_born, will_survive, cell_lives);
}
30 changes: 25 additions & 5 deletions src/game-of-life.render.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,34 @@ fn vertex_main(@builtin(instance_index) i: u32, @location(0) cell: u32, @locatio
let x = (f32(i % w + pos.x) / f32(w) - 0.5) * 2. * f32(w) / f32(w);
let y = (f32((i - (i % w)) / w + pos.y) / f32(h) - 0.5) * 2. * f32(h) / f32(h);

let c = f32(i) / f32(size.x * size.y);
let r = f32(cell) * f32(i % w) / f32(w);
let g = f32(cell) * f32(i / w) / f32(h);
let b = f32(cell) * (1. - max(g, r));
return Out(vec4<f32>(x, y, 0., 1.), vec3(r, g, b));
let max_age_for_color: u32 = 10u;
let intensity = 1.0 - f32(min(cell, max_age_for_color)) / f32(max_age_for_color);

let rgb = select(vec3(0., 0., 0.), spectral_bruton(intensity), cell > 0u);
return Out(vec4<f32>(x, y, 0., 1.), rgb);
}

@fragment
fn fragment_main(@location(0) cell: vec3<f32>) -> @location(0) vec4<f32> {
return vec4<f32>(cell, 1.0);
}

fn spectral_bruton(w: f32) -> vec3<f32> {
if w < 0.15 {
// return vec3<f32>(-(w - 0.15) / 0.15, 0.0, 1.0);
return vec3<f32>(0.0, 1.0, -(w - 0.325) / (0.325 - 0.275));
} else if w >= 0.15 && w < 0.275 {
//return vec3<f32>(0.0, (w - 0.15) / (0.275 - 0.15), 1.0);
return vec3<f32>(0.0, 1.0, -(w - 0.325) / (0.325 - 0.275));
} else if w >= 0.275 && w < 0.325 {
return vec3<f32>(0.0, 1.0, -(w - 0.325) / (0.325 - 0.275));
} else if w >= 0.325 && w < 0.5 {
return vec3<f32>((w - 0.325) / (0.5 - 0.325), 1.0, 0.0);
} else if w >= 0.5 && w < 0.6625 {
return vec3<f32>(1.0, -(w - 0.6625) / (0.6625 - 0.5), 0.0);
} else if w >= 0.6625 {
return vec3<f32>(1.0, 0.0, 0.0);
} else {
return vec3<f32>(0.0, 0.0, 0.0);
}
}

0 comments on commit 77d56fb

Please sign in to comment.