File tree Expand file tree Collapse file tree 4 files changed +37
-8
lines changed Expand file tree Collapse file tree 4 files changed +37
-8
lines changed Original file line number Diff line number Diff line change 1+ use ratatui:: style:: Style ;
12use tui_shader:: { ShaderCanvas , ShaderCanvasState , WgslShader } ;
23
34fn main ( ) {
45 let mut terminal = ratatui:: init ( ) ;
5- let shader = WgslShader :: Path ( "shaders/tutorial.wgsl" ) ;
6- let mut state = ShaderCanvasState :: new ( shader) ;
7- while state. get_instant ( ) . elapsed ( ) . as_secs ( ) < 7 {
6+
7+ let balloon_shader = WgslShader :: Path ( "shaders/tutorial.wgsl" ) ;
8+ let mut balloon_state = ShaderCanvasState :: new ( balloon_shader) ;
9+ let balloon_canvas = ShaderCanvas :: new ( ) . style_rule ( tui_shader:: StyleRule :: Map ( |sample| {
10+ let brightness = ( sample. r ( ) as f32 + sample. g ( ) as f32 + sample. b ( ) as f32 ) / 3.0 ;
11+ if brightness > 127.0 {
12+ Style :: new ( ) . bg ( sample. color ( ) )
13+ } else {
14+ Style :: default ( )
15+ }
16+ } ) ) ;
17+
18+ let bg_shader = WgslShader :: Path ( "shaders/gradient.wgsl" ) ;
19+ let mut bg_state = ShaderCanvasState :: new ( bg_shader) ;
20+ let bg_canvas = ShaderCanvas :: new ( ) ;
21+
22+ while balloon_state. get_instant ( ) . elapsed ( ) . as_secs ( ) < 7 {
823 terminal
924 . draw ( |frame| {
10- frame. render_stateful_widget ( ShaderCanvas :: new ( ) , frame. area ( ) , & mut state) ;
25+ frame. render_stateful_widget ( & bg_canvas, frame. area ( ) , & mut bg_state) ;
26+ frame. render_stateful_widget ( & balloon_canvas, frame. area ( ) , & mut balloon_state) ;
1127 } )
1228 . unwrap ( ) ;
1329 }
Original file line number Diff line number Diff line change 22
33@fragment
44fn main (@location (0 ) uv : vec2 <f32 >) -> @location (0 ) vec4 <f32 > {
5- let r = sin (1 .0 - uv . x + time . x );
6- let g = cos (1 .0 - uv . y + time . x );
5+ let r = sin (1 .0 - uv . x + time . x ) * 0 .5 + 1 ;
6+ let g = - sin (1 .0 - uv . y + time . x ) * 0 .5 + 1 ;
77 let b = 0 .5 ;
8+
9+ let position = vec2 <f32 >(
10+ 0 .5 * (sin (time . x * 2 .1 ) * 0 .5 + 1 ),
11+ 0 .5 * (cos (time . x * 1 .2 ) * 0 .5 + 1 )
12+ );
813
9- let d = 1 .0 - distance (vec2 < f32 >( 0 .5 ) , uv );
14+ let d = 1 .0 - distance (position , uv );
1015 let color = vec4 <f32 >(r , g , b , 1 .0 ) * d ;
1116 return color ;
1217}
Original file line number Diff line number Diff line change @@ -111,6 +111,8 @@ pub use crate::state::*;
111111pub use crate :: style:: * ;
112112pub use crate :: util:: * ;
113113
114+ pub use wgpu:: include_wgsl;
115+
114116#[ cfg( test) ]
115117mod tests {
116118 use ratatui:: { backend:: TestBackend , layout:: Position } ;
Original file line number Diff line number Diff line change 1+ /// Utility `enum` to pass in a shader into [`ShaderCanvasState`](crate::ShaderCanvasState). Another option is to use the re-exported
2+ /// [`include_wgsl!`](wgpu::include_wgsl!) macro, which checks at runtime if the path to the file is valid and returns a
3+ /// [`ShaderModuleDescriptor`](wgpu::ShaderModuleDescriptor).
14pub enum WgslShader < ' a > {
5+ /// Use wgsl source code in a `&str`.
26 Source ( & ' a str ) ,
7+
8+ /// Use a path to a wgsl shader.
39 Path ( & ' a str ) ,
410}
511
@@ -21,7 +27,7 @@ impl<'a> From<WgslShader<'a>> for wgpu::ShaderModuleDescriptor<'a> {
2127 }
2228}
2329
24- pub type Pixel = [ u8 ; 4 ] ;
30+ pub ( crate ) type Pixel = [ u8 ; 4 ] ;
2531
2632pub ( crate ) fn bytes_per_row ( width : u32 ) -> u32 {
2733 let row_size = width * 4 ;
You can’t perform that action at this time.
0 commit comments