From 49f4f42d3916a8f1f77267cb46493510240f6925 Mon Sep 17 00:00:00 2001 From: Jasen Borisov Date: Wed, 17 Mar 2021 07:56:43 +0100 Subject: [PATCH 01/15] render: prelude: add FilterMode --- crates/bevy_render/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_render/src/lib.rs b/crates/bevy_render/src/lib.rs index 33d313eea4c59..f4a3fd7fc0696 100644 --- a/crates/bevy_render/src/lib.rs +++ b/crates/bevy_render/src/lib.rs @@ -32,7 +32,7 @@ pub mod prelude { pass::ClearColor, pipeline::RenderPipelines, shader::Shader, - texture::Texture, + texture::{FilterMode, Texture}, }; } From 28748b18a596e53ed7a98f8c1e571d89dfe390d4 Mon Sep 17 00:00:00 2001 From: Jasen Borisov Date: Wed, 17 Mar 2021 07:57:48 +0100 Subject: [PATCH 02/15] quad mesh primitive: add uv_scale field --- crates/bevy_render/src/mesh/shape/mod.rs | 26 ++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/crates/bevy_render/src/mesh/shape/mod.rs b/crates/bevy_render/src/mesh/shape/mod.rs index 85c61e9ab92f0..49c7350bfca8a 100644 --- a/crates/bevy_render/src/mesh/shape/mod.rs +++ b/crates/bevy_render/src/mesh/shape/mod.rs @@ -126,6 +126,8 @@ pub struct Quad { pub size: Vec2, /// Flips the texture coords of the resulting vertices. pub flip: bool, + /// Scale the texture to tile it. + pub uv_scale: Vec2, } impl Default for Quad { @@ -136,11 +138,19 @@ impl Default for Quad { impl Quad { pub fn new(size: Vec2) -> Self { - Self { size, flip: false } + Self { + size, + flip: false, + uv_scale: Vec2::splat(1.0), + } } pub fn flipped(size: Vec2) -> Self { - Self { size, flip: true } + Self { + size, + flip: true, + uv_scale: Vec2::splat(1.0), + } } } @@ -158,12 +168,12 @@ impl From for Mesh { ( [south_east.x, south_east.y, 0.0], [0.0, 0.0, 1.0], - [1.0, 1.0], + [quad.uv_scale.x, quad.uv_scale.y], ), ( [north_east.x, north_east.y, 0.0], [0.0, 0.0, 1.0], - [1.0, 0.0], + [quad.uv_scale.x, 0.0], ), ( [north_west.x, north_west.y, 0.0], @@ -173,7 +183,7 @@ impl From for Mesh { ( [south_west.x, south_west.y, 0.0], [0.0, 0.0, 1.0], - [0.0, 1.0], + [0.0, quad.uv_scale.y], ), ] } else { @@ -181,7 +191,7 @@ impl From for Mesh { ( [south_west.x, south_west.y, 0.0], [0.0, 0.0, 1.0], - [0.0, 1.0], + [0.0, quad.uv_scale.y], ), ( [north_west.x, north_west.y, 0.0], @@ -191,12 +201,12 @@ impl From for Mesh { ( [north_east.x, north_east.y, 0.0], [0.0, 0.0, 1.0], - [1.0, 0.0], + [quad.uv_scale.x, 0.0], ), ( [south_east.x, south_east.y, 0.0], [0.0, 0.0, 1.0], - [1.0, 1.0], + [quad.uv_scale.x, quad.uv_scale.y], ), ] }; From 4afd20f423f19f0f3cfc66a6afd594fa4f95f3cd Mon Sep 17 00:00:00 2001 From: Jasen Borisov Date: Wed, 17 Mar 2021 15:16:05 +0100 Subject: [PATCH 03/15] Add texture_filtering example (WIP no mipmaps) --- Cargo.toml | 4 + examples/3d/texture_filtering.rs | 267 +++++++++++++++++++++++++++++++ examples/README.md | 1 + 3 files changed, 272 insertions(+) create mode 100644 examples/3d/texture_filtering.rs diff --git a/Cargo.toml b/Cargo.toml index 217249920a53a..d3a9327cc32e8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -165,6 +165,10 @@ path = "examples/3d/spawner.rs" name = "texture" path = "examples/3d/texture.rs" +[[example]] +name = "texture_filtering" +path = "examples/3d/texture_filtering.rs" + [[example]] name = "update_gltf_scene" path = "examples/3d/update_gltf_scene.rs" diff --git a/examples/3d/texture_filtering.rs b/examples/3d/texture_filtering.rs new file mode 100644 index 0000000000000..8dd62d3f0fd65 --- /dev/null +++ b/examples/3d/texture_filtering.rs @@ -0,0 +1,267 @@ +use bevy::prelude::*; + +use std::num::NonZeroU8; + +fn main() { + App::build() + .add_plugins(DefaultPlugins) + .insert_resource(TextureFiltering::Anisotropic) + .add_startup_system(setup_plane.system()) + .add_startup_system(setup_ui.system()) + .add_system(swing_camera.system()) + .add_system(switch_filter_mode.system()) + .run(); +} + +struct MainCamera; +struct ModeText; + +struct MainAssets { + texture: Handle, + material: Handle, +} + +enum TextureFiltering { + Nearest, + Linear, + NearestMipmap, + LinearMipmap, + Anisotropic, +} + +fn setup_plane( + mut commands: Commands, + mode: Res, + mut textures: ResMut>, + mut meshes: ResMut>, + mut materials: ResMut>, +) { + let tex_size = 256; + let mut tex = generate_texture(tex_size, -0.8, 0.156); + mode.apply(&mut tex); + let tex = textures.add(tex); + + let quad_size = Vec2::new(20.0, 200.0); + let quad = meshes.add(Mesh::from(shape::Quad { + size: quad_size, + flip: false, + uv_scale: quad_size, + })); + + let material = materials.add(StandardMaterial { + albedo_texture: Some(tex.clone()), + unlit: true, + ..Default::default() + }); + + commands.insert_resource(MainAssets { + texture: tex, + material: material.clone(), + }); + + commands + .spawn(PbrBundle { + mesh: quad, + material, + transform: Transform { + translation: Vec3::new(0.0, 0.0, -quad_size.y / 4.0), + rotation: Quat::from_rotation_x(-std::f32::consts::PI / 2.0), + ..Default::default() + }, + visible: Visible { + is_transparent: true, + ..Default::default() + }, + ..Default::default() + }) + .spawn(PerspectiveCameraBundle::default()) + .with(MainCamera); +} + +fn setup_ui(mut commands: Commands, asset_server: Res, mode: Res) { + commands + .spawn(UiCameraBundle::default()) + .spawn(TextBundle { + style: Style { + align_self: AlignSelf::FlexEnd, + ..Default::default() + }, + text: Text { + sections: vec![ + TextSection { + value: "(space to change)".to_string(), + style: TextStyle { + font: asset_server.load("fonts/FiraSans-Bold.ttf"), + font_size: 32.0, + color: Color::ALICE_BLUE, + }, + }, + TextSection { + value: " Current Mode: ".to_string(), + style: TextStyle { + font: asset_server.load("fonts/FiraSans-Bold.ttf"), + font_size: 32.0, + color: Color::WHITE, + }, + }, + TextSection { + value: mode.screen_text().to_string(), + style: TextStyle { + font: asset_server.load("fonts/FiraMono-Medium.ttf"), + font_size: 32.0, + color: Color::GOLD, + }, + }, + ], + ..Default::default() + }, + ..Default::default() + }) + .with(ModeText); +} + +fn swing_camera(t: Res