Qt Quick 3D - 피킹 예시

모델 피킹을 시연합니다.

이 예제는 피킹을 보여줍니다. 선택할 수 있는 애니메이션 모델은 세 가지입니다. 모델을 선택하면 애니메이션이 중지되고 모델 색상이 변경되어 어떤 모델이 선택되었는지 명확하게 알 수 있습니다. 선택 결과에 대한 자세한 정보는 애플리케이션 창 상단에 표시됩니다.

모델 선택 가능 만들기

모델을 선택 가능하게 만들려면 pickable 속성을 true 으로 설정해야 합니다. 또한 선택된 모델의 이름을 표시할 수 있도록 objectName 을 추가합니다.

Model {
    id: cubeModel
    objectName: "Cube"
    source: "#Cube"
    pickable: true
    property bool isPicked: false

애니메이션과 색상을 전환하는 데 사용할 isPicked 속성을 추가했습니다.

materials: DefaultMaterial {
    diffuseColor: cubeModel.isPicked ? "#41cd52" : "#09102b"
    ...
SequentialAnimation on eulerRotation {
    running: !cubeModel.isPicked

선택 결과 가져오기

실제 선택을 수행하려면 View3D 전체를 덮도록 MouseArea 을 추가해야 합니다.

MouseArea {
    anchors.fill: view

마우스 영역의 onClicked 핸들러에서 pick 메서드를 사용하여 실제 선택 결과를 가져옵니다.

var result = view.pick(mouse.x, mouse.y);

결과에 target object 이 있는 경우 결과의 세부 사항을 살펴보고 애플리케이션 보기 상단의 텍스트 필드에 표시합니다.

if (result.objectHit) {
    var pickedObject = result.objectHit;
    // Toggle the isPicked property for the model
    pickedObject.isPicked = !pickedObject.isPicked;
    // Get picked model name
    pickName.text = pickedObject.objectName;
    // Get other pick specifics
    uvPosition.text = "("
            + result.uvPosition.x.toFixed(2) + ", "
            + result.uvPosition.y.toFixed(2) + ")";
    distance.text = result.distance.toFixed(2);
    scenePosition.text = "("
            + result.scenePosition.x.toFixed(2) + ", "
            + result.scenePosition.y.toFixed(2) + ", "
            + result.scenePosition.z.toFixed(2) + ")";
    localPosition.text = "("
            + result.position.x.toFixed(2) + ", "
            + result.position.y.toFixed(2) + ", "
            + result.position.z.toFixed(2) + ")";
    worldNormal.text = "("
            + result.sceneNormal.x.toFixed(2) + ", "
            + result.sceneNormal.y.toFixed(2) + ", "
            + result.sceneNormal.z.toFixed(2) + ")";
    localNormal.text = "("
            + result.normal.x.toFixed(2) + ", "
            + result.normal.y.toFixed(2) + ", "
            + result.normal.z.toFixed(2) + ")";

예제 프로젝트 @ code.qt.io

© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.