1
1
//! This example illustrates how to create a texture for use with a `texture_2d_array<f32>` shader
2
2
//! uniform variable.
3
3
4
+ use core:: num:: NonZero ;
5
+
4
6
use bevy:: {
5
7
prelude:: * , reflect:: TypePath , render:: render_resource:: AsBindGroup , shader:: ShaderRef ,
6
8
} ;
9
+ use bevy_image:: ImageLoaderSettings ;
7
10
8
11
/// This example uses a shader source file from the assets subdirectory
9
12
const SHADER_ASSET_PATH : & str = "shaders/array_texture.wgsl" ;
@@ -15,23 +18,15 @@ fn main() {
15
18
MaterialPlugin :: < ArrayTextureMaterial > :: default ( ) ,
16
19
) )
17
20
. add_systems ( Startup , setup)
18
- . add_systems ( Update , create_array_texture)
19
21
. run ( ) ;
20
22
}
21
23
22
- #[ derive( Resource ) ]
23
- struct LoadingTexture {
24
- is_loaded : bool ,
25
- handle : Handle < Image > ,
26
- }
27
-
28
- fn setup ( mut commands : Commands , asset_server : Res < AssetServer > ) {
29
- // Start loading the texture.
30
- commands. insert_resource ( LoadingTexture {
31
- is_loaded : false ,
32
- handle : asset_server. load ( "textures/array_texture.png" ) ,
33
- } ) ;
34
-
24
+ fn setup (
25
+ mut commands : Commands ,
26
+ asset_server : Res < AssetServer > ,
27
+ mut meshes : ResMut < Assets < Mesh > > ,
28
+ mut materials : ResMut < Assets < ArrayTextureMaterial > > ,
29
+ ) {
35
30
// light
36
31
commands. spawn ( (
37
32
DirectionalLight :: default ( ) ,
@@ -43,34 +38,17 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
43
38
Camera3d :: default ( ) ,
44
39
Transform :: from_xyz ( 5.0 , 5.0 , 5.0 ) . looking_at ( Vec3 :: new ( 1.5 , 0.0 , 0.0 ) , Vec3 :: Y ) ,
45
40
) ) ;
46
- }
47
-
48
- fn create_array_texture (
49
- mut commands : Commands ,
50
- asset_server : Res < AssetServer > ,
51
- mut loading_texture : ResMut < LoadingTexture > ,
52
- mut images : ResMut < Assets < Image > > ,
53
- mut meshes : ResMut < Assets < Mesh > > ,
54
- mut materials : ResMut < Assets < ArrayTextureMaterial > > ,
55
- ) {
56
- if loading_texture. is_loaded
57
- || !asset_server
58
- . load_state ( loading_texture. handle . id ( ) )
59
- . is_loaded ( )
60
- {
61
- return ;
62
- }
63
- loading_texture. is_loaded = true ;
64
- let image = images. get_mut ( & loading_texture. handle ) . unwrap ( ) ;
65
-
66
- // Create a new array texture asset from the loaded texture.
67
- let array_layers = 4 ;
68
- image. reinterpret_stacked_2d_as_array ( array_layers) ;
69
41
70
42
// Spawn some cubes using the array texture
43
+ let array_layers = 4 ;
71
44
let mesh_handle = meshes. add ( Cuboid :: default ( ) ) ;
72
45
let material_handle = materials. add ( ArrayTextureMaterial {
73
- array_texture : loading_texture. handle . clone ( ) ,
46
+ array_texture : asset_server. load_with_settings (
47
+ "textures/array_texture.png" ,
48
+ move |settings : & mut ImageLoaderSettings | {
49
+ settings. layers = NonZero :: new ( array_layers) ;
50
+ } ,
51
+ ) ,
74
52
} ) ;
75
53
for x in -5 ..=5 {
76
54
commands. spawn ( (
0 commit comments