Created
December 1, 2022 19:03
-
-
Save tbarusseau/be0b0d3d68db08dd1f4afe09116ba7e0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@group(0) @binding(0) var<storage, read_write> moon_height_data: MoonHeightData; | |
fn smooth_min(a: f32, b: f32, t: f32) -> f32 { | |
let h: f32 = clamp(0.5 + 0.5 * (b - a) / t, 0.0, 1.0); | |
return mix(b, a, h) - t * h * (1.0 - h); | |
} | |
fn smooth_max(a: f32, b: f32, t: f32) -> f32 { | |
return smooth_min(a, b, -t); | |
} | |
@compute @workgroup_size(128, 1, 1) | |
fn compute_moon_height( | |
@builtin(global_invocation_id) invocation_id: vec3<u32>, | |
@builtin(num_workgroups) num_workgroups: vec3<u32> | |
) { | |
let data = moon_height_data; | |
let id = invocation_id.x; | |
if id >= moon_height_data.num_vertices { | |
return; | |
} | |
let vertex_pos = moon_height_data.vertices[invocation_id.x]; | |
var crater_height = 0.0; | |
for (var i: i32 = 0; i < data.num_craters; i++) { | |
// Can't access arrays with a non-constant! How to fix? | |
let x: f32 = length(vertex_pos - data.craters[i].center) / data.craters[i].radius; | |
let cavity: f32 = x * x - 1.0; | |
let rim_x: f32 = min(x - 1.0 - data.rim_width, 0.0); | |
let rim: f32 = data.rim_steepness * rim_x * rim_x; | |
var crater_shape: f32 = smooth_max(cavity, data.floor_height, data.smoothness); | |
crater_shape = smooth_min(crater_shape, rim, data.smoothness); | |
crater_height += crater_shape * data.craters[i].radius; | |
} | |
data.heights[0] = 0.0; | |
data.heights[id] = 1.0 + crater_height; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment