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 {
fn default() -> Self {
Self { samples: 4 }
Self { samples: 1 }
}
}

View file

@ -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<AllocId, Handle<Texture>>,
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: &Texture,
) -> Option<u32> {
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();

View file

@ -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);
}

View file

@ -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),
}
}

View file

@ -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();

View file

@ -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();

View file

@ -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())

View file

@ -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();