Search code examples
svgpngtexturesopacityqt3d

Qt3d svg or png Texture with opacity=0 QTextureMaterial on QPlaneMesh


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


Solution

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