guarded

Evaluates an expression only where a condition holds, and a cheap fallback everywhere else.

guarded(
  cond: Expr,
  inner: Expr,
  fallback: Expr,
  hint: string = 'guarded',
  deps: Expr[] = [],
): Expr

Import materials from shaders/std and use materials.guarded.

Use it to skip a material's whole body for pixels outside the shape. Any value you build inside inner and also read after the guard must be listed in deps.

Parameters

NameTypeNotes
condExpr
innerExpr
fallbackExpr
hintstringOptional. Default 'guarded'.
depsExpr[]Optional. Default [].

Example

return guarded(insideShape(field.sdf, field.pxH), vec4(rgb, silhouette(field, u.edgeSoftness)), vec4(0, 0, 0, 0), 'plastic')

Tip. Values first made inside the guard are not visible after it. Put shared locals in deps.

See also

  • insideShape — True inside the shape and up to two pixels outside it.
  • silhouette — The shape's coverage at this pixel: 1 inside, 0 outside, soft across the edge.

Used by

BrushedMetal, CarbonFiber, Chrome, Frost, Goo, Hologram, Holographic, Irradiance, LiquidMetal, Nebula, Obsidian, Plastic, Water