You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: source/getting-started.md
+45-46Lines changed: 45 additions & 46 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,29 +1,42 @@
1
1
---
2
2
title: Getting Started with Flutter Shaders
3
-
description: Learn how to create stunning visual effects and performant graphics directly in your Flutter applications with this comprehensive guide.
3
+
description: Learn how to create stunning visual effects and performant graphics directly in your
4
+
Flutter applications with this comprehensive guide.
4
5
layout: layouts/content_page.jinja
5
6
directory: getting-started/
6
7
---
7
8
8
9
# Getting Started with Flutter Shaders
10
+
Shaders in Flutter allow you to create stunning visual effects and performant graphics directly in
11
+
your Flutter applications. This guide will take you from basic concepts to implementing real shader
12
+
effects in your apps.
9
13
10
-
Shaders in Flutter allow you to create stunning visual effects and performant graphics directly in your Flutter applications. This guide will take you from basic concepts to implementing real shader effects in your apps.
14
+
## Integrating Shaders from FlutterShaders.com
15
+
TL;DR: To use a shader from this website:
11
16
12
-
## What Are Fragment Shaders?
17
+
1.**Copy the shader code** from the shader page
18
+
2.**Save it** as a `.frag` file in `assets/shaders/`
19
+
3.**Add it to your pubspec.yaml** under `flutter: shaders:`
20
+
4.**Identify the uniforms** the shader expects
21
+
5.**Create a CustomPainter** similar to the example above
22
+
6.**Set the required uniforms** in the `paint` method
13
23
14
-
Fragment shaders are small GPU programs that determine the color of each pixel on the screen. Think of them as functions that run in parallel for every pixel, where:
24
+
## What Are Fragment Shaders?
25
+
Fragment shaders are small GPU programs that determine the color of each pixel on the screen. Think
26
+
of them as functions that run in parallel for every pixel, where:
15
27
16
28
-**Input**: The pixel's position (coordinates) and custom parameters called uniforms
17
29
-**Output**: A single color value for that pixel
18
30
19
-
**NOTE**: Shaders run on the GPU, making them incredibly fast. Fragment shaders are typically the final step in the graphics pipeline, which is why they're so efficient for real-time effects.
31
+
**NOTE**: Shaders run on the GPU, making them incredibly fast. Fragment shaders are typically the
32
+
final step in the graphics pipeline, which is why they're so efficient for real-time effects.
20
33
21
34
## Flutter + Fragment Shaders
22
-
23
-
Flutter is hardware-accelerated, meaning it leverages GPU shaders for rendering. Every widget you see - from a simple `Container` to complex blur effects - is ultimately rendered using optimized shaders. This is what makes Flutter so smooth and performant across platforms.
35
+
Flutter is hardware-accelerated, meaning it leverages GPU shaders for rendering. Every widget you
36
+
see - from a simple `Container` to complex blur effects - is ultimately rendered using optimized
37
+
shaders. This is what makes Flutter so smooth and performant across platforms.
24
38
25
39
## Types of Shader Usage in Flutter
26
-
27
40
There are three main ways to use custom shaders in Flutter:
28
41
29
42
### 1. Paint from Scratch
@@ -46,7 +59,6 @@ Modify content behind widgets:
46
59
47
60
48
61
## Step-by-Step Setup
49
-
50
62
Follow these steps to create your first Flutter shader project:
51
63
52
64
#### Step 1: Create a New Flutter Project
@@ -63,7 +75,6 @@ mkdir -p assets/shaders
63
75
```
64
76
65
77
#### Step 3: Create Your First Shader File
66
-
67
78
Create a file `assets/shaders/animated_colors.frag` with the following content:
68
79
69
80
```glsl
@@ -92,7 +103,6 @@ void main() {
92
103
```
93
104
94
105
#### Step 4: Register the Shader in pubspec.yaml
95
-
96
106
Add the shader to your `pubspec.yaml` file:
97
107
98
108
```yaml
@@ -234,12 +244,14 @@ You should see a colorful animated shader effect!
234
244
#### 2. ImageFiltered + ImageFilter.shader
235
245
- **Use Case**: Apply shader effects to child widgets
236
246
- **Pros**: Easy to apply shader effects to any widget
237
-
- **Cons**: Requires Impeller renderer; cannot control the first two uniforms (vec2 for texture size and sampler2D) as they are set by the Flutter engine
247
+
- **Cons**: Requires Impeller renderer; cannot control the first two uniforms (vec2 for texture
248
+
size and sampler2D) as they are set by the Flutter engine
238
249
239
250
#### 3. BackdropFilter + ImageFilter.shader
240
251
- **Use Case**: Apply shader effects to background content
241
252
- **Pros**: Easy backdrop shader effects
242
-
- **Cons**: Requires Impeller renderer;cannot control the first two uniforms (vec2 for texture size and sampler2D) as they are set by the Flutter engine
253
+
- **Cons**: Requires Impeller renderer;cannot control the first two uniforms (vec2 for texture
254
+
size and sampler2D) as they are set by the Flutter engine
243
255
244
256
#### 4. flutter_shaders Package
245
257
- **Use Case**: Simplified shader usage - easily transform any widget into a shader-compatible image
@@ -252,30 +264,38 @@ You should see a colorful animated shader effect!
252
264
**ImageFilter.shader** (Native Flutter API):
253
265
254
266
* **Integration**: Built directly into the Flutter framework (`dart:ui`).
255
-
* **Rendering Backend**: **Requires the Impeller rendering engine.** It will not work with other backends.
256
-
* **BackdropFilter**: **Supports `BackdropFilter`**, allowing you to apply shader effects to the content behind a widget.
257
-
* **API Flexibility**: Operates on a "convention over configuration" model. It simplifies the process by automatically providing and managing core uniforms (like the input texture), but this reduces flexibility as you have less control over the shader's direct inputs.
267
+
* **Rendering Backend**: **Requires the Impeller rendering engine.** It will not work with other
268
+
backends.
269
+
* **BackdropFilter**: **Supports `BackdropFilter`**, allowing you to apply shader effects to the
270
+
content behind a widget.
271
+
* **API Flexibility**: Operates on a "convention over configuration" model. It simplifies the
272
+
process by automatically providing and managing core uniforms (like the input texture), but this
273
+
reduces flexibility as you have less control over the shader's direct inputs.
258
274
* **Dependencies**: None, as it's part of the Flutter SDK.
259
275
260
276
**flutter_shaders** (Third-party Package):
261
277
262
278
* **Integration**: A community-created package that needs to be added as a dependency.
263
-
* **Rendering Backend**: **Backend-agnostic**, making it compatible with various rendering backends, not just Impeller.
264
-
* **BackdropFilter**: **Does not directly support `BackdropFilter`**. Its effects are generally limited to the widgets it's applied to.
265
-
* **API Flexibility**: Provides more direct control over the `FragmentShader` object. You have full responsibility for declaring and managing all uniforms, which offers maximum flexibility and power at the cost of potentially more boilerplate code.
279
+
* **Rendering Backend**: **Backend-agnostic**, making it compatible with various rendering
280
+
backends, not just Impeller.
281
+
* **BackdropFilter**: **Does not directly support `BackdropFilter`**. Its effects are generally
282
+
limited to the widgets it's applied to.
283
+
* **API Flexibility**: Provides more direct control over the `FragmentShader` object. You have
284
+
full responsibility for declaring and managing all uniforms, which offers maximum flexibility and
285
+
power at the cost of potentially more boilerplate code.
266
286
* **Dependencies**: Requires adding the `flutter_shaders` package to your `pubspec.yaml`.
267
287
268
288
### Rendering Pipeline Performance Impact
269
-
270
-
A critical difference between these approaches lies in **when and how many times rendering occurs** during the Flutter frame lifecycle:
289
+
A critical difference between these approaches lies in **when and how many times rendering occurs**
290
+
during the Flutter frame lifecycle:
271
291
272
292
**ImageFilter.shader** (Native Flutter API):
273
293
- Starts during the **build phase** and integrates directly with Flutter's compositing layer
274
294
- Processes in **single-pass rendering** within the normal pipeline
275
295
- **Does not trigger additional raster operations** beyond the standard frame rendering
276
296
277
-
278
-
You'll notice in the timeline that this process doesn't trigger any additional raster operations beyond what's standard for a frame (compared to the below example).
297
+
You'll notice in the timeline that this process doesn't trigger any additional raster operations
298
+
beyond what's standard for a frame (compared to the below example).
279
299
280
300

281
301
@@ -296,22 +316,18 @@ High-level view of the frame lifecycle:
0 commit comments