ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

保存一段qml里使用opengl shader的代码,由于多平台原因暂时用不了

2022-08-28 16:04:41  阅读:177  来源: 互联网

标签:Qt parent opengl point ctx shader height colors qml


                            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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有