Qt3D point and line picking?
-
Hi there, I've been trying to do point and line picking on a mesh in Qt3D. Here's a working version of triangle picking,
auto renderSettings = new Qt3DRender::QRenderSettings(root); renderSettings->pickingSettings()->setPickMethod(Qt3DRender::QPickingSettings::TrianglePicking); auto entity = new Qt3DCore::QEntity(root); auto picker = new Qt3DRender::QObjectPicker; auto sphere = new Qt3DExtras::QSphereGeometry; entity->addComponent(picker); entity->addComponent(sphere); connect(picker, &Qt3DRender::QObjectPicker::clicked, [](Qt3DRender::QPickEvent* e) { auto p = dynamic_cast<Qt3DRender::QPickTriangleEvent*>(e); auto idx = p->triangleIndex(); });
However, when I switched to point picking or line picking, the clicked event is never triggered. Any idea how to make this work? Thanks in advance.
-
Hi @unclejimbo-0 ,
in my experience, Line and Point Picking doesn't works on Qt5.9, but tested and functionnal on Qt5.12.
The main reason why you can't pick lines or points is that yout entity is NOT a Line or a Point. Actually, your entity represent a Sphere which is composed of triangles only. Thus, no line or point picking available.
if you want to perform Line Picking, I recommend you to start from the custom-mesh example :
https://github.com/qt/qt3d/tree/5.12/tests/manual/custom-mesh-cpp
and adapt it draw lines. Once vertices and indices modified, simply change the MeshRenderer primitive types to Line
// From customMeshRenderer->setPrimitiveType(Qt3DRender::QGeometryRenderer::Triangles); // To customMeshRenderer->setPrimitiveType(Qt3DRender::QGeometryRenderer::Lines);
Then, in the render settings set
renderSettings->pickingSettings()->setPickMethod(Qt3DRender::QPickingSettings::LinePicking);
and add your QObjectPicker component to your custom Mesh.
It should be working. If not working properly, try to increase the picking tolerance
renderSettings->pickingSettings()->setWorldSpaceTolerance(10.0f); // default is 0.1f
This last parameter is tricky as long as it performs tolerance checking in the world space rather than Screen space. Thus, if the camera is really close to the line, it will be easy to intersect. However, as long as you get farther, it gets difficult to pick lines due to pixel to ray resolution.
Hope it helps.
-
@lcadet Hi, thank you for your reply.
I believe you are right. Although I ended up not doing lines or points picking, but I think your explanation is the right way to go.
One more thing I think might be important for picking is that you need to set the vertex count properly. It's quite confusing that Qt3D can render the geometry correctly even without calling
QAttribute::setCount
, as long as you callQGeometryRenderer::setVertexCount
. I've dug the qt3d source code a bit and I think they have a way of guessing the vertex count upon rendering, based on the counts you've provided. However, the picking process seems to merely counting on the count set inQAttribute
. And if you forget to set it, the picking will fail even for triangles.So, that's basically my experience so far. I hope qt3d could provide better documentation and more intuitive APIs.