Qt6 QML Book/图形效果/着色器元素

Shader Elements

着色器元素

For programming shaders, Qt Quick provides two elements. The ShaderEffectSource and the ShaderEffect. The shader effect applies custom shaders and the shader effect source renders a QML item into a texture and renders it. As shader effect can apply custom shaders to its rectangular shape and can use sources for the shader operation. A source can be an image, which is used as a texture or a shader effect source.

对于编程着色器,Qt Quick提供了两个元素类型。ShaderEffectSource和ShaderEffect。着色器效果应用自定义着色器,着色器效果源将QML项目渲染到纹理中并进行渲染。着色器效果可以将自定义着色器应用于其矩形形状,并可以使用源进行着色器操作。源可以是用作纹理或着色器效果源的图像。

The default shader uses the source and renders it unmodified. Below, we first see the QML file with two ShaderEffect elements. One without any shaders specified, and one explicitly specifying default vertex and fragment shaders. We will look at the shaders shortly.

默认着色器使用源并未经修改地渲染它。下面,我们首先看到带有两个ShaderEffect元素类型的QML文件。一个未指定任何着色器,另一个显式指定默认顶点和片段着色器。我们将很快查看着色器。

import QtQuick

Rectangle {
    width: 480; height: 240
    color: '#1e1e1e'

    Row {
        anchors.centerIn: parent
        spacing: 20
        Image {
            id: sourceImage
            width: 80; height: width
            source: '../assets/tulips.jpg'
        }
        ShaderEffect {
            id: effect
            width: 80; height: width
            property variant source: sourceImage
        }
        ShaderEffect {
            id: effect2
            width: 80; height: width
            property variant source: sourceImage
            vertexShader: "default.vert.qsb"
            fragmentShader: "default.frag.qsb"
        }
    }
}

In the above example, we have a row of 3 images. The first is the real image. The second is rendered using the default shader and the third is rendered using the shader code for the fragment and vertex as shown below. Let's have a look at the shaders.

在上面的例子中,我们有一行3张图片。第一个是真实的图像。第二个渲染使用默认着色器,第三个渲染使用片段和顶点的着色器代码,如下所示。让我们看看着色器。

The vertex shader takes the texture coordinate, qt_MultiTexCoord0, and propagates it to the qt_TexCoord0 variable. It also takes the qt_Vertex position and multiplies it with Qt's transformation matrix, ubuf.qt_Matrix, and returns it through the gl_Position variable. This leaves the texture and vertex position on the screen unmodified.

顶点着色器获取纹理坐标qt_MultiTexCoord0,并将其传播到qt_TexCoord0变量。它还获取qt_Vertex位置,并将其与qt的变换矩阵ubuf.qt_Matrix相乘,并通过gl_Position返回它。这会使屏幕上的纹理和顶点位置保持不变。

#version 440

layout(location=0) in vec4 qt_Vertex;
layout(location=1) in vec2 qt_MultiTexCoord0;

layout(location=0) out vec2 qt_TexCoord0;

layout(std140, binding=0) uniform buf {
    mat4 qt_Matrix;
    float qt_Opacity;
} ubuf;

out gl_PerVertex { 
    vec4 gl_Position;
};

void main() {
    qt_TexCoord0 = qt_MultiTexCoord0;
    gl_Position = ubuf.qt_Matrix * qt_Vertex;
}

The fragment shader takes the texture from the source 2D sampler, i.e. the texture, at the coordinate qt_TexCoord0 and multiplies it with the Qt opacity, ubuf.qt_Opacity to calculate the fragColor which is the color to be used for the pixel.

片段着色器在坐标qt_TexCoord0处从source2D采样器获取纹理,即纹理,并将其与qt不透明度ubuf.qt_Opacity相乘。用于计算fragColor,它是用于像素的颜色。

#version 440

layout(location=0) in vec2 qt_TexCoord0;

layout(location=0) out vec4 fragColor;

layout(std140, binding=0) uniform buf {
    mat4 qt_Matrix;
    float qt_Opacity;
} ubuf;

layout(binding=1) uniform sampler2D source;

void main() {
    fragColor = texture(source, qt_TexCoord0) * ubuf.qt_Opacity;
}

Notice that these two shaders can serve as the boilerplate code for your own shaders. The variables, locations and bindings, are what Qt expects. You can read more about the exact details of this on the Shader Effect Documentation.

​请注意,这两个着色器可以作为您自己的着色器的样板代码。变量、位置和绑定是Qt所期望的。你可以在Shader Effect文档中阅读更多关于这方面的详细信息。

Before we can use the shaders, they need to be baked. If the shaders are a part of a larger Qt project and included as resources, this can be automated. However, when working with the shaders and a qml-file, we need to explicitly bake them by hand. This is done using the following two commands:

在我们使用着色器之前,它们需要烘焙。如果着色器是更大的Qt项目的一部分,并作为资源包含,则可以自动执行此操作。但是,在使用着色器和qml文件时,我们需要手动显式烘焙它们。这是使用以下两个命令完成的:

qsb --glsl 100es,120,150 --hlsl 50 --msl 12    -o default.frag.qsb default.frag 
qsb --glsl 100es,120,150 --hlsl 50 --msl 12 -b -o default.vert.qsb default.vert

The qsb tool is located in the bin directory of your Qt 6 installation.

qsb工具位于Qt 6安装的bin目录中。

TIP

If you don’t want to see the source image and only the effected image you can set the Image to invisible (`` visible: false``). The shader effects will still use the image data just the Image element will not be rendered.

如果不想看到源图像,而只想看到受影响的图像,可以将图像设置为“不可见”(`visible:false`)。着色器效果仍将使用图像数据,只是图像元素不会被渲染。

In the next examples, we will be playing around with some simple shader mechanics. First, we concentrate on the fragment shader and then we will come back to the vertex shader.

 在接下来的示例中,我们将介绍一些简单的着色器机制。首先,我们关注片段着色器,然后我们将回到顶点着色器。

示例源码下载

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Qt6 QML实时开发是基于Qt6平台的快速开发框架,它利用Qt Quick语言和QML技术,为开发人员提供了设计和开发图形用户界面(GUI)所需的各种工具。QML是一种描述用户界面的语言,借助它可以轻松地创建各种精美的用户界面。 Qt6 QML实时开发的主要优点是其实时性和高效性。它可以让开发人员在软件开发过程中实时预览结果,使开发效率大大提高。同时,Qt6 QML实时开发还支持快速迭代和动态调整,便于开发人员随时调整和修改界面,同时保证了UI的高度灵活性和可扩展性。 Qt6 QML实时开发还提供了丰富的控件和组件库,这些库都是经过优化和测试的,能够让开发人员快速构建和布局GUI和用户交互界面。此外,Qt6 QML实时开发还支持跨平台开发,可用于开发Android、iOS、Windows、macOS和Linux等平台的应用程序。 综上所述,Qt6 QML实时开发是一种快速、高效、灵活的开发框架,在图形用户界面开发中应用广泛,也是未来GUI开发的一个趋势。它不仅可以大大提高开发效率,同时还提供了丰富的控件和组件,支持跨平台开发,是一款非常有价值的开发工具。 ### 回答2: Qt6 QML是一个开发桌面和移动应用程序的框架,其实时开发方案也得到了广泛的支持和认可。Qt6 QML通过其强大的功能和易于使用的界面设计,使开发人员可以更加方便和快速地开发出高质量的应用程序。 在Qt6 QML实时开发中,其主要特点包括: 1. 设计驱动型开发 Qt6 QML采用的是设计驱动型开发,开发人员可以直接在设计界面中进行开发与调试,而不需要手动写代码。这种开发方式可以大大提高开发效率,同时也可以有效降低错误发生的概率。 2. 实时预览界面效果 Qt6 QML具有实时预览界面效果的功能,这意味着开发人员可以在编写代码的同时观察到其效果,从而快速定位和修复问题。这种实时预览的功能也能够提高开发效率,并且可以让开发过程更加享受。 3. 基于组件的开发 Qt6 QML支持基于组件的开发,可以通过在不同的组件之间进行组合,以构建更高级的组件和应用程序。这种基于组件的开发方式,可以大大提高代码的可重用性,减少代码的冗余度。 综上所述,Qt6 QML实时开发是一个高效、方便和灵活的开发工具,可以使开发人员更加轻松和快速地开发出高质量的应用程序。同时,其具有丰富的功能和易于使用的界面设计,也可以让开发过程更加有趣和愉悦。 ### 回答3: Qt6 QML实时开发,指的是使用Qt6和QML技术进行实时开发的过程。Qt6是一种跨平台的应用程序框架,它可以帮助开发者快速创建高性能、现代化的应用程序,而QML则是一种基于JavaScript的用户界面语言,可以帮助开发者快速构建交互式的用户界面。因此,Qt6 QML实时开发可用于开发实时交互应用程序,如数据可视化、游戏、嵌入式系统等领域。 在Qt6 QML实时开发中,开发者可以使用Qt Creator等集成开发环境(IDE)轻松创建QML应用程序。QML语言支持丰富的界面元素和动画效果,使得界面设计非常灵活。此外,Qt6提供了丰富的C++类库和工具,方便开发者实现高性能的后台逻辑和各种设备的接口。 Qt6 QML实时开发的优势在于快速迭代。开发者可以使用实时预览功能,即在编辑代码时即时看到修改后的效果,从而提高开发效率和设计灵活性。此外,由于Qt6和QML都为开发者提供了丰富的功能和现成的库文件,因此可以极大地减少开发时间和成本。 总之,Qt6 QML实时开发是一种高效的技术和方法,可用于实现高性能、现代化的应用程序,为开发者提供快速迭代和灵活性,是目前最流行的开发方式之一。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值