Skip to content

Rendering

Cursor Agent edited this page Nov 29, 2026 · 1 revision

Rendering

fanalor-render is the only crate that depends on wgpu. It creates the device and the surface, records frames, and draws the editor's egui panels through egui_backend. Games and the editor talk to it in 2D.

Native backends are whichever of Vulkan, Metal, DirectX 12, and OpenGL the machine has. WGPU_BACKEND=vulkan (or dx12, metal, gl) forces one. The web build uses WebGPU where the browser offers it and WebGL2 otherwise. See Export.

2D, painter's order

Sprites are batches of quads. A vertex position is two floats. Pipelines set depth_stencil: None: there is no depth buffer, and nothing is reserved for one. What covers what is draw order. Sorting layers are named bands; inside a layer, sort_order is painter's order.

Camera2D is an orthographic camera. The view turns with the camera's entity. Culling, picking, parallax, and the editor's viewport use that rotated view. Several cameras are supported: split screen, render textures, and pixel-perfect views. CameraFollow2D frames one target or several, with a dead zone, look-ahead, bounds, and a zoom that fits the group. CameraShake offsets and turns the view only (max_angle); it does not move the entity.

A quad or a tilemap chunk outside the view is not drawn. Tilemap chunks keep a mesh cache. Sprite quads are instanced.

What a frame can draw

  • Sprites from textures and atlases, with flips, a pixels-per-unit, and frame animation.
  • Tilemaps with layers, animated tiles, and auto-tiling (a 16-frame terrain and a 47-tile blob). Layers can carry normal maps.
  • Parallax layers and repeating backgrounds, picked and drawn through the camera.
  • Lines, polygons, and trails (Line2D, Polygon2D, Trail2D) in the sprite batch.
  • Particles on the CPU and on the GPU: curves, gradients, emitter shapes, spin, flipbooks, and collisions against the scene's colliders. GPU emitters are culled by a conservative bound.
  • 2D lights: point, spot, and global lights, a light map in the render graph, normal maps, light layers, and cookies (an image that tints and shapes a light). Shadows come from colliders, polygons, or a sprite's alpha silhouette. A caster blocks only lights that share its light layers. Spot lights are culled and shadowed by their cone.
  • Post: a render graph between the world and the HUD. Colour grading and bloom live there.
  • Sprite materials: a game-supplied WGSL fragment and its uniforms, on every backend.
  • Skinned sprite meshes: weights painted in the editor, deformed on the GPU. 2D skeletons, cut-out animation, and two-bone IK drive them.
  • UI in screen space, drawn after the world: text, 9-slice panels, and controls. See the UI chapter.

Text is shaped with HarfBuzz (rustybuzz): ligatures, Arabic, Devanagari, Hebrew, a fallback font chain per language, bidirectional order, and Unicode line breaking. Rich text markup ([b], [color], [size], [font], [icon]) is laid out inline. No CJK font is shipped with the engine.

Colour

sRGB can be applied in the texture format, in the sampler, and again in the shader. Doing it twice darkens mid-tones. egui's textures are gamma-encoded and its blending happens in that space; the egui backend is written to match that, and crates/fanalor-render/tests/headless_egui.rs pins it. The rule is recorded under "egui for the editor UI" in docs/ARCHITECTURE.md.

Shaders and materials

Engine shaders are WGSL under assets/. A sprite material is a game's own fragment shader plus a uniform block, sampled in the same batch as the other sprites. The render graph is how a post pass is inserted; a game does not assemble a wgpu render pass by hand to grade the frame.

Where the work is split

Piece Crate
Device, pipelines, passes, egui backend fanalor-render
Sprite, Transform, tilemaps, lights as components, the batch built from the world fanalor-scene
Texture and atlas caches, hot reload of files under the asset root fanalor-assets
The window and the frame that calls into the renderer fanalor-core

Hot reload watches the asset root, debounces, and swaps bytes under a stable handle. A texture edited in an image tool shows up in the asset browser without a restart; the browser re-reads the directory about once a second.

GPU time per pass is read back without stalling the frame and shown in the editor's Profiler. See Editor.

Clone this wiki locally