使用 ShaderEffectItem 的奇怪 alpha 混合结果 [英] Strange alpha blending results with ShaderEffectItem

查看:24
本文介绍了使用 ShaderEffectItem 的奇怪 alpha 混合结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 ShaderEffectItem 在 QML 项目上应用简单的 alpha 蒙版.

I'm trying to apply a simple alpha mask on a QML item using ShaderEffectItem.

这是一个最小(非)工作示例:我有一个从红到白的渐变作为背景,并想在其上绘制一个绿色的 200x200 正方形.这个正方形的 alpha 蒙版在左边应该是 0.0,在右边框应该是 1.0,所以它应该在左边框是透明的.

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. 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.

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;
        }
        "
    }
}

我希望得到以下输出(使用 GIMP 绘制):

I expected the following output (drawn with GIMP):

但这实际上是显示出来的:

But this is actually shown:

我做错了什么?

我正在使用 qmlviewer (Qt 4.8.2) 显示带有 -opengl 选项的 QML 文件,以启用着色器效果.

I'm using qmlviewer (Qt 4.8.2) to display the QML file with the -opengl option in order to enable the shader effect.

也许这与 这种奇怪的行为与 QGLFramebufferObjects 上的 alpha 混合有关 几周前我发现...

Maybe this is related to this strange behavior with alpha blending on QGLFramebufferObjects I found some weeks ago...

推荐答案

尝试修改fragment shader的main函数为:

Try modifying the main function of the fragment shader to :

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

这篇关于使用 ShaderEffectItem 的奇怪 alpha 混合结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆