Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. how to click on a 3D object?
Forum Updated to NodeBB v4.3 + New Features

how to click on a 3D object?

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
3 Posts 1 Posters 275 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Anton1978A Offline
    Anton1978A Offline
    Anton1978
    wrote on last edited by
    #1

    Good afternoon
    There is a 3D scene

    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(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?

    1 Reply Last reply
    1
    • Anton1978A Offline
      Anton1978A Offline
      Anton1978
      wrote on last edited by Anton1978
      #2

      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?

      1 Reply Last reply
      0
      • Anton1978A Offline
        Anton1978A Offline
        Anton1978
        wrote on last edited by
        #3

        doesn't anyone know? it's sad

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved