The description of the new QScreenRayCaster in Qt3D looks like its exactly what I want to use, but I cannot get it to work for me. I guess that there is something I need to do in the initialization but I cannot find any examples online to point me in the right direction. I've written a very simple program to test the function. It draws a unit sphere in the center of a window and then triggers a QScreenRayCaster at the center point which as far as I can see should then return a list of hits, only in my program it doesn't. Any help or insight appreciated.
MyQt3DWindow:: MyQt3DWindow(QScreen *screen)
: Qt3DExtras::Qt3DWindow(screen)
{
// scene
Qt3DCore::QEntity *rootEntity = new Qt3DCore::QEntity;
Qt3DRender::QMaterial *material = new Qt3DExtras::QPhongMaterial(rootEntity);
Qt3DCore::QEntity *sphereEntity = new Qt3DCore::QEntity(rootEntity);
Qt3DExtras::QSphereMesh *sphereMesh = new Qt3DExtras::QSphereMesh;
sphereEntity->addComponent(sphereMesh);
Qt3DCore::QTransform *sphereTransform = new Qt3DCore::QTransform;
sphereEntity->addComponent(sphereTransform);
sphereEntity->addComponent(material);
// Camera
Qt3DRender::QCamera *camera = this->camera();
camera->lens()->setOrthographicProjection(-2, 2, -2, 2, 0.1f, 1000.0f);
camera->setPosition(QVector3D(0, 0, 40.0f));
camera->setViewCenter(QVector3D(0, 0, 0));
// picker
m_screenRayCaster = new Qt3DRender::QScreenRayCaster(rootEntity);
m_screenRayCaster->setRunMode(Qt3DRender::QAbstractRayCaster::SingleShot);
Qt3DRender::QLayer *pickableLayer = new Qt3DRender::QLayer(rootEntity);
m_screenRayCaster->addLayer(pickableLayer);
sphereEntity->addComponent(pickableLayer);
this->setRootEntity(rootEntity);
this->setGeometry(100, 100, 400, 400);
// and call the caster after a short delay
QTimer::singleShot(1000, this, SLOT(testRayCaster()));
}
void MyQt3DWindow::testRayCaster()
{
m_screenRayCaster->trigger(QPoint(200, 200));
Qt3DRender::QAbstractRayCaster::Hits hits = m_screenRayCaster->hits();
qDebug() << "Hits found = " << hits.size() << "\n";
}
class MyQt3DWindow: public Qt3DExtras::Qt3DWindow
{
Q_OBJECT
public:
MyQt3DWindow(QScreen *screen = nullptr);
public slots:
void testRayCaster();
private:
Qt3DRender::QScreenRayCaster *m_screenRayCaster;
};
You have to add the ray caster to the root entity as a component. You should also connect a slot to the ray caster's hitsChanged()
function and display the hits from there. This way you can be sure that the caster computed the hits already.
By the way: If you have issues with Qt3D, checkout their github repository and look for the test folder. There is a folder inside with manual tests where you can see most of the functioning of Qt3D.
来源:https://stackoverflow.com/questions/52666741/qscreenraycaster-not-finding-entity-what-am-i-doiong-wrong