Use a shader to hide part of an image
-
Greetings !
I'm using Qt 5.15, and I would like to hide part of an image. Basically, cutting out a circle at given coordinates. And I've seen some puzzling things while trying to achieve that.Firstly, I thought that shaders were actually allowing me to modify an image before drawing it. But it seems that the shader actually draws on top o the existing image. Is that so ?
Here's my shader so far:
ShaderEffect { property variant source property point position property real radius: 40 property real centerX: position.x - parent.x property real centerY: position.y - parent.y anchors.fill: parent vertexShader: " uniform highp mat4 qt_Matrix; attribute highp vec4 qt_Vertex; attribute highp vec2 qt_MultiTexCoord0; varying highp vec2 coord; void main() { coord = qt_MultiTexCoord0; gl_Position = qt_Matrix * qt_Vertex; }" fragmentShader: " varying highp vec2 coord; uniform sampler2D source; uniform lowp float qt_Opacity; uniform lowp float centerX; uniform lowp float centerY; uniform lowp float radius; uniform lowp float width; uniform lowp float height; void main() { lowp vec4 tex = texture2D(source, coord); if (coord.x * width >= centerX - radius && coord.x * width <= centerX + radius && coord.y * height >= centerY - radius && coord.y * height <= centerY + radius) { gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0); } else { gl_FragColor = tex * qt_Opacity; } }" }
Currently, it draws a black rectangle where I want it to.
If I setgl_FragColor
tovec4(0.0, 0.0, 0.0, 0.0)
, then it just does nothing... while I was expecting it to hide the part of thesource
image where the black rectangle appears in my test case.Is there any obvious reason why it's not working ? Am I wrong in assuming the shader changes the pixel of the source image ?
Thanks !
-
Ok, I figured it out !
The shader does not change the Image, it just draws something else on the shader item's position.
The solution was pretty darn simple: make the Image invisible and let the shadereffect item handle all the rendering part.