Skip to content

Commit

Permalink
Merge pull request #25 from ForesightMiningSoftwareCorporation/bevy-0.12
Browse files Browse the repository at this point in the history
Update to bevy 0.12
  • Loading branch information
bonsairobo committed Feb 19, 2024
2 parents f699632 + 3b099b9 commit 3cec831
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 66 deletions.
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
[package]
name = "bevy-aabb-instancing"
version = "0.10.0"
version = "0.11.0"
edition = "2021"
description = "Render millions of AABB instances in Bevy."
authors = [
"Duncan Fairbanks <duncan.fairbanks@foresightmining.com>",
"Zhixing Zhang <zhixing.zhang@foresightmining.com>"
"Zhixing Zhang <zhixing.zhang@foresightmining.com>",
]
keywords = ["bevy", "aabb", "cube", "instancing", "render"]
license = "Apache-2.0"
Expand All @@ -15,13 +15,13 @@ repository = "https://github.com/ForesightMiningSoftwareCorporation/bevy-aabb-in
trace = ["bevy/trace_chrome"]

[dependencies.bevy]
version = "0.11"
version = "0.12.1"
default-features = false
features = ["bevy_asset", "bevy_core_pipeline", "bevy_render", "x11"]

[dev-dependencies]
rand = "0.8"
smooth-bevy-cameras = "0.9"
smooth-bevy-cameras = "0.10"

[[example]]
name = "wave"
Expand Down
12 changes: 12 additions & 0 deletions examples/bloom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use bevy::{
bloom::{BloomCompositeMode, BloomPrefilterSettings, BloomSettings},
tonemapping::Tonemapping,
},
input::mouse,
prelude::*,
};
use bevy_aabb_instancing::{Cuboid, CuboidMaterialId, Cuboids, VertexPullingRenderPlugin};
Expand All @@ -19,6 +20,7 @@ fn main() {
FpsCameraPlugin::default(),
))
.add_systems(Startup, setup)
.add_systems(Update, toggle_fps_controller)
.run();
}

Expand Down Expand Up @@ -76,10 +78,20 @@ fn setup(mut commands: Commands) {
.insert(FpsCameraBundle::new(
FpsCameraController {
translate_sensitivity: 10.0,
enabled: false,
..Default::default()
},
Vec3::splat(10.0),
Vec3::ZERO,
Vec3::Y,
));
}

fn toggle_fps_controller(
mouse_button_input: Res<Input<MouseButton>>,
mut controller: Query<&mut FpsCameraController>,
) {
if mouse_button_input.just_pressed(MouseButton::Left) {
controller.single_mut().enabled = true;
}
}
12 changes: 11 additions & 1 deletion examples/wave.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fn main() {
FpsCameraPlugin::default(),
))
.add_systems(Startup, setup)
.add_systems(Update, update_scalar_hue_options)
.add_systems(Update, (update_scalar_hue_options, toggle_fps_controller))
.run();
}

Expand Down Expand Up @@ -62,6 +62,7 @@ fn setup(mut commands: Commands, mut material_map: ResMut<CuboidMaterialMap>) {
.insert(FpsCameraBundle::new(
FpsCameraController {
translate_sensitivity: 200.0,
enabled: false,
..Default::default()
},
Vec3::new(0.0, 100.0, 0.0),
Expand All @@ -76,3 +77,12 @@ fn update_scalar_hue_options(time: Res<Time>, mut material_map: ResMut<CuboidMat
material.scalar_hue.max_visible = tv;
material.scalar_hue.clamp_max = tv;
}

fn toggle_fps_controller(
mouse_button_input: Res<Input<MouseButton>>,
mut controller: Query<&mut FpsCameraController>,
) {
if mouse_button_input.just_pressed(MouseButton::Left) {
controller.single_mut().enabled = true;
}
}
2 changes: 1 addition & 1 deletion src/vertex_pulling/draw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ impl<P: PhaseItem> RenderCommand<P> for DrawVertexPulledCuboids {
let num_cuboids = entry.instance_buffer.get().len().try_into().unwrap();
let index_buffer = index_buffers
.into_inner()
.get(&CUBE_INDICES_HANDLE.typed())
.get(&CUBE_INDICES_HANDLE)
.unwrap();
pass.set_index_buffer(index_buffer.slice(..), 0, IndexFormat::Uint32);
pass.draw_indexed(0..(CUBE_INDICES.len() as u32), 0, 0..num_cuboids);
Expand Down
6 changes: 2 additions & 4 deletions src/vertex_pulling/extract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub(crate) fn extract_cuboids(
&Cuboids,
&GlobalTransform,
&CuboidMaterialId,
Option<&ComputedVisibility>,
Option<&ViewVisibility>,
Or<(Added<Cuboids>, Changed<Cuboids>)>,
)>,
>,
Expand Down Expand Up @@ -58,9 +58,7 @@ pub(crate) fn extract_cuboids(

let transform = CuboidsTransform::from_matrix(transform.compute_matrix());

let is_visible = maybe_visibility
.map(ComputedVisibility::is_visible)
.unwrap_or(true);
let is_visible = maybe_visibility.map(|vis| vis.get()).unwrap_or(true);

let entry = cuboid_buffers.entries.entry(entity).or_default();
if instance_buffer_needs_update {
Expand Down
8 changes: 4 additions & 4 deletions src/vertex_pulling/index_buffer.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use bevy::{
asset::{Asset, Handle},
core::cast_slice,
ecs::system::lifetimeless::SRes,
prelude::HandleUntyped,
reflect::{TypePath, TypeUuid},
render::{
render_asset::RenderAsset,
Expand All @@ -10,12 +10,12 @@ use bevy::{
},
};

#[derive(Default, TypeUuid, TypePath)]
#[derive(Asset, Default, TypeUuid, TypePath)]
#[uuid = "8f6d78a6-fffe-4e54-81db-08b0739a947a"]
pub struct CuboidsIndexBuffer;

pub(crate) const CUBE_INDICES_HANDLE: HandleUntyped =
HandleUntyped::weak_from_u64(CuboidsIndexBuffer::TYPE_UUID, 17343092250772987267);
pub(crate) const CUBE_INDICES_HANDLE: Handle<CuboidsIndexBuffer> =
Handle::weak_from_u128(17343092250772987267);

// Only 3 faces are actually drawn.
const NUM_CUBE_INDICES_USIZE: usize = 3 * 3 * 2;
Expand Down
9 changes: 4 additions & 5 deletions src/vertex_pulling/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use bevy::render::render_resource::ShaderDefVal;
use bevy::render::texture::BevyDefault;
use bevy::{
prelude::*,
reflect::TypeUuid,
render::{
mesh::PrimitiveTopology,
render_resource::{
Expand All @@ -32,8 +31,8 @@ pub(crate) struct CuboidsPipelines {
pub view_layout: BindGroupLayout,
}

pub(crate) const VERTEX_PULLING_SHADER_HANDLE: HandleUntyped =
HandleUntyped::weak_from_u64(Shader::TYPE_UUID, 17343092250772987267);
pub(crate) const VERTEX_PULLING_SHADER_HANDLE: Handle<Shader> =
Handle::weak_from_u128(17343092250772987267);

impl FromWorld for CuboidsPipelines {
fn from_world(world: &mut World) -> Self {
Expand Down Expand Up @@ -122,13 +121,13 @@ impl FromWorld for CuboidsPipelines {
cuboids_layout.clone(),
];
let vertex = VertexState {
shader: VERTEX_PULLING_SHADER_HANDLE.typed(),
shader: VERTEX_PULLING_SHADER_HANDLE,
shader_defs: shader_defs.vertex.clone(),
entry_point: "vertex".into(),
buffers: vec![],
};
let fragment_target = |texture_format| FragmentState {
shader: VERTEX_PULLING_SHADER_HANDLE.typed(),
shader: VERTEX_PULLING_SHADER_HANDLE,
shader_defs: shader_defs.fragment.clone(),
entry_point: "fragment".into(),
targets: vec![Some(ColorTargetState {
Expand Down
15 changes: 9 additions & 6 deletions src/vertex_pulling/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ use super::prepare::{
};
use super::queue::queue_cuboids;
use crate::CuboidMaterialMap;
use bevy::asset::load_internal_asset;
use bevy::core_pipeline::core_3d::Opaque3d;
use bevy::prelude::*;
use bevy::render::view::ViewSet;
use bevy::render::view::prepare_view_uniforms;
use bevy::render::{render_phase::AddRenderCommand, RenderApp};
use bevy::render::{Render, RenderSet};

Expand All @@ -25,18 +26,20 @@ impl Plugin for VertexPullingRenderPlugin {
fn build(&self, app: &mut App) {
app.init_resource::<CuboidMaterialMap>();

app.world.resource_mut::<Assets<Shader>>().set_untracked(
load_internal_asset!(
app,
VERTEX_PULLING_SHADER_HANDLE,
Shader::from_wgsl(include_str!("vertex_pulling.wgsl"), file!()),
"vertex_pulling.wgsl",
Shader::from_wgsl
);
{
use super::index_buffer::{CuboidsIndexBuffer, CUBE_INDICES_HANDLE};
use bevy::render::render_asset::RenderAssetPlugin;
app.add_asset::<CuboidsIndexBuffer>()
app.init_asset::<CuboidsIndexBuffer>()
.add_plugins(RenderAssetPlugin::<CuboidsIndexBuffer>::default());
app.world
.resource_mut::<Assets<CuboidsIndexBuffer>>()
.set_untracked(CUBE_INDICES_HANDLE, CuboidsIndexBuffer);
.insert(CUBE_INDICES_HANDLE, CuboidsIndexBuffer);
}
}

Expand Down Expand Up @@ -74,7 +77,7 @@ impl Plugin for VertexPullingRenderPlugin {
.after(prepare_clipping_planes),
prepare_cuboid_transforms,
prepare_cuboids,
prepare_cuboids_view_bind_group.after(ViewSet::PrepareUniforms),
prepare_cuboids_view_bind_group.after(prepare_view_uniforms),
)
.in_set(RenderSet::Prepare),
)
Expand Down
61 changes: 21 additions & 40 deletions src/vertex_pulling/prepare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ use super::cuboid_cache::CuboidBufferCache;
use super::draw::{AuxiliaryMeta, TransformsMeta, ViewMeta};
use super::pipeline::CuboidsPipelines;

use bevy::render::render_resource::BindGroupEntries;
use bevy::{
prelude::*,
render::{
render_resource::{BindGroupDescriptor, BindGroupEntry},
renderer::{RenderDevice, RenderQueue},
view::ViewUniforms,
},
Expand Down Expand Up @@ -40,20 +40,11 @@ pub(crate) fn prepare_auxiliary_bind_group(
if let (Some(color_binding), Some(planes_binding)) =
(material_uniform.binding(), clipping_plane_uniform.binding())
{
aux_meta.bind_group = Some(render_device.create_bind_group(&BindGroupDescriptor {
label: Some("auxiliary_bind_group"),
layout: &pipeline.aux_layout,
entries: &[
BindGroupEntry {
binding: 0,
resource: color_binding,
},
BindGroupEntry {
binding: 1,
resource: planes_binding,
},
],
}));
aux_meta.bind_group = Some(render_device.create_bind_group(
"auxiliary_bind_group",
&pipeline.aux_layout,
&BindGroupEntries::sequential((color_binding, planes_binding)),
));
}
}

Expand All @@ -72,14 +63,11 @@ pub(crate) fn prepare_cuboid_transforms(
if let Some(transforms_binding) = transform_uniforms.binding() {
let create_bind_group_span = bevy::log::info_span!("prepare_cuboids::create_bind_group");
transforms_meta.transform_buffer_bind_group = create_bind_group_span.in_scope(|| {
Some(render_device.create_bind_group(&BindGroupDescriptor {
label: Some("gpu_cuboids_transforms_bind_group"),
layout: &pipeline.transforms_layout,
entries: &[BindGroupEntry {
binding: 0,
resource: transforms_binding,
}],
}))
Some(render_device.create_bind_group(
"gpu_cuboids_transforms_bind_group",
&pipeline.transforms_layout,
&BindGroupEntries::single(transforms_binding),
))
});
} else {
assert!(transform_uniforms.is_empty());
Expand Down Expand Up @@ -110,14 +98,11 @@ pub(crate) fn prepare_cuboids(
});

entry.instance_buffer_bind_group = create_bind_group_span.in_scope(|| {
Some(render_device.create_bind_group(&BindGroupDescriptor {
label: Some("cuboids_instance_buffer_bind_group"),
layout: &pipeline.cuboids_layout,
entries: &[BindGroupEntry {
binding: 0,
resource: entry.instance_buffer.binding().unwrap(),
}],
}))
Some(render_device.create_bind_group(
"cuboids_instance_buffer_bind_group",
&pipeline.cuboids_layout,
&BindGroupEntries::single(entry.instance_buffer.binding().unwrap()),
))
});

entry.dirty = false;
Expand All @@ -131,14 +116,10 @@ pub(crate) fn prepare_cuboids_view_bind_group(
view_uniforms: Res<ViewUniforms>,
) {
if let Some(view_binding) = view_uniforms.uniforms.binding() {
view_meta.cuboids_view_bind_group =
Some(render_device.create_bind_group(&BindGroupDescriptor {
entries: &[BindGroupEntry {
binding: 0,
resource: view_binding,
}],
label: Some("cuboids_view_bind_group"),
layout: &cuboids_pipeline.view_layout,
}));
view_meta.cuboids_view_bind_group = Some(render_device.create_bind_group(
"cuboids_view_bind_group",
&cuboids_pipeline.view_layout,
&BindGroupEntries::single(view_binding),
));
}
}
2 changes: 2 additions & 0 deletions src/vertex_pulling/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ pub(crate) fn queue_cuboids(
entity,
distance: inverse_view_row_2.dot(entry.position.extend(1.0)),
draw_function: draw_cuboids,
batch_range: 0..1,
dynamic_offset: None,
});
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/vertex_pulling/vertex_pulling.wgsl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#import bevy_render::view View
#import bevy_render::view::View

fn hsl_to_nonlinear_srgb(hue: f32, saturation: f32, lightness: f32) -> vec3<f32> {
// https://en.wikipedia.org/wiki/HSL_and_HSV#HSL_to_RGB
Expand Down

0 comments on commit 3cec831

Please sign in to comment.