| Crates.io | webglue |
| lib.rs | webglue |
| version | 0.1.0 |
| created_at | 2025-10-13 11:41:06.191095+00 |
| updated_at | 2025-10-13 11:41:06.191095+00 |
| description | OpenGL ES 3.0 / WebGL2 wrapper for Rust and WebAssembly |
| homepage | |
| repository | https://github.com/top-5/webglue |
| max_upload_size | |
| id | 1880405 |
| size | 125,042 |
WebGL2 wrapper for Rust and WebAssembly
webglue is a WebGL2 wrapper for Rust that compiles to WebAssembly, providing an OpenGL-like API for browser-based 3D graphics.
โ ๏ธ This crate is WASM-only and must be compiled with
--target wasm32-unknown-unknown.
Built on top of glow, webglue translates familiar OpenGL function calls into WebGL2 operations, with a handle registry to bridge between u32 IDs and glow's opaque handles.
Cargo.toml[dependencies]
webglue = "0.1"
# Optional: Enable GLTF model loading
webglue = { version = "0.1", features = ["gltf"] }
cargo build --target wasm32-unknown-unknown --release
# Or use wasm-pack
wasm-pack build --target web --release
use webglue as gl;
unsafe {
// Initialize once with WebGL2 context
gl::init_with_webgl2_context(webgl2_context);
// Create and bind buffer
let mut vbo = 0;
gl::GenBuffers(1, &mut vbo);
gl::BindBuffer(gl::ARRAY_BUFFER, vbo);
// Upload vertex data
gl::BufferData(
gl::ARRAY_BUFFER,
(vertices.len() * 4) as isize,
vertices.as_ptr() as *const _,
gl::STATIC_DRAW
);
// Setup vertex attributes
gl::VertexAttribPointer(0, 3, gl::FLOAT, gl::FALSE, 0, std::ptr::null());
gl::EnableVertexAttribArray(0);
// Draw
gl::DrawArrays(gl::TRIANGLES, 0, vertex_count as i32);
}
GenBuffers, DeleteBuffers, BindBuffer, BufferData, BufferSubDataGenVertexArrays, DeleteVertexArrays, BindVertexArrayEnableVertexAttribArray, DisableVertexAttribArrayVertexAttribPointer, VertexAttribDivisorCreateShader, DeleteShader, ShaderSource, CompileShaderCreateProgram, DeleteProgram, AttachShader, LinkProgram, UseProgramGetShaderiv, GetShaderInfoLog, GetProgramiv, GetProgramInfoLogGetUniformLocationUniform1f, Uniform1i, Uniform2f, Uniform3f, Uniform4fUniform3fv, Uniform4fvUniformMatrix3fv, UniformMatrix4fvGenTextures, DeleteTextures, BindTexture, ActiveTextureTexImage2D, TexSubImage2D, TexParameteriGenerateMipmap, PixelStoreiDrawArrays, DrawElements, DrawElementsInstanced โกGenFramebuffers, BindFramebuffer, FramebufferTexture2DGenRenderbuffers, BindRenderbuffer, RenderbufferStorageEnable, Disable, Viewport, Clear, ClearColorDepthMask, BlendFunc, BlendFuncSeparate, FinishReadPixels, PointSize (stub)GetError, GetGraphicsResetStatus, GetIntegerv, GetString, GetStringiTotal: 70+ functions โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Your Rust/WASM Application โ
โ (OpenGL-style function calls) โ
โโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโ
โ
โโโโโโโโผโโโโโโโโโโ
โ webglue โ
โ wrapper.rs โ
โโโโโโโโฌโโโโโโโโโโ
โ
โโโโโโโโผโโโโโโโโโโ
โ Handle Registryโ
โ (u32 โ Handle) โ
โโโโโโโโฌโโโโโโโโโโ
โ
โโโโโโโโผโโโโโโโโโโ
โ glow โ
โ (RustโWebGL) โ
โโโโโโโโฌโโโโโโโโโโ
โ
โโโโโโโโผโโโโโโโโโโ
โ WebGL2 โ
โ (Browser) โ
โโโโโโโโโโโโโโโโโโ
WebGL via glow uses opaque handles (glow::Buffer, glow::Program, etc.), not u32 IDs like desktop OpenGL. The registry:
slab for efficient O(1) lookupsBuilt-in 4x4 matrix operations for 3D graphics:
use webglue::math::*;
// Projection matrix
let proj = perspective(45.0_f32.to_radians(), aspect_ratio, 0.1, 100.0);
// View matrix
let view = look_at(
&[0.0, 2.0, 5.0], // camera position
&[0.0, 0.0, 0.0], // look at target
&[0.0, 1.0, 0.0], // up vector
);
// Model transformations
let model = Mat4::identity()
.rotate_y(angle)
.translate(&[x, y, z])
.scale(&[sx, sy, sz]);
// Combined MVP matrix
let mvp = proj * view * model;
// Upload to shader uniform
unsafe {
gl::UniformMatrix4fv(mvp_loc, 1, gl::FALSE, mvp.as_ptr());
}
Available operations:
perspective(), orthographic() - Projection matriceslook_at() - View matrix from camera parametersMat4::identity(), translate(), rotate_x/y/z(), scale() - Transforms* operatorWith the gltf feature enabled, you can load and render 3D models:
use webglue::gltf_model::GltfModel;
// Load GLTF model from URL
let model = GltfModel::load_from_url("models/scene.gltf").await?;
// Render in your draw loop
unsafe {
model.render();
}
Supports:
KHR_lights_punctual, extras, namesWebGL2 instanced rendering provides massive performance gains for repeated geometry:
| Rendering Method | Draw Calls | Objects | Performance |
|---|---|---|---|
| Individual DrawArrays | 1,000 | 1,000 | ~15-30 FPS |
| DrawElementsInstanced | 1 | 1,000 | 60 FPS |
| DrawElementsInstanced | 1 | 10,000 | 60 FPS |
Speedup: 100-500x for scenes with many similar objects ๐
The repository includes a comprehensive Vite-based demo application in demo/:
cd demo
npm install
npm run dev
Live Demo: Opens interactive showcase with multiple samples:
# Build library for WebAssembly
cargo build --target wasm32-unknown-unknown --release
# With GLTF feature
cargo build --target wasm32-unknown-unknown --release --features gltf
# Run unit tests (on host target)
cargo test --lib
# Install wasm-pack
cargo install wasm-pack
# Build for web
wasm-pack build --target web --release --out-dir pkg
# The pkg/ directory is ready to import in JavaScript:
# import init, * as wasm from './pkg/webglue.js';
Release builds are optimized for size:
Cargo.toml configuration:
[profile.release]
opt-level = 3
lto = true
codegen-units = 1
| Dependency | Current | Latest | Notes |
|---|---|---|---|
glow |
0.16 | 0.16.0 | โ Up to date |
slab |
0.4 | 0.4.11 | โ Up to date |
gltf |
1.4 | 1.4.1 | โ ๏ธ Consider updating |
wasm-bindgen |
0.2 | 0.2.104 | โ ๏ธ Consider updating |
web-sys |
0.3 | 0.3.81 | โ ๏ธ Consider updating |
Run
cargo updateto get latest compatible versions within semver ranges.
Copyright ยฉ 2025 Top-5
Licensed under the Apache License, Version 2.0 (LICENSE or http://www.apache.org/licenses/LICENSE-2.0)
Contributions welcome! Priority areas:
Built on top of excellent Rust crates:
glow - OpenGL/WebGL bindingsgltf - GLTF 2.0 loaderslab - Efficient handle registrywasm-bindgen - Rust/WASM/JS glue