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
8 changes: 7 additions & 1 deletion crates/bevy_sprite/src/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use bevy_asset::Handle;
use bevy_ecs::bundle::Bundle;
use bevy_render::{
texture::{Image, DEFAULT_IMAGE_HANDLE},
view::Visibility,
view::{ComputedVisibility, Visibility},
};
use bevy_transform::components::{GlobalTransform, Transform};

Expand All @@ -18,6 +18,8 @@ pub struct SpriteBundle {
pub texture: Handle<Image>,
/// User indication of whether an entity is visible
pub visibility: Visibility,
/// Algorithmically-computed indication of whether an entity is visible and should be extracted for rendering
pub computed_visibility: ComputedVisibility,
}

impl Default for SpriteBundle {
Expand All @@ -28,9 +30,11 @@ impl Default for SpriteBundle {
global_transform: Default::default(),
texture: DEFAULT_IMAGE_HANDLE.typed(),
visibility: Default::default(),
computed_visibility: Default::default(),
}
}
}

/// A Bundle of components for drawing a single sprite from a sprite sheet (also referred
/// to as a `TextureAtlas`)
#[derive(Bundle, Clone, Default)]
Expand All @@ -44,4 +48,6 @@ pub struct SpriteSheetBundle {
pub global_transform: GlobalTransform,
/// User indication of whether an entity is visible
pub visibility: Visibility,
/// Algorithmically-computed indication of whether an entity is visible and should be extracted for rendering
pub computed_visibility: ComputedVisibility,
}
7 changes: 6 additions & 1 deletion crates/bevy_sprite/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ use bevy_reflect::TypeUuid;
use bevy_render::{
render_phase::AddRenderCommand,
render_resource::{Shader, SpecializedPipelines},
view::VisibilitySystems,
RenderApp, RenderStage,
};

Expand All @@ -58,7 +59,11 @@ impl Plugin for SpritePlugin {
app.add_asset::<TextureAtlas>()
.register_type::<Sprite>()
.add_plugin(Mesh2dRenderPlugin)
.add_plugin(ColorMaterialPlugin);
.add_plugin(ColorMaterialPlugin)
.add_system_to_stage(
CoreStage::PostUpdate,
render::calculate_bounds.label(VisibilitySystems::CalculateBounds),
);

if let Ok(render_app) = app.get_sub_app_mut(RenderApp) {
render_app
Expand Down
28 changes: 25 additions & 3 deletions crates/bevy_sprite/src/mesh2d/mesh.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use bevy_app::Plugin;
use bevy_app::{CoreStage, Plugin};
use bevy_asset::{Assets, Handle, HandleUntyped};
use bevy_ecs::{
prelude::*,
Expand All @@ -8,13 +8,17 @@ use bevy_math::{Mat4, Size};
use bevy_reflect::TypeUuid;
use bevy_render::{
mesh::{GpuBufferInfo, Mesh},
primitives::Aabb,
render_asset::RenderAssets,
render_component::{ComponentUniforms, DynamicUniformIndex, UniformComponentPlugin},
render_phase::{EntityRenderCommand, RenderCommandResult, TrackedRenderPass},
render_resource::{std140::AsStd140, *},
renderer::{RenderDevice, RenderQueue},
texture::{BevyDefault, GpuImage, Image, TextureFormatPixelInfo},
view::{ComputedVisibility, ExtractedView, ViewUniform, ViewUniformOffset, ViewUniforms},
view::{
ComputedVisibility, ExtractedView, NoFrustumCulling, ViewUniform, ViewUniformOffset,
ViewUniforms, VisibilitySystems,
},
RenderApp, RenderStage,
};
use bevy_transform::components::GlobalTransform;
Expand Down Expand Up @@ -59,7 +63,11 @@ impl Plugin for Mesh2dRenderPlugin {
.with_import_path("bevy_sprite::mesh2d_view_bind_group"),
);

app.add_plugin(UniformComponentPlugin::<Mesh2dUniform>::default());
app.add_plugin(UniformComponentPlugin::<Mesh2dUniform>::default())
.add_system_to_stage(
CoreStage::PostUpdate,
calculate_bounds.label(VisibilitySystems::CalculateBounds),
);

if let Ok(render_app) = app.get_sub_app_mut(RenderApp) {
render_app
Expand All @@ -72,6 +80,20 @@ impl Plugin for Mesh2dRenderPlugin {
}
}

pub fn calculate_bounds(
mut commands: Commands,
meshes: Res<Assets<Mesh>>,
without_aabb: Query<(Entity, &Mesh2dHandle), (Without<Aabb>, Without<NoFrustumCulling>)>,
) {
for (entity, mesh_handle) in without_aabb.iter() {
if let Some(mesh) = meshes.get(&mesh_handle.0) {
if let Some(aabb) = mesh.compute_aabb() {
commands.entity(entity).insert(aabb);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Batch insertion may be faster. Please try it.

}
}
}
}

#[derive(Component, AsStd140, Clone)]
pub struct Mesh2dUniform {
pub transform: Mat4,
Expand Down
63 changes: 55 additions & 8 deletions crates/bevy_sprite/src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ use bevy_ecs::{
prelude::*,
system::{lifetimeless::*, SystemParamItem},
};
use bevy_math::{const_vec2, Vec2};
use bevy_math::{const_vec2, Vec2, Vec3};
use bevy_reflect::Uuid;
use bevy_render::{
color::Color,
primitives::Aabb,
render_asset::RenderAssets,
render_phase::{
BatchedPhaseItem, DrawFunctions, EntityRenderCommand, RenderCommand, RenderCommandResult,
Expand All @@ -23,7 +24,9 @@ use bevy_render::{
render_resource::{std140::AsStd140, *},
renderer::{RenderDevice, RenderQueue},
texture::{BevyDefault, Image},
view::{Msaa, ViewUniform, ViewUniformOffset, ViewUniforms, Visibility},
view::{
ComputedVisibility, Msaa, NoFrustumCulling, ViewUniform, ViewUniformOffset, ViewUniforms,
},
RenderWorld,
};
use bevy_transform::components::GlobalTransform;
Expand Down Expand Up @@ -178,6 +181,45 @@ impl SpecializedPipeline for SpritePipeline {
}
}

pub fn calculate_bounds(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs doc strings: it's a public system.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ups...that's an oversight, the system doesn't need to be public.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm...well maybe not exactly an oversight, because all the other functions in that file are pub too. But i can make it pub(crate) calculate_bounds if you want.

mut commands: Commands,
images: Res<Assets<Image>>,
atlases: Res<Assets<TextureAtlas>>,
sprite_without_aabb: Query<
(Entity, &Sprite, &Handle<Image>),
(Without<Aabb>, Without<NoFrustumCulling>),
>,
atlas_without_aabb: Query<
(Entity, &TextureAtlasSprite, &Handle<TextureAtlas>),
(Without<Aabb>, Without<NoFrustumCulling>),
>,
) {
for (entity, sprite, texture_handle) in sprite_without_aabb.iter() {
if let Some(image) = images.get(texture_handle) {
let size = sprite.custom_size.unwrap_or_else(|| image.size());
let aabb = Aabb {
center: Vec3::ZERO,
half_extents: size.extend(0.0) * 0.5,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe faster?

Suggested change
half_extents: size.extend(0.0) * 0.5,
half_extents: (0.5 * size).extend(0.0),

Copy link
Contributor Author

@Weasy666 Weasy666 Mar 4, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will try that. Now that you mention this, i noticed that in some other places divisions are used instead of multiplications, but as far as i know or as far as i was taught, multiplications are preferred because they are (in most cases) faster than divisions. Is this something we want to change? Not in this PR, but in a new one.

};
commands.entity(entity).insert(aabb);
}
}
for (entity, atlas_sprite, atlas_handle) in atlas_without_aabb.iter() {
if let Some(atlas) = atlases.get(atlas_handle) {
if let Some(rect) = atlas.textures.get(atlas_sprite.index) {
let size = atlas_sprite
.custom_size
.unwrap_or_else(|| (rect.min - rect.max).abs());
let aabb = Aabb {
center: Vec3::ZERO,
half_extents: size.extend(0.0) * 0.5,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
half_extents: size.extend(0.0) * 0.5,
half_extents: (0.5 * size).extend(0.0),

};
commands.entity(entity).insert(aabb);
}
}
}
}

#[derive(Component, Clone, Copy)]
pub struct ExtractedSprite {
pub transform: GlobalTransform,
Expand Down Expand Up @@ -232,18 +274,23 @@ pub fn extract_sprite_events(
pub fn extract_sprites(
mut render_world: ResMut<RenderWorld>,
texture_atlases: Res<Assets<TextureAtlas>>,
sprite_query: Query<(&Visibility, &Sprite, &GlobalTransform, &Handle<Image>)>,
sprite_query: Query<(
&ComputedVisibility,
&Sprite,
&GlobalTransform,
&Handle<Image>,
)>,
atlas_query: Query<(
&Visibility,
&ComputedVisibility,
&TextureAtlasSprite,
&GlobalTransform,
&Handle<TextureAtlas>,
)>,
) {
let mut extracted_sprites = render_world.get_resource_mut::<ExtractedSprites>().unwrap();
extracted_sprites.sprites.clear();
for (visibility, sprite, transform, handle) in sprite_query.iter() {
if !visibility.is_visible {
for (computed_visibility, sprite, transform, handle) in sprite_query.iter() {
if !computed_visibility.is_visible {
continue;
}
// PERF: we don't check in this function that the `Image` asset is ready, since it should be in most cases and hashing the handle is expensive
Expand All @@ -259,8 +306,8 @@ pub fn extract_sprites(
image_handle_id: handle.id,
});
}
for (visibility, atlas_sprite, transform, texture_atlas_handle) in atlas_query.iter() {
if !visibility.is_visible {
for (computed_visibility, atlas_sprite, transform, texture_atlas_handle) in atlas_query.iter() {
if !computed_visibility.is_visible {
continue;
}
if let Some(texture_atlas) = texture_atlases.get(texture_atlas_handle) {
Expand Down