Skip to content

Commit

Permalink
Fix drawing debug lines from behind clip plane
Browse files Browse the repository at this point in the history
  • Loading branch information
Frizi committed May 18, 2019
1 parent cba60ab commit caf7343
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 22 deletions.
31 changes: 19 additions & 12 deletions amethyst_rendy/shaders/vertex/debug_lines.vert
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,27 @@ layout(location = 0) out VertexData {
} vertex;

void main() {
float factor = float((gl_VertexIndex & 2) >> 1);
vertex.color = mix(color_a, color_b, factor);

mat4 proj_view = proj * view;
vec4 projected_a = proj_view * vec4(position_a, 1.0);
vec4 projected_b = proj_view * vec4(position_b, 1.0);
vec2 screen_a = projected_a.xy / projected_a.w;
vec2 screen_b = projected_b.xy / projected_b.w;

vec2 normal = normalize((screen_b - screen_a)) * dir_mats[gl_VertexIndex & 1];
float factor = float((gl_VertexIndex & 2) >> 1);

normal *= mix(projected_a.w, projected_b.w, factor) * 1.0;
normal.x /= screen_space_thickness.x;
normal.y /= screen_space_thickness.y;

vertex.color = mix(color_a, color_b, factor);
gl_Position = mix(projected_a, projected_b, factor) + vec4(normal, 0.0, 0.0);
vec4 proj_current = mix(projected_a, projected_b, factor);

if (proj_current.w < 0) {
// vertex behind camera clip plane
vec4 proj_next = mix(projected_b, projected_a, factor);
vec3 clip_space_dir = normalize(proj_current.xyw - proj_next.xyw);
float coef = -proj_current.w / clip_space_dir.z;
vec3 intersect_pos = proj_current.xyw + (clip_space_dir * coef);
gl_Position = vec4(intersect_pos.x, intersect_pos.y, 0, intersect_pos.z);
} else {
vec2 screen_a = projected_a.xy / projected_a.w;
vec2 screen_b = projected_b.xy / projected_b.w;
vec2 dir = normalize(screen_b - screen_a);
vec2 normal = dir * dir_mats[gl_VertexIndex & 1];
normal *= proj_current.w * screen_space_thickness;
gl_Position = proj_current + vec4(normal, 0.0, 0.0);
}
}
4 changes: 2 additions & 2 deletions amethyst_rendy/src/pass/debug_lines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ impl<B: Backend> RenderGroup<B, Resources> for DrawDebugLines<B> {
index,
DebugLinesArgs {
screen_space_thickness: [
self.framebuffer_width / (line_width * 2.0),
self.framebuffer_height / (line_width * 2.0),
(line_width * 2.0) / self.framebuffer_width,
(line_width * 2.0) / self.framebuffer_height,
]
.into(),
}
Expand Down
10 changes: 3 additions & 7 deletions examples/assets/prefab/renderable.ron
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@ Prefab (
graphics: (
mesh: Asset(File("mesh/lid.obj", ("OBJ", ()))),
material: (
albedo: Generate(Srgba(1.0, 0.0, 0.0, 1.0)),
albedo: Generate(Srgba(0.0, 0.0, 1.0, 1.0)),
),
),
transform: (
translation: (5.0, 1.0, 5.0),
// rotation: (0.5, -0.5, -0.5, 0.5),
),
),
),
Expand All @@ -25,12 +24,11 @@ Prefab (
graphics: (
mesh: Asset(File("mesh/teapot.obj", ("OBJ", ()))),
material: (
albedo: Generate(Srgba(1.0, 0.0, 0.0, 1.0)),
albedo: Generate(Srgba(0.0, 1.0, 0.0, 1.0)),
),
),
transform: (
translation: (5.0, 1.0, 5.0),
// rotation: (0.5, -0.5, -0.5, 0.5),
),
),
),
Expand Down Expand Up @@ -136,9 +134,7 @@ Prefab (
(
data: (
transform: Transform (
translation: (0.0, 10.0, 20.0),
// translation: (0.0, 0.0, 20.0),
// rotation: (0.6087614, 0.0, 0.0, 0.7933533),
translation: (0.0, 20.0, 20.0),
rotation: (-0.4, 0.0, 0.0, 0.862),

),
Expand Down
2 changes: 1 addition & 1 deletion examples/custom_game_data/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use amethyst::{
},
core::transform::TransformBundle,
ecs::{
prelude::{Component, Entity, ReadExpect, Resources, System, SystemData},
prelude::{Component, Entity, ReadExpect, Resources, SystemData},
NullStorage,
},
input::{is_close_requested, is_key_down, InputBundle, StringBindings},
Expand Down

0 comments on commit caf7343

Please sign in to comment.