MaterialMap
Description
Section titled “Description”One texture map of a material: the texture it samples, the vertex UV set it
reads, and the UV transform it applies, the way glTF’s textureInfo and
KHR_texture_transform
describe a map.
A material hands out one handle per map, such as
PbrMaterial::base_color_map
or PbrMaterial::normal_map.
You don’t build a MaterialMap yourself. Each setter writes into the
material the map came from and returns the map, so the calls chain:
texturesets the texture, from aTexture, from any inputRenderer::create_texturetakes, or from another map, whose texture both maps then sample.uvpicks the UV set,0or1.transformscales, rotates and offsets the UVs.
A map clones as a handle to the same material, as the material does, so a setter through any copy changes what every Model that draws with the material shows.
Example
Section titled “Example”use fragmentcolor::PbrMaterial;
let flat: Vec<u8> = vec![128, 128, 255, 255];let gold = PbrMaterial::new().metallic(1.0)?.roughness(0.3)?;
// A normal map tiled four times over the second UV layout.let map = gold .normal_map() .texture((flat, [1, 1]))? .uv(1)? .transform([0.0, 0.0], [4.0, 4.0], 0.0)?;import { PbrMaterial } from "fragmentcolor";
const flat = [128, 128, 255, 255];const gold = new PbrMaterial().metallic(1.0).roughness(0.3);
// A normal map tiled four times over the second UV layout.const map = gold.normalMap().texture(flat, [1, 1]).uv(1).transform([0.0, 0.0], [4.0, 4.0], 0.0);from fragmentcolor import PbrMaterial
flat = [128, 128, 255, 255]gold = PbrMaterial().metallic(1.0).roughness(0.3)
# A normal map tiled four times over the second UV layout.map = gold.normal_map().texture(flat, size=[1, 1]).uv(1).transform([0.0, 0.0], [4.0, 4.0], 0.0)import FragmentColor
let flat = [128, 128, 255, 255]let gold = try PbrMaterial().metallic(1.0).roughness(0.3)
// A normal map tiled four times over the second UV layout.let map = try gold.normalMap().texture(flat, [1, 1]).uv(1).transform([0.0, 0.0], [4.0, 4.0], 0.0)import org.fragmentcolor.*
val flat = listOf(128, 128, 255, 255)val gold = PbrMaterial().metallic(1.0f).roughness(0.3f)
// A normal map tiled four times over the second UV layout.val map = gold.normalMap().texture(flat, listOf(1u, 1u)).uv(1u).transform(listOf(0.0f, 0.0f), listOf(4.0f, 4.0f), 0.0f)Methods
Section titled “Methods”MaterialMap::texture
Section titled “MaterialMap::texture”Set the texture the map samples, and return the map. The material’s shader
reads it under the map’s name, such as base_color_map, through the UV set
uv picks and the
transform the
map applies.
Accepts another map, or any input Renderer::create_texture takes:
- Another
&MaterialMap, such as a loaded glTF material’s&pbr.base_color_map(). The map samples the texture the given map samples, under the sameTextureId. ATexturethere binds here as it is. A source stays one source, which eachRendereruploads once for both maps, and aRendererthat loaded it for the given map draws it for this one at once. One upload can’t serve two color spaces, so a map that reads its source in another one, such as a normal map given a base color map, keeps the same source and uploads a copy of its own in its own space. A format the caller picked stays, as aTexture’s does. The map keeps its own UV set and transform, sincetexturesets only the texture and the sampler that comes with it. Either map set again later takes a texture of its own and leaves the other as it was. A map with no texture, which draws its 1×1 default, has none to share, and the call refuses it. - A pre-built
&Texture. The map stores the texture’s ID immediately and the GPU texture stays Arc-shared across every material that points at it. Passing the same&shared_textureto the maps of N materials produces one GPU upload and N shader-uniform references: the cheap way to reuse one texture across a scene full of objects without paying for N texture allocations. The texture belongs to theRendererthat created it, and anotherRendererrefuses to draw it. It keeps the options it was created with, and the call refuses options beside it. - Encoded bytes, raw pixels with their size as
(bytes, [w, h]), bytes with a format as(bytes, format), bytes with every option as(bytes, options), a path or a URL, alone or as(path, format)or(path, options), or aMipmap, alone or as(chain, options). The material keeps this source in the map. EachRendererthat draws the material uploads a copy of its own atRenderer::load, or on native targets at its firstRenderer::render, and frees it when theRendereror the material drops. A map set again draws its old copy until theRendererloads the new one.
To share a map across Renderers, pass bytes, a path, a URL or a Mipmap.
Each language takes another map as texture(otherMap), or texture(other_map)
in Python, with no options, and spells the other inputs the way its
create_texture does:
- JavaScript:
texture(input, options?), whereoptionsis a size such as[w, h], aTextureFormat, or{ size?, format?, mipmaps?, sampler?, usage? }. - Python:
texture(input, size=None, format=None, mipmaps=None, sampler=None, usage=None). A NumPy array brings its own size, and asizethat differs from it raisesInvalidInputError. - Swift and Kotlin:
texture(map),texture(texture),texture(bytes),texture(bytes, format),texture(bytes, [w, h]),texture(bytes, size),texture(bytes, options),texture(path),texture(path, format),texture(path, options),texture(chain)andtexture(chain, options). Bytes areData,[UInt8]or[Int]in Swift andByteArrayorList<Int>in Kotlin, in every bytes form. The bytes forms withoutoptionstake an optionalusage. AnIntoutside 0 to 255 throws, and so does a[w, h]that isn’t 2 or 3 numbers.
A source with no format of its own reads in the map’s color space: sRGB in
the base color and emissive maps, as glTF’s color textures do, and linear in
the maps that hold data, such as normals. A Mipmap or a KTX2 container
keeps the format it stores, and takes that sRGB reading only where its bytes
read the same in it, so a float chain or container stays linear. A format you
pass yourself is the one it uploads in, and a Mipmap or KTX2 container
whose bytes can’t be read in it fails the load with an InvalidInput error
that names load.
Unset, a map draws a 1×1 default the renderer hands out lazily, which leaves
the map’s factor as it is, so PbrMaterial::new() renders correctly without
a texture. Each map’s page names its default.
Memory
Section titled “Memory”A map holds its source for as long as the material keeps it. That is the file’s
bytes for an encoded PNG or JPEG, a glTF image inside its file included, the
pixels for raw bytes or a NumPy array, the container for KTX2, and the chain
for a Mipmap. A path or
a URL holds only its string, and each Renderer reads or fetches it again.
renderer.create_texture keeps nothing on the CPU, and the texture it returns
draws in that one Renderer. A map that takes another map’s texture holds the
same source, so it adds nothing to that.
Errors
Section titled “Errors”The call checks its input at once, and a call it refuses leaves the map as it
was. Options beside a Texture return MaterialError::Texture with an
InvalidInput texture error that names the call, such as
PbrMaterial::base_color_map().texture, and so does an input that holds no
data: Rust’s TextureData::Empty, or an empty byte list, path or URL in any
language. So does a map with no texture, whose error names that map too, as in
ToonMaterial::base_color_map().texture: PbrMaterial::normal_map() has no texture to share.
The bindings read their arguments before the setter runs and throw for one
they can’t use: a size that isn’t 2 or 3 whole numbers, options of the wrong
type or shape, options beside a Texture or a map, and in Python a size that
differs from a NumPy array’s own. Python raises TypeError for an argument
of the wrong type and InvalidInputError for the rest. JavaScript throws an Error
of the same name. Swift throws FragmentColorError, and each call needs
try, while Kotlin throws FragmentColorException.
A source that fails to upload (file not found, decode error, unsupported
format) fails Renderer::load(&material).await,
or the native render that runs it. That Renderer keeps no copy and tries
again at each later call, until the source is fixed or replaced, while the
other maps upload.
On the web, render uploads nothing, so await load in each Renderer
before its first frame. Until then the map draws its 1×1 default. On native, only a map set on another thread during a render
meets this, for that frame.
For deterministic error handling, pre-build the texture eagerly:
renderer.create_texture(path).await? returns a Texture that has already
uploaded, so no load can fail on it later, and
texture(&texture) takes an Arc-shared reference. The
lazy path stays useful when the URL / path resolves at render time but the
caller doesn’t want to await yet.
Example
Section titled “Example”use fragmentcolor::{PbrMaterial, Renderer, ToonMaterial};
let renderer = Renderer::new();let albedo_bytes: Vec<u8> = vec![ 255, 200, 120, 255, 255, 240, 180, 255, 230, 180, 100, 255, 255, 220, 150, 255,];let albedo = renderer.create_texture((albedo_bytes, [2, 2])).await?;
// Every material whose map points at `albedo` reuses the same uploaded GPU// texture; passing the same handle to N maps costs one upload and N// shader-uniform references.let material = PbrMaterial::new();let map = material.base_color_map().texture(&albedo)?;
// Raw pixels with their size upload when the scene loads.let tint: Vec<u8> = vec![255, 128, 0, 255];let tinted = PbrMaterial::new();let tinted_map = tinted.base_color_map().texture((tint, [1, 1]))?;
// A toon material samples the tinted texture too, which each Renderer// uploads once for both maps.let toon = ToonMaterial::new();let toon_map = toon.base_color_map().texture(&tinted.base_color_map())?;import { PbrMaterial, Renderer, ToonMaterial } from "fragmentcolor";
const renderer = new Renderer();const albedo_bytes = [ 255, 200, 120, 255, 255, 240, 180, 255, 230, 180, 100, 255, 255, 220, 150, 255, ];const albedo = await renderer.createTexture(albedo_bytes, [2, 2]);
// Every material whose map points at `albedo` reuses the same uploaded GPU// texture; passing the same handle to N maps costs one upload and N// shader-uniform references.const material = new PbrMaterial();const map = material.baseColorMap().texture(albedo);
// Raw pixels with their size upload when the scene loads.const tint = [255, 128, 0, 255];const tinted = new PbrMaterial();const tinted_map = tinted.baseColorMap().texture(tint, [1, 1]);
// A toon material samples the tinted texture too, which each Renderer// uploads once for both maps.const toon = new ToonMaterial();const toon_map = toon.baseColorMap().texture(tinted.baseColorMap());from fragmentcolor import PbrMaterial, Renderer, ToonMaterial
renderer = Renderer()albedo_bytes = [ 255, 200, 120, 255, 255, 240, 180, 255, 230, 180, 100, 255, 255, 220, 150, 255,]albedo = renderer.create_texture(albedo_bytes, size=[2, 2])
# Every material whose map points at `albedo` reuses the same uploaded GPU# texture; passing the same handle to N maps costs one upload and N# shader-uniform references.material = PbrMaterial()map = material.base_color_map().texture(albedo)
# Raw pixels with their size upload when the scene loads.tint = [255, 128, 0, 255]tinted = PbrMaterial()tinted_map = tinted.base_color_map().texture(tint, size=[1, 1])
# A toon material samples the tinted texture too, which each Renderer# uploads once for both maps.toon = ToonMaterial()toon_map = toon.base_color_map().texture(tinted.base_color_map())import FragmentColor
let renderer = Renderer()let albedo_bytes = [ 255, 200, 120, 255, 255, 240, 180, 255, 230, 180, 100, 255, 255, 220, 150, 255,]let albedo = try await renderer.createTexture(albedo_bytes, [2, 2])
// Every material whose map points at `albedo` reuses the same uploaded GPU// texture; passing the same handle to N maps costs one upload and N// shader-uniform references.let material = PbrMaterial()let map = try material.baseColorMap().texture(albedo)
// Raw pixels with their size upload when the scene loads.let tint = [255, 128, 0, 255]let tinted = PbrMaterial()let tinted_map = try tinted.baseColorMap().texture(tint, [1, 1])
// A toon material samples the tinted texture too, which each Renderer// uploads once for both maps.let toon = ToonMaterial()let toon_map = try toon.baseColorMap().texture(tinted.baseColorMap())import org.fragmentcolor.*
val renderer = Renderer()val albedo_bytes = listOf(255, 200, 120, 255, 255, 240, 180, 255, 230, 180, 100, 255, 255, 220, 150, 255)val albedo = renderer.createTexture(albedo_bytes, listOf(2u, 2u))
// Every material whose map points at `albedo` reuses the same uploaded GPU// texture; passing the same handle to N maps costs one upload and N// shader-uniform references.val material = PbrMaterial()val map = material.baseColorMap().texture(albedo)
// Raw pixels with their size upload when the scene loads.val tint = listOf(255, 128, 0, 255)val tinted = PbrMaterial()val tinted_map = tinted.baseColorMap().texture(tint, listOf(1u, 1u))
// A toon material samples the tinted texture too, which each Renderer// uploads once for both maps.val toon = ToonMaterial()val toon_map = toon.baseColorMap().texture(tinted.baseColorMap())MaterialMap::transform
Section titled “MaterialMap::transform”Give the map its own UV transform, the one glTF’s
KHR_texture_transform
describes, and return the map. The map’s UV is scaled first, then rotated,
then offset:
scalemultiplies the UV ([1, 1]keeps it)rotationturns the scaled UV around the origin, in radians. A positive angle turns the UVs counter-clockwise in texture space, wherevpoints down, so the image itself turns clockwiseoffsetmoves the result, in fractions of the texture ([0, 0]keeps it)
Every map starts with the identity transform. The transform applies to the
UV set that uv
picks for the map.
PbrMaterial::uv_transform
sets one transform on all five maps, and a transform after it replaces the
transform of its map alone.
Whether a UV past 0 to 1 repeats the texture depends on the sampler. The PBR shader samples all five maps with one sampler, which takes the options of the first texture the material sets, in this order: base color, metallic-roughness, normal, occlusion, then emissive. A map left at its 1×1 default doesn’t count. So a material with only an occlusion map tiles it with the occlusion map’s own options, and one with a clamped base color texture clamps a tiled normal map too.
A number that is NaN or an infinity returns MaterialError::NotFinite,
which names the part, such as PbrMaterial::normal_map().transform: rotation must be finite, got NaN, and leaves the map as it was: the setter
checks the offset, the scale and the rotation before it writes any of them.
The other languages also refuse a number past the 32-bit float range, such
as 1e39, with a message like MaterialMap.transform: rotation 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.
ToonMaterial::ramp_map
reads the light level in place of the mesh’s UVs, so its transform returns
MaterialError::NoUv, which names the call, and changes nothing.
Example
Section titled “Example”use fragmentcolor::PbrMaterial;
// Every map spans the surface twice, and the normal map eight times.let tiles = PbrMaterial::new().uv_transform([0.0, 0.0], [2.0, 2.0], 0.0)?;let normals = tiles.normal_map().transform([0.0, 0.0], [8.0, 8.0], 0.0)?;import { PbrMaterial } from "fragmentcolor";
// Every map spans the surface twice, and the normal map eight times.const tiles = new PbrMaterial().uvTransform([0.0, 0.0], [2.0, 2.0], 0.0);const normals = tiles.normalMap().transform([0.0, 0.0], [8.0, 8.0], 0.0);from fragmentcolor import PbrMaterial
# Every map spans the surface twice, and the normal map eight times.tiles = PbrMaterial().uv_transform([0.0, 0.0], [2.0, 2.0], 0.0)normals = tiles.normal_map().transform([0.0, 0.0], [8.0, 8.0], 0.0)import FragmentColor
// Every map spans the surface twice, and the normal map eight times.let tiles = try PbrMaterial().uvTransform([0.0, 0.0], [2.0, 2.0], 0.0)let normals = try tiles.normalMap().transform([0.0, 0.0], [8.0, 8.0], 0.0)import org.fragmentcolor.*
// Every map spans the surface twice, and the normal map eight times.val tiles = PbrMaterial().uvTransform(listOf(0.0f, 0.0f), listOf(2.0f, 2.0f), 0.0f)val normals = tiles.normalMap().transform(listOf(0.0f, 0.0f), listOf(8.0f, 8.0f), 0.0f)MaterialMap::uv
Section titled “MaterialMap::uv”Pick the vertex UV set the map samples, and return the map: 0 reads Vertex::UV0
and 1 reads Vertex::UV1. glTF calls this choice texCoord. Baked
lighting and occlusion often live on a second UV layout, apart from the
tiled UVs of the color maps. Every map starts on set 0.
A set other than 0 or 1 returns MaterialError::UvSet, such as
PbrMaterial::occlusion_map().uv takes UV set 0 or 1, got 2, and keeps the
map’s set. JavaScript throws an Error named MaterialError, Python raises MaterialError, a
FragmentColorError that is also a ValueError, Swift throws
FragmentColorError, and Kotlin throws FragmentColorException. The set
is a count like any other: JavaScript throws for a fraction or a negative
number, and Python takes an int, raising TypeError for a float and
InvalidInputError for an int below 0 or past 2**32 - 1.
ToonMaterial::ramp_map
reads the light level in place of the mesh’s UVs, so its uv returns
MaterialError::NoUv, which names the call, and changes nothing.
The PBR shader reads UV1 from every vertex, and a mesh whose vertices
lack it fails to attach with an error that names uv1. Vertex::pbr
seeds UV1 at [0, 0], so a map on set 1 reads that one point until
the mesh sets its own. Scene::load gives a primitive without
TEXCOORD_1 its TEXCOORD_0 values as UV1, so set 1 reads the same UVs
as set 0 there.
The map’s
transform
applies to whichever set it reads.
Example
Section titled “Example”use fragmentcolor::{Mesh, PbrMaterial, Vertex};
let mesh = Mesh::new();mesh.add_vertices([ Vertex::pbr([0.0, 0.5, 0.0]) .set(Vertex::UV0, [0.5, 1.0]) .set(Vertex::UV1, [0.25, 0.75]), Vertex::pbr([-0.5, -0.5, 0.0]) .set(Vertex::UV0, [0.0, 0.0]) .set(Vertex::UV1, [0.0, 0.5]), Vertex::pbr([0.5, -0.5, 0.0]) .set(Vertex::UV0, [1.0, 0.0]) .set(Vertex::UV1, [0.5, 0.5]),]);
// The occlusion map follows the second UV layout.let material = PbrMaterial::new();let map = material.occlusion_map().uv(1)?;import { Mesh, PbrMaterial, Vertex } from "fragmentcolor";
const mesh = new Mesh();mesh.addVertices([ Vertex.pbr([0.0, 0.5, 0.0]) .set(Vertex.UV0, [0.5, 1.0]) .set(Vertex.UV1, [0.25, 0.75]), Vertex.pbr([-0.5, -0.5, 0.0]) .set(Vertex.UV0, [0.0, 0.0]) .set(Vertex.UV1, [0.0, 0.5]), Vertex.pbr([0.5, -0.5, 0.0]) .set(Vertex.UV0, [1.0, 0.0]) .set(Vertex.UV1, [0.5, 0.5]), ]);
// The occlusion map follows the second UV layout.const material = new PbrMaterial();const map = material.occlusionMap().uv(1);from fragmentcolor import Mesh, PbrMaterial, Vertex
mesh = Mesh()mesh.add_vertices([ Vertex.pbr([0.0, 0.5, 0.0]).set(Vertex.UV0, [0.5, 1.0]).set(Vertex.UV1, [0.25, 0.75]), Vertex.pbr([-0.5, -0.5, 0.0]).set(Vertex.UV0, [0.0, 0.0]).set(Vertex.UV1, [0.0, 0.5]), Vertex.pbr([0.5, -0.5, 0.0]).set(Vertex.UV0, [1.0, 0.0]).set(Vertex.UV1, [0.5, 0.5]),])
# The occlusion map follows the second UV layout.material = PbrMaterial()map = material.occlusion_map().uv(1)import FragmentColor
let mesh = Mesh()mesh.addVertices([ try Vertex.pbr([0.0, 0.5, 0.0]).set(Vertex.UV0, [0.5, 1.0]).set(Vertex.UV1, [0.25, 0.75]), try Vertex.pbr([-0.5, -0.5, 0.0]).set(Vertex.UV0, [0.0, 0.0]).set(Vertex.UV1, [0.0, 0.5]), try Vertex.pbr([0.5, -0.5, 0.0]).set(Vertex.UV0, [1.0, 0.0]).set(Vertex.UV1, [0.5, 0.5]),])
// The occlusion map follows the second UV layout.let material = PbrMaterial()let map = try material.occlusionMap().uv(1)import org.fragmentcolor.*
val mesh = Mesh()mesh.addVertices(listOf(Vertex.pbr(listOf(0.0f, 0.5f, 0.0f)).set(Vertex.UV0, listOf(0.5f, 1.0f)).set(Vertex.UV1, listOf(0.25f, 0.75f)), Vertex.pbr(listOf(-0.5f, -0.5f, 0.0f)).set(Vertex.UV0, listOf(0.0f, 0.0f)).set(Vertex.UV1, listOf(0.0f, 0.5f)), Vertex.pbr(listOf(0.5f, -0.5f, 0.0f)).set(Vertex.UV0, listOf(1.0f, 0.0f)).set(Vertex.UV1, listOf(0.5f, 0.5f))))
// The occlusion map follows the second UV layout.val material = PbrMaterial()val map = material.occlusionMap().uv(1u)