Qt Quick 3D - ピッキングの例

モデルのピッキングを実演します。

このサンプルはピッキングを実演しています。ピッキングできる3つの異なるアニメーションモデルがあります。モデルがピッキングされると、アニメーションが停止し、モデルの色が変化して、どのモデルがピッキングされたかがわかります。ピッキング結果の詳細情報は、アプリケーションウィンドウの上部に表示されます。

モデルをピック可能にする

モデルをピック可能にするには、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

ピック結果の取得

実際のピッキングを行うには、MouseArea を追加して、View3D 全体をカバーする必要があります。

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.