如何在QQuickView中正确渲染透明网格物体

时间:2019-04-02 15:44:51

标签: c++ qt qt5 qt3d

我想使用QQuickView类将基于透明对象的Qt3D场景嵌入基于Widget的应用程序中。在QQuickView中渲染具有Qt3D(DiffuseSpecularMaterial,alpha通道值的漫反射颜色0.1和alphaBlending设置为true)的高度透明的网格会导致几乎不透明的黑色(qquickview的颜色)网格,而不是非常透明的网格,其颜色为3D场景的背景,请参见图片:

enter image description here

更改quickview的颜色(请参见c ++代码,quickView-> setColor(...))将更改透明网格收敛的颜色。

使用QQuickView的C ++代码

int main(int argc, char *argv[])
{

    QApplication::setAttribute(Qt::AA_UseDesktopOpenGL);
    QApplication app(argc, argv);
    QMainWindow mainWindow;

    QWidget* viewersContainerWidget = new QWidget();
    QGridLayout* m_viewersLayout = new QGridLayout(viewersContainerWidget);
    m_viewersLayout->setContentsMargins(0, 0, 0, 0);

    QQuickView *quickView = new QQuickView();
    quickView->setSource(QUrl("qrc:/main.qml"));
    quickView->setResizeMode(QQuickView::SizeRootObjectToView);
    quickView->setColor("transparent");
    QWidget *container = QWidget::createWindowContainer(quickView);
    container->setMinimumSize(800, 800);
    m_viewersLayout->addWidget(container, 0, 1);
    mainWindow.setCentralWidget(viewersContainerWidget);

    mainWindow.resize(800, 600);
    mainWindow.show();

    return app.exec();
}

QML代码

Item {
    id: root3DViewer
    Scene3D {
        id: scene3DRoot
        anchors.fill: parent
        aspects: ["input", "logic"]

        Entity {
            id: sceneRoot

            Camera {
                id: camera
                projectionType: CameraLens.PerspectiveProjection
                nearPlane : 0.01
                farPlane : 1000.0
                fieldOfView: 45
                position: Qt.vector3d(0,0,-20)
                viewCenter: Qt.vector3d(0,0,2.5)
            }

            OrbitCameraController {
                camera: camera
            }


            components: [
                RenderSettings {
                     ForwardRenderer {
                        camera: camera
                        clearColor: "black"
                    }
                },
                InputSettings {}
            ]

            Entity {
                DirectionalLight {
                    id:directionalLight
                    worldDirection: camera.viewVector
                }
                components: [directionalLight]
            }



            Entity {
                id: sphereEntity
                DiffuseSpecularMaterial {
                    id: sphereMaterial
                    diffuse: Qt.rgba(1,0,0,0.9)
                    alphaBlending: true
                }
                Transform {
                    id: sphereTransform
                    translation: Qt.vector3d(0,0,0)
                }

                SphereMesh {
                    id: sphereMesh
                    radius: 1
                }
            components: [sphereTransform, sphereMaterial,sphereMesh]
            }
            Entity {
                id: cylinderEntity
                DiffuseSpecularMaterial {
                    id: cylinderMaterial
                    diffuse: Qt.rgba(0,0,1,0.1)
                    alphaBlending: true
                }
                Transform {
                    id: cylinderTransform
                    translation: Qt.vector3d(0,0,5)
                }

                CylinderMesh {
                    id: cylinderMesh
                    length: 4
                    radius: 1
                }
            components: [cylinderTransform, cylinderMaterial,cylinderMesh]
            }

        }
    }
}

使用Qt3DQuickWindow可以按预期工作(网格变得非常透明,并采用3d场景的背景颜色,请参见正确的渲染:

enter image description here

使用Qt3DQuickWindow的C ++代码

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);

    Qt3DExtras::Quick::Qt3DQuickWindow view;
    view.resize(1600, 800);
    view.engine()->qmlEngine()->rootContext()->setContextProperty("_window", &view);
    view.setSource(QUrl("qrc:/main.qml"));
    view.show();

    return app.exec();
}

如何在QQuickView中正确渲染透明网格?

谢谢

0 个答案:

没有答案