Core Concepts

Material Graphs

Node-based materials for MavonEngine — made in the level editor, compiled to three.js TSL and put on primitives, with parameters, textures, noise and subgraphs.

A material graph is a material made of nodes. It compiles to a three.js TSL node material (MeshStandardNodeMaterial), so it runs on the WebGL 2 and the WebGPU backend of the renderer. You make them in the level editor's Graphs view, which shows a live preview of the material over the nodes.

Material graphs use the same file format, editor and subgraphs as logic graphs: graphs/<name>.graph.json with "kind": "material".

Using one

Pick it as the material graph of a primitive in the Properties panel, or set its materialGraph prop in the scene file. Without one the primitive draws with its own material again.

primitive.setMaterialGraph('lava')
primitive.setMaterialGraph(undefined) // back to its own material

On any mesh, through the game's library of material graphs. Every mesh using a graph shares one compiled material, and changing the graph (the editor does on every edit) swaps the new one onto all of them:

import { materialGraphs } from '@mavonengine/core/Graph/Material/MaterialGraphs'

await materialGraphs.apply(mesh, 'lava')
materialGraphs.release(mesh) // stop following the graph, the material stays

Nodes

CategoryNodes
OutputMaterial Output: Base Color, Opacity, Roughness, Metalness, Emissive, Normal, Position Offset. Inputs left unconnected keep the material's defaults. A graph has exactly one
SurfaceUV, World Position, Local Position, World Normal, View Direction
TextureTexture: samples a loaded texture resource by name, at a UV. One that isn't loaded shows magenta
NoisePerlin Noise (3D), Value Noise (2D)
InputFloat, Vec2, Vec3, Vec4, Color, Parameter, Time
Math, VectorThe same as in logic graphs: Add, Multiply, Mix, Clamp, Smoothstep, Remap, Sine, Fraction, … Combine, Split, Length, Dot, Normalize, Cross

Parameter nodes read the graph's parameters, which become uniforms of the compiled material. Comment and Group nodes annotate the graph, as in logic graphs.

Your own nodes

A material node needs a TSL implementation beside its definition. Register it with registerNode() and registerTslNode(), or from a plugin with ctx.registerNode(definition, tsl), see Plugins. A node that only works on the WebGPU backend sets requires: 'webgpu', and the editor warns about it on WebGL 2.

Compiling from code

import { compileMaterialGraph } from '@mavonengine/core/Graph/Material/compileMaterialGraph'

const { material, parameters } = compileMaterialGraph(graph, {
  parameters: { glow: 2 },
  textures: name => game.resources.items[name],
})
parameters.glow.value = 3 // a uniform: changes without compiling again

A graph with problems throws a MaterialGraphError listing them.