Add colours

This commit is contained in:
Matthew Gordon 2025-06-02 15:10:28 -03:00
parent 6bac62f587
commit b3deecc02f
4 changed files with 77 additions and 1 deletions

1
.gitattributes vendored
View File

@ -1 +1,2 @@
*.jpg filter=lfs diff=lfs merge=lfs -text
*.data filter=lfs diff=lfs merge=lfs -text

BIN
data/colormap.data (Stored with Git LFS) Normal file

Binary file not shown.

View File

@ -21,6 +21,9 @@ var dem_texture: texture_2d<u32>;
@group(0) @binding(2)
var dembvh_texture: texture_2d<u32>;
@group(0) @binding(3)
var colormap_texture: texture_1d<f32>;
//#include ray_intersection.wgsl
@vertex
@ -100,7 +103,12 @@ fn fs_solid(vertex: VertexOutput) -> @location(0) vec4<f32> {
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<f32>(vec3<f32>(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<f32>(colormap_color * l, 1.0);
}
}
if color_accumulator.a == 0.0 {

View File

@ -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::<u8>() 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),