Qt3d QML:如何将文本作为覆盖添加到标准示例中

Qt3d QML:如何将文本作为覆盖添加到标准示例中,qt,qml,qt3d,Qt,Qml,Qt3d,这里是长期程序员,但QML nube 我希望从示例开始一个Qt项目:“Qt 3D:Shadow Map QML example”,该示例可以从QtCreator中列出的示例中获得。这里还有一个链接: 我想首先通过添加2d文本对其进行自定义,理想情况下,2d文本会保持在屏幕上的固定位置,随着相机位置/角度的改变而保持在视图中。我会满足于只是能够添加一些简单的文本到屏幕上的任何方式 从该示例开始,我添加了一个文件: Title.qml import Qt3D.Core 2.12 import Qt3

这里是长期程序员,但QML nube

我希望从示例开始一个Qt项目:“Qt 3D:Shadow Map QML example”,该示例可以从QtCreator中列出的示例中获得。这里还有一个链接:

我想首先通过添加2d文本对其进行自定义,理想情况下,2d文本会保持在屏幕上的固定位置,随着相机位置/角度的改变而保持在视图中。我会满足于只是能够添加一些简单的文本到屏幕上的任何方式

从该示例开始,我添加了一个文件:

Title.qml

import Qt3D.Core 2.12
import Qt3D.Extras 2.13

Entity {
    id: titleText
    components: [ Transform { translation: Qt.vector3d(0.0, 10.0, 30.0) } ]

    Text2DEntity {
        font.family: "Sans Serif"
        font.pointSize: 100
        color: "white"
        text: "MY TITLE"
        width: text.length * font.pointSize*2
        height: font.pointSize * 4
    }
}
然后,在main.qml的底部,我尝试合并以下内容:

import QtQuick 2.1 as QQ2
import Qt3D.Core 2.0
import Qt3D.Render 2.0
import Qt3D.Input 2.0
import Qt3D.Extras 2.0

Entity {
    id: sceneRoot

    Camera {
        id: camera
        projectionType: CameraLens.PerspectiveProjection
        fieldOfView: 45
        aspectRatio: _window.width / _window.height
        nearPlane: 0.1
        farPlane: 1000.0
        position: Qt.vector3d(0.0, 10.0, 20.0)
        viewCenter: Qt.vector3d(0.0, 0.0, 0.0)
        upVector: Qt.vector3d(0.0, 1.0, 0.0)
    }

    FirstPersonCameraController { camera: camera }

    ShadowMapLight {
        id: light
    }

    components: [
        ShadowMapFrameGraph {
            id: framegraph
            viewCamera: camera
            lightCamera: light.lightCamera
        },
        // Event Source will be set by the Qt3DQuickWindow
        InputSettings { }
    ]


    AdsEffect {
        id: shadowMapEffect

        shadowTexture: framegraph.shadowTexture
        light: light
    }


    // Trefoil knot entity
    Trefoil {
        material: AdsMaterial {
            effect: shadowMapEffect
            specularColor: Qt.rgba(0.5, 0.5, 0.5, 1.0)
        }
    }

    // Toyplane entity
    Toyplane {
        material: AdsMaterial {
            effect: shadowMapEffect
            diffuseColor: Qt.rgba(0.9, 0.5, 0.3, 1.0)
            shininess: 75
        }
    }

    // Plane entity
    GroundPlane {
        material: AdsMaterial {
            effect: shadowMapEffect
            diffuseColor: Qt.rgba(0.2, 0.5, 0.3, 1.0)
            specularColor: Qt.rgba(0, 0, 0, 1.0)
        }
    }

    // -------------------------------------
    // Title entity
    Title {}
   // -------------------------------------
}

我确信
标题
实体正在被包括在内。为了证明这一点,我在onload和console.logs()中添加了一个声音效果(从本文中删除)。我尝试过以各种方式操纵实体,但从未出现过。我想其他组件一定是在隐藏它/阻止它/使它不兼容而无法显示

text2D实体旨在将文本放入3D场景中(如在特定对象上放置一些文本标记)。我想你只是想把文本作为覆盖物放在屏幕上

这可以通过使用标准QML文本类型来完成

我修改了您的代码,并用//ADDED标记了新行。不再需要的行标记为//已删除

pro文件:

您将需要pro文件中的3dextras模块

QT+=3dextras

main.cpp

使用QQuickView而不是Qt3DQuickWindow更改main.cpp。原因是scene3D和Qt3DQuickWindow之间的渲染机制不同。 Scene3D使用QML的渲染器进行渲染,而Qt3DQuickWindow将创建一个专用的渲染线程。换一种说法:如果你的程序只需要显示3D环境,那么坚持使用Qt3DQuickWindow。 如果要将文本和按钮放在3D环境的顶部,请使用QQuickView

#include <Qt3DQuickExtras/qt3dquickwindow.h>
#include <Qt3DQuick/QQmlAspectEngine>
#include <QGuiApplication>
#include <QQmlContext>
#include <QQmlEngine>
#include <QQuickView>//ADDED

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

//ADDED:
    QQuickView view;

    view.rootContext()->setContextProperty("_window", &view);
    view.setSource(QUrl("qrc:/main.qml"));
    view.setWidth(1600);
    view.setHeight(900);
    view.show();

//REMOVED:
//    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();
}
编辑

下面是在Windows10上运行的示例的打印屏幕,该示例使用Qt5.13.2 MSVC2015 64位,也适用于Qt5.14.0 MSVC2015 64位

这看起来是解决恼人问题的完美方法。我还没有机会证实这一点,但我将在未来几天内着手。谢谢你的帮助!我知道这种感觉。我也遇到过同样的情况…所以你想要的只是在Qt3D场景上添加文本?如果是那样的话,它会的。很高兴相应地更改你第一篇文章的标题。是的,这非常接近我想要的。我想保留在3d世界中移动相机的能力。我不确定这个方法允许吗?如果没有,我将承受这种损失。不幸的是,这对我不太管用。文本显示,但QtQuick.Scene3D未呈现。我更新了我的问题以包含我正在使用的确切QML。如果可能的话,请帮我测试一下?如果它对你有效,也许还有另一个因素在起作用。我希望,我只是有一个小故障的代码。我添加了一个完整的源代码工作的例子在我以前的答案。我希望这有助于学习Qt3D。谢谢所有的帮助!我当然明白,你真的成功地运行了这个,对吗?嗯,我试过你的密码,但在我的机器上不起作用。只需在蓝色屏幕上显示标题。我反复检查了cpp、qml和pro。我可以使用原始问题和QML 3d场景的标准问题的示例。我已删除并重新创建生成文件夹。。。我完全搞不懂。我最终需要它来在合理的范围内跨平台(windows、linux、mac)和跨Qt版本工作。我开始在Linux(Ubuntu)上使用Qt5.14,以防这可能意味着什么。
Rectangle {
    anchors.fill: parent

    Scene3D{
        anchors.fill: parent
        focus: true
        aspects: ["input", "logic"]
        Entity {
            id: sceneRoot

                Camera {
                    id: camera
                    projectionType: CameraLens.PerspectiveProjection
                    fieldOfView: 45
                    aspectRatio: _window.width / _window.height
                    nearPlane: 0.1
                    farPlane: 1000.0
                    position: Qt.vector3d(0.0, 10.0, 20.0)
                    viewCenter: Qt.vector3d(0.0, 0.0, 0.0)
                    upVector: Qt.vector3d(0.0, 1.0, 0.0)
                }

                FirstPersonCameraController { camera: camera }

                ShadowMapLight {
                    id: light
                }

                components: [
                    ShadowMapFrameGraph {
                        id: framegraph
                        viewCamera: camera
                        lightCamera: light.lightCamera
                    },
                    // Event Source will be set by the Qt3DQuickWindow
                    InputSettings { }
                ]


                AdsEffect {
                    id: shadowMapEffect

                    shadowTexture: framegraph.shadowTexture
                    light: light
                }


                // Trefoil knot entity
                Trefoil {
                    material: AdsMaterial {
                        effect: shadowMapEffect
                        specularColor: Qt.rgba(0.5, 0.5, 0.5, 1.0)
                    }
                }

                // Toyplane entity
                Toyplane {
                    material: AdsMaterial {
                        effect: shadowMapEffect
                        diffuseColor: Qt.rgba(0.9, 0.5, 0.3, 1.0)
                        shininess: 75
                    }
                }

                // Plane entity
                GroundPlane {
                    material: AdsMaterial {
                        effect: shadowMapEffect
                        diffuseColor: Qt.rgba(0.2, 0.5, 0.3, 1.0)
                        specularColor: Qt.rgba(0, 0, 0, 1.0)
                    }
                }
            }
        }

    Text {
        id: title
        text: qsTr("TITLE")
        font.family: "Arial"
        font.pointSize: 30
        color: "black"

        anchors.top:parent.top
        anchors.left:parent.left
        anchors.leftMargin: 20
        anchors.topMargin: 20
    }
}