Mesh
Description
Section titled “Description”High-level geometry container. A Mesh owns a list of vertices and optional instances.
Internally it deduplicates vertices and always draws indexed and instanced
(instance_count defaults to 1 when none are provided). It draws a triangle list: without
set_indices, each three vertices make a triangle, and Renderer.render returns
MeshError::VertexCount for a count that isn’t a multiple of 3.
The shader decides the vertex layout. When the mesh attaches through
Shader::add_mesh or Pass::add_mesh, each @location input of the
shader’s vertex function looks for its data by name (instance first, then
vertex), then takes the position at @location(0), then looks by the
attribute’s own location (instance first, then vertex).
A plain number takes the type the shader declares for its input. The attach
checks every vertex and instance, and it fails with an error that names the
attribute when an input finds no data or a value doesn’t fit its type. A vertex
or instance added after the attach holds every attribute the shader reads, or
Renderer.render returns MeshError::InvalidVertex or
MeshError::InvalidInstance naming the attribute and the item. An attribute
no attached shader reads can be left out, and it uploads as zeros: a shader
that lets go of the mesh, through Shader.remove_mesh, Shader.clear_meshes
or by dropping, takes what it reads along. Renderer.render(mesh) draws with
a shader of its own that checks the mesh again whenever it changes, so it adds
nothing to that list. The types a shader gave the attributes stay for as long as the
mesh lives.
A mesh draws with every Renderer that renders it. Each one uploads buffers
of its own from the mesh’s vertices, indices and instances.
Example
Section titled “Example”use fragmentcolor::{Mesh, Vertex};
let mut mesh = Mesh::new();mesh.add_vertex([0.0, 0.5, 0.0]);mesh.add_vertex([-0.5, -0.5, 0.0]);mesh.add_vertex([0.5, -0.5, 0.0]);import { Mesh, Vertex } from "fragmentcolor";
const mesh = new Mesh();mesh.addVertex([0.0, 0.5, 0.0]);mesh.addVertex([-0.5, -0.5, 0.0]);mesh.addVertex([0.5, -0.5, 0.0]);from fragmentcolor import Mesh, Vertex
mesh = Mesh()mesh.add_vertex([0.0, 0.5, 0.0])mesh.add_vertex([-0.5, -0.5, 0.0])mesh.add_vertex([0.5, -0.5, 0.0])import FragmentColor
let mesh = Mesh()try mesh.addVertex([0.0, 0.5, 0.0])try mesh.addVertex([-0.5, -0.5, 0.0])try mesh.addVertex([0.5, -0.5, 0.0])import org.fragmentcolor.*
val mesh = Mesh()mesh.addVertex(listOf(0.0f, 0.5f, 0.0f))mesh.addVertex(listOf(-0.5f, -0.5f, 0.0f))mesh.addVertex(listOf(0.5f, -0.5f, 0.0f))Methods
Section titled “Methods”Mesh::new
Section titled “Mesh::new”Create an empty mesh. Add vertices with add_vertex (or add_vertices)
and instances with add_instance, then attach the mesh to a shader to
draw it.
Example
Section titled “Example”use fragmentcolor::Mesh;let m = Mesh::new();import { Mesh } from "fragmentcolor";const m = new Mesh();from fragmentcolor import Meshm = Mesh()import FragmentColorlet m = Mesh()import org.fragmentcolor.*val m = Mesh()Mesh::from_vertices
Section titled “Mesh::from_vertices”Create a mesh from an iterator of Vertex values.
Example
Section titled “Example”use fragmentcolor::Mesh;
let mesh = Mesh::from_vertices([ [0.0, 0.0], [1.0, 0.0], [0.0, 1.0],]);import { Mesh } from "fragmentcolor";
const mesh = Mesh.fromVertices([ [0.0, 0.0], [1.0, 0.0], [0.0, 1.0], ]);from fragmentcolor import Mesh
mesh = Mesh.from_vertices([ [0.0, 0.0], [1.0, 0.0], [0.0, 1.0],])import FragmentColor
let mesh = try Mesh.fromVertices([ [0.0, 0.0], [1.0, 0.0], [0.0, 1.0],])import org.fragmentcolor.*
val mesh = Mesh.fromVertices(listOf(listOf(0.0f, 0.0f), listOf(1.0f, 0.0f), listOf(0.0f, 1.0f)))Mesh::add_vertex
Section titled “Mesh::add_vertex”Append a single vertex to the mesh. Pass an array literal for the
position ([x, y] or [x, y, z]); for vertices that carry uv, color,
or other per-vertex attributes, build a Vertex
first and pass it in.
A vertex added after the mesh attaches to a shader holds every attribute that
shader reads. Renderer.render returns MeshError::InvalidVertex for one
that lacks an attribute, naming the attribute and the vertex.
Example
Section titled “Example”use fragmentcolor::Mesh;let mut m = Mesh::new();m.add_vertex([0.0, 0.5]);m.add_vertex([-0.5, -0.5]);m.add_vertex([0.5, -0.5]);import { Mesh } from "fragmentcolor";const m = new Mesh();m.addVertex([0.0, 0.5]);m.addVertex([-0.5, -0.5]);m.addVertex([0.5, -0.5]);from fragmentcolor import Meshm = Mesh()m.add_vertex([0.0, 0.5])m.add_vertex([-0.5, -0.5])m.add_vertex([0.5, -0.5])import FragmentColorlet m = Mesh()try m.addVertex([0.0, 0.5])try m.addVertex([-0.5, -0.5])try m.addVertex([0.5, -0.5])import org.fragmentcolor.*val m = Mesh()m.addVertex(listOf(0.0f, 0.5f))m.addVertex(listOf(-0.5f, -0.5f))m.addVertex(listOf(0.5f, -0.5f))Mesh::add_vertices
Section titled “Mesh::add_vertices”Add many vertices to the mesh.
As with add_vertex, a vertex added after the mesh attaches to a shader
holds every attribute that shader reads, or Renderer.render returns
MeshError::InvalidVertex naming the attribute and the vertex.
The vertices land as one change, so a render on another thread sees all of them or none, and a batch of whole triangles never draws part of one.
Example
Section titled “Example”use fragmentcolor::Mesh;let mut m = Mesh::new();m.add_vertices([[0.0, 0.0],[1.0, 0.0],[0.0, 1.0],]);import { Mesh } from "fragmentcolor";const m = new Mesh();m.addVertices([ [0.0, 0.0], [1.0, 0.0], [0.0, 1.0], ]);from fragmentcolor import Meshm = Mesh()m.add_vertices([[0.0, 0.0],[1.0, 0.0],[0.0, 1.0],])import FragmentColorlet m = Mesh()try m.addVertices([[0.0, 0.0],[1.0, 0.0],[0.0, 1.0],])import org.fragmentcolor.*val m = Mesh()m.addVertices(listOf(listOf(0.0f, 0.0f), listOf(1.0f, 0.0f), listOf(0.0f, 1.0f)))Mesh::add_instance
Section titled “Mesh::add_instance”Add a single per-instance attribute set to the mesh.
An Instance is a bag of named attributes (transform, color, id, …) sent to
the shader’s per-instance inputs. It carries no position; the mesh’s vertices
are reused for every instance.
Adding an instance also clears any previously set instance count override
(see Mesh::set_instance_count).
An instance added after the mesh attaches to a shader holds every instance
attribute that shader reads. Renderer.render returns
MeshError::InvalidInstance for one that lacks an attribute, naming the
attribute and the instance.
Example
Section titled “Example”use fragmentcolor::{Mesh, Instance};
let m = Mesh::new();m.add_instance(Instance::new().set("offset", [0.25, 0.10]).set("tint", [1.0, 0.0, 0.0, 1.0]));import { Mesh, Instance } from "fragmentcolor";
const m = new Mesh();m.addInstance(new Instance().set("offset", [0.25, 0.10]).set("tint", [1.0, 0.0, 0.0, 1.0]));from fragmentcolor import Mesh, Instance
m = Mesh()m.add_instance(Instance().set("offset", [0.25, 0.10]).set("tint", [1.0, 0.0, 0.0, 1.0]))import FragmentColor
let m = Mesh()try m.addInstance(Instance().set("offset", [0.25, 0.10]).set("tint", [1.0, 0.0, 0.0, 1.0]))import org.fragmentcolor.*
val m = Mesh()m.addInstance(Instance().set("offset", listOf(0.25f, 0.10f)).set("tint", listOf(1.0f, 0.0f, 0.0f, 1.0f)))Mesh::add_instances
Section titled “Mesh::add_instances”Add many per-instance attribute sets to the mesh in one call.
Each Instance is a bag of named attributes (transform, color, id, …) sent to
the shader’s per-instance inputs. Instances carry no position; the mesh’s
vertices are reused for every instance.
Adding instances also clears any previously set instance count override
(see Mesh::set_instance_count).
The instances land as one change, so a render on another thread sees all of them or none.
Every instance counts, even one without attributes: three empty instances
draw the mesh three times, and the shader tells them apart with
@builtin(instance_index). As with add_instance, an instance added after
the mesh attaches to a shader holds every instance attribute that shader reads.
Example
Section titled “Example”use fragmentcolor::{Mesh, Instance};
let m = Mesh::new();m.add_instances([ Instance::new().set("tint", [1.0, 0.0, 0.0, 1.0]), Instance::new().set("tint", [0.0, 1.0, 0.0, 1.0]), Instance::new().set("tint", [0.0, 0.0, 1.0, 1.0]),]);import { Mesh, Instance } from "fragmentcolor";
const m = new Mesh();m.addInstances([ new Instance().set("tint", [1.0, 0.0, 0.0, 1.0]), new Instance().set("tint", [0.0, 1.0, 0.0, 1.0]), new Instance().set("tint", [0.0, 0.0, 1.0, 1.0]), ]);from fragmentcolor import Mesh, Instance
m = Mesh()m.add_instances([ Instance().set("tint", [1.0, 0.0, 0.0, 1.0]), Instance().set("tint", [0.0, 1.0, 0.0, 1.0]), Instance().set("tint", [0.0, 0.0, 1.0, 1.0]),])import FragmentColor
let m = Mesh()m.addInstances([ try Instance().set("tint", [1.0, 0.0, 0.0, 1.0]), try Instance().set("tint", [0.0, 1.0, 0.0, 1.0]), try Instance().set("tint", [0.0, 0.0, 1.0, 1.0]),])import org.fragmentcolor.*
val m = Mesh()m.addInstances(listOf( Instance().set("tint", listOf(1.0f, 0.0f, 0.0f, 1.0f)), Instance().set("tint", listOf(0.0f, 1.0f, 0.0f, 1.0f)), Instance().set("tint", listOf(0.0f, 0.0f, 1.0f, 1.0f)),))Mesh::clear_instances
Section titled “Mesh::clear_instances”Remove the mesh’s instances and its instance count override.
Specifically, this:
- Drops any per-instance attributes added via
add_instance/add_instances. - Clears any count override previously set with
set_instance_count.
After calling, a shader that reads no instance attributes draws the mesh once,
unless you add instances again or call set_instance_count. A shader that
reads instance attributes needs instances that hold them, so while it holds
the mesh, Renderer.render returns MeshError::InvalidInstance naming the
attribute until you add them again, or until the shader lets the mesh go
through Shader.remove_mesh, Shader.clear_meshes or by dropping.
set_instance_count doesn’t stand in for them.
Example
Section titled “Example”use fragmentcolor::{Mesh, Instance};
let m = Mesh::new();m.add_instance(Instance::new().set("tint", [1.0, 0.0, 0.0, 1.0]));m.clear_instances(); // no instances leftimport { Mesh, Instance } from "fragmentcolor";
const m = new Mesh();m.addInstance(new Instance().set("tint", [1.0, 0.0, 0.0, 1.0]));m.clearInstances(); // no instances leftfrom fragmentcolor import Mesh, Instance
m = Mesh()m.add_instance(Instance().set("tint", [1.0, 0.0, 0.0, 1.0]))m.clear_instances() # no instances leftimport FragmentColor
let m = Mesh()try m.addInstance(Instance().set("tint", [1.0, 0.0, 0.0, 1.0]))m.clearInstances() // no instances leftimport org.fragmentcolor.*
val m = Mesh()m.addInstance(Instance().set("tint", listOf(1.0f, 0.0f, 0.0f, 1.0f)))m.clearInstances() // no instances leftMesh::set_instance_count
Section titled “Mesh::set_instance_count”Override how many instances to draw without providing per-instance attributes.
Use this when driving instance data from a storage buffer and indexing via
@builtin(instance_index) in the vertex shader. Common for compute-driven
simulations and large particle systems.
The override is automatically cleared if you later call add_instance /
add_instances (those carry their own count) or clear_instances.
When the shader reads instance attributes, Renderer.render returns
MeshError::InvalidInstance for a count past the instances the mesh holds.
Example
Section titled “Example”use fragmentcolor::Mesh;let mut m = Mesh::new();m.add_vertices([ [-0.01, -0.01], [ 0.01, -0.01], [ 0.00, 0.01],]);// Draw one million instances, fetching per-particle data from a storage buffer.m.set_instance_count(1_000_000);import { Mesh } from "fragmentcolor";const m = new Mesh();m.addVertices([ [-0.01, -0.01], [ 0.01, -0.01], [ 0.00, 0.01], ]);// Draw one million instances, fetching per-particle data from a storage buffer.m.setInstanceCount(1_000_000);from fragmentcolor import Meshm = Mesh()m.add_vertices([ [-0.01, -0.01], [ 0.01, -0.01], [ 0.00, 0.01],])# Draw one million instances, fetching per-particle data from a storage buffer.m.set_instance_count(1_000_000)import FragmentColorlet m = Mesh()try m.addVertices([ [-0.01, -0.01], [ 0.01, -0.01], [ 0.00, 0.01],])// Draw one million instances, fetching per-particle data from a storage buffer.m.setInstanceCount(1_000_000)import org.fragmentcolor.*val m = Mesh()m.addVertices(listOf(listOf(-0.01f, -0.01f), listOf(0.01f, -0.01f), listOf(0.00f, 0.01f)))// Draw one million instances, fetching per-particle data from a storage buffer.m.setInstanceCount(1_000_000u)Mesh::set_indices
Section titled “Mesh::set_indices”Provide the index buffer directly, bypassing the mesh’s automatic vertex dedup pass. Use this when an asset already carries its own indexing: glTF loaders, OBJ importers, or hand-authored meshes whose corners share positions but need to keep distinct UVs, normals, or tangents (the typical case for sharp creases and texture seams).
By default the mesh dedupes vertices by full attribute equality before
producing an index array. That’s fine for hand-built meshes, wrong for assets
where two corners with identical positions must stay separate because
their other attributes differ. After set_indices, every vertex you added
with add_vertex is packed in insertion order and the indices you supply
are used verbatim. Call clear_indices to return to the auto-derived
path.
The list draws whole triangles of vertices the mesh holds. Renderer.render
returns MeshError::IndexRange for an index past the last vertex, and
MeshError::IndexCount for a count that isn’t a multiple of 3. The check runs
on every render until the indices fit the vertices. An empty list draws nothing.
Example
Section titled “Example”use fragmentcolor::{Mesh, Vertex};
// A quad split into two triangles via explicit indexing. The four corners// happen to carry distinct UVs (only positions repeat), so we keep them// all and reference each by index.let mesh = Mesh::new();mesh.add_vertices([ Vertex::new([-0.5, -0.5]).set("uv", [0.0, 0.0]), Vertex::new([ 0.5, -0.5]).set("uv", [1.0, 0.0]), Vertex::new([ 0.5, 0.5]).set("uv", [1.0, 1.0]), Vertex::new([-0.5, 0.5]).set("uv", [0.0, 1.0]),]);mesh.set_indices([0, 1, 2, 0, 2, 3]);import { Mesh, Vertex } from "fragmentcolor";
// A quad split into two triangles via explicit indexing. The four corners// happen to carry distinct UVs (only positions repeat), so we keep them// all and reference each by index.const mesh = new Mesh();mesh.addVertices([ new Vertex([-0.5, -0.5]).set("uv", [0.0, 0.0]), new Vertex([ 0.5, -0.5]).set("uv", [1.0, 0.0]), new Vertex([ 0.5, 0.5]).set("uv", [1.0, 1.0]), new Vertex([-0.5, 0.5]).set("uv", [0.0, 1.0]), ]);mesh.setIndices([0, 1, 2, 0, 2, 3]);from fragmentcolor import Mesh, Vertex
# A quad split into two triangles via explicit indexing. The four corners# happen to carry distinct UVs (only positions repeat), so we keep them# all and reference each by index.mesh = Mesh()mesh.add_vertices([ Vertex([-0.5, -0.5]).set("uv", [0.0, 0.0]), Vertex([ 0.5, -0.5]).set("uv", [1.0, 0.0]), Vertex([ 0.5, 0.5]).set("uv", [1.0, 1.0]), Vertex([-0.5, 0.5]).set("uv", [0.0, 1.0]),])mesh.set_indices([0, 1, 2, 0, 2, 3])import FragmentColor
// A quad split into two triangles via explicit indexing. The four corners// happen to carry distinct UVs (only positions repeat), so we keep them// all and reference each by index.let mesh = Mesh()mesh.addVertices([ try Vertex([-0.5, -0.5]).set("uv", [0.0, 0.0]), try Vertex([ 0.5, -0.5]).set("uv", [1.0, 0.0]), try Vertex([ 0.5, 0.5]).set("uv", [1.0, 1.0]), try Vertex([-0.5, 0.5]).set("uv", [0.0, 1.0]),])mesh.setIndices([0, 1, 2, 0, 2, 3])import org.fragmentcolor.*
// A quad split into two triangles via explicit indexing. The four corners// happen to carry distinct UVs (only positions repeat), so we keep them// all and reference each by index.val mesh = Mesh()mesh.addVertices(listOf(Vertex(listOf(-0.5f, -0.5f)).set("uv", listOf(0.0f, 0.0f)), Vertex(listOf(0.5f, -0.5f)).set("uv", listOf(1.0f, 0.0f)), Vertex(listOf(0.5f, 0.5f)).set("uv", listOf(1.0f, 1.0f)), Vertex(listOf(-0.5f, 0.5f)).set("uv", listOf(0.0f, 1.0f))))mesh.setIndices(listOf(0u, 1u, 2u, 0u, 2u, 3u))Mesh::clear_indices
Section titled “Mesh::clear_indices”Drop the user-supplied index buffer and return to auto-derived indexing.
After set_indices the mesh draws vertices in insertion order using the
indices you provided. clear_indices reverses that: the next render
re-runs the vertex dedup pass and rebuilds the index buffer from the
deduplicated unique set, the same way a freshly built mesh behaves.
Example
Section titled “Example”use fragmentcolor::{Mesh, Vertex};
let mesh = Mesh::new();mesh.add_vertices([ Vertex::new([-0.5, -0.5]), Vertex::new([ 0.5, -0.5]), Vertex::new([ 0.0, 0.5]),]);mesh.set_indices([0, 1, 2]);mesh.clear_indices(); // back to auto-derived dedupimport { Mesh, Vertex } from "fragmentcolor";
const mesh = new Mesh();mesh.addVertices([ new Vertex([-0.5, -0.5]), new Vertex([ 0.5, -0.5]), new Vertex([ 0.0, 0.5]), ]);mesh.setIndices([0, 1, 2]);mesh.clearIndices(); // back to auto-derived dedupfrom fragmentcolor import Mesh, Vertex
mesh = Mesh()mesh.add_vertices([ Vertex([-0.5, -0.5]), Vertex([ 0.5, -0.5]), Vertex([ 0.0, 0.5]),])mesh.set_indices([0, 1, 2])mesh.clear_indices() # back to auto-derived dedupimport FragmentColor
let mesh = Mesh()mesh.addVertices([ try Vertex([-0.5, -0.5]), try Vertex([ 0.5, -0.5]), try Vertex([ 0.0, 0.5]),])mesh.setIndices([0, 1, 2])mesh.clearIndices() // back to auto-derived dedupimport org.fragmentcolor.*
val mesh = Mesh()mesh.addVertices(listOf(Vertex(listOf(-0.5f, -0.5f)), Vertex(listOf(0.5f, -0.5f)), Vertex(listOf(0.0f, 0.5f))))mesh.setIndices(listOf(0u, 1u, 2u))mesh.clearIndices() // back to auto-derived dedup