|
| 1 | +//! By default Bevy loads images to textures with sampler settings that clamp the image to the edges. |
| 2 | +//! This example shows how to change the sampler settings to repeat the image instead. |
| 3 | +
|
| 4 | +use bevy::app::App; |
| 5 | +use bevy::app::Startup; |
| 6 | +use bevy::asset::{AssetServer, Assets}; |
| 7 | +use bevy::math::Vec3; |
| 8 | +use bevy::prelude::DefaultPlugins; |
| 9 | +use bevy::prelude::OrthographicProjection; |
| 10 | +use bevy::prelude::{Camera2dBundle, ColorMaterial, Commands, Mesh, ResMut, Transform}; |
| 11 | +use bevy::render::camera::ScalingMode; |
| 12 | +use bevy::render::mesh::Indices; |
| 13 | +use bevy::render::mesh::PrimitiveTopology; |
| 14 | +use bevy::render::texture::{ |
| 15 | + ImageAddressMode, ImageLoaderSettings, ImageSampler, ImageSamplerDescriptor, |
| 16 | +}; |
| 17 | +use bevy::sprite::MaterialMesh2dBundle; |
| 18 | + |
| 19 | +fn mesh() -> Mesh { |
| 20 | + let mut mesh = Mesh::new(PrimitiveTopology::TriangleList); |
| 21 | + mesh.insert_attribute( |
| 22 | + Mesh::ATTRIBUTE_POSITION, |
| 23 | + vec![[0., 0., 0.], [1., 0., 0.], [1., 1., 0.], [0., 1., 0.]], |
| 24 | + ); |
| 25 | + mesh.insert_attribute( |
| 26 | + Mesh::ATTRIBUTE_UV_0, |
| 27 | + vec![[-1., 2.], [2., 2.], [2., -1.], [-1., -1.]], |
| 28 | + ); |
| 29 | + mesh.set_indices(Some(Indices::U16(vec![0, 1, 2, 2, 3, 0]))); |
| 30 | + mesh |
| 31 | +} |
| 32 | + |
| 33 | +fn setup( |
| 34 | + mut commands: Commands, |
| 35 | + mut meshes: ResMut<Assets<Mesh>>, |
| 36 | + mut materials: ResMut<Assets<ColorMaterial>>, |
| 37 | + asset_server: ResMut<AssetServer>, |
| 38 | +) { |
| 39 | + commands.spawn(Camera2dBundle { |
| 40 | + projection: OrthographicProjection { |
| 41 | + scaling_mode: ScalingMode::AutoMin { |
| 42 | + min_width: 2., |
| 43 | + min_height: 1., |
| 44 | + }, |
| 45 | + far: 1000., |
| 46 | + near: -1000., |
| 47 | + ..OrthographicProjection::default() |
| 48 | + }, |
| 49 | + ..Camera2dBundle::default() |
| 50 | + }); |
| 51 | + |
| 52 | + // Texture from ambientCG.com, licensed under the Creative Commons CC0 1.0 Universal License. |
| 53 | + // https://ambientCG.com/a/Facade018A |
| 54 | + |
| 55 | + // By default Bevy loads images to textures with sampler settings that clamp the image to the edges. |
| 56 | + let image = asset_server.load("textures/facade018a.png"); |
| 57 | + |
| 58 | + // Here we override the sampler settings to repeat the image instead. |
| 59 | + let image_repeat = asset_server.load_with_settings( |
| 60 | + // We are using another file name, because Bevy ignores different loader settings for the same file. |
| 61 | + // This is probably a bug. |
| 62 | + "textures/facade018a_copy.png", |
| 63 | + |s: &mut ImageLoaderSettings| match &mut s.sampler { |
| 64 | + ImageSampler::Default => { |
| 65 | + s.sampler = ImageSampler::Descriptor(ImageSamplerDescriptor { |
| 66 | + address_mode_u: ImageAddressMode::Repeat, |
| 67 | + address_mode_v: ImageAddressMode::Repeat, |
| 68 | + ..Default::default() |
| 69 | + }); |
| 70 | + } |
| 71 | + ImageSampler::Descriptor(sampler) => { |
| 72 | + sampler.address_mode_u = ImageAddressMode::Repeat; |
| 73 | + sampler.address_mode_v = ImageAddressMode::Repeat; |
| 74 | + } |
| 75 | + }, |
| 76 | + ); |
| 77 | + |
| 78 | + let mesh = meshes.add(mesh()); |
| 79 | + |
| 80 | + commands.spawn(MaterialMesh2dBundle { |
| 81 | + mesh: mesh.clone().into(), |
| 82 | + material: materials.add(ColorMaterial { |
| 83 | + texture: Some(image), |
| 84 | + ..ColorMaterial::default() |
| 85 | + }), |
| 86 | + transform: Transform::from_translation(Vec3::new(-0.95, -0.45, 0.)) |
| 87 | + .with_scale(Vec3::new(0.9, 0.9, 0.9)), |
| 88 | + ..MaterialMesh2dBundle::default() |
| 89 | + }); |
| 90 | + |
| 91 | + commands.spawn(MaterialMesh2dBundle { |
| 92 | + mesh: mesh.into(), |
| 93 | + material: materials.add(ColorMaterial { |
| 94 | + texture: Some(image_repeat), |
| 95 | + ..ColorMaterial::default() |
| 96 | + }), |
| 97 | + transform: Transform::from_translation(Vec3::new(0.05, -0.45, 0.)) |
| 98 | + .with_scale(Vec3::new(0.9, 0.9, 0.9)), |
| 99 | + ..MaterialMesh2dBundle::default() |
| 100 | + }); |
| 101 | +} |
| 102 | + |
| 103 | +fn main() { |
| 104 | + App::new() |
| 105 | + .add_plugins(DefaultPlugins) |
| 106 | + .add_systems(Startup, setup) |
| 107 | + .run(); |
| 108 | +} |
0 commit comments