Crude wide lines

This commit is contained in:
numzero 2024-09-23 22:02:38 +03:00
parent 8736db19a3
commit dca80473d0
2 changed files with 39 additions and 24 deletions

View File

@ -22,21 +22,6 @@ struct Vertex {
position: [f32; 3], position: [f32; 3],
} }
impl Vertex {
fn desc() -> wgpu::VertexBufferLayout<'static> {
use std::mem;
wgpu::VertexBufferLayout {
array_stride: mem::size_of::<Vertex>() as wgpu::BufferAddress,
step_mode: wgpu::VertexStepMode::Vertex,
attributes: &[wgpu::VertexAttribute {
offset: mem::offset_of!(Self, position) as u64,
shader_location: 0,
format: wgpu::VertexFormat::Float32x3,
}],
}
}
}
struct Wireframe { struct Wireframe {
color: Vec3, color: Vec3,
data: wgpu::Buffer, data: wgpu::Buffer,
@ -311,7 +296,23 @@ impl<'a> State<'a> {
vertex: wgpu::VertexState { vertex: wgpu::VertexState {
module: &shader, module: &shader,
entry_point: "vs_main", entry_point: "vs_main",
buffers: &[Vertex::desc()], buffers: &[wgpu::VertexBufferLayout {
array_stride: mem::size_of::<Vertex>() as wgpu::BufferAddress,
step_mode: wgpu::VertexStepMode::Instance,
attributes: &[
wgpu::VertexAttribute {
offset: mem::offset_of!(Vertex, position) as u64,
shader_location: 0,
format: wgpu::VertexFormat::Float32x3,
},
wgpu::VertexAttribute {
offset: (mem::size_of::<Vertex>() + mem::offset_of!(Vertex, position))
as u64,
shader_location: 1,
format: wgpu::VertexFormat::Float32x3,
},
],
}],
compilation_options: Default::default(), compilation_options: Default::default(),
}, },
fragment: Some(wgpu::FragmentState { fragment: Some(wgpu::FragmentState {
@ -328,7 +329,7 @@ impl<'a> State<'a> {
compilation_options: Default::default(), compilation_options: Default::default(),
}), }),
primitive: wgpu::PrimitiveState { primitive: wgpu::PrimitiveState {
topology: wgpu::PrimitiveTopology::LineStrip, topology: wgpu::PrimitiveTopology::TriangleStrip,
..Default::default() ..Default::default()
}, },
depth_stencil: None, depth_stencil: None,
@ -447,7 +448,7 @@ impl<'a> State<'a> {
}; };
render_pass.set_push_constants(ShaderStages::VERTEX, 0, bytemuck::bytes_of(&line)); render_pass.set_push_constants(ShaderStages::VERTEX, 0, bytemuck::bytes_of(&line));
render_pass.set_vertex_buffer(0, wireframe.data.slice(..)); render_pass.set_vertex_buffer(0, wireframe.data.slice(..));
render_pass.draw(0..wireframe.size, 0..1); render_pass.draw(0..4, 0..wireframe.size - 1);
} }
} }

View File

@ -7,10 +7,15 @@ var<uniform> camera: CameraUniform;
struct LineUniform { struct LineUniform {
color: vec3<f32>, color: vec3<f32>,
} }
const width = 0.1;
var<push_constant> line: LineUniform; var<push_constant> line: LineUniform;
struct VertexInput { struct SegmentInput {
@location(0) position: vec3<f32>, @location(0) a: vec3<f32>,
@location(1) b: vec3<f32>,
}
struct OffsetInput {
@builtin(vertex_index) idx: u32,
} }
struct VertexOutput { struct VertexOutput {
@ -19,15 +24,24 @@ struct VertexOutput {
} }
@vertex @vertex
fn vs_main(model: VertexInput) -> VertexOutput { fn vs_main(seg: SegmentInput, off: OffsetInput) -> VertexOutput {
var out: VertexOutput; var out: VertexOutput;
out.vertex_color = line.color; out.vertex_color = line.color;
out.clip_position = camera.mvp * vec4(model.position, 1.0); let a = camera.mvp * vec4(seg.a, 1.);
let b = camera.mvp * vec4(seg.b, 1.);
let dir = normalize(b.xy / b.w - a.xy / a.w);
let d = width * vec4(-dir.y, dir.x, 0., 0.);
switch (off.idx) {
case 0u: { out.clip_position = a - d; }
case 1u: { out.clip_position = a + d; }
case 2u: { out.clip_position = b - d; }
case 3u: { out.clip_position = b + d; }
default: {}
}
return out; return out;
} }
@fragment @fragment
fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> { fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
let opacity = pow(0.5 - 0.5 * in.clip_position.z, 0.25); return 0.5 * vec4(in.vertex_color, 1.0);
return opacity * vec4(in.vertex_color, 1.0);
} }