@@ -17,13 +17,12 @@ const { FPSCounter, MemCounter } = require('./counters');
1717 * 29 Oct 2016
1818 *
1919 * TODOs
20- * - optimize ascii conversion by pulling from canvas data (without png conversion)
2120 * - backbuffer screen updates based on network latency?
2221 * - try rendering to terminal using drawille / braille characters
2322 * - make this runs with more examples! (preferably by automating most stuff)
2423 * - add docopts to configure parameters (scale, renderers)
2524 * - support webgl and software renderers
26- * - add key controls to adjust parameters inside the terminal
25+ * - add key controls to adjust parameters inside the terminal (scaling, screenshot, stats, ascii characters)
2726 * - try ttystudio
2827 * - profit? :D
2928 *
@@ -41,17 +40,18 @@ const { FPSCounter, MemCounter } = require('./counters');
4140 * - Dom Polyfilling
4241 * - TerminalRenderer
4342 * - add nice fps graphs
43+ * - optimize ascii conversion by pulling from canvas data (without png conversion)
4444 *
4545 * Also see,
4646 * https://threejs.org/examples/canvas_ascii_effect.html
4747 * https://github.com/mrdoob/three.js/issues/7085
4848 * https://github.com/mrdoob/three.js/issues/2182
4949 */
5050
51- let y_scale = 2 ;
52- let rendering_scale = 0.15 ;
53- width = 640 * rendering_scale ;
54- height = 480 * rendering_scale ;
51+ let y_scale = 1.23 ; // pixel ratio of a single terminal character height / width
52+ let pixel_sampling = 1 ; // mulitplier of target pixels to actual canvas render size
53+ width = 100 ;
54+ height = y_scale * 50 ;
5555
5656// Create a screen object.
5757const screen = blessed . screen ( {
@@ -73,7 +73,7 @@ const screen = blessed.screen({
7373screen . title = 'Three.js Terminal' ;
7474
7575// placeholder for renderering
76- const canvas = blessed . image ( {
76+ const canvas = blessed . box ( { // box image
7777 parent : screen ,
7878 top : 0 ,
7979 left : 0 ,
@@ -133,6 +133,32 @@ screen.key(['escape', 'q', 'C-c'], function(ch, key) {
133133 return process . exit ( 0 ) ;
134134} ) ;
135135
136+ mode = 0 ;
137+
138+ screen . key ( [ 'm' ] , function ( ch , key ) {
139+ mode = ++ mode % 4 ;
140+ let options = { } ;
141+ switch ( mode ) {
142+ case 0 :
143+ options . plain_formatting = true ;
144+ break ;
145+ case 1 :
146+ options . plain_formatting = false ;
147+ options . bg_formatting = true ;
148+ options . ascii_formatting = false ;
149+ break ;
150+ case 2 :
151+ options . bg_formatting = true ;
152+ options . ascii_formatting = true ;
153+ break ;
154+ case 3 :
155+ options . bg_formatting = false ;
156+ options . ascii_formatting = true ;
157+ break ;
158+ }
159+ renderer . setAnsiOptions ( options ) ;
160+ } ) ;
161+
136162// Focus our element.
137163canvas . focus ( ) ;
138164box . on ( 'click' , clearlog ) ;
@@ -150,24 +176,28 @@ function init() {
150176 controls . panSpeed *= 4 ;
151177
152178 renderer = new THREE . TerminalRenderer ( canvas ) ;
153- renderer . setClearColor ( 0xf0f0f0 ) ;
179+ // renderer.setClearColor( 0xf0f0f0 );
180+ renderer . setClearColor ( 0xffffff ) ;
154181
155182 function onResize ( res ) {
156183 if ( ! res ) {
157- resize ( screen . width , screen . height * y_scale ) ;
184+ setSize ( screen . width , screen . height * y_scale ) ;
158185 return ;
159186 }
160- log ( `Resized ${ screen . program . columns } , ${ screen . program . rows } ` ) ;
187+ screen . debug ( `Resized ${ screen . program . columns } , ${ screen . program . rows } ` ) ;
161188 const fontWidth = res . width / screen . width ;
162189 const fontHeight = res . height / screen . height ;
163190 y_scale = fontHeight / fontWidth ;
164- log ( `Estimated font size ${ fontWidth . toFixed ( 3 ) } x${ fontHeight . toFixed ( 3 ) } , ratio ${ y_scale . toFixed ( 3 ) } ` ) ;
191+ screen . debug ( `Estimated font size ${ fontWidth . toFixed ( 3 ) } x${ fontHeight . toFixed ( 3 ) } , ratio ${ y_scale . toFixed ( 3 ) } ` ) ;
192+
193+ const actual_screen_ratio = res . height / res . width ;
165194
166- width = res . width * rendering_scale | 0 ;
167- height = res . height * rendering_scale | 0 ;
168- log ( `Rendering using ${ width } x${ height } px` ) ;
195+ // target pixels to render
196+ width = screen . width ;
197+ height = screen . width * actual_screen_ratio ;
198+ screen . debug ( `Rendering using ${ width } x${ height } px` ) ;
169199
170- resize ( width , height ) ;
200+ setSize ( width , height ) ;
171201 }
172202
173203 window . addEventListener ( 'resize' , onResize ) ;
@@ -182,6 +212,8 @@ function render() {
182212 sphere . rotation . x = start * 0.0003 ;
183213 sphere . rotation . z = start * 0.0002 ;
184214
215+ scene . rotation . y += 0.005 ;
216+
185217 controls . update ( ) ;
186218
187219 // Render
@@ -234,14 +266,14 @@ setInterval( () => {
234266 ) ;
235267} , 1000 ) ;
236268
237- function resize ( w , h ) {
269+ function setSize ( w , h ) {
270+ width = w * pixel_sampling | 0 ;
271+ height = h * pixel_sampling | 0 ;
238272 // screen.debug('resizing', w, h, screen.width, screen.height);
239273 controls . handleResize ( ) ;
240- width = w ;
241- height = h ;
242- camera . aspect = w / h ;
274+ camera . aspect = width / height ;
243275 camera . updateProjectionMatrix ( ) ;
244- renderer . setSize ( w , h ) ;
276+ renderer . setSize ( width , height ) ;
245277}
246278
247279function saveCanvas ( ) {
0 commit comments