Skip to content

Commit bc49414

Browse files
authored
Update to new renderer (#10)
* Rework to use new bevy rendering pipeline Regressions: * Does not work in 2d anymore * The depth check option is now always on * Removed user Lines (not sure what their purpose was tbh) Improvements: * Does work with the new renerer * Uses wgsl I wrote TODOs laying out improvement paths. There are a few performance low-hanging fruits available. The depth check is kind of a monster. I wanted to have something out before trying it, because I'm strongly considering a uniform for this, so that the depth test can be modified at run time and is probably going to tank performance. Uploading uniforms to GPU under the new renderer requires an awful lot of additional code, due to the nature of wgpu, but it seems completely possible, just check the `shader_material` example in the bevy repository. * Separate retained and immediate lines The persisting lines now are treated and stored separately from the temporary ones. This simplifies the logic for the temporary ones, and since it's the general case, it should improve performance. * Remove indices of immediate mod line meshes With bevyengine/bevy#3415 it is now possible to upload meshes to GPU without `indices` set. Before, it would panic. Removing `indices` enables us to simply remove the `ImmLinesStorage::fill_indices` code, since the ordering is exactly as the GPU would interpret the mesh otherwise. * follow bevy main HEAD * Add back depth check configuration It is configured through the plugin constructor now. It makes more sense than through the `DebugLines` resource, since it's configured at the beginning and won't change in the middle of running the plugin. This was already true before, the difference is that it is now made explicit to the user. * Refactor LineStorage to use line over vertex index At the cost of making the `RetLineStorage::fill_indices` and `mark_expired` methods a bit more complex, it is possible to only store the line index rather than two point indices of expired lines. This should encapsulate neatly the complex underlying storage that mirrors the mesh attribute buffers. Other side benefit is that `timestamp` and `expired` vectors have their size divided by two. I also added the `Line<T>` struct to make the arguments to mutating methods of LineStorage more understandable. * Remove references to thickness in examples * Refactor and document Various name changes and extra doc string. Also fixes a likely issue with `mark_expired`, which would add duplicates to the `expired` stack. * Add back 2d renderning This however, requires adding a feature flag :/ * Update bevy dependency to 0.6 * Revert to exposing DebugLines as a bevy Resource * Fix minor spelling errors * Do not bump already-bumped version
1 parent 811d4a6 commit bc49414

15 files changed

+776
-402
lines changed

Cargo.toml

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
authors = ["Michael Palmos <[email protected]>"]
33
categories = ["game-engines", "rendering", "game-development"]
44
description = "A prototype plugin providing a simple line drawing API for bevy."
5-
edition = "2018"
5+
edition = "2021"
66
homepage = "https://github.com/Toqozz/bevy_debug_lines"
77
keywords = ["debug", "line", "graphics", "bevy", "drawing"]
88
license = "MIT"
@@ -13,37 +13,34 @@ version = "0.4.0"
1313
exclude = ["demo.gif", "demo_2.png", "demo_2.webm"]
1414

1515
[dependencies]
16-
bevy = "0.5"
17-
18-
[patch.crates-io]
19-
bevy = { git = "https://github.com/bevyengine/bevy", default-features = false, features = [
20-
"render",
21-
] }
16+
bevy = { version = "0.6", default-features = false, features = [ "render" ] }
2217

2318
[features]
19+
example_deps_2d = [
20+
"bevy/bevy_winit",
21+
"bevy/bevy_gltf",
22+
"bevy/x11",
23+
]
2424
example_deps = [
25-
"bevy/bevy_wgpu",
2625
"bevy/bevy_winit",
2726
"bevy/bevy_gltf",
2827
"bevy/x11",
28+
"3d",
2929
]
30+
3d = []
3031

3132
[[example]]
3233
name = "3d"
3334
required-features = ["example_deps"]
3435

3536
[[example]]
3637
name = "2d"
37-
required-features = ["example_deps"]
38+
required-features = ["example_deps_2d"]
3839

3940
[[example]]
4041
name = "bench"
4142
required-features = ["example_deps"]
4243

43-
[[example]]
44-
name = "user_lines"
45-
required-features = ["example_deps"]
46-
4744
[[example]]
4845
name = "depth_test"
4946
required-features = ["example_deps"]

README.md

Lines changed: 59 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,36 @@ This plugin uses a shader and sends individual points to the GPU, which then mov
1717
Add `bevy_prototype_debug_lines` to your `Cargo.toml`:
1818
```toml
1919
[dependencies]
20-
bevy_prototype_debug_lines = "0.3.2"
20+
bevy_prototype_debug_lines = "0.4.0"
2121
```
2222

23-
Add the plugin in your `App::build()` phase:
23+
If you are using bevy_debug_lines for 3d rendering, you must add the `3d`
24+
feature like so:
25+
```toml
26+
[dependencies]
27+
bevy_prototype_debug_lines = { version = "0.4.0", features = ["3d"] }
28+
```
29+
30+
31+
Add the plugin in your `App::new()` phase:
2432
```rust
2533
use bevy::prelude::*;
2634
use bevy_prototype_debug_lines::*;
2735

2836
fn main() {
29-
App::build()
37+
App::new()
3038
.add_plugins(DefaultPlugins)
31-
.add_plugin(DebugLinesPlugin)
32-
...
39+
.add_plugin(DebugLinesPlugin::default())
40+
// ...
3341
.run();
3442
}
3543
```
3644

3745
Draw a line in whatever system you have using the `DebugLines` resource:
3846
```rust
3947
fn some_system(
40-
...
41-
mut lines: ResMut<DebugLines>
48+
// ...
49+
mut lines: ResMut<DebugLines>,
4250
) {
4351
let start = Vec3::splat(-1.0);
4452
let end = Vec3::splat(1.0);
@@ -47,24 +55,46 @@ fn some_system(
4755
}
4856
```
4957

50-
Some options, such as depth testing, can be changed by inserting the `DebugLines` resource yourself:
58+
Some options, such as depth testing, can be changed by using the
59+
`DebugLinesPlugin::always_in_front()` method:
5160

5261
```rust
5362
use bevy::prelude::*;
5463
use bevy_prototype_debug_lines::*;
5564

5665
fn main() {
57-
App::build()
66+
App::new()
5867
.add_plugins(DefaultPlugins)
59-
.add_plugin(DebugLinesPlugin)
60-
.insert_resource(DebugLines { depth_test: true, ..Default::default() })
61-
...
68+
.add_plugin(DebugLinesPlugin::always_in_front())
69+
// ...
6270
.run();
6371
}
6472
```
6573

6674
See [the examples](https://github.com/Toqozz/bevy_debug_lines/tree/master/examples) for more complete usage examples.
6775

76+
## Troubleshooting
77+
78+
### Lines do not show up
79+
80+
**Problem**: Lines do not show up on screen, even if I added the `DebugLinesPlugin` and
81+
used `DebugLines::lines`
82+
83+
**Solution**: Check the dimension feature of `bevy_debug_lines`, when running your game,
84+
there should be a log message looking like:
85+
```
86+
INFO bevy_prototype_debug_lines: Loaded 2d debug lines plugin.
87+
```
88+
Pay attention to **`Loaded 2d debug`** this should match what you are using in
89+
your game. Is it a 3d game? If so, you should add the
90+
`bevy_prototype_debug_lines/3d` feature flag to your `Cargo.toml`. It should
91+
look like this:
92+
93+
```toml
94+
bevy_prototype_debug_lines = { version = "0.4.0", features = ["3d"] }
95+
```
96+
97+
6898
## Running Examples
6999
You can run the examples like so:
70100
```shell
@@ -79,10 +109,27 @@ However, if you feel differently, let me know in [this](https://github.com/Toqoz
79109

80110
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.).
81111

112+
## Changes in `0.4.0`
113+
114+
* Complete rewrite
115+
* Support bevy 0.6
116+
* Use a wgsl shader, which should improve web compatibility
117+
* The depth check is not supported through the `DebugLines.depth_check` field
118+
anymore. You need to set it when initializing the plugin. By default depth
119+
test is enabled but can be disabled with:
120+
`.add_plugin(DebugLinesPlugin::always_in_front())`
121+
* `DebugLinesPlugin` now has a constructor, you should replace `.add_plugin(DebugLinesPlugin)`
122+
in your code by `.add_plugin(DebugLinesPlugin::default())`.
123+
* Added the `3d` feature. Due to the changes in the way the plugin works, it is
124+
now necessary to separate the 2d and 3d plugins. If you are using
125+
`bevy_debug_lines` in a 3d game, please add the `features = ["3d"] ` line to
126+
your `bevy_prototype_debug_lines` dependency.
127+
82128
## Bevy Version Support
83129

84130
| bevy | bevy_prototype_debug_lines |
85131
| --- | --- |
132+
| 0.6 | 0.4 |
86133
| 0.5 | 0.3 |
87134
| 0.4 | 0.2.1 |
88135

examples/2d.rs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ fn main() {
66
App::new()
77
.insert_resource(Msaa { samples: 4 })
88
.add_plugins(DefaultPlugins)
9-
.add_plugin(DebugLinesPlugin)
10-
.add_startup_system(setup.system())
11-
.add_system(demo.system())
9+
.add_plugin(DebugLinesPlugin::always_in_front())
10+
.add_startup_system(setup)
11+
.add_system(demo)
1212
.run();
1313
}
1414

@@ -24,11 +24,11 @@ fn demo(mut lines: ResMut<DebugLines>) {
2424
Vec3::new(-400.0, 200.0, 0.0),
2525
Vec3::new(400.0, 200.0, 0.0),
2626
0.0,
27-
); // Units are generally "smaller" for 2d, so thickness should be higher.
27+
);
2828
lines.line_colored(
2929
Vec3::new(-400.0, 0.0, 0.0),
3030
Vec3::new(400.0, 0.0, 0.0),
31-
0.0,
31+
0.9,
3232
Color::GREEN,
3333
);
3434
lines.line_gradient(
@@ -38,4 +38,18 @@ fn demo(mut lines: ResMut<DebugLines>) {
3838
Color::WHITE,
3939
Color::PINK,
4040
);
41+
lines.line_gradient(
42+
Vec3::new(-100.0, 100.0, 0.0),
43+
Vec3::new(100.0, -100.0, 0.0),
44+
0.8,
45+
Color::WHITE,
46+
Color::PINK,
47+
);
48+
lines.line_gradient(
49+
Vec3::new(-100.0, -100.0, 0.0),
50+
Vec3::new(100.0, 100.0, 0.0),
51+
0.3,
52+
Color::MIDNIGHT_BLUE,
53+
Color::YELLOW_GREEN,
54+
);
4155
}

examples/3d.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,25 @@ fn main() {
66
App::new()
77
.insert_resource(Msaa { samples: 4 })
88
.add_plugins(DefaultPlugins)
9-
.add_plugin(DebugLinesPlugin)
10-
.add_startup_system(setup.system())
11-
.add_system(demo.system())
9+
.add_plugin(DebugLinesPlugin::default())
10+
.add_startup_system(setup)
11+
.add_system(demo)
1212
.run();
1313
}
1414

15-
fn setup(mut commands: Commands) {
15+
fn setup(mut commands: Commands, mut lines: ResMut<DebugLines>) {
1616
commands.spawn_bundle(PerspectiveCameraBundle {
1717
transform: Transform::from_translation(Vec3::new(0.0, 0.0, 5.0)),
1818
..Default::default()
1919
});
20+
// A line that stays on screen 9 seconds
21+
lines.line_gradient(
22+
Vec3::new(1.0, -1.0, -1.0),
23+
Vec3::new(-1.0, 1.0, 1.0),
24+
9.0,
25+
Color::CYAN,
26+
Color::MIDNIGHT_BLUE,
27+
);
2028
}
2129

2230
fn demo(time: Res<Time>, mut lines: ResMut<DebugLines>) {

examples/bench.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
11
use bevy::diagnostic::FrameTimeDiagnosticsPlugin;
22
use bevy::diagnostic::LogDiagnosticsPlugin;
33
use bevy::prelude::*;
4-
use bevy::wgpu::diagnostic::WgpuResourceDiagnosticsPlugin;
54

65
use bevy_prototype_debug_lines::{DebugLines, DebugLinesPlugin};
76

87
fn main() {
98
App::new()
109
.add_plugins(DefaultPlugins)
11-
.add_plugin(DebugLinesPlugin)
10+
.add_plugin(DebugLinesPlugin::default())
1211
.add_plugin(FrameTimeDiagnosticsPlugin::default())
13-
.add_plugin(WgpuResourceDiagnosticsPlugin::default())
1412
.add_plugin(LogDiagnosticsPlugin {
1513
wait_duration: bevy::utils::Duration::new(5, 0),
1614
..Default::default()
1715
})
18-
.add_startup_system(setup.system())
19-
.add_system(demo_circle.system())
20-
//.add_system(demo_block.system())
16+
.add_startup_system(setup)
17+
.add_system(demo_circle)
18+
//.add_system(demo_block)
2119
.run();
2220
}
2321

@@ -33,7 +31,7 @@ fn demo_circle(time: Res<Time>, mut lines: ResMut<DebugLines>) {
3331
use std::f32::consts::PI;
3432

3533
const RADIUS: f32 = 1.5;
36-
const THICKNESS: f32 = 0.001;
34+
const DURATION: f32 = 0.0;
3735

3836
let seconds = 0.5 * time.seconds_since_startup() as f32;
3937

@@ -61,14 +59,14 @@ fn demo_circle(time: Res<Time>, mut lines: ResMut<DebugLines>) {
6159
let start_color = Color::rgba(start.x, start.y, 0.5, start.z.max(0.5));
6260
let end_color = Color::rgba(end.x, end.y, 0.5, end.z.max(0.5));
6361

64-
lines.line_gradient(start, end, THICKNESS, start_color, end_color);
62+
lines.line_gradient(start, end, DURATION, start_color, end_color);
6563
}
6664
}
6765

68-
fn _demo_block(mut lines: ResMut<DebugLines>) {
66+
fn _demo_block(mut lines: DebugLines) {
6967
use bevy_prototype_debug_lines::MAX_LINES;
7068

71-
const THICKNESS: f32 = 0.01;
69+
const DURATION: f32 = 10.0;
7270
const X: f32 = 2.0;
7371
const Y: f32 = 1.0;
7472

@@ -81,6 +79,6 @@ fn _demo_block(mut lines: ResMut<DebugLines>) {
8179
let start_color = Color::rgba(start.x, start.y, 0.5, 1.0);
8280
let end_color = Color::rgba(end.x, end.y, 0.5, 1.0);
8381

84-
lines.line_gradient(start, end, THICKNESS, start_color, end_color);
82+
lines.line_gradient(start, end, DURATION, start_color, end_color);
8583
}
8684
}

examples/depth_test.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,7 @@ fn main() {
66
App::new()
77
.insert_resource(Msaa { samples: 4 })
88
.add_plugins(DefaultPlugins)
9-
.add_plugin(DebugLinesPlugin)
10-
.insert_resource(DebugLines {
11-
depth_test: true,
12-
..Default::default()
13-
})
9+
.add_plugin(DebugLinesPlugin::always_in_front())
1410
.add_startup_system(setup.system())
1511
.add_system(demo.system())
1612
.run();

examples/movement.rs

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
use bevy::{input::mouse::MouseMotion, prelude::*};
22

3-
use bevy_prototype_debug_lines::{ DebugLinesPlugin, DebugLines };
3+
use bevy_prototype_debug_lines::{DebugLines, DebugLinesPlugin};
44

55
fn main() {
6-
App::build()
6+
App::new()
77
.insert_resource(Msaa { samples: 4 })
88
.add_plugins(DefaultPlugins)
9-
.add_plugin(DebugLinesPlugin)
9+
.add_plugin(DebugLinesPlugin::default())
1010
.add_startup_system(setup.system())
11-
.add_system_to_stage(CoreStage::Last, move_with_mouse.system().before("draw_lines"))
11+
.add_system_to_stage(CoreStage::Last, move_with_mouse.before("draw_lines"))
1212
.run();
1313
}
1414

@@ -22,15 +22,20 @@ fn setup(
2222
..Default::default()
2323
});
2424

25-
commands.spawn_bundle(PbrBundle {
26-
mesh: meshes.add(Mesh::from(shape::Cube { size: 0.1 })),
27-
material: materials.add(StandardMaterial { base_color: Color::RED, ..Default::default() }),
28-
transform: Transform::from_xyz(0.0, 0.0, -0.5),
29-
..Default::default()
30-
})
31-
.insert(MoveWithMouse);
25+
commands
26+
.spawn_bundle(PbrBundle {
27+
mesh: meshes.add(Mesh::from(shape::Cube { size: 0.1 })),
28+
material: materials.add(StandardMaterial {
29+
base_color: Color::RED,
30+
..Default::default()
31+
}),
32+
transform: Transform::from_xyz(0.0, 0.0, -0.5),
33+
..Default::default()
34+
})
35+
.insert(MoveWithMouse);
3236
}
3337

38+
#[derive(Component)]
3439
struct MoveWithMouse;
3540
fn move_with_mouse(
3641
mut mouse_motion: EventReader<MouseMotion>,
@@ -46,6 +51,11 @@ fn move_with_mouse(
4651
let movement = Vec3::new(delta.x, -delta.y, 0.0) * 0.01;
4752
transform.translation += movement;
4853
let forward = transform.local_z();
49-
lines.line_colored(transform.translation, transform.translation + forward, 0.0, Color::GREEN);
54+
lines.line_colored(
55+
transform.translation,
56+
transform.translation + forward,
57+
0.0,
58+
Color::GREEN,
59+
);
5060
}
51-
}
61+
}

0 commit comments

Comments
 (0)