简体   繁体   English

使用ShaderEffectItem进行奇怪的alpha混合结果

[英]Strange alpha blending results with ShaderEffectItem

I'm trying to apply a simple alpha mask on a QML item using ShaderEffectItem . 我正在尝试使用ShaderEffectItem在QML项目上应用简单的alpha蒙ShaderEffectItem

Here is a minimal (non-)working example: I have a red-to-white gradient as the background and want to draw a green 200x200 square on top of it. 这是一个最小的(非)工作示例:我有一个红色到白色的渐变作为背景,并希望在它上面绘制一个绿色的200x200方形。 The alpha mask for this square should be 0.0 at the left and 1.0 at the right border, so it should be transparent at the left border. 此方块的alpha蒙版应该在左侧为0.0,在右边框为1.0,因此在左边框应该是透明的。

import QtQuick 1.1
import Qt.labs.shaders 1.0

Rectangle {
    width: 300
    height: 300

    id: wrapper

    gradient: Gradient {
        GradientStop { position: 0.0; color: "red" }
        GradientStop { position: 1.0; color: "white" }
    }

    Rectangle {
        id: square
        anchors.centerIn: parent
        width: 200
        height: 200
        color: "green"
    }

    ShaderEffectItem {
        anchors.centerIn: parent
        width: 200
        height: 200

        property variant source: ShaderEffectSource {
            sourceItem: square
            hideSource: true
        }

        fragmentShader: "
        varying vec2 qt_TexCoord0;
        uniform sampler2D source;
        void main(void)
        {
            vec4 sourceColor = texture2D(source, qt_TexCoord0);
            float alpha = qt_TexCoord0.x; // = 0.0 at left, 1.0 at right border
            sourceColor.a *= alpha;       // apply alpha mask
            gl_FragColor = sourceColor;
        }
        "
    }
}

I expected the following output (drawn with GIMP): 我期望以下输出(使用GIMP绘制):

预期结果

But this is actually shown: 但这实际上显示了:

实际结果

What am I doing wrong? 我究竟做错了什么?

I'm using qmlviewer (Qt 4.8.2) to display the QML file with the -opengl option in order to enable the shader effect. 我正在使用qmlviewer (Qt 4.8.2)来显示带有-opengl选项的QML文件,以便启用着色器效果。

Maybe this is related to this strange behavior with alpha blending on QGLFramebufferObjects I found some weeks ago... 也许这与我在几周前发现的QGLFramebufferObjects上的alpha混合这种奇怪的行为有关 ...

Try modifying the main function of the fragment shader to : 尝试将片段着色器的主要功能修改为:

    void main(void)
    {
        gl_FragColor = texture2D(source, qt_TexCoord0).rgba*qt_TexCoord0.x;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM