Shader

self:apply_to_material

Client

Applies the shader to matching materials of a model, or of every model

Syntax

local count = self:apply_to_material(
    model,
    component,
    material,
    priority = 0
)

The shader automatically inherits values from the surface's original material.

  • Model instance — applies to that model only and lasts as long as the model does. It does not apply to models spawned later.
  • Wildcard "*" — applies to all live models and is registered persistently for models spawned later.
  • Priority — when several shaders cover the same surface, the one with the highest priority is shown. On equal priority, the most recently applied shader wins. Applying the same shader to the same target again updates its priority and counts as the latest apply.
  • Fallback — when the shown shader is removed with self:remove_from_material, destroyed or its resource stops, the next best shader takes over automatically. The surface only returns to its original material when no other shader covers it.
  • Auto-binding — refer Uniform Cheatsheet for uniform names populated automatically.

Parameters

TypeNameDescription
model | string | nilmodelModel instance to apply to
Use "*" / nil to match every model and register the assignment for future spawns
stringcomponentMesh component name or wildcard pattern to match
stringmaterialSurface material name or wildcard pattern to match
intpriorityPriority of this assignment, higher values win
Can be negative

Returns

TypeNameDescription
intcountNumber of surfaces on already-live models that matched

Examples

Apply a shader to every material of one model
core.model.load("character", "models/character.glb")

local model = core.model.create("character")
local entity = core.shader.create("assets/shaders/dissolve.glsl", core.shader.shader_mode.SPATIAL)

entity:apply_to_material(model, "*", "*")
Apply a shader to a single material on all models, including future spawns
local entity = core.shader.create("assets/shaders/dissolve.glsl", core.shader.shader_mode.SPATIAL)

entity:apply_to_material("*", "Skeleton3D/Body", "skin")
Let a higher priority shader win regardless of apply order
core.model.load("character", "models/character.glb")

local model = core.model.create("character")
local base = core.shader.create("assets/shaders/toon.glsl", core.shader.shader_mode.SPATIAL)
local highlight = core.shader.create("assets/shaders/highlight.glsl", core.shader.shader_mode.SPATIAL)

highlight:apply_to_material(model, "*", "*", 100)
base:apply_to_material(model, "*", "*") -- highlight is still shown, priority 100 beats 0

On this page