text stringlengths 1 474 |
|---|
style: TextStyle( |
fontFamily: 'Raleway', |
), |
), |
), |
); |
} |
}<code_end> |
<topic_end> |
<topic_start>Custom drawing and graphics |
<topic_end> |
<topic_start> |
Topics |
<topic_end> |
<topic_start>Writing and using fragment shaders |
info Note |
Both the Skia and Impeller backends support writing a |
custom shader. Except where noted, the same |
instructions apply to both.Custom shaders can be used to provide rich graphical effects |
beyond those provided by the Flutter SDK. |
A shader is a program authored in a small, |
Dart-like language, known as GLSL, |
and executed on the user’s GPU.Custom shaders are added to a Flutter project |
by listing them in the pubspec.yaml file, |
and obtained using the FragmentProgram API.<topic_end> |
<topic_start> |
Adding shaders to an application |
Shaders, in the form of GLSL files with the .frag extension, |
must be declared in the shaders section of your project’s pubspec.yaml file. |
The Flutter command-line tool compiles the shader |
to its appropriate backend format, |
and generates its necessary runtime metadata. |
The compiled shader is then included in the application just like an asset.When running in debug mode, |
changes to a shader program trigger recompilation |
and update the shader during hot reload or hot restart.Shaders from packages are added to a project |
with packages/$pkgname prefixed to the shader program’s name |
(where $pkgname is the name of the package).<topic_end> |
<topic_start> |
Loading shaders at runtime |
To load a shader into a FragmentProgram object at runtime, |
use the FragmentProgram.fromAsset constructor. |
The asset’s name is the same as the path to the shader |
given in the pubspec.yaml file.The FragmentProgram object can be used to create |
one or more FragmentShader instances. |
A FragmentShader object represents a fragment program |
along with a particular set of uniforms (configuration parameters). |
The available uniforms depends on how the shader was defined.<topic_end> |
<topic_start> |
Canvas API |
Fragment shaders can be used with most Canvas APIs |
by setting Paint.shader. |
For example, when using Canvas.drawRect |
the shader is evaluated for all fragments within the rectangle. |
For an API like Canvas.drawPath with a stroked path, |
the shader is evaluated for all fragments within the stroked line. |
Some APIs, such as Canvas.drawImage, ignore the value of the shader.<topic_end> |
<topic_start> |
Authoring shaders |
Fragment shaders are authored as GLSL source files. |
By convention, these files have the .frag extension. |
(Flutter doesn’t support vertex shaders, |
which would have the .vert extension.)Any GLSL version from 460 down to 100 is supported, |
though some available features are restricted. |
The rest of the examples in this document use version 460 core.Shaders are subject to the following limitations |
when used with Flutter:<topic_end> |
<topic_start> |
Uniforms |
A fragment program can be configured by defining |
uniform values in the GLSL shader source |
and then setting these values in Dart for |
each fragment shader instance.Floating point uniforms with the GLSL types |
float, vec2, vec3, and vec4 |
are set using the FragmentShader.setFloat method. |
GLSL sampler values, which use the sampler2D type, |
are set using the FragmentShader.setImageSampler method.The correct index for each uniform value is determined by the order |
that the uniform values are defined in the fragment program. |
For data types composed of multiple floats, such as a vec4, |
you must call FragmentShader.setFloat once for each value.For example, given the following uniforms declarations in a GLSL fragment program:The corresponding Dart code to initialize these uniform values is as follows:Observe that the indices used with FragmentShader.setFloat |
do not count the sampler2D uniform. |
This uniform is set separately with FragmentShader.setImageSampler, |
with the index starting over at 0.Any float uniforms that are left uninitialized will default to 0.0.<topic_end> |
<topic_start>Current position |
The shader has access to a varying value that contains the local coordinates for |
the particular fragment being evaluated. Use this feature to compute |
effects that depend on the current position, which can be accessed by |
importing the flutter/runtime_effect.glsl library and calling the |
FlutterFragCoord function. For example:The value returned from FlutterFragCoord is distinct from gl_FragCoord. |
gl_FragCoord provides the screen space coordinates and should generally be |
avoided to ensure that shaders are consistent across backends. |
When targeting a Skia backend, |
the calls to gl_FragCoord are rewritten to access local |
coordinates but this rewriting isn’t possible with Impeller.<topic_end> |
<topic_start>Colors |
There isn’t a built-in data type for colors. |
Instead they are commonly represented as a vec4 |
with each component corresponding to one of the RGBA |
color channels.The single output fragColor expects that the color value |
is normalized to be in the range of 0.0 to 1.0 |
and that it has premultiplied alpha. |
This is different than typical Flutter colors which use |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.