0

I tried to add the svg image which contains opacity=0 area into QTextureMaterial on QPlaneMesh, but it shows that the Plane's background is always in gray.

I want that the plane can cantain my image and penetrate to other object in opacity=0 area.

The svg file like https://image.flaticon.com/icons/svg/3154/3154348.svg .

code:

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

xyLi
  • 27
  • 5

1 Answers1

0

QTextureMaterial doesn't support transparency in the textures.

Check out my answer to this question to see how to implement transparency in textures yourself. There is not out-of-the-box solution in Qt3D.

Florian Blume
  • 3,237
  • 17
  • 37
  • I use `planeMaterial->setAlphaBlendingEnabled(true);` and it shows the transparency. [like this](https://imgur.com/a/xjHYIFS) But it will be covered by other object if it is not the last order added. Can I make it draw always in last order? – xyLi Jul 17 '20 at 10:31
  • Wow, you're right, it does support transparency. I'm just wondering why I didn't get it to work using that function in my answer... To fix your other issue add a `QDepthTest` to your framegraph to enable depth-testing. This way everything should be drawn correctly. – Florian Blume Jul 17 '20 at 11:38
  • oh~~ in FlippedTextureImage, I add `painter->setCompositionMode(QPainter::CompositionMode_SourceIn);` that I found other people's solution. But this post does not update. sorry. May I ask you how to use `QDepthTest` like some examples ? My code is **Qt 3D simple-cpp**. – xyLi Jul 17 '20 at 12:54
  • Can you post a new question including code? Comments are not there to solve further problems. I thought saying that you should use `QDepthTest` will solve your issue quickly but if it doesn't you should ask a new question. – Florian Blume Jul 17 '20 at 13:57
  • Sorry. Thanks for your help. I don't know how to use `QDepthTest`, so I need some example. [New Question is here](https://stackoverflow.com/questions/62957001/qt3d-transparency-texture-will-be-covered-how-to-slove-that) – xyLi Jul 17 '20 at 15:24