Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
edition = "2021"
name = "bevy_polyline"
version = "0.13.0"
version = "0.14.0"
description = "Polyline Rendering for Bevy"
license = "MIT OR Apache-2.0"
repository = "https://github.com/ForesightMiningSoftwareCorporation/bevy_polyline"
Expand All @@ -17,20 +17,20 @@ authors = [
]

[dependencies]
bitflags = "2.3"
bevy = { version = "0.17", default-features = false, features = [
bitflags = "2.11"
bevy = { version = "0.18", default-features = false, features = [
"bevy_core_pipeline",
"bevy_render",
"bevy_asset",
] }
bevy_post_process = "0.17"
bytemuck = "1.16.1"
bevy_post_process = "0.18"
bytemuck = "1.25"

[dev-dependencies]
lazy_static = "1.4.0"
rand = "0.8.4"
ringbuffer = "0.15"
bevy = { version = "0.17", default-features = false, features = [
lazy_static = "1.5.0"
rand = "0.10"
ringbuffer = "0.16"
bevy = { version = "0.18", default-features = false, features = [
"bevy_window",
"bevy_winit",
"bevy_pbr",
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ We intend to track the `main` branch of Bevy. PRs supporting this are welcome!

| bevy | bevy_polyline |
| ---- | ------------- |
| 0.18 | 0.14 |
| 0.17 | 0.13 |
| 0.16 | 0.12 |
| 0.15 | 0.11 |
Expand Down
16 changes: 8 additions & 8 deletions examples/nbody.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use bevy_polyline::prelude::*;
use bevy_post_process::bloom::Bloom;

use lazy_static::*;
use rand::{prelude::*, Rng};
use rand::prelude::*;
use ringbuffer::{ConstGenericRingBuffer, RingBuffer};

const NUM_BODIES: usize = 512;
Expand Down Expand Up @@ -44,14 +44,14 @@ fn setup(
) {
let mut rng = StdRng::seed_from_u64(0);
for _index in 0..NUM_BODIES {
let r = rng.gen_range(2f32..800f32);
let theta = rng.gen_range(0f32..2.0 * PI);
let r = rng.random_range(2f32..800f32);
let theta = rng.random_range(0f32..2.0 * PI);
let position = Vec3A::new(
r * f32::cos(theta),
rng.gen_range(-500f32..500f32),
rng.random_range(-500f32..500f32),
r * f32::sin(theta),
);
let size = rng.gen_range(50f32..1000f32);
let size = rng.random_range(50f32..1000f32);
commands.spawn((
Body {
mass: size,
Expand All @@ -67,7 +67,7 @@ fn setup(
material: PolylineMaterialHandle(
polyline_materials.add(PolylineMaterial {
width: (size * 0.1).powf(1.8),
color: Color::hsl(rng.gen_range(0.0..360.0), 1.0, rng.gen_range(1.2..3.0))
color: Color::hsl(rng.random_range(0.0..360.0), 1.0, rng.random_range(1.2..3.0))
.to_linear(),
perspective: true,
..Default::default()
Expand Down Expand Up @@ -217,7 +217,7 @@ fn update_trails(
};
let gt_min_angle = last_vec.dot(last_last_vec) > MINIMUM_ANGLE;
if gt_min_angle {
trail.0.push(body.position);
trail.0.enqueue(body.position);
polylines.get_mut(&polyline.0).unwrap().vertices =
trail.0.iter().map(|v| Vec3::from(*v)).collect()
} else {
Expand All @@ -233,7 +233,7 @@ fn update_trails(
}
}
} else {
trail.0.push(body.position);
trail.0.enqueue(body.position);
polylines.get_mut(&polyline.0).unwrap().vertices =
trail.0.iter().map(|v| Vec3::from(*v)).collect()
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub struct PolylinePlugin;
pub const SHADER_HANDLE: Handle<Shader> = uuid_handle!("b180bfe9-10c8-48fe-b27a-dfa41436d7d0");

impl Plugin for PolylinePlugin {
fn build(&self, app: &mut bevy::prelude::App) {
fn build(&self, app: &mut App) {
load_internal_asset!(
app,
SHADER_HANDLE,
Expand Down
20 changes: 10 additions & 10 deletions src/material.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use bevy::{
prepass::{OpaqueNoLightmap3dBatchSetKey, OpaqueNoLightmap3dBinKey},
},
ecs::{
component::Tick,
change_detection::Tick,
query::ROQueryItem,
system::{
lifetimeless::{Read, SRes},
Expand Down Expand Up @@ -76,8 +76,8 @@ impl Default for PolylineMaterial {
}

impl PolylineMaterial {
pub fn bind_group_layout(render_device: &RenderDevice) -> BindGroupLayout {
render_device.create_bind_group_layout(
pub fn bind_group_layout_descriptor() -> BindGroupLayoutDescriptor {
BindGroupLayoutDescriptor::new(
"polyline_material_layout",
&BindGroupLayoutEntries::single(
ShaderStages::VERTEX,
Expand Down Expand Up @@ -116,14 +116,15 @@ impl RenderAsset for GpuPolylineMaterial {
type SourceAsset = PolylineMaterial;
type Param = (
SRes<RenderDevice>,
SRes<PipelineCache>,
SRes<RenderQueue>,
SRes<PolylineMaterialPipeline>,
);

fn prepare_asset(
polyline_material: Self::SourceAsset,
_: AssetId<Self::SourceAsset>,
(device, queue, polyline_pipeline): &mut bevy::ecs::system::SystemParamItem<Self::Param>,
(device, cache, queue, polyline_pipeline): &mut SystemParamItem<Self::Param>,
_: Option<&Self>,
) -> Result<Self, PrepareAssetError<Self::SourceAsset>> {
let value = PolylineMaterialUniform {
Expand All @@ -141,7 +142,7 @@ impl RenderAsset for GpuPolylineMaterial {

let bind_group = device.create_bind_group(
Some("polyline_material_bind_group"),
&polyline_pipeline.material_layout,
&cache.get_bind_group_layout(&polyline_pipeline.material_layout),
&BindGroupEntries::single(buffer_binding),
);

Expand Down Expand Up @@ -190,13 +191,12 @@ impl Plugin for PolylineMaterialPlugin {
#[derive(Resource)]
pub struct PolylineMaterialPipeline {
pub polyline_pipeline: PolylinePipeline,
pub material_layout: BindGroupLayout,
pub material_layout: BindGroupLayoutDescriptor,
}

impl FromWorld for PolylineMaterialPipeline {
fn from_world(world: &mut World) -> Self {
let render_device = world.get_resource::<RenderDevice>().unwrap();
let material_layout = PolylineMaterial::bind_group_layout(render_device);
let material_layout = PolylineMaterial::bind_group_layout_descriptor();
let pipeline = world.get_resource::<PolylinePipeline>().unwrap();
PolylineMaterialPipeline {
polyline_pipeline: pipeline.to_owned(),
Expand Down Expand Up @@ -234,9 +234,9 @@ type DrawPolylineMaterial = (

pub struct SetPolylineViewBindGroup<const I: usize>;
impl<const I: usize, P: PhaseItem> RenderCommand<P> for SetPolylineViewBindGroup<I> {
type Param = ();
type ViewQuery = (Read<ViewUniformOffset>, Read<PolylineViewBindGroup>);
type ItemQuery = ();
type Param = ();

#[inline]
fn render<'w>(
Expand All @@ -253,9 +253,9 @@ impl<const I: usize, P: PhaseItem> RenderCommand<P> for SetPolylineViewBindGroup

pub struct SetMaterialBindGroup<const I: usize>;
impl<const I: usize, P: PhaseItem> RenderCommand<P> for SetMaterialBindGroup<I> {
type Param = SRes<RenderAssets<GpuPolylineMaterial>>;
type ViewQuery = ();
type ItemQuery = Read<PolylineMaterialHandle>;
type Param = SRes<RenderAssets<GpuPolylineMaterial>>;

fn render<'w>(
_item: &P,
Expand Down
35 changes: 20 additions & 15 deletions src/polyline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl RenderAsset for GpuPolyline {
fn prepare_asset(
polyline: Self::SourceAsset,
_: AssetId<Self::SourceAsset>,
render_device: &mut bevy::ecs::system::SystemParamItem<Self::Param>,
render_device: &mut SystemParamItem<Self::Param>,
_: Option<&Self>,
) -> Result<Self, PrepareAssetError<Self::SourceAsset>> {
let vertex_buffer_data = bytemuck::cast_slice(polyline.vertices.as_slice());
Expand Down Expand Up @@ -145,23 +145,22 @@ pub fn extract_polylines(

#[derive(Clone, Resource)]
pub struct PolylinePipeline {
pub view_layout: BindGroupLayout,
pub polyline_layout: BindGroupLayout,
pub view_layout: BindGroupLayoutDescriptor,
pub polyline_layout: BindGroupLayoutDescriptor,
pub shader: Handle<Shader>,
}

impl FromWorld for PolylinePipeline {
fn from_world(world: &mut World) -> Self {
let render_device = world.get_resource::<RenderDevice>().unwrap();
let view_layout = render_device.create_bind_group_layout(
fn from_world(_world: &mut World) -> Self {
let view_layout = BindGroupLayoutDescriptor::new(
"polyline_view_layout",
&BindGroupLayoutEntries::single(
ShaderStages::VERTEX,
uniform_buffer::<ViewUniform>(true),
),
);

let polyline_layout = render_device.create_bind_group_layout(
let polyline_layout = BindGroupLayoutDescriptor::new(
"polyline_layout",
&BindGroupLayoutEntries::single(
ShaderStages::VERTEX,
Expand Down Expand Up @@ -286,9 +285,9 @@ bitflags::bitflags! {
// MSAA uses the highest 3 bits for the MSAA log2(sample count) to support up to 128x MSAA.
pub struct PolylinePipelineKey: u32 {
const NONE = 0;
const PERSPECTIVE = (1 << 0);
const TRANSPARENT_MAIN_PASS = (1 << 1);
const HDR = (1 << 2);
const PERSPECTIVE = 1 << 0;
const TRANSPARENT_MAIN_PASS = 1 << 1;
const HDR = 1 << 2;
const MSAA_RESERVED_BITS = Self::MSAA_MASK_BITS << Self::MSAA_SHIFT_BITS;
}
}
Expand Down Expand Up @@ -325,13 +324,14 @@ pub fn prepare_polyline_bind_group(
mut commands: Commands,
polyline_pipeline: Res<PolylinePipeline>,
render_device: Res<RenderDevice>,
pipeline_cache: Res<PipelineCache>,
polyline_uniforms: Res<ComponentUniforms<PolylineUniform>>,
) {
if let Some(binding) = polyline_uniforms.uniforms().binding() {
commands.insert_resource(PolylineBindGroup {
value: render_device.create_bind_group(
Some("polyline_bind_group"),
&polyline_pipeline.polyline_layout,
&pipeline_cache.get_bind_group_layout(&polyline_pipeline.polyline_layout),
&BindGroupEntries::single(binding),
),
});
Expand All @@ -347,15 +347,20 @@ pub struct PolylineViewBindGroup {
pub fn prepare_polyline_view_bind_groups(
mut commands: Commands,
render_device: Res<RenderDevice>,
pipeline_cache: Res<PipelineCache>,
polyline_pipeline: Res<PolylinePipeline>,
view_uniforms: Res<ViewUniforms>,
views: Query<Entity, With<bevy::render::view::ExtractedView>>,
) {
for entity in views.iter() {
let Some(view_uniforms_binding) = view_uniforms.uniforms.binding() else {
continue;
};

let view_bind_group = render_device.create_bind_group(
Some("polyline_view_bind_group"),
&polyline_pipeline.view_layout,
&BindGroupEntries::single(&view_uniforms.uniforms),
&pipeline_cache.get_bind_group_layout(&polyline_pipeline.view_layout),
&BindGroupEntries::single(view_uniforms_binding),
);

commands.entity(entity).insert(PolylineViewBindGroup {
Expand All @@ -366,9 +371,9 @@ pub fn prepare_polyline_view_bind_groups(

pub struct SetPolylineBindGroup<const I: usize>;
impl<const I: usize, P: PhaseItem> RenderCommand<P> for SetPolylineBindGroup<I> {
type Param = SRes<PolylineBindGroup>;
type ViewQuery = ();
type ItemQuery = Read<DynamicUniformIndex<PolylineUniform>>;
type Param = SRes<PolylineBindGroup>;

#[inline]
fn render<'w>(
Expand All @@ -388,9 +393,9 @@ impl<const I: usize, P: PhaseItem> RenderCommand<P> for SetPolylineBindGroup<I>

pub struct DrawPolyline;
impl<P: PhaseItem> RenderCommand<P> for DrawPolyline {
type Param = SRes<RenderAssets<GpuPolyline>>;
type ViewQuery = ();
type ItemQuery = Read<PolylineHandle>;
type Param = SRes<RenderAssets<GpuPolyline>>;

#[inline]
fn render<'w>(
Expand Down