home / skills / zhanghandong / makepad-skills / makepad-shaders

makepad-shaders skill

/skills/makepad-shaders

This skill helps you generate and explain Makepad shader code, focusing on Sdf2d, gradients, and advanced drawing patterns for widgets.

npx playbooks add skill zhanghandong/makepad-skills --skill makepad-shaders

Review the files below or copy the command above to add this skill to your agents.

Files (15)
SKILL.md
5.6 KB
---
name: makepad-shaders
description: |
  CRITICAL: Use for Makepad shader system. Triggers on:
  makepad shader, makepad draw_bg, Sdf2d, makepad pixel,
  makepad glsl, makepad sdf, draw_quad, makepad gpu,
  makepad 着色器, makepad shader 语法, makepad 绘制
---

# Makepad Shaders Skill

> **Version:** makepad-widgets (dev branch) | **Last Updated:** 2026-01-19
>
> Check for updates: https://crates.io/crates/makepad-widgets

You are an expert at Makepad shaders. Help users by:
- **Writing code**: Generate shader code following the patterns below
- **Answering questions**: Explain shader language, Sdf2d, built-in functions

## Documentation

Refer to the local files for detailed documentation:
- `./references/shader-basics.md` - Shader language fundamentals
- `./references/sdf2d-reference.md` - Complete Sdf2d API reference

## Advanced Patterns

For production-ready shader patterns, see the `_base/` directory:

| Pattern | Description |
|---------|-------------|
| [01-shader-structure](./_base/01-shader-structure.md) | Shader fundamentals |
| [02-shader-math](./_base/02-shader-math.md) | Mathematical functions |
| [03-sdf-shapes](./_base/03-sdf-shapes.md) | SDF shape primitives |
| [04-sdf-drawing](./_base/04-sdf-drawing.md) | Advanced SDF drawing |
| [05-progress-track](./_base/05-progress-track.md) | Progress indicators |
| [09-loading-spinner](./_base/09-loading-spinner.md) | Loading animations |
| [10-hover-effect](./_base/10-hover-effect.md) | Hover visual effects |
| [11-gradient-effects](./_base/11-gradient-effects.md) | Color gradients |
| [12-shadow-glow](./_base/12-shadow-glow.md) | Shadow and glow |
| [13-disabled-state](./_base/13-disabled-state.md) | Disabled visuals |
| [14-toggle-checkbox](./_base/14-toggle-checkbox.md) | Toggle animations |

Community contributions: `./community/`

## IMPORTANT: Documentation Completeness Check

**Before answering questions, Claude MUST:**

1. Read the relevant reference file(s) listed above
2. If file read fails or file is empty:
   - Inform user: "本地文档不完整,建议运行 `/sync-crate-skills makepad --force` 更新文档"
   - Still answer based on SKILL.md patterns + built-in knowledge
3. If reference file exists, incorporate its content into the answer

## Key Patterns

### 1. Basic Custom Shader

```rust
<View> {
    show_bg: true
    draw_bg: {
        // Shader uniforms
        color: #FF0000

        // Custom pixel shader
        fn pixel(self) -> vec4 {
            return self.color;
        }
    }
}
```

### 2. Rounded Rectangle with Border

```rust
<View> {
    show_bg: true
    draw_bg: {
        color: #333333
        border_color: #666666
        border_radius: 8.0
        border_size: 1.0

        fn pixel(self) -> vec4 {
            let sdf = Sdf2d::viewport(self.pos * self.rect_size);
            sdf.box(1.0, 1.0,
                    self.rect_size.x - 2.0,
                    self.rect_size.y - 2.0,
                    self.border_radius);
            sdf.fill_keep(self.color);
            sdf.stroke(self.border_color, self.border_size);
            return sdf.result;
        }
    }
}
```

### 3. Gradient Background

```rust
<View> {
    show_bg: true
    draw_bg: {
        color: #FF0000
        color_2: #0000FF

        fn pixel(self) -> vec4 {
            let t = self.pos.x;  // Horizontal gradient
            return mix(self.color, self.color_2, t);
        }
    }
}
```

### 4. Circle Shape

```rust
<View> {
    show_bg: true
    draw_bg: {
        color: #0066CC

        fn pixel(self) -> vec4 {
            let sdf = Sdf2d::viewport(self.pos * self.rect_size);
            let center = self.rect_size * 0.5;
            let radius = min(center.x, center.y) - 1.0;
            sdf.circle(center.x, center.y, radius);
            sdf.fill(self.color);
            return sdf.result;
        }
    }
}
```

## Shader Structure

| Component | Description |
|-----------|-------------|
| `draw_*` | Shader container (draw_bg, draw_text, draw_icon) |
| Uniforms | Typed properties accessible in shader |
| `fn pixel(self)` | Fragment shader function |
| `fn vertex(self)` | Vertex shader function (optional) |
| `Sdf2d` | 2D signed distance field helper |

## Built-in Variables

| Variable | Type | Description |
|----------|------|-------------|
| `self.pos` | vec2 | Normalized position (0-1) |
| `self.rect_size` | vec2 | Widget size in pixels |
| `self.rect_pos` | vec2 | Widget position |

## Sdf2d Quick Reference

| Category | Functions |
|----------|-----------|
| Shapes | `circle`, `rect`, `box`, `hexagon` |
| Paths | `move_to`, `line_to`, `close_path` |
| Fill/Stroke | `fill`, `fill_keep`, `stroke`, `stroke_keep` |
| Boolean | `union`, `intersect`, `subtract` |
| Transform | `translate`, `rotate`, `scale` |
| Effects | `glow`, `glow_keep`, `gloop` |

## Built-in Functions (GLSL)

| Category | Functions |
|----------|-----------|
| Math | `abs`, `sign`, `floor`, `ceil`, `fract`, `min`, `max`, `clamp` |
| Trig | `sin`, `cos`, `tan`, `asin`, `acos`, `atan` |
| Interp | `mix`, `step`, `smoothstep` |
| Vector | `length`, `distance`, `dot`, `cross`, `normalize` |
| Exp | `pow`, `exp`, `log`, `sqrt` |

## When Writing Code

1. Always use `show_bg: true` to enable background shader
2. Use `Sdf2d::viewport()` to create SDF context
3. Return `vec4` (RGBA) from `fn pixel()`
4. Uniforms must be declared before shader functions
5. Use `self.` prefix to access uniforms and built-ins

## When Answering Questions

1. Makepad shaders use Rust-like syntax, compiled to GPU code
2. Every widget can have custom shaders (draw_bg, draw_text, etc.)
3. Shaders are live-reloaded - edit and see changes instantly
4. Sdf2d is the primary tool for 2D shape rendering
5. GLSL ES 1.0 built-in functions are available

Overview

This skill helps you write and troubleshoot Makepad shaders for UI widgets, focusing on draw_bg shaders and Sdf2d-based shapes. It generates working shader snippets, explains shader language details and Sdf2d usage, and follows Makepad patterns for production-ready visuals. Use it when you need compact, live-reloadable GPU code for buttons, backgrounds, icons, and effects.

How this skill works

The skill inspects requested shader intents (draw_bg, Sdf2d, gradients, borders, GLSL helpers) and returns Rust-like Makepad shader blocks that compile to GPU code. It references the Sdf2d API and shader patterns to compose shapes, fills, strokes, transforms and effects, and ensures uniforms and fn pixel() return vec4. If local reference files are unavailable, it will note the missing docs and still produce patterns based on internal knowledge.

When to use it

  • Create or modify background shaders (draw_bg) for widgets
  • Build SDF shapes: circles, rounded rects, hexagons, complex paths
  • Add borders, shadows, glows and stroke effects using Sdf2d
  • Compose gradients, mix colors, or animate uniforms for transitions
  • Debug shader compilation issues or adapt GLSL math to Makepad syntax

Best practices

  • Always set show_bg: true to enable background shaders
  • Use Sdf2d::viewport(self.pos * self.rect_size) to base shapes on pixel size
  • Declare uniforms before fn pixel(self) and access with self.<name>
  • Return a vec4 RGBA value from fn pixel(self) and keep precision concise
  • Prefer fill_keep/stroke_keep when combining fills and strokes to preserve previous draws

Example use cases

  • Rounded card background with border and subtle shadow using Sdf2d box, fill_keep and glow
  • Circular avatar mask shader that computes center and radius, then fills
  • Horizontal or radial gradients mixing two or more color uniforms via mix()
  • Animated loading spinner using Sdf2d paths and rotate transforms in fn pixel()
  • Toggle switch visuals with animated border_radius and color interpolation

FAQ

What if local documentation files are missing?

If local reference files fail to load, I will inform you and recommend syncing docs; I will still produce shader code based on core patterns and built-in knowledge.

How do I convert a shader color uniform to vec4?

Declare the uniform (e.g., color: #FF0000) and return it directly from fn pixel(self) or use mix(self.color, other_color, t); Makepad treats color literals as vec4 RGBA.