Vertex
Description
Section titled “Description”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.
Canonical attribute names
Section titled “Canonical attribute names”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.
Example
Section titled “Example”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]);import { Vertex } from "fragmentcolor";const v = new Vertex([0.0, 0.0, 0.0]).set(Vertex.UV0, [0.5, 0.5]).set(Vertex.NORMAL, [0.0, 1.0, 0.0]);from fragmentcolor import Vertexv = Vertex([0.0, 0.0, 0.0]).set(Vertex.UV0, [0.5, 0.5]).set(Vertex.NORMAL, [0.0, 1.0, 0.0])import FragmentColorlet v = try Vertex([0.0, 0.0, 0.0]).set(Vertex.UV0, [0.5, 0.5]).set(Vertex.NORMAL, [0.0, 1.0, 0.0])import org.fragmentcolor.*val v = Vertex(listOf(0.0f, 0.0f, 0.0f)).set(Vertex.UV0, listOf(0.5f, 0.5f)).set(Vertex.NORMAL, listOf(0.0f, 1.0f, 0.0f))Methods
Section titled “Methods”Vertex::new
Section titled “Vertex::new”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.
Example
Section titled “Example”use fragmentcolor::Vertex;let v = Vertex::new([0.0, 0.0]);import { Vertex } from "fragmentcolor";const v = new Vertex([0.0, 0.0]);from fragmentcolor import Vertexv = Vertex([0.0, 0.0])import FragmentColorlet v = try Vertex([0.0, 0.0])import org.fragmentcolor.*val v = Vertex(listOf(0.0f, 0.0f))Vertex::pbr
Section titled “Vertex::pbr”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 |
Example
Section titled “Example”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]));import { Mesh, Vertex } from "fragmentcolor";
// Build a triangle and set only what the mesh carries. NORMAL, COLOR0,// UV1 and TANGENT keep their identity defaults from Vertex.pbr.const mesh = new Mesh();mesh.addVertex(Vertex.pbr([ 0.0, 0.5, 0.0]).set(Vertex.UV0, [0.5, 1.0]));mesh.addVertex(Vertex.pbr([-0.5, -0.5, 0.0]).set(Vertex.UV0, [0.0, 0.0]));mesh.addVertex(Vertex.pbr([ 0.5, -0.5, 0.0]).set(Vertex.UV0, [1.0, 0.0]));from fragmentcolor import Mesh, Vertex
# Build a triangle and set only what the mesh carries. NORMAL, COLOR0,# UV1 and TANGENT keep their identity defaults from Vertex.pbr.mesh = Mesh()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]))import FragmentColor
// 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()try mesh.addVertex(Vertex.pbr([ 0.0, 0.5, 0.0]).set(Vertex.UV0, [0.5, 1.0]))try mesh.addVertex(Vertex.pbr([-0.5, -0.5, 0.0]).set(Vertex.UV0, [0.0, 0.0]))try mesh.addVertex(Vertex.pbr([ 0.5, -0.5, 0.0]).set(Vertex.UV0, [1.0, 0.0]))import org.fragmentcolor.*
// Build a triangle and set only what the mesh carries. NORMAL, COLOR0,// UV1 and TANGENT keep their identity defaults from Vertex.pbr.val mesh = Mesh()mesh.addVertex(Vertex.pbr(listOf(0.0f, 0.5f, 0.0f)).set(Vertex.UV0, listOf(0.5f, 1.0f)))mesh.addVertex(Vertex.pbr(listOf(-0.5f, -0.5f, 0.0f)).set(Vertex.UV0, listOf(0.0f, 0.0f)))mesh.addVertex(Vertex.pbr(listOf(0.5f, -0.5f, 0.0f)).set(Vertex.UV0, listOf(1.0f, 0.0f)))Vertex::set
Section titled “Vertex::set”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
setfor 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.
positionnames the positionVertex::newsets, so no property takes it. A mesh with a vertex that setspositionfails to attach withShaderError::InvalidKey, andRenderer.renderreturnsMeshError::InvalidVertexfor one added later. Both name the vertex.
Example
Section titled “Example”use fragmentcolor::{Mesh, Shader, Vertex};
let shader = Shader::new(r#"@vertexfn 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)?;import { Mesh, Shader, Vertex } from "fragmentcolor";
const shader = new Shader(`
@vertexfn 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); }
`);
const vertex = new Vertex([0.0, 0.5]).set("id", 7).set("weight", 0.5);
const mesh = new Mesh();mesh.addVertices([ vertex, new Vertex([-0.5, -0.5]).set("id", 8).set("weight", 1.0), new Vertex([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.addMesh(mesh);from fragmentcolor import Mesh, Shader, Vertex
shader = Shader("""@vertexfn 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); }
""")
vertex = Vertex([0.0, 0.5]).set("id", 7).set("weight", 0.5)
mesh = Mesh()mesh.add_vertices([ vertex, Vertex([-0.5, -0.5]).set("id", 8).set("weight", 1.0), Vertex([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)import FragmentColor
let shader = try Shader("""@vertexfn 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 = try Vertex([0.0, 0.5]).set("id", 7).set("weight", 0.5)
let mesh = Mesh()mesh.addVertices([ vertex, try Vertex([-0.5, -0.5]).set("id", 8).set("weight", 1.0), try Vertex([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.try shader.addMesh(mesh)import org.fragmentcolor.*
val shader = Shader("""@vertexfn 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); }
""")
val vertex = Vertex(listOf(0.0f, 0.5f)).set("id", 7).set("weight", 0.5f)
val mesh = Mesh()mesh.addVertices(listOf(vertex, Vertex(listOf(-0.5f, -0.5f)).set("id", 8).set("weight", 1.0f), Vertex(listOf(0.5f, -0.5f)).set("id", 9).set("weight", 1.0f)))
// The first id uploads as the u32 7 and its weight as the f32 0.5.shader.addMesh(mesh)Vertex::create_instance
Section titled “Vertex::create_instance”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.
Example
Section titled “Example”use fragmentcolor::Vertex;let v = Vertex::new([0.0, 0.0]);let inst = v.create_instance();import { Vertex } from "fragmentcolor";const v = new Vertex([0.0, 0.0]);const inst = v.createInstance();from fragmentcolor import Vertexv = Vertex([0.0, 0.0])inst = v.create_instance()import FragmentColorlet v = try Vertex([0.0, 0.0])let inst = v.createInstance()import org.fragmentcolor.*val v = Vertex(listOf(0.0f, 0.0f))val inst = v.createInstance()