Show HN: 在终端中旋转的圆环(使用 Kitty 图形协议)
Show HN: Rotating torus in terminal (but with kitty graphics protocol)

原始链接: https://andreadimatteo.com/torus-v0-5.html

Andrea 将 Kitty Graphics Protocol 成功集成到了一个圆环渲染项目中,实现了终端内的直接可视化。为达成此目的,需将原始像素数据进行 Base64 编码,并以 4096 字节为数据块,通过特定的终端转义序列(`_G;\`)进行传输。 通过在终端中直接渲染圆环,Andrea 能够直观地调试各种故障,例如光照不一致和几何剪切问题,从而改进了色彩表现并增加了对双光源的支持。该项目包含一个(主要用于美观的)帧率计数器,现已托管在 GitHub 上。Andrea 正在考虑该项目的未来方向,例如利用 Apple 的 Metal API 来提升性能、实现 OBJ 模型加载,或是彻底放弃该项目。文章最后分享了一个趣味知识:品红色并非真实的光谱色,而是人脑的一种构造。

抱歉。
相关文章

原文
·

Hi, this is Andrea.

After playing a little bit with the torus program that hasn't fallen into the oblivion for now, I decided to render it using the Kitty Graphics Protocol that also my terminal supports. If you don't pay too much attention like me at first, you could think of sending raw pixel data encoded in plain ASCII. Well, no too easy hehe. You must send base64 encoded images (RGB, RGBA, PNG). For those who don't know, the encoding (included me before reading about it) works in the following way:

  • takes 3 bytes as input
  • splits it into words of 6 bits each
  • given `i`, an 6 bits unsigned, return the char `map[i]` where `map` is an encoding specific map, do this for each of the four 6 bits unsigned
  • if the input isn't multiple of 3 bytes then it's padded with `=` (special char) as needed

If you want to know more about it and you want to do it the old way, just read about it on the RFC. It's deceptively simple to implement for RGB data and you can flex that you know that RFC stands for Request For Comment. Once you have your base64 encoded image, you can send it, in chunks of maximum 4096 bytes, using the kitty graphics protocol. All the graphics follow the format <ESC>_G<control data>;<payload><ESC>\, which, translated in printf() means: printf("\033_G<control data>;<payload>\033\\");, where <control data> is a comma separated key value string that ends with ;. Some basic information for this map are:

  • display on terminal: `a=T` ~must specify, otherwise will not display anything :)~
  • type of data: `f=24/32/100` for RGB, RGBA and PNG respectively (must specify)
  • width: `s=%n` (must specify)
  • height: `v=%n` (must specify)
  • compression: `o=z` (optional)
  • terminal columns and rows: `c=%d,r=%d` (optional)
  • more (if there will be chunks following): `m=%1`

Now, with a real image rendering I could finally see what was happening in fact, some very strange things were happening, like cut in half torus, not consistent light, and it was much easier to visually debug and also add cool stuff like colors and a second light (of a different color). It would be interesting to write Metal code to speed all this thing up on Apple Silicon, might be interesting to learn or just to suffer; or also make it a rendering engine that can actually load OBJ meshes or again, forget about this project forever. This is the result for now, cool right :) ? The code can be found the following repo

torus

the fps counter makes it even more profesional even though it's useless, because it sleeps not to go over 33.33 (0.o) fun fact: did you know that magenta (blue + red) isn't a real color? it doesn't really exist on the visible spectrum, our brain just makes it up :)
联系我们 contact @ memedata.com