Files
snow_trail/src/shaders/snow_deform.wgsl

47 lines
1.4 KiB
WebGPU Shading Language

@group(0) @binding(0)
var snow_depth: texture_storage_2d<r32float, read_write>;
@group(0) @binding(1)
var<uniform> params: DeformParams;
struct DeformParams {
position_x: f32,
position_z: f32,
radius: f32,
depth: f32,
terrain_width: f32,
terrain_height: f32,
_padding1: f32,
_padding2: f32,
}
@compute @workgroup_size(16, 16, 1)
fn deform(@builtin(global_invocation_id) global_id: vec3<u32>) {
let texture_size = textureDimensions(snow_depth);
if (global_id.x >= texture_size.x || global_id.y >= texture_size.y) {
return;
}
let coords = vec2<i32>(i32(global_id.x), i32(global_id.y));
let terrain_size = vec2<f32>(params.terrain_width, params.terrain_height);
let half_size = terrain_size / 2.0;
let uv = vec2<f32>(f32(global_id.x) / f32(texture_size.x), f32(global_id.y) / f32(texture_size.y));
let world_pos = uv * terrain_size - half_size;
let deform_center = vec2<f32>(params.position_x, params.position_z);
let distance = length(world_pos - deform_center);
if (distance < params.radius) {
let current_depth = textureLoad(snow_depth, coords).r;
let falloff = 1.0 - (distance / params.radius);
let deform_amount = params.depth * falloff * falloff;
let new_depth = max(0.0, current_depth - deform_amount);
textureStore(snow_depth, coords, vec4<f32>(new_depth, 0.0, 0.0, 0.0));
}
}