how to click on a 3D object?
Unsolved
QML and Qt Quick
-
Good afternoon
There is a 3D sceneWindow { 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(150,150,50) source: "#Sphere" objectName: "Sphere" pickable : true materials: [PrincipledMaterial{baseColor: "cyan"}] } 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)) } } OrbitCameraController { anchors.fill: parent origin: persCam camera: persCam } } MouseArea{ anchors.fill: viewport onClicked: (mouse) => { var result = viewport.pick(mouse.x, mouse.y); if (result.objectHit) { console.log(result); console.log(result.distance); console.log(result.objectHit.objectName); var pickedObject = result.objectHit; console.info("xxx " + pickedObject.objectName) } } } }
implementation of the Head object
import QtQuick import QtQuick3D Node { id: node objectName: "RootNode - node" PrincipledMaterial { id: defaultMaterial_material objectName: "DefaultMaterial" baseColor: "#ffcccccc" } Node { id: rootNode objectName: "RootNode - node" Model { id: cube objectName: "RootNode - model" pickable: true rotation: Qt.quaternion(0.707107, -0.707107, 0, 0) scale: Qt.vector3d(100, 100, 100) source: "qrc://Test05/res/cube_001_mesh.mesh" materials:[PrincipledMaterial{ baseColor: "green" metalness: 0.8 roughness: 0.3 }] } } Node { y: -30 eulerRotation.y: -10 z: 150 Rectangle { anchors.horizontalCenter: parent.horizontalCenter color: "orange" width: text.width+10 height: text.height+10 Text { anchors.centerIn: parent id: text text: "I'm Suzanne" font.pointSize: 14 color: "black" } } } }
if I comment out the Head, then everything works as expected - I can get the name of the object I clicked on.
But if you turn on the Head, nothing happens.
It's not clear why?
Isn't a model contained inside another object already a model? -
By the way, if you implement the Head like this, then everything works. Why?
Model { PrincipledMaterial { id: defaultMaterial_material objectName: "DefaultMaterial" baseColor: "#ffcccccc" } id: cube objectName: "RootNode - model" pickable: true rotation: Qt.quaternion(0.707107, -0.707107, 0, 0) scale: Qt.vector3d(100, 100, 100) source: "qrc://Test05/res/cube_001_mesh.mesh" materials:[PrincipledMaterial{ baseColor: "green" metalness: 0.8 roughness: 0.3 }] }
This means that only something that has a root object - Model - works. Or not?
And if tomorrow I need to wrap the model in a physical body, then how?