PySide6.QtCanvasPainter.QCanvasCustomBrush¶
- class QCanvasCustomBrush¶
QCanvasCustomBrushis a brush with custom shaders. More…Synopsis¶
Methods¶
def
__init__()def
__ne__()def
__eq__()def
setData1()def
setData2()def
setData3()def
setData4()def
swap()
Note
This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE
Detailed Description¶
QCanvasCustomBrushis a stroke/fill brush with custom vertex and/or fragment shaders.These shaders are expected to be written in Vulkan-style GLSL, similarly to Qt Quick ShaderEffect shaders. They must always contain a
QC_INCLUDEstatement, either with"customfrag.glsl"or"customvert.glsl". This makes available a uniform block, the image and font textures, and a few helper functions.iTimeis an example of a commonly used member in the built-in uniform block. CallingsetAnimationRunning()withtruewill make this value update automatically every frame, and can be used to drive animated content.Built-in Shader Inputs, Uniforms, and Helper Functions¶
The
QC_INCLUDEstatement is not a standard preprocessor directive. It is handled by qt_add_custom_brush_shaders() at build time, before the shader is passed to the regular shader compilation pipeline. The statement is replaced by a block of GLSL source code that declares the shader inputs and outputs, the texture samplers, a shared uniform block, and, for fragment shaders, a set of helper functions. Use"customvert.glsl"in vertex shaders and"customfrag.glsl"in fragment shaders.This means that a custom brush shader does not declare these inputs, outputs, samplers, or uniforms itself; they are all made available by the
QC_INCLUDEstatement.Vertex Shader Interface¶
A vertex shader that includes
"customvert.glsl"has the following inputs and outputs declared:in vec2 vertex- The vertex position in the canvas coordinate system.in vec2 tcoord- The texture coordinate associated with the vertex.out vec2 texCoord- Forwarded to the fragment shader. Typically set totcoord.out vec2 fragCoord- Forwarded to the fragment shader. Typically set tovertex.
Note
These variables are available implicitly via the
QC_INCLUDEdirective. The vertex shader snippet itself must not declare them.In addition, the following transformation-related uniforms are available:
vec4 viewRect- The viewport rectangle, as (x, y, width, height).int ndcIsYDown- Non-zero when the normalized device coordinate system has its Y axis pointing downwards, as is the case with some graphics APIs. Take this into account when computinggl_Position.mat3 vertMatrix- The current transformation matrix.
A vertex shader must write
gl_Position, and is expected to forwardtexCoordandfragCoordto the fragment stage.Note
Custom vertex shaders are less common. Most custom brushes are expected to use the default, built-in vertex shader in combination with a custom, application-provided fragment shader.
A typical vertex shader looks like this:
#version 440 QC_INCLUDE "customvert.glsl" void main() { texCoord = tcoord; fragCoord = vertex; vec2 v = (vertMatrix * vec3(vertex, 1.0)).xy; if (ndcIsYDown != 0) gl_Position = vec4(2.0 * (v.x + viewRect.x) / viewRect.z - 1.0, -1.0 + 2.0 * (v.y + viewRect.y) / viewRect.w, 0.0, 1.0); else gl_Position = vec4(2.0 * (v.x + viewRect.x) / viewRect.z - 1.0, 1.0 - 2.0 * (v.y + viewRect.y) / viewRect.w, 0.0, 1.0); }
Fragment Shader Interface¶
A fragment shader that includes
"customfrag.glsl"has the following inputs and output declared:in vec2 texCoord- The interpolated texture coordinate.in vec2 fragCoord- The interpolated fragment position, in the same coordinate system as the geometry. Commonly used to drive procedural effects.out vec4 fragColor- The resulting fragment color, which the shader must write. The expected output uses premultiplied alpha.
Note
These variables are available implicitly via the
QC_INCLUDEdirective. The fragment shader snippet itself must not declare them.Two texture samplers are available:
sampler2D tex- The image texture.sampler2D fontTex- The font texture, holding a signed distance field of the glyphs. Relevant when the brush is used to fill text.
The convenience constants
TAU(equal to 2 * pi) andSQRT2are also defined.Common Uniforms¶
Both vertex and fragment shaders that use
QC_INCLUDEhave access to a shared uniform block. The most commonly used members are:float iTime- A time value, in seconds, that is updated every frame whileisAnimationRunning()istrue. Use it to drive animations. SeesetAnimationRunning().vec4 data1,vec4 data2,vec4 data3,vec4 data4- Custom data exposed to the shader. Set these from C++ viasetData1(),setData2(),setData3(), andsetData4().float globalAlpha- The painter’s current global opacity. Fragment shaders should normally multiplyfragColorby this value.vec4 colorEffects- The active color effect parameters. Normally applied through applyColorEffects() rather than accessed directly.float fontAlphaMin,float fontAlphaMax- The signed distance field thresholds used when antialiasing glyphs.
Fragment Shader Helper Functions¶
The fragment shader include provides the following helper functions:
float clipMask()- Returns the clip (scissor) coverage, in the [0, 1] range, for the current fragment. MultiplyfragColorby this value to honor the painter’s clipping.float antialiasingAlpha()- Returns the antialiasing coverage, in the [0, 1] range, derived fromtexCoord. MultiplyfragColorby this value to get antialiased edges.float sdfFontAlphaRaw()- Returns the raw signed distance field value sampled fromfontTexattexCoord, without antialiasing. Applysmoothstep()manually as needed.float sdfFontAlpha()- Returns the glyph alpha sampled fromfontTexattexCoord, with the default antialiasing applied based onfontAlphaMinandfontAlphaMax.void applyColorEffects(inout vec4 color)- Applies the active contrast, brightness, and saturation effects tocolorin place.
A typical fragment shader computes
fragColor, multiplies it byglobalAlpha, optionally multiplies byclipMask()andantialiasingAlpha()to support clipping and antialiasing, and finally calls applyColorEffects().When text is involved,
sdfFontAlpha()should be taken into account too. For example:#version 440 QC_INCLUDE "customfrag.glsl" void main() { float a = 0.6 + 0.2 * sin(0.1 * fragCoord.x + 4.0 * iTime); vec4 color = vec4(a, a, a, 1.0); fragColor = sdfFontAlpha() * globalAlpha * color; applyColorEffects(fragColor); }
Adding the Shaders to the Project¶
Shaders that are used with
QCanvasCustomBrushmust always be added to the application project via the qt_add_custom_brush_shaders CMake function, provided by the Qt Canvas Painter package. This function performs additional preprocessing at build time before internally invoking the standardqt_add_shaders().For example:
qt_add_custom_brush_shaders(app "app_custombrush_shaders" PREFIX "/shaders" FILES brush1.frag )
Using the Brush¶
At run time, the generated
.qsbfile can be used for example like this:QCanvasCustomBrush customBrush(":/shaders/brush1.frag.qsb")); customBrush.setAnimationRunning(true); // iTime updates automatically // expose custom data to the shader in data1 customBrush.setData1(QVector4D(1.0, 2.0, 3.0, 4.0));
The
QCanvasCustomBrushcan then be used in a fill, for example:painter->setFillStyle(customBrush);
See qt_add_custom_brush_shaders for the details of the CMake function, and the Qt Shader Tools module documentation for working with cross-platform shader code in Qt.
Note
Shaders for custom brushes must always contain the
QC_INCLUDEstatement and must be added to the project via the qt_add_custom_brush_shaders CMake function. qt_add_shaders() is not suitable for custom brush shaders.Note
qt_add_custom_brush_shaders translates the shader code to the following targets: GLSL
300 es,150,130, HLSL5.0, and MSL1.2. There is currently no further configurability offered for this.See also
qt_add_custom_brush_shaders Qt-Canvas-Painter—Gallery-Example
- __init__()¶
Constructs a default custom brush.
- __init__(arg__1)
- Parameters:
arg__1 –
QCanvasCustomBrush
Move-constructs an instance of
QCanvasCustomBrush.- isAnimationRunning()¶
- Return type:
bool
Returns true if the time is running.
- __ne__(rhs)¶
- Parameters:
rhs –
QCanvasCustomBrush- Return type:
bool
- __eq__(rhs)¶
- Parameters:
rhs –
QCanvasCustomBrush- Return type:
bool
- setAnimationRunning(running)¶
- Parameters:
running – bool
Sets the time running state to
running. When this is true, the shader uniformiTimeis updated automatically, and can be used to get the current animation running time in the shader.The default value is
false.See also
Sets the uniform data1 value to
data. This allows setting custom data into shaders.Sets the uniform data2 value to
data. This allows setting custom data into shaders.Sets the uniform data3 value to
data. This allows setting custom data into shaders.Sets the uniform data4 value to
data. This allows setting custom data into shaders.Sets the custom brush to use
fragmentShader.- setFragmentShader(fragmentShader)
- Parameters:
fragmentShader – str
Sets the custom brush to use
fragmentShader. This must be path to a valid qsb file. The file can be a local file or embedded in the application via the The Qt Resource System.Sets the custom brush to use
vertexShader.- setVertexShader(vertexShader)
- Parameters:
vertexShader – str
Sets the custom brush to use
vertexShader. This must be path to a valid qsb file. The file can be a local file or embedded in the application via the The Qt Resource System.- swap(other)¶
- Parameters:
other –
QCanvasCustomBrush