render: add atlas padding support to work around MSAA artifacts, disable MSAA by default

This commit is contained in:
Carter Anderson 2020-07-30 14:35:07 -07:00
parent 54eaa2bdc6
commit ccf81edd8f
8 changed files with 23 additions and 14 deletions

View File

@ -19,7 +19,7 @@ pub struct Msaa {
impl Default for Msaa { impl Default for Msaa {
fn default() -> Self { fn default() -> Self {
Self { samples: 4 } Self { samples: 1 }
} }
} }

View File

@ -1,20 +1,19 @@
use crate::{Rect, TextureAtlas}; use crate::{Rect, TextureAtlas};
use bevy_asset::{Assets, Handle}; use bevy_asset::Assets;
use bevy_math::Vec2; use bevy_math::Vec2;
use bevy_render::texture::Texture; use bevy_render::texture::Texture;
use guillotiere::{size2, AllocId, Allocation, AtlasAllocator}; use guillotiere::{size2, Allocation, AtlasAllocator};
use std::collections::HashMap;
pub struct DynamicTextureAtlasBuilder { pub struct DynamicTextureAtlasBuilder {
pub allocation_textures: HashMap<AllocId, Handle<Texture>>,
pub atlas_allocator: AtlasAllocator, pub atlas_allocator: AtlasAllocator,
pub padding: i32,
} }
impl DynamicTextureAtlasBuilder { impl DynamicTextureAtlasBuilder {
pub fn new(size: Vec2) -> Self { pub fn new(size: Vec2, padding: i32) -> Self {
Self { Self {
allocation_textures: Default::default(),
atlas_allocator: AtlasAllocator::new(to_size2(size)), atlas_allocator: AtlasAllocator::new(to_size2(size)),
padding,
} }
} }
@ -24,13 +23,17 @@ impl DynamicTextureAtlasBuilder {
textures: &mut Assets<Texture>, textures: &mut Assets<Texture>,
texture: &Texture, texture: &Texture,
) -> Option<u32> { ) -> Option<u32> {
let allocation = self let allocation = self.atlas_allocator.allocate(size2(
.atlas_allocator texture.size.x() as i32 + self.padding,
.allocate(size2(texture.size.x() as i32, texture.size.y() as i32)); texture.size.y() as i32 + self.padding,
));
if let Some(allocation) = allocation { if let Some(allocation) = allocation {
let atlas_texture = textures.get_mut(&texture_atlas.texture).unwrap(); let atlas_texture = textures.get_mut(&texture_atlas.texture).unwrap();
self.place_texture(atlas_texture, allocation, texture); 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) Some((texture_atlas.len() - 1) as u32)
} else { } else {
None None
@ -66,7 +69,9 @@ impl DynamicTextureAtlasBuilder {
allocation: Allocation, allocation: Allocation,
texture: &Texture, 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 atlas_width = atlas_texture.size.x() as usize;
let rect_width = rect.width() as usize; let rect_width = rect.width() as usize;
let format_size = atlas_texture.format.pixel_size(); let format_size = atlas_texture.format.pixel_size();

View File

@ -45,7 +45,7 @@ void main() {
vec2(sprite_rect.end.x, sprite_rect.begin.y), vec2(sprite_rect.end.x, sprite_rect.begin.y),
sprite_rect.end 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; v_Color = TextureAtlasSprite_color;
gl_Position = ViewProj * SpriteTransform * vec4(ceil(vertex_position), 1.0); gl_Position = ViewProj * SpriteTransform * vec4(ceil(vertex_position), 1.0);
} }

View File

@ -25,7 +25,7 @@ impl FontAtlas {
Self { Self {
texture_atlas: texture_atlases.add(texture_atlas), texture_atlas: texture_atlases.add(texture_atlas),
glyph_to_index: HashMap::new(), glyph_to_index: HashMap::new(),
dynamic_texture_atlas_builder: DynamicTextureAtlasBuilder::new(size), dynamic_texture_atlas_builder: DynamicTextureAtlasBuilder::new(size, 1),
} }
} }

View File

@ -2,6 +2,7 @@ use bevy::prelude::*;
fn main() { fn main() {
App::build() App::build()
.add_resource(Msaa { samples: 4 })
.add_default_plugins() .add_default_plugins()
.add_startup_system(setup.system()) .add_startup_system(setup.system())
.run(); .run();

View File

@ -2,6 +2,7 @@ use bevy::prelude::*;
fn main() { fn main() {
App::build() App::build()
.add_resource(Msaa { samples: 4 })
.add_default_plugins() .add_default_plugins()
.add_startup_system(setup.system()) .add_startup_system(setup.system())
.run(); .run();

View File

@ -4,6 +4,7 @@ struct Rotator;
fn main() { fn main() {
App::build() App::build()
.add_resource(Msaa { samples: 4 })
.add_default_plugins() .add_default_plugins()
.add_startup_system(setup.system()) .add_startup_system(setup.system())
.add_system(rotator_system.system()) .add_system(rotator_system.system())

View File

@ -3,6 +3,7 @@ use bevy::prelude::*;
/// This example illustrates various ways to load assets /// This example illustrates various ways to load assets
fn main() { fn main() {
App::build() App::build()
.add_resource(Msaa { samples: 4 })
.add_default_plugins() .add_default_plugins()
.add_startup_system(setup.system()) .add_startup_system(setup.system())
.run(); .run();