How to click (pick) a 3D object?
Unsolved
QML and Qt Quick
-
Good afternoon
I’m experimenting with 3D, everything seems to be correct, the click works, but the object is not located on the 3D scene.import QtQuick import QtQuick3D import QtQuick3D.Helpers Window { width: 640 height: 480 visible: true title: qsTr("Hello World") View3D{ id: viewport anchors.fill: parent renderMode: View3D.Underlay environment: SceneEnvironment{ clearColor: "#222222" backgroundMode: SceneEnvironment.SkyBox lightProbe: Texture{ source: "qrc:\\res\\heaven.png" } probeExposure: 0.75 } Model{ position: Qt.vector3d(0,0,0); scale: Qt.vector3d(1,1.25,1); source: "#Cone" objectName: "Cone" pickable : true materials:[PrincipledMaterial{ baseColor: "green" metalness: 0.8 roughness: 0.3 }] } Model{ position: Qt.vector3d(80,0,50) source: "#Sphere" objectName: "Sphere" pickable : true materials: [PrincipledMaterial{baseColor: "cyan"}] } Model{ position: Qt.vector3d(50,50,-150) source: "#Rectangle" objectName: "Rectangle" pickable: true materials: [PrincipledMaterial{baseColor:"red"}] } Head{ id: headID } DirectionalLight{ eulerRotation.x: -20 eulerRotation.y: 110 castsShadow: true } PerspectiveCamera{ id: persCam position: Qt.vector3d(0,300,350) Component.onCompleted: { lookAt(Qt.vector3d(0,0,0)) } } } MouseArea{ anchors.fill: viewport onClicked: (mouse) => { var result = viewport.pick(mouse.x,mouse.y); console.log(result); console.log(result.distance); console.log(result.objectHit.objectName); if (result.objectHit) { var pickedObject = result.objectHit; console.log(pickedObject.objectName); } } } }
Output to the console after clicking
qrc:/qt/qml/Test05/Main.qml:79: TypeError: Cannot read property 'objectName' of null qml: QQuick3DPickResult(, 0, , , , , , 0) qml: 0 qrc:/qt/qml/Test05/Main.qml:79: TypeError: Cannot read property 'objectName' of null qml: QQuick3DPickResult(, 0, , , , , , 0) qml: 0 qrc:/qt/qml/Test05/Main.qml:79: TypeError: Cannot read property 'objectName' of null
Apparently I'm missing something, but I can't understand what