Skip to content

Commit

Permalink
Remove thickness option, add duration, fix various issues. 0.3.0.
Browse files Browse the repository at this point in the history
- Cleaned up the fix for #1.
- Removed `thickness` option, resolved #2.
- Added `duration`, for keeping lines alive for some duration
  • Loading branch information
Toqozz committed Apr 14, 2021
1 parent 7331c32 commit cc86533
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 127 deletions.
6 changes: 1 addition & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bevy_prototype_debug_lines"
version = "0.2.3"
version = "0.3.0"
authors = ["Michael Palmos <toqoz@hotmail.com>"]
edition = "2018"
license = "MIT"
Expand Down Expand Up @@ -37,7 +37,3 @@ required-features = ["example_deps"]
[[example]]
name = "user_lines"
required-features = ["example_deps"]

[[example]]
name = "bug"
required-features = ["example_deps"]
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ This plugin uses a shader and sends individual points to the GPU, which then mov
Add `bevy_prototype_debug_lines` to your `Cargo.toml`:
```toml
[dependencies]
bevy_prototype_debug_lines = "0.2.2"
bevy_prototype_debug_lines = "0.3.0"
```

Add the plugin in your `App::build()` phase:
Expand All @@ -42,8 +42,8 @@ fn some_system(
) {
let start = Vec3::splat(-1.0);
let end = Vec3::splat(1.0);
let thickness = 0.01;
lines.line(start, end, thickness);
let duration = 0.0; // Duration of 0 will show the line for 1 frame.
lines.line(start, end, duration);
}
```

Expand All @@ -57,6 +57,12 @@ cargo run --example 3d --features="example_deps"

Where `3d` is one of the files in [the examples](https://github.com/Toqozz/bevy_debug_lines/tree/master/examples)

## Changes in `0.3.0`
In `0.3.0`, the `thickness` parameter has been removed. I don't believe it provides enough value for the performance, time, or issues.
However, if you feel differently, let me know in [this](https://github.com/Toqozz/bevy_debug_lines/issues/2) issue.

This is technically a non-breaking change (i.e. your code will still compile) because `duration` was added which takes the same spot, but beware that your code still needs to be updated (probably just set old `thickness` values to `0`, if you don't care about duration stuff.).

Let me know if you have any requests, improvements, or suggestions on how to make this crate more ergonomic.
---

Please do not hesitate to let me know if you have any requests, improvements, or suggestions on how to make this crate more ergonomic or otherwise.
Binary file modified demo.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 3 additions & 5 deletions examples/2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,15 @@ fn setup(
}

fn demo(mut lines: ResMut<DebugLines>) {
//let start = Vec3::splat(-200.0);
//let end = Vec3::splat(200.0);
lines.line(Vec3::new(-400.0, 200.0, 0.0), Vec3::new(400.0, 200.0, 0.0), 10.0); // Units are generally "smaller" for 2d, so thickness should be higher.
lines.line(Vec3::new(-400.0, 200.0, 0.0), Vec3::new(400.0, 200.0, 0.0), 0.0); // Units are generally "smaller" for 2d, so thickness should be higher.
lines.line_colored(
Vec3::new(-400.0, 0.0, 0.0), Vec3::new(400.0, 0.0, 0.0),
10.0,
0.0,
Color::GREEN
);
lines.line_gradient(
Vec3::new(-400.0, -200.0, 0.0), Vec3::new(400.0, -200.0, 0.0),
10.0,
0.0,
Color::WHITE,
Color::PINK
);
Expand Down
6 changes: 3 additions & 3 deletions examples/3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fn setup(
fn demo(time: Res<Time>, mut lines: ResMut<DebugLines>) {
let seconds = time.seconds_since_startup() as f32;

lines.line(Vec3::new(-1.0, f32::sin(seconds), 0.0), Vec3::new(1.0, f32::sin(seconds + 3.14), 0.0), 0.01);
lines.line_colored(Vec3::new(f32::sin(seconds), -1.0, 0.0), Vec3::new(f32::sin(seconds + 3.14), 1.0, 0.0), 0.01, Color::WHITE);
lines.line_gradient(Vec3::new(-1.0, -1.0, f32::sin(seconds)), Vec3::new(1.0, 1.0, f32::sin(seconds + 3.14)), 0.01, Color::GOLD, Color::PINK);
lines.line(Vec3::new(-1.0, f32::sin(seconds), 0.0), Vec3::new(1.0, f32::sin(seconds + 3.14), 0.0), 0.0);
lines.line_colored(Vec3::new(f32::sin(seconds), -1.0, 0.0), Vec3::new(f32::sin(seconds + 3.14), 1.0, 0.0), 0.0, Color::WHITE);
lines.line_gradient(Vec3::new(-1.0, -1.0, f32::sin(seconds)), Vec3::new(1.0, 1.0, f32::sin(seconds + 3.14)), 0.0, Color::GOLD, Color::PINK);
}
4 changes: 2 additions & 2 deletions examples/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ fn main() {
.add_plugin(DebugLinesPlugin)
.add_plugin(FrameTimeDiagnosticsPlugin::default())
.add_plugin(WgpuResourceDiagnosticsPlugin::default())
.add_plugin(LogDiagnosticsPlugin::default())
.add_plugin(LogDiagnosticsPlugin { wait_duration: bevy::utils::Duration::new(5, 0), ..Default::default() })
.add_startup_system(setup.system())
.add_system(demo_circle.system())
//.add_system(demo_block.system())
Expand Down Expand Up @@ -64,7 +64,7 @@ fn demo_circle(time: Res<Time>, mut lines: ResMut<DebugLines>) {
}
}

fn demo_block(time: Res<Time>, mut lines: ResMut<DebugLines>) {
fn _demo_block(mut lines: ResMut<DebugLines>) {
use bevy_prototype_debug_lines::MAX_LINES;

const THICKNESS: f32 = 0.01;
Expand Down
6 changes: 3 additions & 3 deletions examples/user_lines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ fn setup(

// User lines are not cleared every frame, so we only need to push them once. We are
// also responsible for removing them, however.
let line1 = Line::new(Vec3::new(-2.0, -1.0, 0.0), Vec3::new(2.0, -1.0, 0.0), 0.01, Color::PINK, Color::GOLD);
let line2 = Line::new(Vec3::new(-2.0, 0.0, 0.0), Vec3::new(2.0, 0.0, 0.0), 0.01, Color::YELLOW, Color::GREEN);
let line3 = Line::new(Vec3::new(-2.0, 1.0, 0.0), Vec3::new(2.0, 1.0, 0.0), 0.01, Color::RED, Color::TEAL);
let line1 = Line::new(Vec3::new(-2.0, -1.0, 0.0), Vec3::new(2.0, -1.0, 0.0), 0.0, Color::PINK, Color::GOLD);
let line2 = Line::new(Vec3::new(-2.0, 0.0, 0.0), Vec3::new(2.0, 0.0, 0.0), 0.0, Color::YELLOW, Color::GREEN);
let line3 = Line::new(Vec3::new(-2.0, 1.0, 0.0), Vec3::new(2.0, 1.0, 0.0), 0.0, Color::RED, Color::TEAL);
lines.user_lines.push(line1);
lines.user_lines.push(line2);
lines.user_lines.push(line3);
Expand Down
94 changes: 47 additions & 47 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#![allow(dead_code)]

use bevy::prelude::*;
use bevy::render::mesh::{VertexAttributeValues, Indices};
use bevy::render::mesh::VertexAttributeValues;
use bevy::render::pipeline::{PrimitiveTopology, PipelineDescriptor, RenderPipeline, PrimitiveState, CullMode };
use bevy::render::shader::{ShaderStages, ShaderStage};
use bevy::render::render_graph::{AssetRenderResourcesNode, RenderGraph};
Expand Down Expand Up @@ -42,33 +42,13 @@ impl Plugin for DebugLinesPlugin {

/// Maximum number of unique lines to draw at once.
pub const MAX_LINES: usize = 128000;
const MAX_POINTS: usize = MAX_LINES * 2;
const VERTICES_PER_LINE: usize = 4;
const TRIANGLES_PER_LINE: usize = 2;
/// Maximum number of points.
pub const MAX_POINTS: usize = MAX_LINES * 2;

fn create_mesh() -> Mesh {
let mut mesh = Mesh::new(PrimitiveTopology::TriangleList);

let positions = vec![[0.0, 0.0, 0.0]; MAX_LINES * VERTICES_PER_LINE];
let mut indices = vec![0; MAX_LINES * TRIANGLES_PER_LINE * 3];
// For each line, we set up indices to make a rectangle out of 2 triangles, connecting the points.
for i in 0..MAX_LINES {
// Index of first triangle for this line.
let idx: usize = i * TRIANGLES_PER_LINE * 3;
// Index of first vertex of this line.
let v_idx: u32 = (i * VERTICES_PER_LINE) as u32;

// Bevy uses a COUNTER-CLOCKWISE WINDING ORDER -- counter-clickwise tri indices are facing the camera.
indices[idx + 0] = v_idx; // v1 top
indices[idx + 1] = v_idx + 1; // v1 bottom
indices[idx + 2] = v_idx + 3; // v2 bottom
indices[idx + 3] = v_idx; // v1 top
indices[idx + 4] = v_idx + 3; // v2 bottom
indices[idx + 5] = v_idx + 2; // v2 top
}

let mut mesh = Mesh::new(PrimitiveTopology::LineList);
let positions = vec![[0.0, 0.0, 0.0]; MAX_LINES * 2];
mesh.set_attribute(Mesh::ATTRIBUTE_POSITION,VertexAttributeValues::Float3(positions.into()));
mesh.set_indices(Some(Indices::U32(indices.into())));

mesh
}
Expand Down Expand Up @@ -147,32 +127,32 @@ pub struct Line {
start: Vec3,
end: Vec3,
color: [Color; 2],
thickness: f32,
duration: f32,
}

impl Line {
pub fn new(start: Vec3, end: Vec3, thickness: f32, start_color: Color, end_color: Color) -> Self {
Self { start, end, color: [start_color, end_color], thickness }
pub fn new(start: Vec3, end: Vec3, duration: f32, start_color: Color, end_color: Color) -> Self {
Self { start, end, color: [start_color, end_color], duration }
}
}

/// Bevy resource providing facilities to draw lines.
///
/// # Usage
/// ```
/// // Draws 3 horizontal lines.
/// // Draws 3 horizontal lines, which disappear after 1 frame.
/// fn some_system(mut lines: ResMut<DebugLines>) {
/// lines.line(Vec3::new(-1.0, 1.0, 0.0), Vec3::new(1.0, 1.0, 0.0), 0.01);
/// lines.line(Vec3::new(-1.0, 1.0, 0.0), Vec3::new(1.0, 1.0, 0.0), 0.0);
/// lines.line_colored(
/// Vec3::new(-1.0, 0.0, 0.0),
/// Vec3::new(1.0, 0.0, 0.0),
/// 0.01,
/// 0.0,
/// Color::WHITE
/// );
/// lines.line_gradient(
/// Vec3::new(-1.0, -1.0, 0.0),
/// Vec3::new(1.0, -1.0, 0.0),
/// 0.01,
/// 0.0,
/// Color::WHITE, Color::PINK
/// );
/// }
Expand Down Expand Up @@ -207,9 +187,9 @@ impl DebugLines {
///
/// * `start` - The start of the line in world space
/// * `end` - The end of the line in world space
/// * `thickness` - Line thickness
pub fn line(&mut self, start: Vec3, end: Vec3, thickness: f32) {
self.line_colored(start, end, thickness, Color::WHITE);
/// * `duration` - Duration (in seconds) that the line should show for -- a value of zero will show the line for 1 frame.
pub fn line(&mut self, start: Vec3, end: Vec3, duration: f32) {
self.line_colored(start, end, duration, Color::WHITE);
}

/// Draw a line in world space with a specified color, or update an existing line
Expand All @@ -218,10 +198,10 @@ impl DebugLines {
///
/// * `start` - The start of the line in world space
/// * `end` - The end of the line in world space
/// * `thickness` - Line thickness
/// * `duration` - Duration (in seconds) that the line should show for -- a value of zero will show the line for 1 frame.
/// * `color` - Line color
pub fn line_colored(&mut self, start: Vec3, end: Vec3, thickness: f32, color: Color) {
self.lines.push(Line::new(start, end, thickness, color, color));
pub fn line_colored(&mut self, start: Vec3, end: Vec3, duration: f32, color: Color) {
self.line_gradient(start, end, duration, color, color);
}

/// Draw a line in world space with a specified gradient color, or update an existing line
Expand All @@ -230,17 +210,26 @@ impl DebugLines {
///
/// * `start` - The start of the line in world space
/// * `end` - The end of the line in world space
/// * `thickness` - Line thickness
/// * `duration` - Duration (in seconds) that the line should show for -- a value of zero will show the line for 1 frame.
/// * `start_color` - Line color
/// * `end_color` - Line color
pub fn line_gradient(&mut self, start: Vec3, end: Vec3, thickness: f32, start_color: Color, end_color: Color) {
self.lines.push(Line::new(start, end, thickness, start_color, end_color));
pub fn line_gradient(&mut self, start: Vec3, end: Vec3, duration: f32, start_color: Color, end_color: Color) {
let line = Line::new(start, end, duration, start_color, end_color);

// If we are at maximum capacity, we push the first line out.
if self.lines.len() == MAX_LINES {
//bevy::log::warn!("Hit max lines, so replaced most recent line.");
self.lines.pop();
}

self.lines.push(line);
}
}

fn draw_lines(
mut assets: ResMut<Assets<LineShader>>,
mut lines: ResMut<DebugLines>,
time: Res<Time>,
query: Query<&Handle<LineShader>>,
) {
// One line changing makes us update all lines.
Expand All @@ -258,10 +247,8 @@ fn draw_lines(
let mut i = 0;
let all_lines = lines.lines.iter().chain(lines.user_lines.iter());
for line in all_lines {
// First point is start of line, second is end.
// point.w property is used for thickness.
shader.points[i] = line.start.extend(line.thickness);
shader.points[i+1] = line.end.extend(line.thickness);
shader.points[i] = line.start.extend(0.0);
shader.points[i+1] = line.end.extend(0.0);
shader.colors[i] = line.color[0].as_rgba_f32().into();
shader.colors[i+1] = line.color[1].as_rgba_f32().into();

Expand All @@ -270,7 +257,7 @@ fn draw_lines(

let count = lines.lines.len() + lines.user_lines.len();
let size = if count > MAX_LINES {
warn!("DebugLines: Maximum number of lines exceeded: line count: {}, max lines: {}", count, MAX_LINES);
bevy::log::warn!("DebugLines: Maximum number of lines exceeded: line count: {}, max lines: {}", count, MAX_LINES);
MAX_LINES
} else {
count
Expand All @@ -280,5 +267,18 @@ fn draw_lines(
}
}

lines.lines.clear();
let mut i = 0;
let mut len = lines.lines.len();
while i != len {
lines.lines[i].time -= time.delta_seconds();
if lines.lines[i].time < 0.0 {
lines.lines.swap(i, len - 1);
len -= 1;
i -= 1;
}

i += 1;
}

lines.lines.truncate(len);
}
7 changes: 3 additions & 4 deletions src/line.frag
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
layout(location = 0) in vec3 v_Position;
//layout(location = 1) in vec3 v_Normal;
layout(location = 1) in vec4 v_Color;
layout(location = 2) flat in int v_Rendered;

layout(location = 0) out vec4 o_Target;

Expand All @@ -11,12 +10,12 @@ layout (set = 0, binding = 0) uniform CameraViewProj {
};

void main() {
vec4 output_color = v_Color;

if (v_Rendered == 0) {
if (v_Color.a < 0) {
discard;
}

vec4 output_color = v_Color;

// Always render.
gl_FragDepth = 0.0;
o_Target = output_color;
Expand Down
Loading

0 comments on commit cc86533

Please sign in to comment.