-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathTerminalRenderer.js
135 lines (102 loc) · 3.73 KB
/
TerminalRenderer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
'use strict'
const fs = require('fs');
const Canvas = require('canvas');
const DrawilleCanvas = require('drawille');
const SoftwareCanvas = require('./SoftwareCanvas');
require('three/examples/js/renderers/Projector');
require('three/examples/js/renderers/SoftwareRenderer');
require('three/examples/js/renderers/CanvasRenderer');
// TODO support WebGLRenderer via headless-gl or node-webgl?
// TODO move terminal dimensions to pixel dimensions conversion here
const ansi = require('./ansi');
class TerminalRenderer {
constructor(screen) {
this.screen = screen;
this.pixel_sampling = 1; // mulitplier of target pixels to actual canvas render size
// Set up fake canvas
const canvas = new Canvas();
// const canvas = new SoftwareCanvas();
canvas.style = {};
const params = {
canvas: canvas, // pass in fake canvas
};
this.ctx = canvas.getContext('2d');
// const renderer = new THREE.SoftwareRenderer(params); // TODO pass in raw arrays and render that instead
const renderer = new THREE.CanvasRenderer(params);
this.canvas = canvas;
this.renderer = renderer;
this.drawille = new DrawilleCanvas();
}
setSize(w, h) {
this.width = w;
this.height = h;
const multiplier = (this.braille ? 4 : 1) * this.pixel_sampling;
w = w * multiplier | 0;
h = h * multiplier | 0;
this.renderer.setSize(w, h);
}
setClearColor(c) {
this.renderer.setClearColor(c);
}
render(scene, camera) {
// console.error(`Size ${this.canvas.width},${this.canvas.height} ${this.screen.width},${this.screen.height} `);
this.renderer.render(scene, camera);
// A) only for AnsiImage widget
// seems to be faster but less configurable
// this.screen.setImage(this.canvas.toBuffer())
this.image = this.ctx.getImageData(0, 0, this.canvas.width, this.canvas.height);
if (!this.braille) {
// B) Convert to ASCII and render
this.screen.setContent(ansi.convert(this.image, this.screen.width, this.screen.height));
}
else {
// C) Do Draville something.
this.renderDrawille(this.image, this.screen);
}
// D) Render to file
}
renderDrawille(image, screen) {
const tw = screen.width * 2;
const th = screen.height * 4;
const sw = image.width;
const sh = image.height;
const data = image.data;
const drawille = this.drawille;
drawille.width = tw;
drawille.height = th;
drawille.clear();
let tx, ty, p, r, g, b, a, intensity;
for (let y = 0; y < th; y++) {
for (let x = 0; x < tw; x++) {
tx = x / tw * sw | 0;
ty = y / th * sh | 0;
p = (ty * sw + tx) * 4;
r = data[p + 0] / 255;
g = data[p + 1] / 255;
b = data[p + 2] / 255;
a = data[p + 3] / 255;
intensity = (0.2126 * r + 0.7152 * g + 0.0722 * b) * a;
if (intensity < 0.8) drawille.set(x, y);
}
}
screen.setContent(drawille.frame());
}
setAnsiOptions(o) {
ansi.setOptions(o);
}
setBrailleMode(mode) {
this.braille = mode;
this.setSize(this.width, this.height);
}
setPixelScale(s) {
this.pixel_sampling = s;
this.setSize(this.width, this.height);
}
saveRenderToFile(canvas, file) {
// Write canvas to file
const out = fs.createWriteStream(file);
return canvas.pngStream().pipe(out);
}
}
THREE.TerminalRenderer = TerminalRenderer;
module.exports = TerminalRenderer;