---------------------------------------------------
Qt quick 2d shader effect
---------------------------------------------------
概念
着色器和普通的图像处理程序不同,它只针对一个点做运算,它包括:
vertext shader: 顶点着色器,主要用于处理位置,要求输出当前点的新位置。
fragment shader(pixel shader):片段着色器(像素着色器),主要用于处理色彩。要求输出当前点的新色彩。
系统会自动处理循环,并行运算多个点,自动处理图像边界
主要有两类Shader程序: GLSL(OpenGL提供), HLSL(DirectX提供)
注:好像时 5.6 版本后才支持directx,所以事情复杂了。现在Qt3D的shader就复杂无比. GLSL(OpenGL Shader Language)
参数修饰词
uniform : 表示每次运算时参数值不变的
varying : 表示每次运算时参数值时变动的。如当前点的纹理坐标
lowp : 表示低精度
highp : 表示高精度
预定义参数
uniform mat4 qt_Matrix : 变形矩阵- combined transformation matrix, the product of the matrices from the root item to this ShaderEffect, and an orthogonal projection.
uniform float qt_Opacity : 透明度- combined opacity, the product of the opacities from the root item to this ShaderEffect.
attribute vec4 qt_Vertex : 当前计算点的坐标
attribute vec2 qt_MultiTexCoord0 : 纹理坐标(输入的图片/item等)
输入参数类型映射
在ShaderEffect中定义的参数会自动映射到Shader里面去
bool, int, qreal -> bool, int, float
QColor -> vec4 : 注意色彩输入shader时会先做半透明预处理,如Qt.rgba(0.2, 0.6, 1.0, 0.5) 会转化为 vec4(0.1, 0.3, 0.5, 0.5)
QRect, QRectF -> vec4(x, y, w, h)
QPoint, QPointF, QSize, QSizeF -> vec2(x, y)
QVector3D -> vec3(x, y, z)
QVector4D -> vec4(x, y, w, w)
QTransform -> mat3
QMatrix4x4 -> mat4
QQuaternion -> vec4, scalar value is w.
Image -> sampler2D
ShaderEffectSource -> sampler2D
输出
vertext shader: 顶点着色器,主要用于处理位置,要求输出gl_Position参数。
fragment shader(pixel shader):片段着色器(像素着色器),主要用于处理色彩。要求输出gl_FragColor参数。
常用方法
lowp vect4 text = texture2D(src, qt_TexCoord0); // 取纹理色彩
abs/min/max/....
示例
ShaderEffect {
width: 200
height: 200
mesh: GridMesh { resolution: Qt.size(20, 20) }
property var source: Image {
source: "qt-logo.png"
sourceSize { width: 200; height: 200 }
}
vertexShader: "
uniform highp mat4 qt_Matrix;
attribute highp vec4 qt_Vertex;
attribute highp vec2 qt_MultiTexCoord0;
varying highp vec2 qt_TexCoord0;
uniform highp float width;
void main() {
highp vec4 pos = qt_Vertex;
highp float d = .5 * smoothstep(0., 1., qt_MultiTexCoord0.y);
pos.x = width * mix(d, 1.0 - d, qt_MultiTexCoord0.x);
gl_Position = qt_Matrix * pos;
qt_TexCoord0 = qt_MultiTexCoord0;
}"
} HLSL(Direct3D)
blablabla,有很多不同,
示例
Image { id: img; sourceSize { width:; height: 100 } source: "qt-logo.png" }
ShaderEffect {
width:; height: 100
property variant src: img
fragmentShader: "qrc:/effect_ps.cso"
}
cbuffer ConstantBuffer : register(b0)
{
float4x4 qt_Matrix;
float qt_Opacity;
};
Texture2D src : register(t0);
SamplerState srcSampler : register(s0);
float4 ExamplePixelShader(float4 position : SV_POSITION, float2 coord : TEXCOORD0) : SV_TARGET
{
float4 tex = src.Sample(srcSampler, coord);
float3 col = dot(tex.rgb, float3(0.344, 0.5, 0.156));
return float4(col, tex.a) * qt_Opacity;
} 跨平台的Shader写法
方法1(根据 GraphicsInfo.shaderType 判断):
Image { id: img; sourceSize { width:; height: 100 } source: "qt-logo.png" }
ShaderEffect {
width:; height: 100
property variant src: img
property variant color: Qt.vector3d(0.344, 0.5, 0.156)
fragmentShader:
GraphicsInfo.shaderType === GraphicsInfo.GLSL ?
"varying highp vec2 coord;
uniform sampler2D src;
uniform lowp float qt_Opacity;
void main() {
lowp vec4 tex = texture2D(src, coord);
gl_FragColor = vec4(vec3(dot(tex.rgb, vec3(0.344, 0.5, 0.156))), tex.a) * qt_Opacity;
}"
: GraphicsInfo.shaderType === GraphicsInfo.HLSL ?
"cbuffer ConstantBuffer : register(b0)
{
float4x4 qt_Matrix;
float qt_Opacity;
};
Texture2D src : register(t0);
SamplerState srcSampler : register(s0);
float4 ExamplePixelShader(float4 position : SV_POSITION, float2 coord : TEXCOORD0) : SV_TARGET
{
float4 tex = src.Sample(srcSampler, coord);
float3 col = dot(tex.rgb, float3(0.344, 0.5, 0.156));
return float4(col, tex.a) * qt_Opacity;
}"
: ""
}
方法2(自动选择):
Image { id: img; sourceSize { width:; height: 100 } source: "qt-logo.png" }
ShaderEffect {
width:; height: 100
property variant src: img
property variant color: Qt.vector3d(0.344, 0.5, 0.156)
fragmentShader: "qrc:shaders/effect.frag" // 系统会自动选择 shaders/effect.frag 或 shaders/+hlsl/effect.frag
} 示例
// 去色
ShaderEffect {
fragmentShader: "
uniform lowp sampler2D source; // this item
uniform lowp float qt_Opacity; // inherited opacity of this item
varying highp vec2 qt_TexCoord0;
void main() {
lowp vec4 p = texture2D(source, qt_TexCoord0);
lowp float g = dot(p.xyz, vec3(0.344, 0.5, 0.156));
gl_FragColor = vec4(g, g, g, p.a) * qt_Opacity;
}"
} // 图层混合
ShaderEffect {
property var colorSource: gradientRect;
fragmentShader: "
uniform lowp sampler2D colorSource;
uniform lowp sampler2D maskSource;
uniform lowp float qt_Opacity;
varying highp vec2 qt_TexCoord0;
void main() {
gl_FragColor =
texture2D(colorSource, qt_TexCoord0)
* texture2D(maskSource, qt_TexCoord0).a
* qt_Opacity;
}
"
} // 上下两边虚化
Flickable{
....
}
ShaderEffectSource {
id: flickableAreaSource
sourceItem: flickableArea
hideSource: true
visible: false
}
ShaderEffect {
property variant src: flickableAreaSource
anchors.fill: flickableArea
fragmentShader: "
varying highp vec2 qt_TexCoord0;
uniform lowp float qt_Opacity;
uniform sampler2D src;
void main() {
lowp vec4 tex = texture2D(src, qt_TexCoord0);
lowp float dist = abs(qt_TexCoord0.y-0.5)*4.0;
tex*= min(1.0, (2.0 - dist));
gl_FragColor = tex * qt_Opacity;
}"
}

Qt QML 2D shader的更多相关文章

  1. Qt qml 单例模式

    Qt qml 单例模式,没什么好说的,看代码吧.单例模式很适合做全局的配置文件. [示例下载] http://download.csdn.net/detail/surfsky/8539313 [以下是 ...

  2. Qt qml listview 列表视图控件(下拉刷新、上拉分页、滚动轴)

    Qt qml listview下拉刷新和上拉分页主要根据contentY来判断.但要加上顶部下拉指示器.滚动条,并封装成可简单调用的组件,着实花了我不少精力:) [先看效果]    [功能] 下拉刷新 ...

  3. qt qml qchart 图表组件

    qt qml qchart 图表组件 * Author: Julien Wintz * Created: Thu Feb 13 23:41:59 2014 (+0100) 这玩意是从chart.js迁 ...

  4. qt qml中PropertyAnimation的几种使用方法

    qml文章 qt qml中PropertyAnimation的几种使用方法 动画应用场景有以下几种: 首先如果一个Rectangle.动画是要改变它的x和y值 1,Rectangle一旦被创建,就要移 ...

  5. Qt QML referenceexamples attached Demo hacking

    /********************************************************************************************* * Qt ...

  6. Qt qml的软件架构设计

    google: qt qml application architecture 有很多资源. 1 https://www.ics.com/blog/multilayered-architecture- ...

  7. qt qml 刮刮卡效果

    用canvas+mouseArea实现的刮刮卡效果. 表层是一层色彩,用手指划开,可看到下面的文字Lisence: MIT, 请保留本文档说明Author: surfsky.cnblogs.com 2 ...

  8. QT QML目录导航列表视图

    [功能] /目录.文件 /文件过滤 /递归 /事件 /高亮当前行 /当前选项 /目录切换动画 /限制根目录 [下载]:http://download.csdn.net/detail/surfsky/8 ...

  9. Qt中2D绘图问题总结(一)----------基本的绘制与填充

    刚刚开始学习Qt不久,才开始渐渐地熟悉基础内容,学习过程中的一些知识的总结和感悟希望通过博客记录下来,与大家分享学习的同时,也是对自己坚持下去的鞭策,废话不多说了,开始第一次的小总结吧. Qt提供了强 ...

随机推荐

  1. PHP 根据子ID递归获取父级ID,实现逐级分类导航效果

    代码: //当前路径 $cate=M('wangpan_class')->select(); function get_top_parentid($cate,$id){ $arr=array() ...

  2. helm-chart7,调试与hook

    调试 几个命令可以帮助进行调试 helm lint 首选工具,返回错误和警告信息. helm install --dry-run --debug:服务器会渲染你的模板,然后返回结果清单文件. helm ...

  3. 循环结构while

    Note:高能:语句结构都是由关键字开头,用冒号结束! 一:语句结构        while 判断条件:            语句  二:基本规则 (1)使用缩进来划分语句块,相同缩进数的语句在一 ...

  4. 华大单片机开发板HC32L13X上手入门

    HC32L136开发板(如下图所示)分为板载调试模块(左半部分)和MCU开发电路(右半部分).二者中间通过邮票孔相连,如果将板子从中间掰开,板载调试模块就可以当一个CMSIS-DAP的仿真器来使用.此 ...

  5. 邮局 100分代码(dfs+多重剪枝)

    蓝桥杯真题-邮局 #include<iostream> #include<algorithm> #include<set> #include<string&g ...

  6. Java 通过getbean取出的类为什么要强转为接口类

    这个问题是之前一个同学问我的,这些是我在网上找到的资料,由于我自己也没有完全搞明白,先大概记录一下 首先问题是为什么在bean文件中注入的是实现类,但是通过getBean()取出的时候却必须强制转化为 ...

  7. centos7 部署vnc

    不做过多介绍了,下面直接记录下centos7系统下安装配置vncserver的操作记录 0)更改为启动桌面或命令行模式 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 1 ...

  8. 树莓派3 之 启动显示和wifi相关参数设置

    最近将树莓派操作系统重新安装了,然后发现了一些问题.这里分享出来给大家 问题一:连接外置显示器黑屏 解决方法:将SD卡 插入电脑,在电脑中找到SD卡 修改其中的config.txt文件 #强制使用HD ...

  9. centos7内核升级及curl访问https证书过期处理

    centos7内核升级及curl访问https证书过期处理 先看下当前系统的linux内核版本 uname -r 3.10.0-229.el7.x86_64 升级步骤 1.rpm --import h ...

  10. vue-cli3快速原型开发

    先来讲一下,什么是快速原型开发. 当我们需要紧急或提前开发单独的一个页面时,有时候不需要在原项目中创建一个页面,再开发,我们可以单独的区开发这个项目,那么怎样单独的区开发这个项目呢,之前使用过vue- ...