Qt Quick 3D - XR シンプルタッチの例

Qt Quick 3D XR におけるハンドトラッキング入力を実演します。

「XR Touch Example」と表示された仮想画面に、3つのボタンがある

このサンプルでは、Qt Quick 3D XR のハンドトラッキング API を使用して、シーン内の 2D および 3D オブジェクトとインタラクションを行う方法を示します。xr_simpleサンプルの基本構造に従っています。

まず、各手に対して必要なすべてのロジックをカプセル化した、再利用可能なコンポーネント `TouchHand ` を作成します。

// Copyright (C) 2025 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
import QtQuick
import QtQuick3D
import QtQuick3D.Xr

Node {
    id: root
    property color color: "#ddaa88"
    required property int touchId
    required property int hand
    required property XrView view

    property alias touchPosition: handController.scenePos
    property bool touchActive: false

    XrController {
        id: handController
        controller: root.hand

        property vector3d scenePos: view.xrOrigin.mapPositionToScene(pokePosition)

        onScenePosChanged: {
            const touchOffset = view.processTouch(scenePos, root.touchId)
            handModel.position = touchOffset

            const touchState = view.touchpointState(root.touchId)
            const gadget = touchState.model as XrGadget
            if (gadget) {
                gadget.handleTouch(touchState.uvPosition, root.touchId, touchState.pressed)
            }
        }
    }

    XrHandModel {
        id: handModel
        hand: root.hand
        materials: PrincipledMaterial {
            baseColor: root.color
            roughness: 0.5
        }
    }
}

このコンポーネントには、人差し指の 3D 位置を取得する `XrController` と、手の位置を表示する `XrHandModel ` が含まれています。`onTouchPositionChanged ` ハンドラーこそが、魔法が起こる場所です。ここでは `XrView.processTouch()` を呼び出し、この関数が主要な処理を行います。具体的には、3D のタッチ位置を `XrItem ` 上の 2D 位置にマッピングしようとし、アイテムが見つかった場合はタッチイベントを送信します。 その後、3D位置から2Dサーフェス上のタッチポイントまでのオフセットが返されます。そこで、そのオフセットを使ってXrHandModel の位置をずらし、手がサーフェスに阻まれたかのような錯覚を与えます。

注: Apple Vision Proでは、システムがユーザーの実手の映像を表示し、XrHandModel は表示されないため、この効果は 機能しません。

XrOrigin 内に、各手用に1つずつ、計2つのTouchHandコンポーネントのインスタンスを作成します:

XrOrigin {
    id: theOrigin
    z: 50
    TouchHand {
        id: rightHandModel
        hand: XrHandModel.RightHand
        view: xrView
        touchId: 0
        onTouchPositionChanged: buttons.handleTouch(touchPosition)
    }
    TouchHand {
        id: leftHandModel
        hand: XrHandModel.LeftHand
        view: xrView
        touchId: 1
        onTouchPositionChanged: buttons.handleTouch(touchPosition)
    }
}
xrOrigin: theOrigin

ここで、buttons はhandleTouch 関数を持つ3Dボタンのグループです。(実装はXR固有のものではないため、詳細についてはここでは説明しません。)

XrOrigin をシーンの原点から50センチメートルの位置に配置します。これは、快適にタッチできる距離となるはずです。

シーンの残りの部分には、いくつかの3Dモデルと、タッチイベントに反応するXrItem が含まれています。

サンプルプロジェクト @ code.qt.io

© 2026 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.