1414//! }
1515//!
1616//! fn setup(mut commands: Commands) {
17- //! commands.spawn(Camera2dBundle::default());
18- //! commands.spawn(MapBundle::default());
17+ //! let camera_bundle = (
18+ //! Camera2d::default(),
19+ //! );
20+ //! commands.spawn(camera_bundle);
21+ //! commands.spawn(MapBundle::default());
1922//! }
2023//! ```
2124use bevy:: {
2225 prelude:: * ,
2326 render:: { render_asset:: RenderAssetUsages , render_resource:: TextureFormat } ,
2427} ;
28+ use colorgrad:: { Gradient , LinearGradient } ;
2529use image:: { imageops:: FilterType , DynamicImage , Pixel } ;
2630use serde:: { Deserialize , Serialize } ;
2731
@@ -64,7 +68,7 @@ pub struct MapBundle {
6468 /// See [`Map`](./struct.Map.html)
6569 pub map : Map ,
6670 /// See [`ImageBundle`](../../bevy/prelude/struct.ImageBundle.html)
67- pub image_bundle : ImageBundle ,
71+ pub image_bundle : ImageNode ,
6872}
6973
7074impl Default for Map {
@@ -79,38 +83,34 @@ impl Default for Map {
7983 }
8084 }
8185}
82- fn generate_map ( mut images : ResMut < Assets < Image > > , mut query : Query < ( & mut Map , & mut UiImage ) > ) {
86+ fn generate_map ( mut images : ResMut < Assets < Image > > , mut query : Query < ( & mut Map , & mut ImageNode ) > ) {
8387 for ( mut map, mut ui_image) in & mut query {
8488 map. noise . size = map. size ;
8589 let noise_values = generate_noise_map ( & map. noise ) ;
8690 let noise = & mut map. noise ;
8791
8892 let mut colors: Vec < colorgrad:: Color > = Vec :: with_capacity ( noise. regions . len ( ) ) ;
89- let mut domain: Vec < f64 > = Vec :: with_capacity ( noise. regions . len ( ) ) ;
93+ let mut domain: Vec < f32 > = Vec :: with_capacity ( noise. regions . len ( ) ) ;
9094 for region in & noise. regions {
9195 colors. push ( colorgrad:: Color {
92- r : f64 :: from ( region. color [ 0 ] ) / 255.0 ,
93- g : f64 :: from ( region. color [ 1 ] ) / 255.0 ,
94- b : f64 :: from ( region. color [ 2 ] ) / 255.0 ,
95- a : f64 :: from ( region. color [ 3 ] ) / 255.0 ,
96+ r : f32 :: from ( region. color [ 0 ] ) / 255.0 ,
97+ g : f32 :: from ( region. color [ 1 ] ) / 255.0 ,
98+ b : f32 :: from ( region. color [ 2 ] ) / 255.0 ,
99+ a : f32 :: from ( region. color [ 3 ] ) / 255.0 ,
96100 } ) ;
97101 domain. push ( region. position ) ;
98102 }
99- let mut grad = colorgrad:: CustomGradient :: new ( )
103+ let grad = colorgrad:: GradientBuilder :: new ( )
100104 . colors ( & colors)
101105 . domain ( & domain)
102- . build ( )
106+ . build :: < LinearGradient > ( )
103107 . unwrap_or_else ( |_| {
104- colorgrad:: CustomGradient :: new ( )
108+ colorgrad:: GradientBuilder :: new ( )
105109 . colors ( & colors)
106- . build ( )
110+ . build :: < LinearGradient > ( )
107111 . expect ( "Gradient generation failed" )
108112 } ) ;
109113
110- if noise. gradient . segments != 0 {
111- grad = grad. sharp ( noise. gradient . segments , noise. gradient . smoothness ) ;
112- }
113-
114114 let mut gradient_buffer = image:: ImageBuffer :: from_pixel (
115115 noise. gradient . size [ 0 ] ,
116116 noise. gradient . size [ 1 ] ,
@@ -119,7 +119,7 @@ fn generate_map(mut images: ResMut<Assets<Image>>, mut query: Query<(&mut Map, &
119119
120120 for ( x, _, pixel) in gradient_buffer. enumerate_pixels_mut ( ) {
121121 let rgba = grad
122- . at ( f64:: from ( x) * 100.0 / f64:: from ( noise. gradient . size [ 0 ] ) )
122+ . at ( ( f64:: from ( x) * 100.0 / f64:: from ( noise. gradient . size [ 0 ] ) ) as f32 )
123123 . to_rgba8 ( ) ;
124124 pixel. blend ( & image:: Rgba ( rgba) ) ;
125125 }
@@ -142,7 +142,7 @@ fn generate_map(mut images: ResMut<Assets<Image>>, mut query: Query<(&mut Map, &
142142
143143 for ( x, y, pixel) in image_buffer. enumerate_pixels_mut ( ) {
144144 let height = noise_values[ x as usize ] [ y as usize ] ;
145- let target_color = grad. at ( height) . to_rgba8 ( ) ;
145+ let target_color = grad. at ( height as f32 ) . to_rgba8 ( ) ;
146146 pixel. blend ( & image:: Rgba ( target_color) ) ;
147147 }
148148 if !map. same_size {
@@ -167,6 +167,6 @@ fn generate_map(mut images: ResMut<Assets<Image>>, mut query: Query<(&mut Map, &
167167 . convert ( TextureFormat :: Rgba8UnormSrgb )
168168 . expect ( "Could not convert to Rgba8UnormSrgb" ) ;
169169
170- ui_image. texture = images. add ( map_texture) ;
170+ ui_image. image = images. add ( map_texture) ;
171171 }
172172}
0 commit comments