【问题标题】:Qt3d svg or png Texture with opacity=0 QTextureMaterial on QPlaneMeshQPlaneMesh 上不透明度 = 0 QTextureMaterial 的 Qt3d svg 或 png 纹理
【发布时间】:2020-07-17 17:11:34
【问题描述】:

我尝试将包含 opacity=0 区域的 svg 图像添加到 QPlaneMesh 上的 QTextureMaterial 中,但它显示平面的背景始终为灰色。

我希望飞机可以控制我的图像并穿透到 opacity=0 区域中的其他对象。

https://image.flaticon.com/icons/svg/3154/3154348.svg这样的svg文件。

代码:

// Background
        Qt3DCore::QEntity *planeEntity = new Qt3DCore::QEntity(rootEntity);
        Qt3DExtras::QPlaneMesh *planeMesh = new Qt3DExtras::QPlaneMesh(planeEntity);
        planeMesh->setHeight(2);
        planeMesh->setWidth(2);

        Qt3DExtras::QTextureMaterial *planeMaterial = new Qt3DExtras::QTextureMaterial(planeEntity);
        Qt3DRender::QTexture2D *planeTexture = new Qt3DRender::QTexture2D(planeMaterial);
        FlippedTextureImage *planeTextureImage = new FlippedTextureImage(planeTexture);
        planeTextureImage->setSize(QSize(3507, 3000));
        planeTexture->addTextureImage(planeTextureImage);
        planeMaterial->setTexture(planeTexture);

        Qt3DCore::QTransform *planeTransform = new Qt3DCore::QTransform(planeEntity);
        planeTransform->setRotationX(90);
        planeTransform->setTranslation(QVector3D(0, 0, 0));

        Qt3DExtras::QPhongAlphaMaterial *pam2 = new Qt3DExtras::QPhongAlphaMaterial(planeEntity);
        pam2->setAlpha(0);

        planeEntity->addComponent(planeMesh);
        planeEntity->addComponent(pam2);
        planeEntity->addComponent(planeMaterial);
        planeEntity->addComponent(planeTransform);
class FlippedTextureImage : public Qt3DRender::QPaintedTextureImage
{
public:
    FlippedTextureImage(Qt3DCore::QNode *parent = Q_NULLPTR):Qt3DRender::QPaintedTextureImage(parent) {}
    void paint(QPainter *painter) override {
        QSvgRenderer renderer(QString(qApp->applicationDirPath() + "/gift.svg"));
        QImage image(3000, 3000, QImage::Format_RGBA64);  // 512x512 RGBA
        image.fill(0x00ffffff);                           // white background
        QPainter painter2(&image);
        renderer.render(&painter2);
        painter->drawImage(0, 0, image);
    }
};

and run code like this

【问题讨论】:

    标签: svg png textures opacity qt3d


    【解决方案1】:

    QTextureMaterial 不支持纹理中的透明度。

    查看my answer to this question,了解如何自己在纹理中实现透明度。 Qt3D 中没有开箱即用的解决方案。

    【讨论】:

    • 我使用planeMaterial->setAlphaBlendingEnabled(true);,它显示了透明度。 like this 但是如果不是最后添加的订单,它会被其他对象覆盖。我可以让它总是按最后的顺序绘制吗?
    • 哇,你说得对,它确实支持透明度。我只是想知道为什么我没有在我的回答中使用该功能使其工作...要解决您的其他问题,请在您的框架图中添加 QDepthTest 以启用深度测试。这样一切都应该正确绘制。
    • oh~~ 在 FlippedTextureImage 中,我添加了painter->setCompositionMode(QPainter::CompositionMode_SourceIn);,我找到了其他人的解决方案。但是这个帖子没有更新。对不起。请问你如何使用QDepthTest 像一些例子?我的代码是 Qt 3D simple-cpp
    • 你能发布一个包含代码的新问题吗?没有评论来解决进一步的问题。我认为说您应该使用QDepthTest 会很快解决您的问题,但如果不是,您应该提出一个新问题。
    • 对不起。谢谢你的帮助。我不知道如何使用QDepthTest,所以我需要一些例子。 New Question is here
    猜你喜欢
    • 1970-01-01
    • 2020-04-14
    • 1970-01-01
    • 2016-01-04
    • 2013-01-10
    • 2013-10-04
    • 1970-01-01
    • 2011-03-28
    • 1970-01-01
    相关资源
    最近更新 更多