demo 教程
本教程介绍如何运行仓库中的终端演示:动画场景、曝光效果、录制与回放、静态图像以及视频导出。你需要仓库的克隆和原生 MoonBit 工具链;不需要编写代码。
快速开始
在仓库根目录下,把球体场景的一帧渲染到 48 × 14 的帧中:
COLUMNS=48 LINES=15 moon run src/demo --target native -- --sphere --once
输出(在清屏转义序列之后):
................................................
................................................
................................................
................................................
....................*#%%%%@@....................
..................=+**##%%%%@@..................
................. -+**####%%%%#.................
................. :=++**#######.................
..................:-==++******..................
.....................-====+=....................
................................................
................................................
................................................
................................................
球体从右上方受光;其左下方的面背向光源,用最暗的字符绘制。去掉 --once 可以让演示持续运行,用 Ctrl-C 停止。
常见任务
观看动画场景
just 配方会测量终端并把尺寸传给演示程序:
just cube # spinning cube (the default scene)
just torus # spinning torus
just hitchcock # dolly zoom with a central cube and background shapes
just dolly # dolly zoom against a wall of discs
不用 just 时,请自己设置 COLUMNS 和 LINES,例如 COLUMNS=$(tput cols) LINES=$(tput lines) moon run src/demo --target native -- --torus。
添加运动模糊
--long-exposure 把之前运动的十二次渲染平均到每一帧中;--flow-exposure 先用光流把它们对齐,从而让边缘更清晰:
moon run src/demo --target native -- --hitchcock --long-exposure
moon run src/demo --target native -- --torus --flow-exposure
曝光帧的开销约为普通帧的十二倍(光流对齐还要多得多),因此在大终端中运行较慢。
录制并播放序列
mkdir -p target
moon run src/demo --target native -- --torus --record target/torus.tui3d --duration 4 --fps 24
moon run src/demo --target native -- --play target/torus.tui3d
--record 先渲染所有帧,最后再写文件。录制较长内容时,请改为流式输出帧:
COLUMNS=240 LINES=91 moon run src/demo --target native -- \
--dolly --record-stdout --duration 12 --fps 24 > target/dolly.tui3d
导出并显示静态图像
moon run src/demo --target native -- --hitchcock --export-image target/hitchcock.tuiimg
moon run src/demo --target native -- --show-image target/hitchcock.tuiimg
just export-image 和 just show-image 对默认场景做同样的事。
把录像转换为视频
python3 tools/tui3d_to_video.py target/dolly.tui3d target/dolly.mp4
转换器需要 Python 3、Pillow 和 FFmpeg;见 tools/README.md。
进阶
演示程序是库之上的一层薄封装:每一帧都是 @frontend.build_draw_list 后接 @tui.render_frame,曝光模式则使用 @tui.draw_list_to_tui_luma、LumaBuffer::add_weighted_sample 和 estimate_optical_flow。对照 TUI 教程阅读 src/demo/main.mbt,就能构建自己的播放器。圆环和推拉场景的浏览器版本位于 demo_canvas 和 demo_gsap。
常见陷阱
- 原生目标。 演示程序要读取时钟、环境变量和文件;请用
--target native运行。 - 终端尺寸。 没有
COLUMNS和LINES时,演示程序使用 80 × 32,在较小的终端中可能会滚屏。许多 shell 不会把这些变量导出给子进程;just配方会设置它们。 - 整数选项。
--duration和--fps接受整数;解析器只保留数字,因此0.5会变成5。 - 未知选项会被忽略。 拼错的选项会静默回退为旋转的立方体。
- CPU 占用。 实时循环在帧之间轮询时钟,会占满一个核心。