#miniquad #graphics #rendering #binding #web #cross-platform

sapp-wasm

Miniquad 渲染库的一部分。GL 和 sokol-app API 的绑定和 JS 实现。

26 个版本

0.1.26 2021 年 5 月 14 日
0.1.23 2021 年 3 月 22 日
0.1.20 2020 年 12 月 31 日
0.1.19 2020 年 10 月 5 日
0.1.15 2020 年 3 月 31 日

#184图形 API

Download history 400/week @ 2024-03-13 589/week @ 2024-03-20 587/week @ 2024-03-27 788/week @ 2024-04-03 434/week @ 2024-04-10 582/week @ 2024-04-17 621/week @ 2024-04-24 482/week @ 2024-05-01 475/week @ 2024-05-08 532/week @ 2024-05-15 546/week @ 2024-05-22 619/week @ 2024-05-29 445/week @ 2024-06-05 393/week @ 2024-06-12 489/week @ 2024-06-19 433/week @ 2024-06-26

每月 1,848 次下载
用于 15 个 Crates(直接使用 2 个)

MIT/Apache

140KB
3.5K SLoC

Rust 2.5K SLoC // 0.0% comments JavaScript 1K SLoC // 0.0% comments

Miniquad

Github Actions Docs Crates.io version Discord chat Matrix

在不需要深层依赖树和数千行代码来使用计算机绘制东西的世界里,Miniquad 是一个梦想的实现。

Miniquad 旨在提供一种图形抽象,在具有 GPU 的任何平台上工作方式相同,尽可能轻量,同时覆盖尽可能多的机器。

支持的平台

  • Windows,OpenGL 3,OpenGL 2.2;
  • Linux,OpenGL 2.2,OpenGL 3,GLES 2,GLES 3;
  • macOS,OpenGL 3,Metal;
  • iOS,GLES 2,GLES 3,Metal;
  • WASM,WebGL 1 - 在 iOS Safari、Firefox、Chrome 上测试过;
  • Android,GLES 2,GLES 3。

示例

Imgur

examples/quad.rs: web 示例
examples/offscreen.rs: web 示例

PonasKovas/miniquad-mandelbrot: web 示例

构建示例

Linux

cargo run --example quad

在 NixOS Linux 上,您可以使用 shell.nix 启动一个开发环境,在其中可以构建和运行 Miniquad。

Windows

# both MSVC and GNU target is supported:
rustup target add x86_64-pc-windows-msvc
# or
rustup target add x86_64-pc-windows-gnu

cargo run --example quad

WASM

rustup target add wasm32-unknown-unknown
cargo build --example quad --target wasm32-unknown-unknown

然后使用以下 .html 来加载 .wasm

index.html
<html lang="en">

<head>
    <meta charset="utf-8">
    <title>TITLE</title>
    <style>
        html,
        body,
        canvas {
            margin: 0px;
            padding: 0px;
            width: 100%;
            height: 100%;
            overflow: hidden;
            position: absolute;
            background: black;
            z-index: 0;
        }
    </style>
</head>

<body>
    <canvas id="glcanvas" tabindex='1'></canvas>
    <!-- Minified and statically hosted version of https://github.com/not-fl3/miniquad/blob/master/native/sapp-wasm/js/gl.js -->
    <script src="https://not-fl3.github.io/miniquad-samples/gl.js"></script>
    <script>load("quad.wasm");</script> <!-- Your compiled wasm file -->
</body>

</html>

提供静态 .wasm 和 .html 的一种方法

cargo install basic-http-server
basic-http-server .

Android

构建 Android 的推荐方法是使用 Docker。
miniquad 使用稍作修改的 cargo-apk 版本

docker run --rm -v $(pwd)":/root/src" -w /root/src notfl3/cargo-apk cargo quad-apk build --example quad

APK 文件将在 target/android-artifacts/(debug|release)/apk

启用 "log-impl" 后,所有日志调用将被转发到 adb 控制台。无需对 Android 进行代码修改,一切应该都能正常工作。

iOS

在模拟器上运行

mkdir MyGame.app
cargo build --target x86_64-apple-ios --release
cp target/release/mygame MyGame.app
# only if the game have any assets
cp -r assets MyGame.app
cat > MyGame.app/Info.plist << EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleExecutable</key>
<string>mygame</string>
<key>CFBundleIdentifier</key>
<string>com.mygame</string>
<key>CFBundleName</key>
<string>mygame</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
</dict>
</plist>
EOF

xcrun simctl install booted MyGame.app/
xcrun simctl launch booted com.mygame

有关为真实 iPhone 配置的详细信息和建议,请查看https://macroquad.rs/articles/ios/

交叉编译


# windows target from linux host:
# this is how windows builds are tested from linux machine:
rustup target add x86_64-pc-windows-gnu
cargo run --example quad --target x86_64-pc-windows-gnu

目标

  • 快速编译时间。目前从 "cargo clean" 开始大约需要 5 秒,适用于桌面和网页。

  • 跨平台。所需平台特定用户代码的量应尽可能少。

  • 低端设备支持。

  • 可修改性。在制作自己的游戏时,很可能会发现一些硬件兼容性问题。绕过这类错误应该很容易,实现细节不应被抽象层所隐藏。

  • 可分支性。每个平台实现通常只是一个纯 Rust 文件。这个文件非常适合复制粘贴 - 它不使用任何 miniquad 特定的抽象。复制 miniquad 的平台实现的一部分并将其单独使用非常容易。

非目标

  • 最终类型安全性。库应完全符合 Rust 的安全定义 - 无 UB 或内存不安全。但正确的 GPU 状态不能保证类型正确。用户代码中可以自由提供安全性抽象!

  • 高端 API,如 Vulkan/DirectX 12。请参阅 gfx-rsvulkano

白金赞助商

Miniquad 由以下机构支持

SourceGear

无运行时依赖