diff --git a/src/bin/wireframe/main.rs b/src/bin/wireframe/main.rs index 95e2b3e..8c6eceb 100644 --- a/src/bin/wireframe/main.rs +++ b/src/bin/wireframe/main.rs @@ -22,21 +22,6 @@ struct Vertex { position: [f32; 3], } -impl Vertex { - fn desc() -> wgpu::VertexBufferLayout<'static> { - use std::mem; - wgpu::VertexBufferLayout { - array_stride: mem::size_of::() 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 { color: Vec3, data: wgpu::Buffer, @@ -311,7 +296,23 @@ impl<'a> State<'a> { vertex: wgpu::VertexState { module: &shader, entry_point: "vs_main", - buffers: &[Vertex::desc()], + buffers: &[wgpu::VertexBufferLayout { + array_stride: mem::size_of::() 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::() + mem::offset_of!(Vertex, position)) + as u64, + shader_location: 1, + format: wgpu::VertexFormat::Float32x3, + }, + ], + }], compilation_options: Default::default(), }, fragment: Some(wgpu::FragmentState { @@ -328,7 +329,7 @@ impl<'a> State<'a> { compilation_options: Default::default(), }), primitive: wgpu::PrimitiveState { - topology: wgpu::PrimitiveTopology::LineStrip, + topology: wgpu::PrimitiveTopology::TriangleStrip, ..Default::default() }, 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_vertex_buffer(0, wireframe.data.slice(..)); - render_pass.draw(0..wireframe.size, 0..1); + render_pass.draw(0..4, 0..wireframe.size - 1); } } diff --git a/src/bin/wireframe/ray.wgsl b/src/bin/wireframe/ray.wgsl index a03f6f1..4d5cc4e 100644 --- a/src/bin/wireframe/ray.wgsl +++ b/src/bin/wireframe/ray.wgsl @@ -7,10 +7,15 @@ var camera: CameraUniform; struct LineUniform { color: vec3, } +const width = 0.1; var line: LineUniform; -struct VertexInput { - @location(0) position: vec3, +struct SegmentInput { + @location(0) a: vec3, + @location(1) b: vec3, +} +struct OffsetInput { + @builtin(vertex_index) idx: u32, } struct VertexOutput { @@ -19,15 +24,24 @@ struct VertexOutput { } @vertex -fn vs_main(model: VertexInput) -> VertexOutput { +fn vs_main(seg: SegmentInput, off: OffsetInput) -> VertexOutput { var out: VertexOutput; 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; } @fragment fn fs_main(in: VertexOutput) -> @location(0) vec4 { - let opacity = pow(0.5 - 0.5 * in.clip_position.z, 0.25); - return opacity * vec4(in.vertex_color, 1.0); + return 0.5 * vec4(in.vertex_color, 1.0); }