Skip to content

Vertex

A single vertex: a required 2D or 3D position plus any per-vertex attributes your shader declares. Commonly that’s uv and color, but any key the shader’s vertex stage reads is fair game. Build vertices and feed them into a Mesh to draw.

For the channels you’ll reach for most often, use the typed constants instead of bare strings. The loader, the shader, and any later glTF import all agree on names without bikeshedding:

Position is set via Vertex::new(...) and is not part of the constants table; the rest live as &'static str literals on Vertex:

Constant Value Notes
Vertex::NORMAL "normal" Per-vertex normal.
Vertex::TANGENT "tangent" Per-vertex tangent (for normal-mapped shading).
Vertex::UV0 / UV1 "uv0" / "uv1" Texture coordinates. Use the numbered form when a mesh carries multiple UV sets (typical for glTF).
Vertex::COLOR0 / COLOR1 "color0" / "color1" Vertex colors.

The constants are plain &'static str literals: vertex.set(Vertex::UV0, [...]) and vertex.set("uv0", [...]) do the same thing. Mix the two styles freely; the constants exist to prevent typos and to give the glTF loader a stable vocabulary.

use fragmentcolor::Vertex;
let v = Vertex::new([0.0, 0.0, 0.0])
.set(Vertex::UV0, [0.5, 0.5])
.set(Vertex::NORMAL, [0.0, 1.0, 0.0]);

Construct a Vertex from a 2D or 3D position. Set additional attributes (uv, color, custom keys) with set to match the per-vertex inputs your shader declares.

A 1-component position becomes 2D with y at 0. A 4-component position uploads as 3D: the mesh drops the 4th component.

In JavaScript, Python, Swift and Kotlin, a position of another length, or a number that is NaN, infinite or past the 32-bit float range, is an error that names the call.

use fragmentcolor::Vertex;
let v = Vertex::new([0.0, 0.0]);

Construct a Vertex that matches the PBR shader’s vertex input layout. Seeds every attribute Material::pbr reads (NORMAL, UV0, COLOR0, UV1, TANGENT) with a neutral identity default; chain .set(...) to override the slots a real mesh has data for.

The defaults are the values Scene::load falls back to for a glTF primitive that lacks those attributes, with two exceptions. The loader computes a missing normal from the faces, where Vertex::pbr points every normal along +Z. And the loader copies a primitive’s TEXCOORD_0 into UV1 when it has no TEXCOORD_1, where Vertex::pbr keeps UV1 at [0, 0] after you set UV0, until you set UV1 too. Useful for building PBR meshes by hand without spelling out every default attribute.

Defaults:

attribute default value what it means
NORMAL [0, 0, 1] forward-facing surface
UV0 [0, 0] corner of the texture
COLOR0 [1, 1, 1, 1] identity vertex tint
UV1 [0, 0] read by slots texture_uv puts on UV set 1
TANGENT [1, 0, 0, 1] T = +X, bitangent sign +1
use fragmentcolor::{Mesh, Vertex};
// Build a triangle and set only what the mesh carries. NORMAL, COLOR0,
// UV1 and TANGENT keep their identity defaults from Vertex::pbr.
let mesh = Mesh::new();
mesh.add_vertex(Vertex::pbr([ 0.0, 0.5, 0.0]).set(Vertex::UV0, [0.5, 1.0]));
mesh.add_vertex(Vertex::pbr([-0.5, -0.5, 0.0]).set(Vertex::UV0, [0.0, 0.0]));
mesh.add_vertex(Vertex::pbr([ 0.5, -0.5, 0.0]).set(Vertex::UV0, [1.0, 0.0]));

Attach a property to the vertex. set returns a new Vertex with the property added, so chain the calls and keep the result.

The value is a number or a list of 1 to 4 numbers, and the shader decides its type when the mesh attaches. A number fills a float input unless the shader declares an integer type at that location: with @location(1) id: u32, set("id", 7) uploads the u32 7. An integer input takes whole numbers that fit its type. A value that doesn’t fit, such as -1 for a u32 or 2.5 for an i32, fails the attach with an error that names the attribute, the declared type and the value. A property no shader reads uploads as floats.

In Rust, an f32 or a u32 value keeps its type and must match the input exactly. An unsuffixed literal, an f64 or an i32 takes the shader’s type like a number from any other language.

Locations and mapping:

  • The first set for a key gives the property the next free @location, starting after the position. Setting the same key again keeps its location.
  • When the mesh attaches, each shader input looks for its property by name (instance first, then vertex), then takes the position at @location(0), then looks by the property’s own location (instance first, then vertex).
  • The first shader that reads a property fixes its type for that mesh. A shader that declares another type for it fails to attach.
  • position names the position Vertex::new sets, so no property takes it. A mesh with a vertex that sets position fails to attach with ShaderError::InvalidKey, and Renderer.render returns MeshError::InvalidVertex for one added later. Both name the vertex.
use fragmentcolor::{Mesh, Shader, Vertex};
let shader = Shader::new(r#"
@vertex
fn vs_main(@location(0) pos: vec2<f32>, @location(1) id: u32, @location(2) weight: f32) -> @builtin(position) vec4<f32> {
return vec4<f32>(pos * weight, f32(id) * 0.1, 1.0);
}
@fragment fn fs_main() -> @location(0) vec4<f32> { return vec4<f32>(1.0, 0.0, 0.0, 1.0); }
"#)?;
let vertex = Vertex::new([0.0, 0.5])
.set("id", 7)
.set("weight", 0.5);
let mesh = Mesh::new();
mesh.add_vertices([
vertex,
Vertex::new([-0.5, -0.5]).set("id", 8).set("weight", 1.0),
Vertex::new([0.5, -0.5]).set("id", 9).set("weight", 1.0),
]);
// The first id uploads as the u32 7 and its weight as the f32 0.5.
shader.add_mesh(&mesh)?;

Create an Instance that inherits this vertex’s attributes. Use this when you want a starting point for an instance whose per-instance values mostly match the source vertex, then call set on the result to change the fields that differ.

use fragmentcolor::Vertex;
let v = Vertex::new([0.0, 0.0]);
let inst = v.create_instance();