qt qml:水平淡入图像(即从左到右…不是整个图像)

qt qml:水平淡入图像(即从左到右…不是整个图像),qt,qml,Qt,Qml,我使用PathLine一次水平显示3个图像。 左右图像需要显示为从左到右50%褪色和从右到50%褪色。 在这里,我不能使用黑色矩形渐变来显示淡入效果,因为我的父对象是20%透明的&它在背景视频上浮动 所以,有什么方法可以将“不透明度”属性应用为水平淡入动画吗?使用,您可以使用着色器程序向QML项目添加后期效果。这使您能够在GPU上执行一些图形效果 正如您在我上面链接的文档中看到的,例如,这可能是波浪效应。通常,对每个输出像素应用一个小“程序”。这就是GLSL中所谓的片段着色器 为了让您了解如何在

我使用PathLine一次水平显示3个图像。 左右图像需要显示为从左到右50%褪色和从右到50%褪色。 在这里,我不能使用黑色矩形渐变来显示淡入效果,因为我的父对象是20%透明的&它在背景视频上浮动

所以,有什么方法可以将“不透明度”属性应用为水平淡入动画吗?

使用,您可以使用着色器程序向QML项目添加后期效果。这使您能够在GPU上执行一些图形效果

正如您在我上面链接的文档中看到的,例如,这可能是波浪效应。通常,对每个输出像素应用一个小“程序”。这就是GLSL中所谓的片段着色器

为了让您了解如何在项目上实现alpha遮罩(这可能是您的场景,其中包含正在设置动画的路径上的图像),我将给出一个示例,其中我实现了一个简单的线性渐变作为alpha遮罩,其中左侧的不透明度为零,右侧为完全不透明度

import QtQuick 1.0
import Qt.labs.shaders 1.0

Item {
    width: 300
    height: 300

    id: wrapper

    Item {
        id: scene
        anchors.fill: parent
        // Here is your scene with the images ...
    }

    ShaderEffectItem {
        anchors.fill: wrapper

        // Any property you add to the ShaderEffectItem is accessible as a
        // "uniform" value in the shader program. See GLSL doc for details.
        // Essentially, this is a value you pass to the fragment shader,
        // which is the same for every pixel, thus its name.

        // Here we add a source item (the scene) as a uniform value. Within the
        // shader, we can access it as a sampler2D which is the type used to
        // access pixels in a texture. So the source item becomes a texture.
        property variant source: ShaderEffectSource
        {
            sourceItem: scene // The item you want to apply the effect to
            hideSource: true  // Only show the modified item, not the original
        }

        // This is the fragment shader code in GLSL (GL Shading Language)
        fragmentShader: "
        varying highp vec2 qt_TexCoord0;  // The coords within the source item
        uniform sampler2D source;         // The source item texture
        void main(void)
        {
            // Read the source color of the pixel
            vec4 sourceColor = texture2D(source, qt_TexCoord0);

            // The alpha value of the mask
            float alpha = qt_TexCoord0.x; // = 0.0 at left, 1.0 at right border

            // Multiply the alpha mask on the color to be drawn:
            sourceColor *= alpha;

            // Write the pixel to the output image
            gl_FragColor = sourceColor;
        }
        "
    }
}
着色器程序中最重要的一行是
alpha
变量的值。在这个小示例中,我只是将纹理坐标的X分量设置为alpha值,因此在左边界处为0,在右边界处为1。请注意,纹理坐标不是以像素为单位测量的,而是在[0..1]范围内测量的。如果要访问像素坐标,可以使用
gl\u FragCoord.x
/
y
。请注意,
y
是从下到上测量的,因此0是底部边框,
height
是顶部边框。默认情况下,您无法访问整个生成图像的
高度
,但因此我们可以使用制服。只需指定一个新的
属性inth:height
,并使用着色器中的
统一浮点h
访问它。

使用,您可以使用着色器程序向QML项目添加后期效果。这使您能够在GPU上执行一些图形效果

正如您在我上面链接的文档中看到的,例如,这可能是波浪效应。通常,对每个输出像素应用一个小“程序”。这就是GLSL中所谓的片段着色器

为了让您了解如何在项目上实现alpha遮罩(这可能是您的场景,其中包含正在设置动画的路径上的图像),我将给出一个示例,其中我实现了一个简单的线性渐变作为alpha遮罩,其中左侧的不透明度为零,右侧为完全不透明度

import QtQuick 1.0
import Qt.labs.shaders 1.0

Item {
    width: 300
    height: 300

    id: wrapper

    Item {
        id: scene
        anchors.fill: parent
        // Here is your scene with the images ...
    }

    ShaderEffectItem {
        anchors.fill: wrapper

        // Any property you add to the ShaderEffectItem is accessible as a
        // "uniform" value in the shader program. See GLSL doc for details.
        // Essentially, this is a value you pass to the fragment shader,
        // which is the same for every pixel, thus its name.

        // Here we add a source item (the scene) as a uniform value. Within the
        // shader, we can access it as a sampler2D which is the type used to
        // access pixels in a texture. So the source item becomes a texture.
        property variant source: ShaderEffectSource
        {
            sourceItem: scene // The item you want to apply the effect to
            hideSource: true  // Only show the modified item, not the original
        }

        // This is the fragment shader code in GLSL (GL Shading Language)
        fragmentShader: "
        varying highp vec2 qt_TexCoord0;  // The coords within the source item
        uniform sampler2D source;         // The source item texture
        void main(void)
        {
            // Read the source color of the pixel
            vec4 sourceColor = texture2D(source, qt_TexCoord0);

            // The alpha value of the mask
            float alpha = qt_TexCoord0.x; // = 0.0 at left, 1.0 at right border

            // Multiply the alpha mask on the color to be drawn:
            sourceColor *= alpha;

            // Write the pixel to the output image
            gl_FragColor = sourceColor;
        }
        "
    }
}

着色器程序中最重要的一行是
alpha
变量的值。在这个小示例中,我只是将纹理坐标的X分量设置为alpha值,因此在左边界处为0,在右边界处为1。请注意,纹理坐标不是以像素为单位测量的,而是在[0..1]范围内测量的。如果要访问像素坐标,可以使用
gl\u FragCoord.x
/
y
。请注意,
y
是从下到上测量的,因此0是底部边框,
height
是顶部边框。默认情况下,您无法访问整个生成图像的
高度
,但因此我们可以使用制服。只需指定一个新的
属性inth:height
,并使用着色器中的
统一浮点h
访问它。

我认为您必须使用着色器效果来执行此操作。。。您使用的是QML1(Qt4)还是QML2(Qt5)?m使用的是Qt4.7。我已经看过ShaderEffects示例,但不明白如何将其用于我的目的。我只需要图像在接近Pathlines边缘时随着动画逐渐淡出,所以基本上需要的是每像素不透明度。我想知道这对整个场景来说是一样的(所以你只是在整个场景的边缘有一个淡出),还是你想对单个图像应用这样的alpha遮罩。你能试着画一个草图,或者更具体地描述一下动画是如何工作的吗?我不明白的是:你说你有一个淡入淡出的动画。但是我认为你想要沿着一条路径移动图像,最左和最右的图像在离开场景时逐渐消失。因此,基本上你希望场景边界平滑?是的,你会得到它。。事实上,我没有10+的声誉,所以不允许我附加图像。。我可以将alpha遮罩(黑色到透明渐变)应用于边缘的整个父对象。。但是,由于我的父母被派去观看背景视频(即使菜单滑出,我也要看背景视频),所以我无法遮掩。。它将在我的菜单中显示黑色补丁,而不是不透明度为0.6的完整黑色背景。关于淡入淡出动画。。。只是当图像从中心向右移动时,我想从左向右逐渐降低不透明度。。好像它接近一个黑色的梯度马斯基认为你必须这样做使用着色器效果。。。您使用的是QML1(Qt4)还是QML2(Qt5)?m使用的是Qt4.7。我已经看过ShaderEffects示例,但不明白如何将其用于我的目的。我只需要图像在接近Pathlines边缘时随着动画逐渐淡出,所以基本上需要的是每像素不透明度。我想知道这对整个场景来说是一样的(所以你只是在整个场景的边缘有一个淡出),还是你想对单个图像应用这样的alpha遮罩。你能试着画一个草图,或者更具体地描述一下动画是如何工作的吗?我不明白的是:你说你有一个淡入淡出的动画。但是我认为你想要沿着一条路径移动图像,最左和最右的图像在离开场景时逐渐消失。因此,基本上你希望场景边界平滑?是的,你会得到它。。事实上,我没有10+的声誉,所以不允许我附加图像。。我可以将alpha遮罩(黑色到透明渐变)应用于边缘的整个父对象。。但由于我的父母被派去观看背景视频,即使