Embedded-3dgfx
A complete no_std 3D graphics and physics engine for embedded systems. Features real-time rendering, rigid body dynamics, soft body physics, skeletal animation, and advanced visual effects optimized for resource-constrained devices.
Note: This is a fork of embedded-gfx by Kezii. This fork adds texture mapping, fog/dithering effects, DMA rendering, Z-buffer improvements, and a complete physics engine.
๐ Key Features
๐จ 3D Graphics Rendering
- Complete MVP Pipeline - Model-View-Projection with perspective projection
- Z-buffering - Hardware-style depth testing with tunable epsilon
- Advanced Shading - Flat, Gouraud, directional lighting, Blinn-Phong specular
- Texture Mapping - Affine UV mapping with multi-texture support
- Visual Effects - Fog, dithering, billboards, vertex animation
- LOD System - Distance-based mesh detail switching
- Performance - Frustum culling, backface culling, DMA rendering
โ๏ธ Physics Engine
- Rigid Body Dynamics - Linear/angular motion, forces, torques
- Collision Detection - Sphere, AABB, and Capsule colliders
- Collision Response - Impulse-based with friction and restitution
- Constraints - Distance, ball-socket, and fixed joints
- Ray Casting - Line-of-sight, shooting, object selection
- Body Management - Activation/deactivation, lifecycle control
๐ฆด Skeletal Animation
- Bone Hierarchy - Parent-child skeletal structures
- Linear Blend Skinning - Smooth vertex deformation (SSD)
- Multi-Bone Influence - Up to 4 bones per vertex
- Real-time Animation - Interactive character animation
๐ง Soft Body Physics
- Mass-Spring Systems - Deformable objects (cloth, jelly, soft bodies)
- Pressure Preservation - Volume maintenance for enclosed bodies
- Collision Response - Ground collision with friction
- Pre-built Shapes - Cloth grids, jelly cubes, soft spheres
๐ Quick Start
Installation
[]
# For embedded (no_std)
= { = "0.1", = false }
# For desktop/simulator with all features
= { = "0.1", = ["std"] }
Basic Example
use ;
use Vector3;
// Create engine
let mut engine = new;
engine.camera.set_position;
// Create a cube mesh
let geometry = Geometry ;
let mut mesh = new;
mesh.set_render_mode;
// Render
engine.render;
Physics Example
use ;
use Vector3;
// Create physics world
let mut world = new;
world.set_gravity;
// Add a falling sphere
let sphere = new
.with_collider
.with_position;
world.add_body;
// Add static ground
let ground = new_static
.with_collider;
world.add_body;
// Simulate
world.;
// Ray cast
let ray = new;
if let Some = world.ray_cast
๐ฎ Examples
The project includes 23+ interactive examples. Run with:
3D Rendering Examples
Core Rendering:
basic_rendering- Render mode cycling (Points, Lines, Solid)rotating_cube- Animated 3D transformationsscene_viewer- Interactive multi-mesh scene
Lighting & Effects:
lighting_demo- Directional lighting with ambientgouraud_demo- Smooth color interpolationblinn_phong_demo- Specular highlightsfog_dithering_demo- Atmospheric effects
Advanced Features:
texture_mapping_demo- UV-mapped texturesdma_rendering_demo- Double-buffer performancebillboard_demo- Camera-facing quadslod_demo- Level of detail switchingvertex_animation_demo- Keyframe animationstl_viewer- Load and view STL models
Physics Examples
Rigid Body Dynamics: (See PHYSICS_EXAMPLES.md)
physics_rolling_ballโญ START HERE - Beginner-friendly introphysics_bouncing_balls- Multiple colliding spheresphysics_pendulum- Swinging pendulum with constraintsphysics_newtons_cradle- Newton's cradle with chain constraintsphysics_stack_tower- Stacking boxes with frictionphysics_domino_chain- Domino effect simulationphysics_wrecking_ball- Wrecking ball demolitionphysics_demo- Comprehensive physics showcasecapsule_physics_demo- Capsule collider interactions
Advanced Physics: (See SKELETAL_AND_SOFTBODY.md)
skeletal_animation_demo- Bone hierarchy and skinningcloth_simulation- Hanging cloth with windjelly_cube_demo- Soft deformable cuberaycast_demo- Ray casting and hit detection
๐ Feature Flags
std (includes perfcounter)
- Enables Painter's Algorithm (uses
std::vec::Vec) - Performance counter with std timing
- Required for desktop simulator examples
perfcounter (optional)
- Enables FPS/timing measurements
- Requires timing source:
stdorembassy-time - Automatically included with
std
embassy-time (optional)
- Timing for embedded targets using Embassy framework
- Use with
perfcounterfor embedded performance monitoring
row_width_* (mutually exclusive)
row_width_160- Optimize for 160px wide displaysrow_width_240- Optimize for 240px wide displays (default)row_width_320- Optimize for 320px wide displays
๐ฏ Core Rendering Features
Rendering Pipeline
- Full MVP Transforms - Model-View-Projection with perspective
- Z-buffering - 16.16 fixed-point depth testing
- Backface Culling - ~50% face elimination
- Frustum Culling - 50-200% speedup for off-screen objects
- Optimized Rasterization - Integer-only triangle filling
Shading & Lighting
- Flat Shading - Per-face solid colors
- Gouraud Shading - Smooth vertex color interpolation
- Directional Lighting - Diffuse with ambient term
- Blinn-Phong - Specular highlights with half-vector
- Pre-computed Constants - Per-mesh optimization
Visual Effects
- Fog - Depth-based atmospheric effects
- Dithering - 4ร4 Bayer matrix ordered dithering
- Billboards - Camera-facing sprites
- Vertex Animation - Keyframe interpolation
Texturing
- Affine Mapping - UV-based texture coordinates
- Multi-texture - Multiple textures per scene
- Power-of-2 Optimization - Fast wrapping with bit masks
- RGB565 Format - Memory-efficient 16-bit color
Performance Systems
- DMA Rendering - ~30% FPS boost with double buffering
- LOD - 3-10ร triangle reduction at distance
- Swap Chain - Asynchronous buffer transfers
- Inline Optimization - Aggressive hot-path inlining
โ๏ธ Physics System
Rigid Body Dynamics
let body = new
.with_collider
.with_position
.with_restitution // Bounciness
.with_friction; // Surface friction
Collision Detection
- Sphere Collider - Cheapest, perfect for balls and particles
- AABB Collider - Boxes, platforms, walls (axis-aligned)
- Capsule Collider - Best for characters (efficient + smooth)
- Broad Phase - O(nยฒ) narrow-phase only (spatial partitioning planned)
Constraints & Joints
// Distance constraint (rope, rod)
world.add_constraint;
// Ball-socket joint (ragdoll)
world.add_constraint;
// Fixed joint (welding)
world.add_constraint;
Ray Casting
let ray = new;
if let Some = world.ray_cast
๐ฆด Skeletal Animation
let mut skeleton = new;
let root = skeleton.add_bone.unwrap;
let arm = skeleton.add_bone.unwrap;
skeleton.update_transforms;
skeleton.compute_inverse_bind_poses;
// Animate
skeleton.get_bone_mut.unwrap.set_rotation;
skeleton.update_transforms;
// Apply to mesh
apply_skinning;
๐ง Soft Body Physics
// Create cloth (8ร8 grid)
let mut cloth = create_cloth.unwrap;
cloth.set_gravity;
// Create jelly cube (3ร3ร3 particles)
let mut jelly = create_jelly_cube.unwrap;
jelly.pressure_config.enabled = true; // Volume preservation
// Simulate
cloth.step;
jelly.step;
// Get deformed vertices for rendering
cloth.get_vertex_positions;
๐ System Requirements
Embedded Targets
Minimum:
- ARM Cortex-M4F (with FPU)
- 128KB RAM (single-buffer + small scenes)
- 256KB+ Flash
Recommended:
- ARM Cortex-M33 with FPU (e.g., STM32WBA)
- 512KB+ RAM (double-buffer + Z-buffer + physics)
- 512KB+ Flash
Performance Budget (240ร135 @ 60 FPS)
- Rendering: ~10-13ms per frame
- Physics (16 bodies): ~2-3ms per frame
- Display transfer (DMA): ~3ms (parallel)
- Total: 60+ FPS achievable
Memory Usage
| Feature | Memory Cost |
|---|---|
| Single framebuffer (240ร135) | 65 KB |
| Double framebuffer (240ร135) | 130 KB |
| Z-buffer (240ร135) | 130 KB |
| Physics (16 bodies) | ~4 KB |
| Soft body (64 particles) | ~2 KB |
| Skeleton (8 bones) | ~1 KB |
Total typical usage: 200-300 KB RAM
๐งช Testing
# Run all 182 tests
# Run library tests only
# Test with all features
Test Coverage:
- โ 182 passing tests
- โ Rendering pipeline (transformations, clipping, rasterization)
- โ Physics (collisions, constraints, integration)
- โ Skeletal animation (skinning, bone hierarchy)
- โ Soft body (springs, particles, pressure)
- โ Ray casting (all collider types)
๐ฆ no_std Compatibility
Fully no_std compatible by default. All core features work without the standard library.
Feature Flag Combinations
Pure Embedded (no_std):
= { = "0.1", = false }
Embedded with Embassy Timing:
= { = "0.1", = false, = ["perfcounter", "embassy-time"] }
Desktop/Simulator:
= { = "0.1", = ["std"] }
Custom Row Width:
= { = "0.1", = false, = ["row_width_320"] }
๐ฏ Use Cases
Game Development
- โ Character controllers (capsule colliders + ray casting)
- โ Projectile physics (rigid bodies + continuous motion)
- โ Skeletal animation (characters, creatures)
- โ Particle systems (billboards + soft bodies)
- โ Level geometry (static AABB colliders)
Simulations
- โ Robotics visualization (skeletal arms, kinematics)
- โ Physics demonstrations (pendulum, Newton's cradle)
- โ Cloth/soft body (flags, deformable objects)
- โ Mechanical systems (constraints, joints)
Embedded Displays
- โ STM32 with LCD displays
- โ ESP32 with TFT screens
- โ RP2040 with displays
- โ M5Stack devices
๐ Documentation
- PHYSICS_EXAMPLES.md - Complete physics demo guide
- SKELETAL_AND_SOFTBODY.md - Animation & soft body documentation
- examples/README.md - All examples with screenshots
- API Docs -
cargo doc --open
๐๏ธ Architecture
embedded-3dgfx/
โโโ src/
โ โโโ lib.rs # Main engine & rendering
โ โโโ camera.rs # View/projection matrices
โ โโโ mesh.rs # Geometry & LOD
โ โโโ draw.rs # Rasterization & effects
โ โโโ physics.rs # Rigid body dynamics (3048 lines)
โ โโโ skeleton.rs # Skeletal animation (470 lines)
โ โโโ softbody.rs # Soft body physics (749 lines)
โ โโโ texture.rs # Texture management
โ โโโ billboard.rs # Camera-facing quads
โ โโโ animation.rs # Keyframe animation
โ โโโ swapchain.rs # DMA double-buffering
โ โโโ perfcounter.rs # Performance monitoring
โ โโโ painters.rs # Painter's algorithm
โ
โโโ examples/ # 23 interactive demos
โ โโโ basic_rendering.rs
โ โโโ rotating_cube.rs
โ โโโ physics_*.rs # 8 physics demos
โ โโโ cloth_simulation.rs
โ โโโ skeletal_animation_demo.rs
โ โโโ raycast_demo.rs
โ โโโ ...
โ
โโโ load_stl/ # STL file embedding macro
๐ง Performance Optimizations
Recent performance improvements (2026):
- โ Interpolator-based rasterization - Faster triangle filling
- โ FnvIndexSet edge deduplication - Faster mesh processing
- โ Configurable row buffers - Memory/speed trade-off
- โ Inline hot paths - Better compiler optimization
- โ mem::swap optimizations - Reduced allocations
Measured improvements:
- Triangle rasterization: ~20% faster
- Edge generation: ~30% faster
- Memory usage: Configurable (160-320px row buffers)
๐ Learning Resources
Start with these examples in order:
rotating_cube- Learn basic 3D renderingphysics_rolling_ballโญ - Understand physics basicslighting_demo- Explore shadingskeletal_animation_demo- See advanced animationcloth_simulation- Experience soft body physics
๐ค Contributing
Contributions welcome! Priority areas:
- Hardware-specific display backends (ESP32, STM32, RP2040)
- Spatial partitioning (octree/grid for broad-phase)
- More joint types (hinge, slider, prismatic)
- Perspective-correct texture mapping
- Character controller system
- Mesh colliders (convex hulls)
๐ Production Use
Working embedded example: Rust on M5Stack Cardputer
๐ References
Graphics:
- Tricks of the 3D Game Programming Gurus
- Michael Abrash's Graphics Programming Black Book
- PSX Graphics Programming
Physics:
- Game Physics Engine Development
- Real-Time Collision Detection
- Game Programming Gems (Physics sections)
๐ License
MIT OR Apache-2.0 - See LICENSE file for details.
Built with โค๏ธ for the embedded Rust community