保存一段qml里使用opengl shader的代码,由于多平台原因暂时用不了
作者:互联网
RowLayout {
Layout.fillWidth: true
height: 60
Rectangle {
Layout.margins: 5
radius: 5
width: 40
height: width
Layout.alignment: Qt.AlignVCenter | Qt.AlignLeft
clip: true
layer.enabled: true
layer.effect: ShaderEffect {
readonly property var dataTex: dataCanvas
readonly property real count: dataCanvas.colors.length
fragmentShader: "
varying highp vec2 qt_TexCoord0;
uniform lowp float qt_Opacity;
uniform sampler2D dataTex;
const uniform lowp int count;
void main() {
vec3 col = 0.0;
for (int i = 0; i < count; i++) {
vec4 color = texture2D(dataTex, vec2(i / (float)count + 0.01, 0.0));
vec2 point = texture2D(dataTex, vec2(i / (float)count + 0.01, 1.0)).rg;
float dist = distance(qt_TexCoord0, point);
col += color * (1.0 - dist);
}
gl_FragColor = vec4(col, 1.0);
}
"
}
// Because ShaderEffect doesn't allow passing arrays to shader, we will
// convert arrays to graphical representation and pass them as texture.
Canvas {
id: dataCanvas
readonly property var colors: ["green", "yellow", "cyan", "red"]
readonly property var positions: [Qt.point(
0, 0), Qt.point(parent.width,
0), Qt.point(
0, parent.height), Qt.point(
parent.width, parent.height)]
height: 2
width: colors.length
antialiasing: false
visible: false
onPaint: {
if (colors.length !== positions.length) {
console.error(
"Array size of 'colors' doesn't equal array size of 'positions'")
return
}
var ctx = getContext("2d")
ctx.reset()
ctx.lineWidth = 1
for (var i = 0; i < colors.length; i++) {
ctx.beginPath()
ctx.strokeStyle = colors[i]
ctx.moveTo(i, 0)
ctx.lineTo(i + 1, 0)
ctx.stroke()
ctx.closePath()
ctx.beginPath()
ctx.strokeStyle = Qt.rgba(
positions[i].x / parent.width,
positions[i].y / parent.height,
0, 1)
ctx.moveTo(i, 1)
ctx.lineTo(i + 1, 1)
ctx.stroke()
ctx.closePath()
}
}
}
}
标签:Qt,parent,opengl,point,ctx,shader,height,colors,qml 来源: https://www.cnblogs.com/nocanstillbb/p/16632908.html