Log inGet started

Per-entity shader data

Updated December 5, 2026

Per-entity shader data — a per-instance property override, without a material each

A material per entity is what a dissolve, a hit flash or an effect at its own age would otherwise cost: an authored material each or a runtime renderer.material.create clone each, a separate pipeline binding and a separate row in the material table for every one of them, and a setProperty call per entity per frame to drive it. The per-instance channel is the way out: every drawn object carries a block of four vec4 lanes of its own, a surface shader reads lane i as input.shader_data[i], and the engine attaches no meaning to what is in them.

local DISSOLVE_LANE = 0
renderer.instanceData.laneCount()                    --> 4   lanes per entity
renderer.instanceData.set(subject, DISSOLVE_LANE, progress)
renderer.instanceData.clear(subject)                 -- back to zero lanes
fn fragment(input: FragmentData) -> vec4<f32> {
    let progress = input.shader_data[0].x;
    if progress > fract(sin(dot(input.uv, vec2<f32>(12.9898, 78.233))) * 43758.5) {
        discard;
    }
    return material.base_color;
}

Twenty subjects sharing that one material each dissolve at their own rate, and a write reaches the frame after it. Reach for a material per entity when what differs is the LOOK — a different shader, texture or blend — and for a lane when what differs is a number the shader reads. A feature picks the lane indices it owns and names them where it writes them, so its writer and its shader read the block the same way — the baked-lightmap section of topics/rendering-lighting is that pattern in the engine's own code, holding lane 0 for the atlas rect and lane 1 for the layer and enable flag.

A lane an entity was never given reads zero, so a shader tells "not set" from "set to zero" by keeping a flag in one of the components rather than by testing the value it wants.

A material property that follows a named value

A lane is a block of numbers a writer and its shader agree on. When what an entity carries is a named value (car.values.set("brake", true), the topics/values guide), a material binds any of its shader's properties to it in its values: block, and the property draws each wearer's own value, with no lane, no shader of its own and no script writing anything per frame:

values:
  emissive_intensity: { value: "brake", scale: 3 }   # value * scale + offset
  base_color: "paint"                                 # a vector fills the colour

The wearer's own value is read, else its nearest ancestor's, so a lamp mesh under a car lights from the car's brake; a wearer that holds none draws the material's own value. renderer.material.create({ ..., values = { ... } }, key) binds the same way at runtime.

Per-instance colour without a shader: stress colour, heat maps, instanced tint

Colouring many members every frame (a stress map over a bridge, a heat map, damage flashes, team colours, a selection highlight, a gauge) is a lane, and the built-in materials already read one. @builtin::shaders.pbr and unlit take the property instance_color_lane: the lane it names carries this instance's colour in rgb and how much of it covers the material's own colour in a, 0 none through 1 all. @builtin::materials.instance_color (lit) and @builtin::materials.instance_color_unlit are both ready made on lane 2.

local LANE = 2       -- lanes 0 and 1 carry a baked lightmap's rect and layer
for _, member in members do
    member.host.component.add("Model", { model = "cube", material = "@builtin::materials.instance_color" })
end

-- every frame: green at rest, red at its limit
for _, member in members do
    local load = math.clamp(member.load / member.limit, 0, 1)
    renderer.instanceData.set(member.host, LANE, load, 1 - load, 0, 1)
end
Lane componentMeaning
x, y, zthe colour, linear rgb
wcoverage: 0 leaves the material's own colour, 1 is the lane colour alone

instance_emission on the material makes the lane colour glow where it covers. On pbr the colour replaces the base colour before lighting, so the member still shades and the albedo capture pass reads it, which is where Model.tint lands too; on unlit the colour covers the material's own, and over a white unlit material that is the picture Model.tint gives for the same colour and blend. A population drawn by renderer.mesh.drawInstanced colours each copy from the same lane of its instanceDataBuffer, with no entity per copy at all; it takes the material as an AssetRef, asset.resolve("@builtin::materials.instance_color", "material").

This is the per-frame, local route. A lane write replicates nothing, and for 200 members recoloured every frame it costs about 0.4 ms of Luau, against about 8.6 ms through Model.tintR/G/B/tintBlend, whose fields are synced so every write is also a replicated property change. Use Model.tint for a colour every participant must see; use the lane for a readout each engine draws for itself. A material that leaves instance_color_lane at -1 draws a program without the read (it is the PBR_INSTANCE_COLOR / UNLIT_INSTANCE_COLOR feature), so it costs nothing where it is not used.

Which material an instance is drawing with is a number, because a shader cannot read a name. Every renderable's per-instance record carries a material index, and renderer.materialIdentity() is the table those indices are drawn from — assigned the first time the renderer draws with that material and stable while the renderer holds it.

local id = renderer.materialIdentity()
id.slots["@builtin::materials.plastic_red"]  --> 4     the index that material took
id.renderables[1]                            --> { entity = "ent_…", slot = 4, index = 4, material = "…" }
id.populations[1]                            --> { slot = 96, count = 4096, index = 5, material = "…" }
renderer.materialIndex("@builtin::materials.plastic_red")  --> 4

A surface shader reads its own instance's index as input.material_index, so a shader drawn by several materials can branch on which one it is under, and a compute pass that traces the scene resolves the same index through zeroMaterial(). Two renderables that differ only in their material read different indices; one renderable reads the same index frame after frame, and a material swap moves it on the next frame. A GPU-driven population is one row in populations rather than one per member: every slot of its reserved run carries the single material renderer.mesh.drawInstanced was given, so a member reads that index wherever the population moves.

A renderable draws with the material its entity references, so a renderable whose entity names none reads index 0. An index is an identity for as long as the renderer holds its material: once the material is destroyed and nothing draws with its name, the name leaves slots and the next material takes its index. A renderable still naming a destroyed material keeps a row for that name until it stops drawing with it. So the table holds a row per name the renderer holds a material under or something draws with, and a session that makes and destroys materials under fresh names keeps it the same size.

A population's members carry their own shader data. One material is what keeps a population one draw, so what a member differs in has to arrive per-instance. instanceDataBuffer is that channel: a GPU buffer holding one block of four vec4 lanes per instance, which the renderer copies into those instances' lanes every frame, and which a surface shader reads as input.shader_data[lane] — the same lanes an entity gets from renderer.instanceData.set, on slots no entity owns.

local lanes = substrate.createBuffer({ name = "crowd.lanes", type = "vec4", len = COUNT * 4, kind = "gpu" })
renderer.mesh.drawInstanced(mesh, {
    transformBuffer = "crowd.xf",
    instanceDataBuffer = lanes.name,   -- the NAME, not the buffer
    instanceCount = COUNT,
    material = M,
})

The copy re-runs every frame, so a compute pass (or a :write) that rewrites the buffer changes the next frame with no second registration. The buffer must back the whole reservation — a short one is refused at the call rather than letting the instances past its end read the next population's lanes. Omit it and the lanes read zero, whatever the slots held before the registration took them.

Instanced foliage built in the vertex shader — grass, kelp, hair cards, debris

A field of thousands of shaped instances — grass blades that sway and cast shadows, kelp fronds, hair cards, leaves, debris — is one mesh drawn once per instance by renderer.mesh.drawInstanced, each instance shaped in its material's vertex() hook from its own lanes and from the index of the vertex being placed: per-instance data in a surface shader vertex hook.

A member's vertex() hook reads the same lanes. VertexData.instance_id is the instance's slot, so zero_object_data(v.instance_id, lane) is the lane the fragment reads as input.shader_data[lane], and VertexData.vertex_index is the vertex's index in its mesh. Both hold the same values in the colour pass, the depth views, every shadow page and the previous-frame evaluation the motion vector comes from, so geometry a hook builds from them casts the shadow it draws. A blade field is one strip mesh drawn once per blade, each blade placed by its transform and shaped by its lanes:

const BLADE_LANE: u32 = 0u;   // x = height, y = width at the root
const LEAN_LANE: u32 = 1u;    // xy = lean direction, z = stiffness

fn vertex(v: VertexData) -> VertexData {
    var o = v;
    let blade = zero_object_data(v.instance_id, BLADE_LANE);
    let lean = zero_object_data(v.instance_id, LEAN_LANE);
    // A strip of pairs: vertex 2k and 2k+1 stand at row k, left and right.
    let row = f32(v.vertex_index / 2u);
    let side = f32(v.vertex_index % 2u) - 0.5;
    let t = row / 6.0;                                  // a strip of 7 rows
    let bend = t * t * (1.0 - lean.z) * blade.x;
    o.position = vec3<f32>(side * blade.y * (1.0 - t), t * blade.x, 0.0)
        + vec3<f32>(lean.x, 0.0, lean.y) * bend;
    return o;
}
renderer.mesh.drawInstanced(stripMesh, {
    transformBuffer = "meadow.xf",         -- where each blade stands
    instanceDataBuffer = "meadow.lanes",   -- lanes 0 and 1 per blade
    instanceCount = BLADES,
    material = bladeMaterial,
})

The strip mesh's own vertices can all stand at the origin: the hook places them, and the shader's vertexBoundsDilation property (the shader README, "Declaring how far a hook moves geometry") states how far from the mesh's bounds the tallest blade reaches, so each copy is culled by the space its blade fills.

The blades are an engine material, so they are lit by the scene's lights and its environment, cast and receive shadows, carry motion vectors (the hook runs again over the previous frame's time, so a blade swaying with v.time reports its own motion), and take fog and the post-process chain. The shader holds the shape and the surface; the pipeline around them is the engine's. A render feature's raster pass (types/rasterShader, and the renderFeature assetType README's "Raster passes") is the other way to draw an instanced field: a hand-written vertex and fragment program over buffers the feature owns, with the pipeline, the depth test and the blend it states. It suits a port of a hand-written renderer whose pipeline is the thing being kept; the vertex hook suits geometry that should look and behave like the rest of the scene.

  • documentation
  • guide
  • rendering
  • instancing
  • instanced
  • per-instance
  • vertex
  • foliage
  • grass