---
title: "guarded"
description: "Evaluates an expression only where a condition holds, and a cheap fallback everywhere else."
group: "light"
groupTitle: "Noise, light & materials"
categoryId: "materials"
categorySlug: "materials"
categoryTitle: "Materials & surfaces"
kind: "function"
sourceUrl: "https://github.com/shader-effects-inc/shaders/blob/main/packages/core/src/std/paint/materials.ts#L85"
---

# guarded

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

```ts
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

| Name | Type | Notes |
|---|---|---|
| `cond` | `Expr` |  |
| `inner` | `Expr` |  |
| `fallback` | `Expr` |  |
| `hint` | `string` | Optional. Default `'guarded'`. |
| `deps` | `Expr[]` | Optional. Default `[]`. |

## Example

```ts
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`](/docs/primitives/materials/insideshape) — True inside the shape and up to two pixels outside it.
- [`silhouette`](/docs/primitives/materials/silhouette) — The shape's coverage at this pixel: 1 inside, 0 outside, soft across the edge.

## Used by

[BrushedMetal](/docs/components/brushedmetal), [CarbonFiber](/docs/components/carbonfiber), [Chrome](/docs/components/chrome), [Frost](/docs/components/frost), [Goo](/docs/components/goo), [Hologram](/docs/components/hologram), [Holographic](/docs/components/holographic), [Irradiance](/docs/components/irradiance), [LiquidMetal](/docs/components/liquidmetal), [Nebula](/docs/components/nebula), [Obsidian](/docs/components/obsidian), [Plastic](/docs/components/plastic), [Water](/docs/components/water)