Skip to content

crate

weft_graphics

Weft graphics: platform-independent 2D and 3D rendering data.

Everything here is pure computation so it can be unit tested natively; weft-web turns DrawCommands into Canvas 2D calls (including ParticleSystem backgrounds), and Scenes, particle fields and shader presets into WebGL2 draws.

Structs

  • Animation

    A model's animation: its name, and a channel per animated property of a node.

  • Attribute

    Extra per-vertex data for a custom shader: size floats per vertex.

  • Camera

    A scene's camera: where it is, what it looks at, and its field of view and clipping planes.

  • Channel

    One node property over time.

  • Color

    A color with components in 0.0..=1.0.

  • GlyphSet

    Glyphs made ahead of time (weft assets font): metrics, kerning and a mesh per character, at one depth and bevel.

  • Light

    A directional key light plus hemisphere (sky/ground) ambient light. direction points towards the light.

  • Mat4

    A 4×4 matrix stored column-major (m[col * 4 + row]), as WebGL expects.

  • Mesh

    Triangle geometry: positions, normals, texture coordinates, optional colours and skinning, and the indices of its triangles.

  • Model

    A loaded 3D model (glTF, OBJ, STL, PLY): its nodes, meshes, materials, textures and animations.

  • ModelMaterial

    A model's material, mapped onto Weft's physical material (glTF's metallic-roughness PBR) or, for formats without PBR, the standard one.

  • ModelMesh

    One drawable piece: a mesh and its material.

  • ModelRef

    model: a model file and how to play it.

  • ModelTexture

    A model's texture: where its image is, and how it wraps and is filtered.

  • Modes

    Pointer reactions, for hovering or clicking.

  • Motion

    Continuous animation applied on top of an object's transform.

  • Node

    A node of a model: its name, transform, parent and mesh.

  • Object3D

    An object in a scene: its geometry, transform, colour and material, motion and animation, label and shadows.

  • Particle

    One particle.

  • ParticleField

    A cloud of glowing points that orbit the field's Y axis.

  • ParticleOptions

    Everything about a particle background. Sizes and distances are CSS pixels; speeds are pixels per frame at 60 frames a second, halved (as in particles.js, whose options these follow).

  • ParticleSystem

    A running particle background.

  • PhysicalMaterial

    The physically based material: Cook-Torrance (GGX) shading with a metallic-roughness workflow. The base colour (albedo) is the object's colour, times map.

  • PointLight

    A light at a point, fading out over distance (0 = no falloff).

  • Rect

    Axis-aligned rectangle (also used for accessibility bounds).

  • Rng

    Small deterministic xorshift* generator.

  • Scene

    A 3D scene: its camera, lights, objects and backdrop.

  • ShaderSpec

    A full-surface animated shader (shader element).

  • Shadow

    How a light's shadows are drawn (a shadow map).

  • Skin

    A skeleton: its joints (nodes) and each one's inverse bind matrix.

  • SpotLight

    A cone of light from a point towards target, fading out over distance (0 = no falloff).

  • Style

    Fill/stroke styling for a shape.

  • TextSpec

    text3d: a string set in a font.

  • TextureRef

    An image a material samples.

  • Transform

    Position, Euler rotation (radians) and scale.

  • Tweener

    Smoothly animates objects between successive scene states: whenever an object's target transform or color changes and it has a non-zero transition_ms, it eases from where it currently appears.

  • Vec3

    A 3D vector: a point or direction.

Enums

  • Blending

    How a transparent material combines with what's behind it.

  • Detect

    Where the pointer is followed.

  • Direction

    The way particles drift.

  • DrawCommand

    One Canvas 2D API call.

  • Edge

    What happens to a particle that reaches the edge.

  • Filter

    How a texture is sampled: smoothly (Linear), or pixel by pixel (Nearest).

  • Geometry

    What an object is made of.

  • Interpolation

    How an animation channel moves between keyframes.

  • Material

    What an object's surface looks like.

  • ParticleShape

    The shape particles are scattered in.

  • Path

    Which property of a node an animation channel moves.

  • Primitive

    A built-in 3D shape, with its dimensions.

  • Shape

    A shape on a 2D canvas.

  • Side

    Which faces draw.

  • Sprite

    A particle's shape.

  • TextAlign

    Horizontal text alignment relative to x.

  • TextAlign3

    How 3D text lines up with its position.

  • TextureSource

    Where a texture's image is.

  • Uniform

    A shader uniform's value.

  • Wrap

    How a texture repeats beyond its edges.

Traits

  • Glyphs

    What text layout needs from a font. Units are font units; meshes are in ems (one em = units_per_em), front face towards +z.

Functions

angle_from_top

pub fn angle_from_top(dx: f32, dy: f32) -> f32

Clockwise angle in degrees from 12 o'clock of the vector (dx, dy) (screen coordinates, y down), in 0..360.

catmull_rom

pub fn catmull_rom(points: &[(f32, f32)]) -> Vec<DrawCommand>

Cubic Bézier segments for a Catmull-Rom spline through points (the caller emits the initial MoveTo).

custom_fragment

pub fn custom_fragment(body: &str) -> String

Wrap user GLSL (which must define main writing out_color) with the standard uniforms and helpers.

custom_mesh_program

pub fn custom_mesh_program(vertex: Option<&str>, fragment: &str) -> (String, String)

The vertex and fragment sources for a custom mesh material. A source with its own #version is used as it is; otherwise it gets the prelude: the standard attributes (a_position, a_normal, a_uv, a_color), uniforms (u_size — the mesh's own size in world units — u_mvp, u_model, u_view, u_projection, u_normal_matrix, u_time, u_resolution, u_camera, u_color, u_opacity, u_map, the lights), snoise(vec3), and in fragments weft_lambert(n, world), weft_lin, weft_gamma and out_color. With no vertex source, CUSTOM_DEFAULT_VERTEX passes v_uv, v_normal, v_world, v_position and v_color on.

deg_to_rad

pub fn deg_to_rad(deg: f32) -> f32

Degrees to radians.

directional_shadow_matrix

pub fn directional_shadow_matrix(direction: Vec3, casters: &[Bounds], receivers: &[Bounds]) -> Option<Mat4>

The key light's shadow view-projection: an orthographic box looking along direction (which points towards the light), fitted around casters and deep enough to reach receivers. None without casters.

display_list

pub fn display_list(shapes: &[Shape], width: f32, height: f32) -> Vec<DrawCommand>

Full frame: clear, then each shape inside its own save/restore.

distance_to_segment

pub fn distance_to_segment(p: (f32, f32), a: (f32, f32), b: (f32, f32)) -> f32

Shortest distance from p to the segment a–b.

ease_in_out_cubic

pub fn ease_in_out_cubic(t: f32) -> f32

Cubic ease-in-out of t (0 to 1).

ease_out_cubic

pub fn ease_out_cubic(t: f32) -> f32

Decelerating curve, clamped to 0..=1.

hit_test

pub fn hit_test(shapes: &[Shape], x: f32, y: f32) -> Option<usize>

Index of the topmost (last drawn) shape containing the point.

joint_bounds

pub fn joint_bounds(mesh: &Mesh) -> Vec<Option<Bounds>>

For a skinned mesh, each joint's box around the (bind-pose) vertices it moves (None for joints that move none). See skinned_bounds.

layout_text

pub fn layout_text(glyphs: &dyn Glyphs, font_key: &str, spec: &TextSpec, cache: &mut HashMap<String, Mesh>) -> Mesh

spec's text as one mesh, in scene units (size per em), aligned per align with its first baseline at y = 0 and lines going down. Glyph meshes are cached in cache by font and settings.

lerp

pub fn lerp(a: f32, b: f32, t: f32) -> f32

The value t of the way from a to b.

point_in_polygon

pub fn point_in_polygon(p: (f32, f32), poly: &[(f32, f32)]) -> bool

Even-odd point-in-polygon test.

point_shadow_faces

pub fn point_shadow_faces(position: Vec3, near: f32, far: f32) -> [Mat4; 6]

A point light's six cube-face view-projections, in GL's cube-map face order and orientation (+X, −X, +Y, −Y, +Z, −Z).

preset_fragment

pub fn preset_fragment(name: &str) -> Option<&'static str>

A preset's body (void main() and its helpers), as a mesh material's fragment shader (shaderMaterial(preset: "aurora")).

preset_source

pub fn preset_source(name: &str) -> Option<String>

Complete fragment shader for a preset.

shadow_far

pub fn shadow_far(position: Vec3, boxes: &[Bounds]) -> f32

How far a light at position must see to reach every box.

shadow_near

pub fn shadow_near(far: f32) -> f32

The near plane for a perspective shadow map reaching far.

skinned_bounds

pub fn skinned_bounds(joint_bounds: &[Option<Bounds>], joints: &[f32], model: &Mat4) -> Option<Bounds>

Where a skinned mesh is, posed: each joint's joint_bounds box moved by its joint matrix (16 floats each, as [crate::Model::joint_matrices] gives them), then by model. Every posed vertex is a blend of its joints' moves, so it lies inside. None without joint boxes.

slerp

pub fn slerp(a: [f32; 4], b: [f32; 4], t: f32) -> [f32; 4]

Spherical linear interpolation between unit quaternions.

spot_shadow_matrix

pub fn spot_shadow_matrix(spot: &SpotLight, far: f32) -> Mat4

A spot light's shadow view-projection: a perspective frustum just wider than its cone, reaching far.

uniform_identifiers

pub fn uniform_identifiers(src: &str) -> Vec<String>

Every u_* identifier in a shader, sorted and de-duplicated.

world_bounds

pub fn world_bounds(local: Bounds, m: &Mat4) -> Bounds

The world-space box around local (a box in an object's own space) placed by m.

Constants

ATTR_COLOR

pub const ATTR_COLOR: u32 = 3;

Per-vertex rgba (white when a mesh has none).

ATTR_JOINTS

pub const ATTR_JOINTS: u32 = 4;

Skinning: four joint indices and four weights per vertex.

ATTR_NORMAL

pub const ATTR_NORMAL: u32 = 1;

Where vertex normals go.

ATTR_POSITION

pub const ATTR_POSITION: u32 = 0;

Where vertex positions go in the mesh shaders.

ATTR_SEED

pub const ATTR_SEED: u32 = 1;

Per-particle (phase, size, color mix) attribute (shares location 1).

ATTR_UV

pub const ATTR_UV: u32 = 2;

Where texture coordinates go.

ATTR_WEIGHTS

pub const ATTR_WEIGHTS: u32 = 5;

Skinning weights: four per vertex, for the joints in ATTR_JOINTS.

CUSTOM_DEFAULT_VERTEX

pub const CUSTOM_DEFAULT_VERTEX: &str = …;

The vertex shader a custom material gets when it has none.

FRAGMENT_HEADER

pub const FRAGMENT_HEADER: &str = …;

Declarations and helpers shared by every preset and custom fragment.

FRAGMENT_SHADER

pub const FRAGMENT_SHADER: &str = MESH_FRAGMENT;

Back-compat alias for the lit mesh program's fragment shader.

FULLSCREEN_VERTEX

pub const FULLSCREEN_VERTEX: &str = …;

Full-screen triangle with no vertex buffers (drawArrays(TRIANGLES, 0, 3)).

MAX_POINT_LIGHTS

pub const MAX_POINT_LIGHTS: usize = 4;

How many point lights the renderer supports.

MAX_SPOT_LIGHTS

pub const MAX_SPOT_LIGHTS: usize = 4;

How many spot lights the renderer supports.

MESH_FRAGMENT

pub const MESH_FRAGMENT: &str = …;

Lit mesh fragment shader: hemisphere ambient + Blinn-Phong key, point and spot lights (with shadows) + Fresnel rim + emissive, optional vertical color gradient and fog, ACES tone mapping in linear space.

MESH_VERTEX

pub const MESH_VERTEX: &str = …;

Lit mesh vertex shader. With u_wobble > 0 vertices are displaced along their normals by animated 3D simplex noise (Ashima Arts, MIT) and normals are recomputed by finite differences.

PARTICLE_FRAGMENT

pub const PARTICLE_FRAGMENT: &str = …;

The fragment shader for 3D particles.

PARTICLE_VERTEX

pub const PARTICLE_VERTEX: &str = …;

The vertex shader for 3D particles.

PBR_FRAGMENT

pub const PBR_FRAGMENT: &str = …;

Physically based fragment shader (metallic-roughness): GGX distribution, height-correlated Smith visibility and Schlick Fresnel for the key, point and spot lights (with shadows); the hemisphere ambient as diffuse light and, along the reflection, as a blurred environment (Karis's analytic split-sum approximation). Normal maps use tangents from screen-space derivatives, so meshes need no tangents.

PRESETS

pub const PRESETS: &[&str] = _;

Preset names, usable as shader(preset: …) and scene3d(backdrop: …).

SHADOW_FRAGMENT

pub const SHADOW_FRAGMENT: &str = …;

Shadow-map depth pass: MESH_VERTEX (so skinning and wobble move shadows too) with a fragment shader that only writes depth.

VERTEX_SHADER

pub const VERTEX_SHADER: &str = MESH_VERTEX;

Back-compat aliases for the lit mesh program.

Type aliases

Bounds

pub type Bounds = (Vec3, Vec3);

An axis-aligned box, (min, max).