Skip to content

Model

A Model pairs a Mesh with a Material and adds a per-Model 4×4 transform. It’s the unit you actually add to a Pass when rendering 3D content; the Material handles shading, the Mesh handles geometry, the Model handles “where in the world”.

Each Model::new takes ownership of the Material it’s given. Cloning the Material before passing it in is cheap: Material::clone shares the handle and copies nothing. The per-Model transform is not a Material uniform. The renderer reads it at draw time and feeds it to the vertex stage as four vec4<f32> instance attributes, model_0 to model_3, the columns of a mat4x4<f32>. The PBR shader reads them at locations 3 to 6.

Models that share a Material and a Mesh draw together. On every render, the renderer packs the transforms of the visible Opaque and Mask Models with the same Shader and Mesh into one instance buffer and draws them in one instanced call. Blend Models draw one at a time, back to front, since their order matters.

For the fan-out case (one Material, many positioned items), the pattern is:

template = Material::pbr().base_color(...)
for each item:
model = Model::new(item.mesh, template.clone())
model.set_transform(item.matrix)
pass.add(&model)

The pipeline is cached once per shader, the bind groups are set once per pass, and the pass issues one draw per Mesh. Give every Model the same mesh.clone() to draw them all in one call: Mesh::clone shares the handle, so the Models share one Mesh. A transform changed after pass.add(&model) shows up on the next render, without adding the Model again.


Borrow the Material handle the Model owns. The Material wraps a Shader; reach through model.material().shader() if you need direct uniform manipulation (camera state, custom uniforms not exposed by Material’s setters).

Note: Material::clone is a shallow Arc-share, so the handle stored on this Model points at the same underlying shader state as whatever Material was passed to Model::new. Mutations made through model.material() are visible to every other Model that received the same source handle (and vice versa). To detach a Model’s appearance, construct a fresh Material::pbr() or Material::custom() rather than cloning.

use fragmentcolor::{Material, Mesh, Model, Vertex};
let mesh = Mesh::new();
mesh.add_vertices([
Vertex::pbr([0.0, 0.5, 0.0]).set(Vertex::UV0, [0.5, 1.0]),
Vertex::pbr([-0.5, -0.5, 0.0]).set(Vertex::UV0, [0.0, 0.0]),
Vertex::pbr([0.5, -0.5, 0.0]).set(Vertex::UV0, [1.0, 0.0]),
]);
let model = Model::new(mesh, Material::pbr());
model.material().shader().set("camera.position", [0.0, 0.0, 5.0])?;

Borrow the Mesh the Model owns. Useful for adding more vertices or instances to the geometry after construction without losing access through the Model handle.

use fragmentcolor::{Material, Mesh, Model, Vertex};
let mesh = Mesh::new();
mesh.add_vertices([
Vertex::pbr([0.0, 0.5, 0.0]).set(Vertex::UV0, [0.5, 1.0]),
Vertex::pbr([-0.5, -0.5, 0.0]).set(Vertex::UV0, [0.0, 0.0]),
Vertex::pbr([0.5, -0.5, 0.0]).set(Vertex::UV0, [1.0, 0.0]),
]);
let model = Model::new(mesh, Material::pbr());
// A second triangle joins the first through the Model.
model.mesh().add_vertices([
Vertex::pbr([0.5, -0.5, 0.0]).set(Vertex::UV0, [1.0, 0.0]),
Vertex::pbr([0.5, 0.5, 0.0]).set(Vertex::UV0, [1.0, 1.0]),
Vertex::pbr([0.0, 0.5, 0.0]).set(Vertex::UV0, [0.5, 1.0]),
]);

Construct a Model from a Mesh and a Material. Both arguments are taken by value; the Model owns them. The transform starts at the 4×4 identity matrix. The renderer reads it at draw time and passes it to the vertex stage as the model_0 to model_3 instance attributes, the four columns of a mat4x4<f32>.

If you want several Models that share a look, clone the Material before each Model::new. Material::clone shares the handle and copies nothing, so the Models share one bind-group setup, and Models whose meshes lay out their vertices alike share one pipeline. The transform belongs to the Model, not to the Material’s Shader or the Mesh, so Models can share both. Models with the same Material and Mesh draw in one instanced call.

Material::pbr reads six attributes from every vertex: a 3D position (vec3), normal (vec3), uv0 (vec2), color0 (vec4), uv1 (vec2) and tangent (vec4). Each input finds its attribute by name, then by the location Vertex::set gave the attribute, so a vertex that sets them under these names can set them in any order. An attribute under another name stands in for an input when it sits at the input’s location and fits its type. The shader has no default for any input, so Scene.add and Pass.add refuse a Model when an input finds no attribute by either rule. The error names the input, or the attribute at the input’s location when that attribute has the wrong type: a vertex that sets only uv0 fails on uv0, which sits where normal is read. Vertex::pbr sets all five after the position to neutral values, and .set(...) replaces the ones your mesh has data for. The model_0 to model_3 columns come from the Model, not the mesh. Custom shaders via Material::custom(...) can use any layout.

use fragmentcolor::{Material, Mesh, Model, Vertex};
let mesh = Mesh::new();
mesh.add_vertices([
Vertex::pbr([0.0, 0.5, 0.0]).set(Vertex::UV0, [0.5, 1.0]),
Vertex::pbr([-0.5, -0.5, 0.0]).set(Vertex::UV0, [0.0, 0.0]),
Vertex::pbr([0.5, -0.5, 0.0]).set(Vertex::UV0, [1.0, 0.0]),
]);
let model = Model::new(mesh, Material::pbr());

Rotate the Model around axis (in local space) by radians. Post-multiplies the current transform by an axis-angle rotation, so a rotated-then-translated Model spins in place around its own origin rather than orbiting the world origin. The axis is normalized internally, so an axis of any length but 0 turns the Model the same way.

An axis item or an angle that is NaN or an infinity returns ModelError::NotFinite, which names the call and the item, such as Model::rotate: radians must be finite, got NaN. The other languages also refuse a number past the 32-bit float range, such as 1e39, with a message like Model.rotate: axis: item 1 must be a finite number that fits a 32-bit float, got 1e39. An axis of length 0, such as [0, 0, 0], has no direction to turn around and returns ModelError::ZeroAxis in every language. A rotation that would carry a number of the transform past the 32-bit float range returns ModelError::Overflow. Either way the Model keeps its transform. JavaScript throws an Error named InvalidInputError, Python raises InvalidInputError, Swift throws FragmentColorError and Kotlin throws FragmentColorException.

For pure world-space rotations (e.g. tumbling around a fixed pivot), compose the matrix yourself and call set_transform.

use fragmentcolor::{Material, Mesh, Model, Renderer, Vertex};
let renderer = Renderer::new();
let mesh = Mesh::new();
mesh.add_vertices([
Vertex::pbr([0.0, 0.5, 0.0]).set(Vertex::UV0, [0.5, 1.0]),
Vertex::pbr([-0.5, -0.5, 0.0]).set(Vertex::UV0, [0.0, 0.0]),
Vertex::pbr([0.5, -0.5, 0.0]).set(Vertex::UV0, [1.0, 0.0]),
]);
let model = Model::new(mesh, Material::pbr());
model.rotate([0.0, 1.0, 0.0], 1.571)?; // 90° around Y

Scale the Model by per-axis factor (in local space). Post-multiplies the current transform by a scale matrix, so the Model grows or shrinks around its own origin without sliding through space.

Use uniform scales ([s, s, s]) when possible. Non-uniform scale breaks the cheap normal-transform path the default PBR shader uses, so a stretched Model will shade slightly off. For a correct non-uniform-scale normal, use Material::custom with a shader that ships the explicit cofactor matrix.

A factor that is NaN or an infinity returns ModelError::NotFinite, which names the call and the item, and the Model keeps its size. The other languages also refuse a factor past the 32-bit float range, such as 1e39. JavaScript throws an Error named InvalidInputError, Python raises InvalidInputError, Swift throws FragmentColorError and Kotlin throws FragmentColorException. A factor of 0 is a valid scale, which flattens the Model along that axis.

A scale that would carry a number of the transform past the 32-bit float range, such as [1e20, 1e20, 1e20] twice, returns ModelError::Overflow, Model::scale would carry the transform past the f32 range, and the Model keeps its size. JavaScript and Python raise it as InvalidInputError, too.

use fragmentcolor::{Material, Mesh, Model, Renderer, Vertex};
let renderer = Renderer::new();
let mesh = Mesh::new();
mesh.add_vertices([
Vertex::pbr([0.0, 0.5, 0.0]).set(Vertex::UV0, [0.5, 1.0]),
Vertex::pbr([-0.5, -0.5, 0.0]).set(Vertex::UV0, [0.0, 0.0]),
Vertex::pbr([0.5, -0.5, 0.0]).set(Vertex::UV0, [1.0, 0.0]),
]);
let model = Model::new(mesh, Material::pbr());
model.scale([2.0, 2.0, 2.0])?;

Replace the Model’s 4×4 transform wholesale, in column-major order. Useful when you already have a matrix from a math library or a glTF node and want to apply it directly without composing through translate / rotate / scale.

The renderer reads the new matrix on the next render, so a Model already added to a Pass or Scene moves without being added again. To read the current transform, see Model::transform.

A number that is NaN or an infinity returns ModelError::NotFinite, which names the call, the column and the item: Model::set_transform: column 3: item 0 must be finite, got inf. The Model keeps the transform it had. The other languages also refuse a number past the 32-bit float range, such as 1e39, with a message like Model.setTransform: column 3: item 0 must be a finite number that fits a 32-bit float, got 1e39, or item 12 for the 16 numbers of a flat matrix. JavaScript throws an Error named InvalidInputError, Python raises InvalidInputError, Swift throws FragmentColorError and Kotlin throws FragmentColorException.

use fragmentcolor::{Material, Mesh, Model, Renderer, Vertex};
let renderer = Renderer::new();
let mesh = Mesh::new();
mesh.add_vertices([
Vertex::pbr([0.0, 0.5, 0.0]).set(Vertex::UV0, [0.5, 1.0]),
Vertex::pbr([-0.5, -0.5, 0.0]).set(Vertex::UV0, [0.0, 0.0]),
Vertex::pbr([0.5, -0.5, 0.0]).set(Vertex::UV0, [1.0, 0.0]),
]);
let model = Model::new(mesh, Material::pbr());
model.set_transform([
[2.0, 0.0, 0.0, 0.0],
[0.0, 2.0, 0.0, 0.0],
[0.0, 0.0, 2.0, 0.0],
[3.0, 0.0, 0.0, 1.0],
])?;

Toggle the Model in or out of the next frame. Hidden Models are skipped by the renderer in both the opaque-batched and blend-sorted draw paths; no Pass rebuild needed. Hiding a Model hides what its Material draws. The skip holds per shader and Mesh: a shader that draws any of the pass’s Models skips the hidden Model’s Mesh, unless a visible Model of the pass draws the same Mesh through the same shader, as Models that share a Mesh and a Material do. A shader of your own that draws none of the pass’s Models, such as an outline, still draws the Mesh when it reads no Model columns (model_0 to model_3). One that reads them makes Renderer::render return MeshError::InvalidInstance, which names the columns it reads, whether the Model is hidden or not.

The flag is Arc-shared with ModelEntry (the renderer’s queue), so toggles take effect on the very next Renderer::render call. Cheap: one bool flip + one entry check during the draw-queue build.

Typical uses:

  • LOD switches that hide whole sets of detail Models at a distance.
  • Level / chapter transitions that swap which subset of a Scene renders without rebuilding the Scene.
  • Temporary hides for editor gizmos, debug helpers, or selection highlights.
use fragmentcolor::{Material, Mesh, Model, Vertex};
let mesh = Mesh::new();
mesh.add_vertices([
Vertex::pbr([0.0, 0.5, 0.0]),
Vertex::pbr([-0.5, -0.5, 0.0]),
Vertex::pbr([0.5, -0.5, 0.0]),
]);
let detail = Model::new(mesh, Material::pbr());
// Far from the camera, hide the detail Model.
detail.set_visible(false);
// Closer again, show it.
detail.set_visible(true);

Read the Model’s current 4×4 transform as its four columns of four numbers, the [[f32; 4]; 4] Rust returns, and four lists of four in every other language (four Float32Arrays in JavaScript). Column-major order matches WGSL’s mat4x4<f32> layout, so transform()[3] holds the translation. The transform starts at the identity matrix and is modified by set_transform, translate, rotate, and scale.

For the setter, see Model::set_transform. Rust doesn’t allow getter/setter overloads on the same name, so the read side is transform and the write side is set_transform.

use fragmentcolor::{Material, Mesh, Model, Renderer, Vertex};
let renderer = Renderer::new();
let mesh = Mesh::new();
mesh.add_vertices([
Vertex::pbr([0.0, 0.5, 0.0]).set(Vertex::UV0, [0.5, 1.0]),
Vertex::pbr([-0.5, -0.5, 0.0]).set(Vertex::UV0, [0.0, 0.0]),
Vertex::pbr([0.5, -0.5, 0.0]).set(Vertex::UV0, [1.0, 0.0]),
]);
let model = Model::new(mesh, Material::pbr());
let identity = model.transform();

Move the Model by offset in world coordinates. Pre-multiplies the current transform by a translation matrix, so the result is independent of the Model’s current rotation. translate([1, 0, 0]) always moves one unit along the world X axis.

An item that is NaN or an infinity returns ModelError::NotFinite, which names the call and the item, and the Model stays where it was. The other languages also refuse an item past the 32-bit float range, such as 1e39, with a message like Model.translate: item 1 must be a finite number that fits a 32-bit float, got 1e39. JavaScript throws an Error named InvalidInputError, Python raises InvalidInputError, Swift throws FragmentColorError and Kotlin throws FragmentColorException.

A move of finite numbers that would carry a number of the transform past the 32-bit float range, such as two moves of 3e38 along one axis, returns ModelError::Overflow: Model::translate would carry the transform past the f32 range. The Model keeps its transform, and JavaScript and Python raise it as InvalidInputError, too.

use fragmentcolor::{Material, Mesh, Model, Renderer, Vertex};
let renderer = Renderer::new();
let mesh = Mesh::new();
mesh.add_vertices([
Vertex::pbr([0.0, 0.5, 0.0]).set(Vertex::UV0, [0.5, 1.0]),
Vertex::pbr([-0.5, -0.5, 0.0]).set(Vertex::UV0, [0.0, 0.0]),
Vertex::pbr([0.5, -0.5, 0.0]).set(Vertex::UV0, [1.0, 0.0]),
]);
let model = Model::new(mesh, Material::pbr());
model.translate([5.0, 0.0, -2.0])?;

Read the current visibility flag. Returns true for newly-built Models; toggle with Model::set_visible.

The renderer reads this every frame. Hidden Models are skipped in both the opaque-batched and blend-sorted draw paths without requiring a Pass rebuild.

use fragmentcolor::{Material, Mesh, Model, Vertex};
let mesh = Mesh::new();
mesh.add_vertices([
Vertex::pbr([0.0, 0.5, 0.0]),
Vertex::pbr([-0.5, -0.5, 0.0]),
Vertex::pbr([0.5, -0.5, 0.0]),
]);
let model = Model::new(mesh, Material::pbr());
// Models start visible; toggle with `set_visible`.
let visible_now = model.visible();