From ccf81edd8f5f0181835e73dff59dce2f7e21f304 Mon Sep 17 00:00:00 2001 From: Carter Anderson Date: Thu, 30 Jul 2020 14:35:07 -0700 Subject: [PATCH] render: add atlas padding support to work around MSAA artifacts, disable MSAA by default --- crates/bevy_render/src/render_graph/base.rs | 2 +- .../src/dynamic_texture_atlas_builder.rs | 27 +++++++++++-------- .../bevy_sprite/src/render/sprite_sheet.vert | 2 +- crates/bevy_text/src/font_atlas.rs | 2 +- examples/3d/3d_scene.rs | 1 + examples/3d/load_model.rs | 1 + examples/3d/parenting.rs | 1 + examples/asset/asset_loading.rs | 1 + 8 files changed, 23 insertions(+), 14 deletions(-) diff --git a/crates/bevy_render/src/render_graph/base.rs b/crates/bevy_render/src/render_graph/base.rs index c3d8ce21da..688527cced 100644 --- a/crates/bevy_render/src/render_graph/base.rs +++ b/crates/bevy_render/src/render_graph/base.rs @@ -19,7 +19,7 @@ pub struct Msaa { impl Default for Msaa { fn default() -> Self { - Self { samples: 4 } + Self { samples: 1 } } } diff --git a/crates/bevy_sprite/src/dynamic_texture_atlas_builder.rs b/crates/bevy_sprite/src/dynamic_texture_atlas_builder.rs index 3f227ac760..3bcbd327d4 100644 --- a/crates/bevy_sprite/src/dynamic_texture_atlas_builder.rs +++ b/crates/bevy_sprite/src/dynamic_texture_atlas_builder.rs @@ -1,20 +1,19 @@ use crate::{Rect, TextureAtlas}; -use bevy_asset::{Assets, Handle}; +use bevy_asset::Assets; use bevy_math::Vec2; use bevy_render::texture::Texture; -use guillotiere::{size2, AllocId, Allocation, AtlasAllocator}; -use std::collections::HashMap; +use guillotiere::{size2, Allocation, AtlasAllocator}; pub struct DynamicTextureAtlasBuilder { - pub allocation_textures: HashMap>, pub atlas_allocator: AtlasAllocator, + pub padding: i32, } impl DynamicTextureAtlasBuilder { - pub fn new(size: Vec2) -> Self { + pub fn new(size: Vec2, padding: i32) -> Self { Self { - allocation_textures: Default::default(), atlas_allocator: AtlasAllocator::new(to_size2(size)), + padding, } } @@ -24,13 +23,17 @@ impl DynamicTextureAtlasBuilder { textures: &mut Assets, texture: &Texture, ) -> Option { - let allocation = self - .atlas_allocator - .allocate(size2(texture.size.x() as i32, texture.size.y() as i32)); + let allocation = self.atlas_allocator.allocate(size2( + texture.size.x() as i32 + self.padding, + texture.size.y() as i32 + self.padding, + )); if let Some(allocation) = allocation { let atlas_texture = textures.get_mut(&texture_atlas.texture).unwrap(); self.place_texture(atlas_texture, allocation, texture); - texture_atlas.add_texture(allocation.rectangle.into()); + let mut rect: Rect = allocation.rectangle.into(); + *rect.max.x_mut() -= self.padding as f32; + *rect.max.y_mut() -= self.padding as f32; + texture_atlas.add_texture(rect); Some((texture_atlas.len() - 1) as u32) } else { None @@ -66,7 +69,9 @@ impl DynamicTextureAtlasBuilder { allocation: Allocation, texture: &Texture, ) { - let rect = allocation.rectangle; + let mut rect = allocation.rectangle; + rect.max.x -= self.padding; + rect.max.y -= self.padding; let atlas_width = atlas_texture.size.x() as usize; let rect_width = rect.width() as usize; let format_size = atlas_texture.format.pixel_size(); diff --git a/crates/bevy_sprite/src/render/sprite_sheet.vert b/crates/bevy_sprite/src/render/sprite_sheet.vert index 89eecc6683..d967ac5411 100644 --- a/crates/bevy_sprite/src/render/sprite_sheet.vert +++ b/crates/bevy_sprite/src/render/sprite_sheet.vert @@ -45,7 +45,7 @@ void main() { vec2(sprite_rect.end.x, sprite_rect.begin.y), sprite_rect.end ); - v_Uv = (atlas_positions[gl_VertexIndex] + vec2(0.05, 0.05)) / AtlasSize; + v_Uv = (atlas_positions[gl_VertexIndex] + vec2(0.01, 0.01)) / AtlasSize; v_Color = TextureAtlasSprite_color; gl_Position = ViewProj * SpriteTransform * vec4(ceil(vertex_position), 1.0); } \ No newline at end of file diff --git a/crates/bevy_text/src/font_atlas.rs b/crates/bevy_text/src/font_atlas.rs index 3c70491a3f..1eedbf6cc3 100644 --- a/crates/bevy_text/src/font_atlas.rs +++ b/crates/bevy_text/src/font_atlas.rs @@ -25,7 +25,7 @@ impl FontAtlas { Self { texture_atlas: texture_atlases.add(texture_atlas), glyph_to_index: HashMap::new(), - dynamic_texture_atlas_builder: DynamicTextureAtlasBuilder::new(size), + dynamic_texture_atlas_builder: DynamicTextureAtlasBuilder::new(size, 1), } } diff --git a/examples/3d/3d_scene.rs b/examples/3d/3d_scene.rs index e71c0e2eb1..bae9313a34 100644 --- a/examples/3d/3d_scene.rs +++ b/examples/3d/3d_scene.rs @@ -2,6 +2,7 @@ use bevy::prelude::*; fn main() { App::build() + .add_resource(Msaa { samples: 4 }) .add_default_plugins() .add_startup_system(setup.system()) .run(); diff --git a/examples/3d/load_model.rs b/examples/3d/load_model.rs index fb6a81b298..2af55ab588 100644 --- a/examples/3d/load_model.rs +++ b/examples/3d/load_model.rs @@ -2,6 +2,7 @@ use bevy::prelude::*; fn main() { App::build() + .add_resource(Msaa { samples: 4 }) .add_default_plugins() .add_startup_system(setup.system()) .run(); diff --git a/examples/3d/parenting.rs b/examples/3d/parenting.rs index fb7a289dcf..720b6efc81 100644 --- a/examples/3d/parenting.rs +++ b/examples/3d/parenting.rs @@ -4,6 +4,7 @@ struct Rotator; fn main() { App::build() + .add_resource(Msaa { samples: 4 }) .add_default_plugins() .add_startup_system(setup.system()) .add_system(rotator_system.system()) diff --git a/examples/asset/asset_loading.rs b/examples/asset/asset_loading.rs index 9037ab8de8..216f32d3cd 100644 --- a/examples/asset/asset_loading.rs +++ b/examples/asset/asset_loading.rs @@ -3,6 +3,7 @@ use bevy::prelude::*; /// This example illustrates various ways to load assets fn main() { App::build() + .add_resource(Msaa { samples: 4 }) .add_default_plugins() .add_startup_system(setup.system()) .run();