Skip to content

类型与数学 ​

Vec2 ​

Vec2 { x: f32, y: f32 },提供常用向量运算,可直接用 + - * / 与 f32 组合。

rust
let p = Vec2::new(3.0, 4.0);
let q = vec2(1.0, 2.0);
p.length();          // 5.0
p.normalize();       // 单位向量
p.dot(q);
p.perp();
p.distance(q);
p.angle();           // atan2(y, x)
p.rotated(0.5);      // 弧度
p.lerp(q, 0.5);
p.clamp_length(10.0);

常量:Vec2::ZERO、ONE、X、Y。

Vec3 ​

Vec3 { x, y, z },用于 3D 相机与模型:

rust
let p = Vec3::new(1.0, 2.0, 3.0);
let q = vec3(0.0, 1.0, 0.0);
p.dot(q);
p.cross(q);
p.normalize();
p.length();

常量:Vec3::ZERO、ONE、X、Y、Z。

Rect ​

rust
let r = Rect::new(10.0, 20.0, 200.0, 100.0);
r.left(); r.top(); r.right(); r.bottom();
r.center();          // Vec2
r.size();            // Vec2
r.contains(Vec2::new(50.0, 50.0));
r.union(Rect::new(0.0, 0.0, 50.0, 50.0));
Rect::from_center_size(Vec2::ZERO, Vec2::new(64.0, 64.0));

Color ​

rust
rgba(r, g, b, a);          // f32 ∈ [0, 1]
rgb(r, g, b);
rgb8(255, 140, 0);
rgba8(255, 140, 0, 180);
hex(0xff8c00);
hexa(0xff8c0080);
color.with_alpha(0.5);
color.lerp(other, 0.5);
color.to_rgba8();                // [u8; 4]

Camera2D ​

rust
Camera2D {
    target: Vec2,
    zoom: f32,
    rotation: f32,
    render_target: Option<RenderTarget>,
}

构造:Camera2D::default()、Camera2D::new(target)、Camera2D::at(x, y),链式 with_zoom / with_rotation / with_render_target。Camera2D 可克隆。

Texture2D ​

rust
texture.width();
texture.height();
texture.size();     // (u32, u32)
texture.aspect();
texture.id();
texture.wgpu_texture();   // &wgpu::Texture
texture.filter();         // FilterMode
texture.set_filter(FilterMode::Nearest);
texture.wrap();           // WrapMode
texture.set_wrap(WrapMode::Repeat);

克隆 Texture2D 只增加引用计数。

DrawTextureParams:dest_size、source、rotation、flip_x、flip_y、pivot。

RenderTarget ​

rust
let rt = render_target(512, 512);
rt.texture();
rt.width();
rt.height();
rt.size();

TextureAtlas ​

rust
let mut builder = AtlasBuilder::new(1024, 1024);
let region = builder.add(&image)?;
let atlas = builder.build()?;
atlas.draw(region, x, y, WHITE);
atlas.texture();

Image ​

rust
Image::new(w, h, color);
Image::from_file(path);
Image::from_bytes(&bytes);
Image::from_rgba8(w, h, data);
image.get_pixel(x, y);
image.set_pixel(x, y, color);
image.resize(w, h);
image.to_png();
image.export_png(path);

InputState ​

rust
input.is_key_down(Key::A);
input.is_key_pressed(Key::Space);
input.is_key_released(Key::Escape);
input.is_mouse_button_down(MouseButton::Left);
input.mouse_position();
input.mouse_wheel();
input.typed_text();
input.keys_down();        // 当前按下的键迭代器
input.keys_pressed();     // 本帧按下的键
input.last_key_pressed();
input.super_down();
input.touches();

Touch { id, phase, position },TouchPhase:Started、Moved、Ended、Cancelled。Key 与 MouseButton 是 Copy + Eq + Hash 的枚举。

Material 与模型 ​

  • Material::new(fragment, uniforms)、set_uniforms。
  • Model { vertices, normals, uvs, triangles },三角形包含索引、UV 与法线。

文本类型 ​

rust
pub struct TextParams {
    pub font_size: f32,
    pub color: Color,
    pub font: Option<FontId>,
    pub font_scale: f32,
    pub font_scale_aspect: f32,
    pub rotation: f32,
    pub letter_spacing: f32,
}

pub struct TextMeasure {
    pub width: f32,
    pub height: f32,
}

pub struct Time {
    pub dt: f32,
    pub elapsed: f32,
    pub frame: u64,
    pub fps: f32,
}

音频类型 ​

  • Audio:load_sound、load_music、latency、recover_if_needed。
  • Sound:play、play_volume、length。
  • Music:play、pause、is_paused、set_volume、seek、position、length、set_low_pass、fade_in、fade_out。
  • MusicParams:loop_mix_time、amplifier、playback_rate、command_buffer_size。

详见音频。

配置与错误 ​

rust
Config::default()
    .with_title("game")
    .with_size(1280, 720)
    .with_antialiasing(AntiAliasing::Msaa4)
    .with_pixel_perfect(true)
    .with_culling(true)
    .with_backend(Backend::Vulkan);

pub type Error = Box<dyn std::error::Error + Send + Sync + 'static>;
pub type Result<T> = std::result::Result<T, Error>;
  • Backend:Auto、Vulkan、OpenGl、Metal、Dx12
  • AntiAliasing:None、Sdf、Msaa4
  • FilterMode:Nearest、Linear
  • WrapMode:ClampToEdge、Repeat、MirrorRepeat
  • shader::ShaderStage:Vertex、Fragment、Compute

简单、强力、跨平台的 2D 游戏库