Skip to content

Commit 34982fe

Browse files
authored
Fix infinite grid zoomout geometry (#187)
* Improve grid ray intersection precision * Adjust names of infinite grid's outputs * Correct unproject_point_no_translation call
1 parent 55b2161 commit 34982fe

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

crates/bevy_infinite_grid/src/render/grid.wgsl

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ struct InfiniteGridSettings {
2020
struct View {
2121
projection: mat4x4<f32>,
2222
inverse_projection: mat4x4<f32>,
23+
/// Expected to be World from view
2324
view: mat4x4<f32>,
25+
/// Expected to be View from world
2426
inverse_view: mat4x4<f32>,
2527
world_position: vec3<f32>,
2628
};
@@ -34,15 +36,33 @@ struct Vertex {
3436
@builtin(vertex_index) index: u32,
3537
};
3638

39+
fn strip_translation(m: mat4x4<f32>) -> mat4x4<f32> {
40+
return mat4x4<f32>(
41+
m[0],
42+
m[1],
43+
m[2],
44+
vec4<f32>(0.0, 0.0, 0.0, 1.0)
45+
);
46+
}
47+
48+
// Transforms point `p` from NDC to World Space Coordinates
3749
fn unproject_point(p: vec3<f32>) -> vec3<f32> {
3850
let unprojected = view.view * view.inverse_projection * vec4<f32>(p, 1.0);
3951
return unprojected.xyz / unprojected.w;
4052
}
4153

54+
// Transforms point `p` from NDC to World Space Coordinates
55+
// but relative to View's translation
56+
fn unproject_point_no_translation(p: vec3<f32>) -> vec3<f32> {
57+
let unprojected = strip_translation(view.view) * view.inverse_projection * vec4<f32>(p, 1.0);
58+
return unprojected.xyz / unprojected.w;
59+
}
60+
4261
struct VertexOutput {
4362
@builtin(position) clip_position: vec4<f32>,
44-
@location(0) near_point: vec3<f32>,
45-
@location(1) far_point: vec3<f32>,
63+
@location(0) ray_origin: vec3<f32>,
64+
@location(1) ray_direction_near: vec3<f32>,
65+
@location(2) ray_direction_far: vec3<f32>,
4666
};
4767

4868
@vertex
@@ -59,8 +79,9 @@ fn vertex(vertex: Vertex) -> VertexOutput {
5979
var out: VertexOutput;
6080

6181
out.clip_position = vec4<f32>(p, 1.);
62-
out.near_point = unproject_point(p);
63-
out.far_point = unproject_point(vec3<f32>(p.xy, 0.001)); // unprojecting on the far plane
82+
out.ray_origin = unproject_point(p);
83+
out.ray_direction_near = unproject_point_no_translation(p);
84+
out.ray_direction_far = unproject_point_no_translation(vec3<f32>(p.xy, 0.001));
6485
return out;
6586
}
6687

@@ -71,8 +92,8 @@ struct FragmentOutput {
7192

7293
@fragment
7394
fn fragment(in: VertexOutput) -> FragmentOutput {
74-
let ray_origin = in.near_point;
75-
let ray_direction = normalize(in.far_point - in.near_point);
95+
let ray_origin = in.ray_origin;
96+
let ray_direction = normalize(in.ray_direction_far - in.ray_direction_near);
7697
let plane_normal = grid_position.normal;
7798
let plane_origin = grid_position.origin;
7899

crates/bevy_infinite_grid/src/render/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,9 @@ struct InfiniteGridBindGroup {
142142
pub struct GridViewUniform {
143143
projection: Mat4,
144144
inverse_projection: Mat4,
145+
/// Expected to be World from view
145146
view: Mat4,
147+
/// Expected to be View from world
146148
inverse_view: Mat4,
147149
world_position: Vec3,
148150
}

0 commit comments

Comments
 (0)