Skip to content

Materials

System · Materials · server and client scripts

Materials lists, reads, defines, and removes the surface materials of a place. A material sets how its parts look, collide, and sound. A material with a Shader draws its parts with a shader from the project's shaders folder; see Writing a material shader.

GetMaterials

Function
GetMaterials() -> {MaterialEntry}

GetMaterials returns an array of MaterialEntry tables with the asset id, name, pipeline, and surface model of every material in the place.

Example

-- Server script.
local materials = Materials.GetMaterials()

-- count the materials in this place
local count = #materials

GetMaterial

Function
GetMaterial(name: string) -> MaterialSpec?

GetMaterial returns the MaterialSpec of a material by asset id or name, in the form DefineMaterial takes.

Example

-- Server script.
local spec = Materials.GetMaterial("Plastic")

-- Surface can be nil
if spec and spec.Surface then
    local roughness = spec.Surface.RoughnessMult
end
Errors (1)
Error When
no material '<idOrName>' No material has that asset id or name.

DefineMaterial

FunctionServer only
DefineMaterial(spec: MaterialSpec, parent: string?) -> string

DefineMaterial defines a material, or redefines the one with the same Id, and returns its asset id. Without a Name, the material gets the next free name. To add a material, pick an Id that no material in the place has. Studio numbers its ids upward from the lowest project id, so the highest ones, which the examples use, stay free.

Effects

  • Passing parent, a material asset id, sets the material RemoveMaterial moves surfaces to when no replacement is given. From the next server tick, the tables spec leaves out (Emission, Studs, Water, Physics, Footsteps, Impacts, and Shader with its Params) come from parent; Surface, Pipeline and Model always come from spec. When the server cannot load a material file for parent, it logs an error and the material keeps only the tables in spec.
  • Updates the material's surfaces and their physics until the server stops. The project's materials folder keeps its files; only Studio's material editor writes them.

Example

-- Server script.
local id = Materials.DefineMaterial({
    Name = "CustomMetal",
    Pipeline = "Opaque",
    Model = "UV",
    Surface = {
        RoughnessMult = 0.2,
        MetallicMult = 0.9,
        TileSize = 2.0,
    },
    Physics = {
        Friction = 0.4,
        Restitution = 0.1,
    },
})
Errors (3)
Error When
Pipeline must be 'Opaque', 'Cutout' or 'Transparent' for a material with a Shader, got '<Pipeline>' With a Shader, set Pipeline to "Opaque", "Cutout" or "Transparent", or leave it out for "Opaque".
Materials.DefineMaterial: no shader file 'shaders/<Shader>.slang' in the project Set Shader to "toon" for shaders/toon.slang.
DefineMaterial can only be called from the server Called from a client script.

RemoveMaterial

FunctionServer only
RemoveMaterial(name: string, replacement: string?) -> ()

RemoveMaterial removes a material and moves its surfaces to replacementIdOrName, or when that is omitted, to the material's parent or Plastic.

Effects

  • Removes the material until the server stops. The project's materials folder keeps its files.

Example

-- Server script.
Materials.DefineMaterial({
    Name = "OldBrick",
    Pipeline = "Opaque",
    Model = "UV",
    Surface = {},
})

-- parts made of OldBrick now use Plastic
Materials.RemoveMaterial("OldBrick", "Plastic")
Errors (1)
Error When
RemoveMaterial can only be called from the server Called from a client script.

ReplaceMaterial

FunctionServer only
ReplaceMaterial(oldName: string, newName: string) -> ()

ReplaceMaterial moves every surface from one material to another and removes the first.

Example

-- Server script.
Materials.DefineMaterial({
    Name = "CrackedConcrete",
    Pipeline = "Opaque",
    Model = "UV",
    Surface = {},
})

-- move every CrackedConcrete surface to Plastic
Materials.ReplaceMaterial("CrackedConcrete", "Plastic")
Errors (1)
Error When
ReplaceMaterial can only be called from the server Called from a client script.

ReloadMaterial

FunctionServer only
ReloadMaterial(name: string) -> ()

ReloadMaterial reads a material file from the project's materials folder again and redefines the material with it. Its surfaces keep the material.

Example

-- Server script, after editing materials/Lagoon in the project.
if Materials.GetMaterial("Lagoon") then
    Materials.ReloadMaterial("Lagoon")
end
Errors (2)
Error When
the material does not reload: no-dir The server does not run from a project.
ReloadMaterial can only be called from the server Called from a client script.

SetParam

FunctionServer only
SetParam(material: string, field: string, value: MaterialParamValue?) -> ()

SetParam sets one field of a material's shader on the server; client scripts use Override for client-local parameter updates. Pass a number, Vector3 or Color, boolean, or texture asset id string that matches the field; see parameters.

Effects

  • Override sends nothing over the network; the material file, the server, and other players keep the material's Params. A shader that animates with time alone can read SurfaceInput.time instead.

Example

-- Client script in a place whose Ink material has an inkWeight shader field.
local clock = 0

-- pulse the ink width of the Ink material every frame
function Update(dt: number)
    clock += dt
    Materials.Override("Ink", "inkWeight", 4 + 2 * math.sin(clock * 2))
end
Errors (1)
Error When
SetParam can only be called from the server Called from a client script.

GetParam

Function
GetParam(material: string, field: string) -> MaterialParamValue?

GetParam returns the shared value of shader parameter field on material.

Example

-- Server script.
if Materials.GetMaterial("Ink") then
    local val = Materials.GetParam("Ink", "inkWeight")
    print(val)
end

Override

FunctionClient only
Override(material: string, field: string, value: MaterialParamValue) -> ()

Override sets a client-local override for shader parameter field on material without changing the shared value.

Example

-- Client script.
Materials.Override("Plastic", "Roughness", 0.5)
Errors (1)
Error When
Override can only be called from a client script Called from a server script.

ClearOverride

FunctionClient only
ClearOverride(material: string, field: string) -> ()

ClearOverride removes a client-local override for shader parameter field on material.

Example

-- Client script.
Materials.ClearOverride("Plastic", "Roughness")
Errors (1)
Error When
ClearOverride can only be called from a client script Called from a server script.

GetOverride

FunctionClient only
GetOverride(material: string, field: string) -> MaterialParamValue?

GetOverride returns the client-local override for shader parameter field on material, or nil when none is set.

Example

-- Client script.
local ov = Materials.GetOverride("Plastic", "Roughness")
print(ov)
Errors (1)
Error When
GetOverride can only be called from a client script Called from a server script.