我正在尝试在图像内制作一个可选择的矩形。
Image {
id: image
Rectangle { //full-transparent like a viewport
id: rect
}
}
看起来就像使用屏幕截图,您选择的区域是全透明的,而其余区域是半透明的(或模糊)。
我发现opacitymask有点相似,但我希望该区域的其余部分显示为半透明的,而不仅仅是白色。
该项目的完整代码:https://github.com/arkceajin/QtDemos/tree/master/CropBox
答案 0 :(得分:1)
在这些情况下,您必须使用ShaderEffect:
import QtQuick 2.9
import QtQuick.Window 2.2
Window {
visible: true
width: 640
height: 480
title: qsTr("ShaderEffect Example")
Image {
id: image
source: "qrc:/dog.png"
anchors.centerIn: parent
layer.enabled: true
layer.effect: ShaderEffect {
property real alpha: 0.4
property rect sourceRect: Qt.rect(width/4, height/4, width/2, height/2)
property real xStep: 1/width
property real yStep: 1/height
fragmentShader: "
uniform lowp sampler2D source;
varying mediump vec2 qt_TexCoord0;
uniform lowp float qt_Opacity;
uniform highp float alpha;
uniform highp vec4 sourceRect;
uniform highp float xStep;
uniform highp float yStep;
bool insideBox(vec2 topLeft, vec2 bottomRight, vec2 point){
vec2 s = step(topLeft, point) - step(bottomRight, point);
return (s.x * s.y) > 0.0;
}
void main() {
vec2 topLeft = vec2(sourceRect.x*xStep, sourceRect.y*yStep);
vec2 bottomRight = topLeft + vec2(sourceRect.z*xStep, sourceRect.w*yStep);
gl_FragColor = texture2D(source, qt_TexCoord0) * qt_Opacity;
if(!insideBox(topLeft, bottomRight, qt_TexCoord0))
gl_FragColor *= alpha;
}
"
}
}
}
输出: