webglue
WebGL2 wrapper for Rust and WebAssembly
Overview
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.
Key Features
- ๐ฏ 70+ WebGL2 functions - Comprehensive rendering pipeline
- ๐ Instanced rendering - DrawElementsInstanced for high-performance graphics
- ๐ Matrix math library - Perspective, lookAt, rotate, translate, scale operations
- ๐ฆ Optional GLTF support - Load and render GLTF/GLB 3D models
- ๐ Handle-based registry - Bridges u32 IDs โ glow's opaque WebGL handles
- โก Optimized builds - ~105KB WASM (gzipped with LTO)
Quick Start
1. Add to your Cargo.toml
[]
= "0.1"
# Optional: Enable GLTF model loading
= { = "0.1", = ["gltf"] }
2. Build for WebAssembly
# Or use wasm-pack
3. Basic Usage
use webglue as gl;
unsafe
Implemented WebGL2 Functions
Buffer Management (5)
GenBuffers,DeleteBuffers,BindBuffer,BufferData,BufferSubData
Vertex Arrays (7)
GenVertexArrays,DeleteVertexArrays,BindVertexArrayEnableVertexAttribArray,DisableVertexAttribArrayVertexAttribPointer,VertexAttribDivisor
Shaders & Programs (13)
CreateShader,DeleteShader,ShaderSource,CompileShaderCreateProgram,DeleteProgram,AttachShader,LinkProgram,UseProgramGetShaderiv,GetShaderInfoLog,GetProgramiv,GetProgramInfoLog
Uniforms (10)
GetUniformLocationUniform1f,Uniform1i,Uniform2f,Uniform3f,Uniform4fUniform3fv,Uniform4fvUniformMatrix3fv,UniformMatrix4fv
Textures (9)
GenTextures,DeleteTextures,BindTexture,ActiveTextureTexImage2D,TexSubImage2D,TexParameteriGenerateMipmap,PixelStorei
Drawing (3)
DrawArrays,DrawElements,DrawElementsInstancedโก
Framebuffers & Renderbuffers (6)
GenFramebuffers,BindFramebuffer,FramebufferTexture2DGenRenderbuffers,BindRenderbuffer,RenderbufferStorage
State Management (11)
Enable,Disable,Viewport,Clear,ClearColorDepthMask,BlendFunc,BlendFuncSeparate,FinishReadPixels,PointSize(stub)
Queries (5)
GetError,GetGraphicsResetStatus,GetIntegerv,GetString,GetStringi
Total: 70+ functions โ
Architecture
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Your Rust/WASM Application โ
โ (OpenGL-style function calls) โ
โโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโ
โ
โโโโโโโโผโโโโโโโโโโ
โ webglue โ
โ wrapper.rs โ
โโโโโโโโฌโโโโโโโโโโ
โ
โโโโโโโโผโโโโโโโโโโ
โ Handle Registryโ
โ (u32 โ Handle) โ
โโโโโโโโฌโโโโโโโโโโ
โ
โโโโโโโโผโโโโโโโโโโ
โ glow โ
โ (RustโWebGL) โ
โโโโโโโโฌโโโโโโโโโโ
โ
โโโโโโโโผโโโโโโโโโโ
โ WebGL2 โ
โ (Browser) โ
โโโโโโโโโโโโโโโโโโ
Why Handle Registry?
WebGL via glow uses opaque handles (glow::Buffer, glow::Program, etc.), not u32 IDs like desktop OpenGL. The registry:
- Translates u32 IDs โ opaque glow handles
- Maintains OpenGL-style API compatibility
- Uses
slabfor efficient O(1) lookups - Minimal overhead - direct index-to-handle mapping
Math Library
Built-in 4x4 matrix operations for 3D graphics:
use *;
// Projection matrix
let proj = perspective;
// View matrix
let view = look_at;
// Model transformations
let model = identity
.rotate_y
.translate
.scale;
// Combined MVP matrix
let mvp = proj * view * model;
// Upload to shader uniform
unsafe
Available operations:
perspective(),orthographic()- Projection matriceslook_at()- View matrix from camera parametersMat4::identity(),translate(),rotate_x/y/z(),scale()- Transforms- Matrix multiplication with
*operator
GLTF Model Loading
With the gltf feature enabled, you can load and render 3D models:
use GltfModel;
// Load GLTF model from URL
let model = load_from_url.await?;
// Render in your draw loop
unsafe
Supports:
- GLTF 2.0 and GLB binary format
- Embedded textures and images
- Multiple meshes and materials
- Extensions:
KHR_lights_punctual,extras,names
Performance
WebGL2 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 ๐
Demo & Examples
The repository includes a comprehensive Vite-based demo application in demo/:
Live Demo: Opens interactive showcase with multiple samples:
- Textured Sphere - Earth texture mapping with UV coordinates
- Textured Cube - 3D cube with image textures
- Colored Cube - RGB vertex colors
- Instanced Rendering - 1000+ objects in a single draw call
- GLTF Models - Load and render 3D scenes (Box, FlightHelmet, Head)
- Primitives - Sphere, cone geometry generation
- Matrix Transforms - Rotation, translation, scaling
- Blend Modes - BlendFunc and BlendFuncSeparate demos
Building
Library Build (WASM target required)
# Build library for WebAssembly
# With GLTF feature
# Run unit tests (on host target)
Using wasm-pack (Recommended)
# Install wasm-pack
# Build for web
# The pkg/ directory is ready to import in JavaScript:
# import init, * as wasm from './pkg/webglue.js';
Size Optimization
Release builds are optimized for size:
- LTO enabled - Link-time optimization
- Single codegen unit - Maximum optimization
- Result: ~105KB WASM (gzipped)
Cargo.toml configuration:
[]
= 3
= true
= 1
Version Status
| 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.
License
Copyright ยฉ 2025 Top-5
Licensed under the Apache License, Version 2.0 (LICENSE or http://www.apache.org/licenses/LICENSE-2.0)
Contributing
Contributions welcome! Priority areas:
- ๐ง Update to latest dependency versions (gltf 1.4.1, wasm-bindgen 0.2.104, etc.)
- ๐ More WebGL2 functions (additional state management, queries)
- ๐งฎ Extended math utilities (quaternions, frustum culling)
- ๐ฆ More GLTF features (animations, skinning)
- ๐ Documentation and examples
- ๐งช Test coverage improvements
Credits
Built on top of excellent Rust crates:
glow- OpenGL/WebGL bindingsgltf- GLTF 2.0 loaderslab- Efficient handle registrywasm-bindgen- Rust/WASM/JS glue