diff --git a/src/quick3d/qdemoncamera.cpp b/src/quick3d/qdemoncamera.cpp index 7ba65703ec8cee6e2538e48e347e33062cc660d1..e742b1546b69ae6ed1c8a5e8241462c0b3fa5c30 100644 --- a/src/quick3d/qdemoncamera.cpp +++ b/src/quick3d/qdemoncamera.cpp @@ -157,6 +157,8 @@ void QDemonCamera::setProjectionMode(QDemonCamera::QDemonCameraProjectionMode pr * is normalized between 0 and 1. The top-left of the viewport is (0,0) and * the botton-right is (1,1). If the position is not visible in the viewport, a * position of [-1, -1] is returned. + * + * \sa QDemonView3D::worldToView */ QVector2D QDemonCamera::worldToViewport(const QVector3D &worldPos) const { diff --git a/src/quick3d/qdemonview3d.cpp b/src/quick3d/qdemonview3d.cpp index 3156ef37f8c3efb7d0b09f1cf3ccac1c6e05c2b3..d796853a68fcfde9433a31c742e818095dd790e1 100644 --- a/src/quick3d/qdemonview3d.cpp +++ b/src/quick3d/qdemonview3d.cpp @@ -4,6 +4,7 @@ #include "qdemonscenemanager_p.h" #include "qdemonimage.h" #include "qdemonscenerenderer.h" +#include "qdemoncamera.h" #include <QtDemonRuntimeRender/QDemonRenderLayer> #include <QOpenGLFunctions> @@ -421,6 +422,26 @@ QSurfaceFormat QDemonView3D::idealSurfaceFormat() return f; } +/*! + * Transforms \a worldPoint from world space into view space. If the position + * is not visible in the viewport, a position of [-1, -1] is returned. This + * function requires that a camera is assigned to the view. + * + * \sa QDemonCamera::worldToViewport + */ +QVector2D QDemonView3D::worldToView(const QVector3D &worldPos) const +{ + if (!m_camera) { + qmlWarning(this) << "Cannot resolve position in view without a camera assigned!"; + return QVector2D(-1, -1); + } + + const QVector2D normalizedPos = m_camera->worldToViewport(worldPos); + if (normalizedPos.x() < 0) + return normalizedPos; + return normalizedPos * QVector2D(float(width()), float(height())); +} + void QDemonView3D::invalidateSceneGraph() { m_node = nullptr; diff --git a/src/quick3d/qdemonview3d.h b/src/quick3d/qdemonview3d.h index e50923eb49d166b4b3adbc7ebe2f680610d3a8fc..08f068920b37f69ea993f58bc9a3031c98313b11 100644 --- a/src/quick3d/qdemonview3d.h +++ b/src/quick3d/qdemonview3d.h @@ -57,6 +57,8 @@ public: static QSurfaceFormat idealSurfaceFormat(); + Q_INVOKABLE QVector2D worldToView(const QVector3D &worldPos) const; + protected: void geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry) override; QSGNode *updatePaintNode(QSGNode *, UpdatePaintNodeData *) override;