QScreenRayCaster 未找到实体。我做错了什么?
QScreenRayCaster not finding entity. What am I doiong wrong?
Qt3D 中新 QScreenRayCaster 的描述看起来正是我想要使用的,但我无法让它为我工作。我想我需要在初始化中做一些事情,但我无法在网上找到任何示例来指出正确的方向。我写了一个非常简单的程序来测试这个功能。它在 window 的中心绘制一个单位球体,然后在中心点触发 QScreenRayCaster,据我所知,然后应该 return 一个命中列表,只有在我的程序中它没有'吨。任何帮助或见解表示赞赏。
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;
};
您必须将光线投射器作为组件添加到根实体。您还应该将插槽连接到光线投射器的 hitsChanged()
函数并从那里显示命中。这样你就可以确定施法者已经计算了命中率。
顺便说一句:如果您对 Qt3D 有疑问,请查看他们的 github 存储库并查找 test folder。里面有一个手动测试的文件夹,你可以在其中看到Qt3D的大部分功能。
对于在尝试让屏幕投射器工作时偶然发现这个问题的人,除了已接受的答案:
如果想对所有实体进行交集,需要将QLayer设置为root的组件,然后设置为recursive。 (而不是单独将其设置为子实体,如问题中的 sphereEntity)。
_rayCaster = new QScreenRayCaster(_rootEntity);
_rayCaster->setRunMode(QAbstractRayCaster::SingleShot);
auto pickableLayer = new QLayer(_rootEntity);
pickableLayer->setRecursive(true);
_rootEntity->addComponent(pickableLayer);
_rayCaster->addLayer(pickableLayer);
_rootEntity->addComponent(_rayCaster);
connect(_rayCaster, &QScreenRayCaster::hitsChanged, this, &SceneWidget::onRayCastResult);
Qt3D 中新 QScreenRayCaster 的描述看起来正是我想要使用的,但我无法让它为我工作。我想我需要在初始化中做一些事情,但我无法在网上找到任何示例来指出正确的方向。我写了一个非常简单的程序来测试这个功能。它在 window 的中心绘制一个单位球体,然后在中心点触发 QScreenRayCaster,据我所知,然后应该 return 一个命中列表,只有在我的程序中它没有'吨。任何帮助或见解表示赞赏。
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;
};
您必须将光线投射器作为组件添加到根实体。您还应该将插槽连接到光线投射器的 hitsChanged()
函数并从那里显示命中。这样你就可以确定施法者已经计算了命中率。
顺便说一句:如果您对 Qt3D 有疑问,请查看他们的 github 存储库并查找 test folder。里面有一个手动测试的文件夹,你可以在其中看到Qt3D的大部分功能。
对于在尝试让屏幕投射器工作时偶然发现这个问题的人,除了已接受的答案:
如果想对所有实体进行交集,需要将QLayer设置为root的组件,然后设置为recursive。 (而不是单独将其设置为子实体,如问题中的 sphereEntity)。
_rayCaster = new QScreenRayCaster(_rootEntity);
_rayCaster->setRunMode(QAbstractRayCaster::SingleShot);
auto pickableLayer = new QLayer(_rootEntity);
pickableLayer->setRecursive(true);
_rootEntity->addComponent(pickableLayer);
_rayCaster->addLayer(pickableLayer);
_rootEntity->addComponent(_rayCaster);
connect(_rayCaster, &QScreenRayCaster::hitsChanged, this, &SceneWidget::onRayCastResult);