Make lookup point configurable

This commit is contained in:
numzero 2024-12-24 02:27:58 +03:00
parent ad9515d8d6
commit 884ea5e15b
3 changed files with 30 additions and 16 deletions

View File

@ -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.,
},
);

View File

@ -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);
}
}

View File

@ -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<uniform> 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);