From 884ea5e15b269d2f20424ab37d1a27103a3a7f02 Mon Sep 17 00:00:00 2001 From: numzero Date: Tue, 24 Dec 2024 02:27:58 +0300 Subject: [PATCH] Make lookup point configurable --- src/bin/envmap/main.rs | 10 ++++------ src/bin/envmap/perlin.rs | 21 +++++++++++++++------ src/bin/envmap/perlin.wgsl | 15 +++++++++++---- 3 files changed, 30 insertions(+), 16 deletions(-) diff --git a/src/bin/envmap/main.rs b/src/bin/envmap/main.rs index cff6101..98fb418 100644 --- a/src/bin/envmap/main.rs +++ b/src/bin/envmap/main.rs @@ -15,9 +15,9 @@ fn make_viewport(w: u32, h: u32) -> [Vertex; 4] { let h = h as f32; let (w, h) = (1.0f32.max(w / h), 1.0f32.max(h / w)); let screen_coord = [vec2(-1., -1.), vec2(1., -1.), vec2(-1., 1.), vec2(1., 1.)]; - let world_coord = [vec3(-w, -h, 0.), vec3(w, -h, 0.), vec3(-w, h, 0.), vec3(w, h, 0.)]; + let world_coord = [vec3(-w, -h, 2.), vec3(w, -h, 2.), vec3(-w, h, 2.), vec3(w, h, 2.)]; [0, 1, 2, 3].map(|k| Vertex { - world: 10. * world_coord[k], + dir: world_coord[k], screen: screen_coord[k], }) } @@ -36,10 +36,8 @@ fn main() { noiser.set_params( &queue, perlin::Params { - seed: 42, - layers: 8, - roughness: 0.9, - scale: 1.7, + origin: vec3(0., 0., -12.), + radius: 12., }, ); diff --git a/src/bin/envmap/perlin.rs b/src/bin/envmap/perlin.rs index 4a2b725..3e20c0b 100644 --- a/src/bin/envmap/perlin.rs +++ b/src/bin/envmap/perlin.rs @@ -9,22 +9,21 @@ use glam::{Vec2, Vec3}; #[derive(Debug, Clone, Copy, Pod, Zeroable)] #[repr(C)] pub struct Params { - pub seed: u32, - pub layers: u32, - pub roughness: f32, - pub scale: f32, + pub origin: Vec3, + pub radius: f32, } #[derive(Debug, Clone, Copy, Pod, Zeroable)] #[repr(C)] pub struct Vertex { - pub world: Vec3, + pub dir: Vec3, pub screen: Vec2, } pub struct Pipeline { view_buf: wgpu::Buffer, params_buf: wgpu::Buffer, + bindings: wgpu::BindGroup, pipeline: wgpu::RenderPipeline, } @@ -66,7 +65,7 @@ impl Pipeline { }, wgpu::VertexAttribute { shader_location: 1, - offset: offset_of!(Vertex, world) as u64, + offset: offset_of!(Vertex, dir) as u64, format: wgpu::VertexFormat::Float32x3, }, ], @@ -95,9 +94,18 @@ impl Pipeline { multiview: None, cache: None, }); + let bindings = device.create_bind_group(&wgpu::BindGroupDescriptor { + label: None, + layout: &pipeline.get_bind_group_layout(0), + entries: &[wgpu::BindGroupEntry { + binding: 0, + resource: params_buf.as_entire_binding(), + }], + }); Self { view_buf, params_buf, + bindings, pipeline, } } @@ -113,6 +121,7 @@ impl Pipeline { pub fn render(&self, pass: &mut wgpu::RenderPass) { pass.set_pipeline(&self.pipeline); pass.set_vertex_buffer(0, self.view_buf.slice(..)); + pass.set_bind_group(0, &self.bindings, &[]); pass.draw(0..4, 0..1); } } diff --git a/src/bin/envmap/perlin.wgsl b/src/bin/envmap/perlin.wgsl index c478595..d9de939 100644 --- a/src/bin/envmap/perlin.wgsl +++ b/src/bin/envmap/perlin.wgsl @@ -5,24 +5,31 @@ struct Params { scale: f32, } +struct LookParams { + origin: vec3f, + radius: f32, +} + struct Vertex { @location(0) screen: vec2f, - @location(1) world: vec3f, + @location(1) dir: vec3f, } struct Varying { - @location(0) world: vec3f, + @location(0) dir: vec3f, @builtin(position) screen: vec4f, } +@group(0) @binding(0) var params: LookParams; + @vertex fn on_vertex(in: Vertex) -> Varying { - return Varying(in.world, vec4(in.screen, 0.0, 1.0)); + return Varying(in.dir, vec4(in.screen, 0.0, 1.0)); } @fragment fn on_fragment(in: Varying) -> @location(0) vec4f { - let point = 30. * (normalize(in.world + vec3(0., 0., 30.)) - vec3(0., 0., 1.)); + let point = params.origin + params.radius * normalize(in.dir); let sharp_area = perlin_noise(Params(1, 3, 0.9, 2.0), 0.1 * point); let sharp_base = perlin_noise(Params(1, 6, 0.9, 2.0), 0.1 * point); let cloud_base = perlin_noise(Params(2, 8, 0.6, 2.0), 0.1 * point);