File tree 7 files changed +52
-3
lines changed
7 files changed +52
-3
lines changed Original file line number Diff line number Diff line change 14
14
15
15
## Assets
16
16
17
- * Generic RPG Pack (CC0 license) by [ Bakudas] ( https://twitter.com/bakudas ) and [ Gabe Fern] ( https://twitter.com/_Gabrielfer )
17
+ * Generic RPG Pack (CC0 license) by [ Bakudas] ( https://twitter.com/bakudas ) and [ Gabe Fern] ( https://twitter.com/_Gabrielfer )
18
+ * Environment maps (` .hdr ` files) from [ HDRIHaven] ( https://hdrihaven.com ) (CC0 license)
Original file line number Diff line number Diff line change @@ -22,6 +22,7 @@ spirv-reflect = "0.2.3"
22
22
glsl-to-spirv = { git = " https://github.com/cart/glsl-to-spirv" }
23
23
# TODO: move this to its own crate
24
24
png = " 0.16.0"
25
+ image = " 0.23"
25
26
26
27
# misc
27
28
log = { version = " 0.4" , features = [" release_max_level_info" ] }
Original file line number Diff line number Diff line change @@ -38,7 +38,7 @@ use render_graph::{
38
38
} ;
39
39
use renderer:: { AssetRenderResourceBindings , RenderResourceBindings } ;
40
40
use std:: ops:: Range ;
41
- use texture:: { PngTextureLoader , TextureResourceSystemState } ;
41
+ use texture:: { HdrTextureLoader , PngTextureLoader , TextureResourceSystemState } ;
42
42
43
43
pub mod stage {
44
44
/// Stage where render resources are set up
@@ -75,6 +75,7 @@ impl AppPlugin for RenderPlugin {
75
75
. add_asset :: < Texture > ( )
76
76
. add_asset :: < Shader > ( )
77
77
. add_asset :: < PipelineDescriptor > ( )
78
+ . add_asset_loader :: < Texture , HdrTextureLoader > ( )
78
79
. add_asset_loader :: < Texture , PngTextureLoader > ( )
79
80
. register_component :: < Camera > ( )
80
81
. register_component :: < Draw > ( )
Original file line number Diff line number Diff line change
1
+ use super :: { Texture , TextureFormat } ;
2
+ use anyhow:: Result ;
3
+ use bevy_asset:: AssetLoader ;
4
+ use bevy_math:: Vec2 ;
5
+ use std:: path:: Path ;
6
+
7
+ #[ derive( Clone , Default ) ]
8
+ pub struct HdrTextureLoader ;
9
+
10
+ impl AssetLoader < Texture > for HdrTextureLoader {
11
+ fn from_bytes ( & self , _asset_path : & Path , bytes : Vec < u8 > ) -> Result < Texture > {
12
+ let format = TextureFormat :: Rgba32Float ;
13
+ debug_assert_eq ! (
14
+ format. pixel_size( ) ,
15
+ 4 * 4 * 1 ,
16
+ "Format should have 32bit x 4 size"
17
+ ) ;
18
+
19
+ let decoder = image:: hdr:: HdrDecoder :: new ( bytes. as_slice ( ) ) ?;
20
+ let info = decoder. metadata ( ) ;
21
+ let rgb_data = decoder. read_image_hdr ( ) ?;
22
+ let mut rgba_data = Vec :: with_capacity ( rgb_data. len ( ) * format. pixel_size ( ) ) ;
23
+
24
+ for rgb in rgb_data {
25
+ let alpha = 1.0f32 ;
26
+
27
+ rgba_data. extend_from_slice ( & rgb. 0 [ 0 ] . to_ne_bytes ( ) ) ;
28
+ rgba_data. extend_from_slice ( & rgb. 0 [ 1 ] . to_ne_bytes ( ) ) ;
29
+ rgba_data. extend_from_slice ( & rgb. 0 [ 2 ] . to_ne_bytes ( ) ) ;
30
+ rgba_data. extend_from_slice ( & alpha. to_ne_bytes ( ) ) ;
31
+ }
32
+
33
+ Ok ( Texture :: new (
34
+ Vec2 :: new ( info. width as f32 , info. height as f32 ) ,
35
+ rgba_data,
36
+ format,
37
+ ) )
38
+ }
39
+ fn extensions ( & self ) -> & [ & str ] {
40
+ static EXTENSIONS : & [ & str ] = & [ "hdr" ] ;
41
+ EXTENSIONS
42
+ }
43
+ }
Original file line number Diff line number Diff line change
1
+ mod hdr_texture_loader;
1
2
mod png_texture_loader;
2
3
mod sampler_descriptor;
3
4
mod texture;
4
5
mod texture_descriptor;
5
6
mod texture_dimension;
6
7
8
+ pub use hdr_texture_loader:: * ;
7
9
pub use png_texture_loader:: * ;
8
10
pub use sampler_descriptor:: * ;
9
11
pub use texture:: * ;
Original file line number Diff line number Diff line change @@ -67,7 +67,8 @@ impl Texture {
67
67
self . size = size;
68
68
let width = size. x ( ) as usize ;
69
69
let height = size. y ( ) as usize ;
70
- self . data . resize ( width * height * self . format . pixel_size ( ) , 0 ) ;
70
+ self . data
71
+ . resize ( width * height * self . format . pixel_size ( ) , 0 ) ;
71
72
}
72
73
73
74
pub fn texture_resource_system (
You can’t perform that action at this time.
0 commit comments