Skip to content

Commit

Permalink
fix egui tab offset for single pass raymarching distortion
Browse files Browse the repository at this point in the history
  • Loading branch information
FreddyFunk committed Nov 5, 2023
1 parent 9eb841e commit 1d6fccc
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 7 deletions.
44 changes: 41 additions & 3 deletions src/apps/ray_marching_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ struct RayMarchingRendererResources {
uniform_buffer_focal_length: wgpu::Buffer,
uniform_buffer_aspect_ratio: wgpu::Buffer,
uniform_buffer_viewport_size: wgpu::Buffer,
uniform_buffer_viewport_position: wgpu::Buffer,
uniform_buffer_ray_origin: wgpu::Buffer,
uniform_buffer_top_aabb: wgpu::Buffer,
uniform_buffer_bottom_aabb: wgpu::Buffer,
Expand All @@ -39,6 +40,7 @@ impl RayMarchingRendererResources {
focal_length: f32,
aspect_ratio: f32,
viewport_size: glam::Vec2,
viewport_position: glam::Vec2,
ray_origin: glam::Vec3,
top_aabb: glam::Vec3,
bottom_aabb: glam::Vec3,
Expand Down Expand Up @@ -81,6 +83,11 @@ impl RayMarchingRendererResources {
0,
bytemuck::cast_slice(&[viewport_size]),
);
queue.write_buffer(
&self.uniform_buffer_viewport_position,
0,
bytemuck::cast_slice(&[viewport_position]),
);
queue.write_buffer(
&self.uniform_buffer_ray_origin,
0,
Expand Down Expand Up @@ -128,6 +135,7 @@ pub struct RayMarchingRenderer {
focal_length: f32,

Check failure on line 135 in src/apps/ray_marching_renderer.rs

View workflow job for this annotation

GitHub Actions / Clippy

multiple fields are never read

Check warning on line 135 in src/apps/ray_marching_renderer.rs

View workflow job for this annotation

GitHub Actions / Cargo Check x86_64

multiple fields are never read

Check warning on line 135 in src/apps/ray_marching_renderer.rs

View workflow job for this annotation

GitHub Actions / Build for macOS (arm64)

multiple fields are never read

Check warning on line 135 in src/apps/ray_marching_renderer.rs

View workflow job for this annotation

GitHub Actions / Build for macOS (Intel)

multiple fields are never read

Check warning on line 135 in src/apps/ray_marching_renderer.rs

View workflow job for this annotation

GitHub Actions / Unit Tests

multiple fields are never read

Check warning on line 135 in src/apps/ray_marching_renderer.rs

View workflow job for this annotation

GitHub Actions / Build for Linux x86_64 (Ubuntu)

multiple fields are never read

Check warning on line 135 in src/apps/ray_marching_renderer.rs

View workflow job for this annotation

GitHub Actions / Build for Windows

multiple fields are never read
aspect_ratio: f32,
viewport_size: glam::Vec2,
viewport_position: glam::Vec2,
ray_origin: glam::Vec3,
top_aabb: glam::Vec3,
bottom_aabb: glam::Vec3,
Expand Down Expand Up @@ -286,6 +294,15 @@ impl RayMarchingRenderer {
usage: wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::UNIFORM,
});

let viewport_position = glam::Vec2::default();

let uniform_buffer_viewport_position =
device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("viewport size"),
contents: bytemuck::cast_slice(&[viewport_position]),
usage: wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::UNIFORM,
});

let ray_origin = glam::Vec3::default();

let uniform_buffer_ray_origin =
Expand Down Expand Up @@ -426,6 +443,16 @@ impl RayMarchingRenderer {
},
count: None,
},
wgpu::BindGroupLayoutEntry {
binding: 10,
visibility: wgpu::ShaderStages::FRAGMENT,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Uniform,
has_dynamic_offset: false,
min_binding_size: None,
},
count: None,
},
],
});

Expand Down Expand Up @@ -461,18 +488,22 @@ impl RayMarchingRenderer {
},
wgpu::BindGroupEntry {
binding: 6,
resource: uniform_buffer_ray_origin.as_entire_binding(),
resource: uniform_buffer_viewport_position.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 7,
resource: uniform_buffer_top_aabb.as_entire_binding(),
resource: uniform_buffer_ray_origin.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 8,
resource: uniform_buffer_bottom_aabb.as_entire_binding(),
resource: uniform_buffer_top_aabb.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 9,
resource: uniform_buffer_bottom_aabb.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 10,
resource: uniform_buffer_camera_position.as_entire_binding(),
},
],
Expand Down Expand Up @@ -552,6 +583,7 @@ impl RayMarchingRenderer {
uniform_buffer_focal_length,
uniform_buffer_aspect_ratio,
uniform_buffer_viewport_size,
uniform_buffer_viewport_position,
uniform_buffer_ray_origin,
uniform_buffer_top_aabb,
uniform_buffer_bottom_aabb,
Expand Down Expand Up @@ -590,6 +622,7 @@ impl RayMarchingRenderer {
focal_length,
aspect_ratio,
viewport_size,
viewport_position,
ray_origin,
top_aabb,
bottom_aabb,
Expand Down Expand Up @@ -643,6 +676,10 @@ impl RayMarchingRenderer {
x: rect.width(),
y: rect.height(),
};
let viewport_position = Vec2 {
x: rect.left_top().x,
y: rect.left_top().y,
};

let z_near = 0.0001;
let z_far = 10.0;
Expand Down Expand Up @@ -709,6 +746,7 @@ impl RayMarchingRenderer {
focal_length,
aspect_ratio,
viewport_size,
viewport_position,
ray_origin,
top_aabb,
bottom_aabb,
Expand Down
15 changes: 11 additions & 4 deletions src/apps/ray_marching_shader.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,14 @@ var<uniform> aspect_ratio: f32;
@group(2) @binding(5)
var<uniform> viewport_size: vec2<f32>;
@group(2) @binding(6)
var<uniform> ray_origin: vec3<f32>;
var<uniform> viewport_position: vec2<f32>;
@group(2) @binding(7)
var<uniform> top_aabb: vec3<f32>;
var<uniform> ray_origin: vec3<f32>;
@group(2) @binding(8)
var<uniform> bottom_aabb: vec3<f32>;
var<uniform> top_aabb: vec3<f32>;
@group(2) @binding(9)
var<uniform> bottom_aabb: vec3<f32>;
@group(2) @binding(10)
var<uniform> camera_position: vec3<f32>;

fn intersectAABB(ray_origin: vec3<f32>, rayDir: vec3<f32>, boxMin: vec3<f32>, boxMax: vec3<f32>) -> vec2<f32> {
Expand Down Expand Up @@ -109,8 +111,10 @@ fn fs_main(@builtin(position) frag_coord: vec4<f32>) -> @location(0) vec4<f32> {
// let value = textureSample(t_diffuse, s_diffuse, position)[0];
// return vec4<f32>(value, 0.0, 0.0, 1.0);

let local_frag_coord: vec2<f32> = frag_coord.xy - viewport_position;

var ray_direction_xy: vec2<f32> = (2.0 * local_frag_coord.xy / viewport_size - 1.0);

var ray_direction_xy: vec2<f32> = (2.0 * frag_coord.xy / viewport_size - 1.0);
ray_direction_xy.x *= aspect_ratio;
var ray_direction: vec3<f32> = vec3<f32>(ray_direction_xy, -focal_length);
ray_direction = (vec4<f32>(ray_direction, 0.0) * view_model_matrix_without_model_scale).xyz;
Expand Down Expand Up @@ -178,6 +182,9 @@ fn fs_main(@builtin(position) frag_coord: vec4<f32>) -> @location(0) vec4<f32> {
value = vec4<f32>(firstHit, 0.0);
}

// value.x = frag_coord.x / viewport_size.x;
// value.y = frag_coord.y / viewport_size.y;

return value;

// gl_FragDepth = length(position - ray_start) / gl_FragCoord.w;
Expand Down

0 comments on commit 1d6fccc

Please sign in to comment.