From b3deecc02f3e31762d403cbf0d4d7f4f8be426bc Mon Sep 17 00:00:00 2001 From: Matthew Gordon Date: Mon, 2 Jun 2025 15:10:28 -0300 Subject: [PATCH] Add colours --- .gitattributes | 1 + data/colormap.data | 3 ++ src/app/dem_renderer/dem_renderer.wgsl | 10 +++- src/app/dem_renderer/mod.rs | 64 ++++++++++++++++++++++++++ 4 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 data/colormap.data diff --git a/.gitattributes b/.gitattributes index 4fae6dc..15f0a09 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1 +1,2 @@ *.jpg filter=lfs diff=lfs merge=lfs -text +*.data filter=lfs diff=lfs merge=lfs -text diff --git a/data/colormap.data b/data/colormap.data new file mode 100644 index 0000000..6566e08 --- /dev/null +++ b/data/colormap.data @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:cf44f8b9d69f641466f1c65e308f446e2c46b022108059dc55076e2459151413 +size 48 diff --git a/src/app/dem_renderer/dem_renderer.wgsl b/src/app/dem_renderer/dem_renderer.wgsl index dab531a..0b500c5 100644 --- a/src/app/dem_renderer/dem_renderer.wgsl +++ b/src/app/dem_renderer/dem_renderer.wgsl @@ -21,6 +21,9 @@ var dem_texture: texture_2d; @group(0) @binding(2) var dembvh_texture: texture_2d; +@group(0) @binding(3) +var colormap_texture: texture_1d; + //#include ray_intersection.wgsl @vertex @@ -100,7 +103,12 @@ fn fs_solid(vertex: VertexOutput) -> @location(0) vec4 { let fill_lambertian_value = dot(hit_normal, sun_direction * vec3(1.0, -1.0, 1.0)); let fill_strength = 0.25; let l = fill_strength * fill_lambertian_value + (1.0 - fill_strength) * sun_lambertian_value * shadow_value; - color_accumulator += 0.25 * vec4(vec3(l), 1.0); + let colormap_color = textureLoad(colormap_texture, + u32(clamp((hit_location.z - uniforms.dem_z_range.x) + / (uniforms.dem_z_range.y - uniforms.dem_z_range.x), 0.0, 1.0) + * 15.9), + 0).rgb; + color_accumulator += 0.25 * vec4(colormap_color * l, 1.0); } } if color_accumulator.a == 0.0 { diff --git a/src/app/dem_renderer/mod.rs b/src/app/dem_renderer/mod.rs index b672a1c..a9d0196 100644 --- a/src/app/dem_renderer/mod.rs +++ b/src/app/dem_renderer/mod.rs @@ -186,6 +186,8 @@ impl DemRenderer { let camera = Camera::new(surface_config.width as f32 / surface_config.height as f32); + let colormap_texture_view = create_colormap_texture(device, queue); + let mut uniforms = UniformBufferManager::new(device); uniforms.set_dem_min_corner(glam::Vec2::new(source.x_min, source.y_min)); uniforms.set_dem_cell_size( @@ -228,6 +230,16 @@ impl DemRenderer { }, count: None, }, + wgpu::BindGroupLayoutEntry { + binding: 3, + visibility: wgpu::ShaderStages::FRAGMENT, + ty: wgpu::BindingType::Texture { + multisampled: false, + sample_type: wgpu::TextureSampleType::Float { filterable: false }, + view_dimension: wgpu::TextureViewDimension::D1, + }, + count: None, + }, ], }); @@ -246,6 +258,10 @@ impl DemRenderer { binding: 2, resource: wgpu::BindingResource::TextureView(&dembvh_texture_view), }, + wgpu::BindGroupEntry { + binding: 3, + resource: wgpu::BindingResource::TextureView(&colormap_texture_view), + }, ], label: Some("DemRendererBindGroup"), }); @@ -505,6 +521,54 @@ fn create_dembvh_texture( texture.create_view(&wgpu::TextureViewDescriptor::default()) } +fn create_colormap_texture(device: &wgpu::Device, queue: &wgpu::Queue) -> wgpu::TextureView { + let texture = device.create_texture(&wgpu::TextureDescriptor { + label: Some("Colormap Texture"), + size: wgpu::Extent3d { + width: 16, + height: 1, + depth_or_array_layers: 1, + }, + mip_level_count: 1, + sample_count: 1, + dimension: wgpu::TextureDimension::D1, + format: wgpu::TextureFormat::Rgba8Unorm, + usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST, + view_formats: &[], + }); + + let colormap_data_rgb = include_bytes!("../../../data/colormap.data"); + assert!(dbg!(colormap_data_rgb.len()) == 16 * 3); + let mut colormap_data = [0 as u8; 16 * 4]; + for i in 0..16 { + for j in 0..3 { + colormap_data[(15 - i) * 4 + j] = colormap_data_rgb[i * 3 + j]; + } + } + + queue.write_texture( + wgpu::TexelCopyTextureInfo { + texture: &texture, + mip_level: 0, + origin: wgpu::Origin3d::ZERO, + aspect: wgpu::TextureAspect::All, + }, + &colormap_data[..], + wgpu::TexelCopyBufferLayout { + offset: 0, + bytes_per_row: Some(std::mem::size_of::() as u32 * 4 * 16), + rows_per_image: None, + }, + wgpu::Extent3d { + width: 16, + height: 1, + depth_or_array_layers: 1, + }, + ); + + texture.create_view(&wgpu::TextureViewDescriptor::default()) +} + fn get_animated_camera_position(animation_phase: f32, dem_size: f32) -> glam::Vec3 { glam::Vec3::new( dem_size * f32::sin(animation_phase),