Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/examples/asset/repeated_texture.rs
6592 views
1
//! By default Bevy loads images to textures that clamps the image to the edges
2
//! This example shows how to configure it to repeat the image instead.
3
4
use bevy::{
5
image::{ImageAddressMode, ImageLoaderSettings, ImageSampler, ImageSamplerDescriptor},
6
math::Affine2,
7
prelude::*,
8
};
9
10
fn main() {
11
App::new()
12
.add_plugins(DefaultPlugins)
13
.add_systems(Startup, setup)
14
.run();
15
}
16
17
fn setup(
18
mut commands: Commands,
19
asset_server: Res<AssetServer>,
20
mut meshes: ResMut<Assets<Mesh>>,
21
mut materials: ResMut<Assets<StandardMaterial>>,
22
) {
23
let image_with_default_sampler =
24
asset_server.load("textures/fantasy_ui_borders/panel-border-010.png");
25
26
// central cube with not repeated texture
27
commands.spawn((
28
Mesh3d(meshes.add(Cuboid::new(1.0, 1.0, 1.0))),
29
MeshMaterial3d(materials.add(StandardMaterial {
30
base_color_texture: Some(image_with_default_sampler.clone()),
31
..default()
32
})),
33
Transform::from_translation(Vec3::ZERO),
34
));
35
36
// left cube with repeated texture
37
commands.spawn((
38
Mesh3d(meshes.add(Cuboid::new(1.0, 1.0, 1.0))),
39
MeshMaterial3d(materials.add(StandardMaterial {
40
base_color_texture: Some(asset_server.load_with_settings(
41
"textures/fantasy_ui_borders/panel-border-010-repeated.png",
42
|s: &mut _| {
43
*s = ImageLoaderSettings {
44
sampler: ImageSampler::Descriptor(ImageSamplerDescriptor {
45
// rewriting mode to repeat image,
46
address_mode_u: ImageAddressMode::Repeat,
47
address_mode_v: ImageAddressMode::Repeat,
48
..default()
49
}),
50
..default()
51
}
52
},
53
)),
54
55
// uv_transform used here for proportions only, but it is full Affine2
56
// that's why you can use rotation and shift also
57
uv_transform: Affine2::from_scale(Vec2::new(2., 3.)),
58
..default()
59
})),
60
Transform::from_xyz(-1.5, 0.0, 0.0),
61
));
62
63
// right cube with scaled texture, because with default sampler
64
commands.spawn((
65
Mesh3d(meshes.add(Cuboid::new(1.0, 1.0, 1.0))),
66
MeshMaterial3d(materials.add(StandardMaterial {
67
// there is no sampler set, that's why
68
// by default you see only one small image in a row/column
69
// and other space is filled by image edge
70
base_color_texture: Some(image_with_default_sampler),
71
72
// uv_transform used here for proportions only, but it is full Affine2
73
// that's why you can use rotation and shift also
74
uv_transform: Affine2::from_scale(Vec2::new(2., 3.)),
75
..default()
76
})),
77
Transform::from_xyz(1.5, 0.0, 0.0),
78
));
79
80
// light
81
commands.spawn((
82
PointLight {
83
shadows_enabled: true,
84
..default()
85
},
86
Transform::from_xyz(4.0, 8.0, 4.0),
87
));
88
// camera
89
commands.spawn((
90
Camera3d::default(),
91
Transform::from_xyz(0.0, 1.5, 4.0).looking_at(Vec3::ZERO, Vec3::Y),
92
));
93
}
94
95