外观
渲染与绘制
以下示例直接使用全局绘制函数。
坐标系
- 原点在窗口左上角,x 向右,y 向下。
- 所有绘制与输入坐标都是逻辑像素,HiDPI 由窗口 DPI 自动缩放。
screen_width()/screen_height()返回逻辑尺寸,dpi_scale()返回缩放系数。
rust
let (w, h) = (screen_width(), screen_height());
draw_rectangle(w * 0.5 - 100.0, h * 0.5 - 60.0, 200.0, 120.0, SKYBLUE);清屏
rust
clear(hex(0x0a0f18));
// 或
clear_background(BLACK);clear 设置本帧的清屏色,实际清屏发生在 present 时。
颜色
rust
rgba(0.2, 0.6, 1.0, 1.0) // r, g, b, a ∈ [0, 1]
rgb8(255, 140, 0)
rgba8(255, 140, 0, 180)
hex(0xff8c00) // RRGGBB
hexa(0xff8c0080) // RRGGBBAA
WHITE.with_alpha(0.5)
BLACK.lerp(WHITE, 0.25)内置常量:WHITE、BLACK、RED、GREEN、BLUE、YELLOW、ORANGE、PURPLE、PINK、GRAY、DARKGRAY、LIGHTGRAY、BROWN、CYAN、MAGENTA、LIME、MAROON、NAVY、TEAL、OLIVE、SILVER、GOLD、SKYBLUE、VIOLET、DARKGREEN、DARKBLUE、DARKRED、BEIGE、TRANSPARENT。
抗锯齿
通过 Config::with_antialiasing 选择:
| 值 | 效果 |
|---|---|
AntiAliasing::None | 关闭 SDF 平滑与 MSAA,边缘为硬边 |
AntiAliasing::Sdf | 默认。SDF + fwidth 逐像素解析抗锯齿 |
AntiAliasing::Msaa4 | 在 SDF 基础上增加 4x MSAA 渲染目标 |
rust
let config = Config::default().with_antialiasing(AntiAliasing::Msaa4);Msaa4 会增加显存占用与带宽;普通 2D 图元使用默认 Sdf 已经足够。纹理与文字始终使用线性采样。
矩形与圆角矩形
rust
// 左上角 + 宽高
draw_rectangle(40.0, 40.0, 240.0, 120.0, SKYBLUE);
draw_rectangle_lines(40.0, 40.0, 240.0, 120.0, 2.0, WHITE);
draw_rounded_rectangle(320.0, 40.0, 240.0, 120.0, 24.0, VIOLET);
draw_rounded_rectangle_lines(320.0, 40.0, 240.0, 120.0, 24.0, 2.0, WHITE);
// 绕中心旋转
draw_rectangle_rotated(40.0, 200.0, 240.0, 80.0, 0.3, GOLD);
draw_rounded_rectangle_rotated(320.0, 200.0, 240.0, 80.0, 40.0, -0.2, ORANGE);
// Rect
draw_rect(Rect::new(40.0, 320.0, 240.0, 80.0), PINK);thickness 为描边宽度;描边以边界为中心,向内向外各占一半。
圆与线
rust
draw_circle(200.0, 300.0, 70.0, ORANGE);
draw_circle_lines(200.0, 300.0, 70.0, 3.0, WHITE);
draw_line(40.0, 500.0, 400.0, 460.0, 4.0, LIME);线段使用圆头端帽,厚度会自动扩展两端。
椭圆、弧与圆环
rust
draw_ellipse(200.0, 360.0, 80.0, 40.0, LIME);
draw_ellipse_lines(200.0, 360.0, 80.0, 40.0, 3.0, GOLD);
draw_arc(Vec2::new(420.0, 360.0), 60.0, 0.0, 4.0, 32, 5.0, PINK);
draw_ring(Vec2::new(560.0, 360.0), 50.0, 8.0, CYAN);draw_arc 的角度单位为弧度,segments 越大越平滑。
三角形与多边形
rust
let a = Vec2::new(500.0, 100.0);
let b = Vec2::new(620.0, 260.0);
let c = Vec2::new(380.0, 260.0);
draw_triangle(a, b, c, GOLD);
// 扇形三角化,适用于凸多边形
let points = [
Vec2::new(700.0, 100.0),
Vec2::new(820.0, 160.0),
Vec2::new(780.0, 280.0),
Vec2::new(660.0, 240.0),
];
draw_polygon(&points, TEAL);draw_polygon 以第一个点为扇心三角化,凹多边形请自行拆成多个三角形。
描边版本:
rust
draw_triangle_lines(a, b, c, 2.0, WHITE);
draw_polygon_lines(&points, 2.0, WHITE);纹理
rust
// 桌面端从文件加载(PNG / JPEG / GIF / BMP / TGA / WebP / TIFF)
let tex = load_texture("assets/player.png")?;
// 跨平台:从内存字节加载
let tex = load_texture_from_bytes(&png_bytes)?;
// 从 RGBA8 原始数据创建:长度必须为 w * h * 4
let tex = create_texture(width, height, &rgba_bytes);
// 程序生成
let circle = generate_texture(64, 64, |x, y| {
let dx = x as f32 - 32.0;
let dy = y as f32 - 32.0;
if dx * dx + dy * dy < 32.0 * 32.0 {
WHITE
} else {
TRANSPARENT
}
});
draw_texture(&tex, 100.0, 100.0, WHITE);
draw_texture_ex(&tex, 200.0, 100.0, 128.0, 128.0, 0.2, WHITE);
// 源矩形以像素为单位
let frame = Rect::new(0.0, 0.0, 32.0, 32.0);
draw_texture_rect(&tex, Rect::new(400.0, 100.0, 64.0, 64.0), frame, WHITE);Texture2D 提供 width()、height()、size()、aspect()、id()。克隆纹理句柄只增加引用计数。
绘制顺序与批处理
- 绘制顺序就是调用顺序。
- 相同纹理、相邻的图元会自动合批。
- 15000 个同纹理精灵只会产生少量绘制调用。
- 频繁在多个纹理之间来回切换会增加绘制调用,尽量把同纹理的图元集中绘制。
文字使用独立的字形图集纹理,因此“图元 → 文字 → 图元”会自然分成多个批次,但顺序仍然正确。
批处理
- 同一纹理与材质的连续图元会自动合批。
- 文字与不同纹理会形成多个批次,但绘制顺序保持不变。
- 设备丢失、窗口缩放会自动恢复;遮挡与最小化不会暂停渲染循环。
裁剪与视口
rust
set_scissor(40.0, 40.0, 240.0, 120.0);
draw_rectangle(0.0, 0.0, 800.0, 600.0, RED); // 只显示裁剪区
clear_scissor();
set_viewport(40.0, 400.0, 320.0, 180.0);
draw_scene(); // 整个画面缩放到视口内
clear_viewport();裁剪可与相机、批处理同时使用;视口会改变投影,把画面缩放到指定区域。
性能建议
- 大量精灵使用同一张纹理(或图集)。
- 屏幕外图元默认会自动剔除;需要保留时关闭
Config::with_culling。 - 避免每帧创建纹理;纹理与音频解码应在加载阶段完成。
- 文本按需缓存,避免每帧传入超长字符串;字号变化会增加缓存。
- 需要更高上限时设置
Config::with_max_instances,超出时也会自动扩容。