Qt Quick 3D - Esempio di XR Simple Touch
Dimostra l'input tramite tracciamento delle mani in Qt Quick 3D Xr.

Questo esempio illustra come utilizzare l'API di tracciamento delle mani in Qt Quick 3D Xr per interagire con oggetti 2D e 3D presenti nella scena. Segue la struttura di base dell'esempio xr_simple.
Iniziamo creando un componente riutilizzabile TouchHand che incapsula tutta la logica necessaria per ciascuna mano.
// 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
}
}
}Il componente contiene un ` XrController`, che ci fornisce la posizione 3D dell’indice, e un ` XrHandModel ` per mostrare dove si trova la mano. È nel gestore ` onTouchPositionChanged ` che avviene la magia. Chiamiamo ` XrView.processTouch()`, che svolge il lavoro più impegnativo: cerca di mappare la posizione di tocco 3D su una posizione 2D su un ` XrItem ` e invia un evento di tocco se trova un elemento. Restituisce quindi l’offset dalla posizione 3D al punto di tocco sulla superficie 2D. Utilizziamo quindi tale offset per spostare la posizione dell’ XrHandModel, in modo da dare l’illusione che la mano sia stata fermata dalla superficie.
Nota: questo effetto non funzionerà su Apple Vision Pro poiché il sistema mostra le mani reali dell’utente e l’ XrHandModel non viene visualizzato.
Creiamo due istanze del componente TouchHand all’interno dell’ XrOrigin, una per ciascuna mano:
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: theOriginIn questo caso, " buttons " è un gruppo di pulsanti 3D dotato della funzione " handleTouch ". (L'implementazione non è specifica per XR, pertanto i dettagli non sono documentati in questa sede.)
Posizioniamo l’ XrOrigin a 50 centimetri dall’origine della scena, che dovrebbe rappresentare una distanza di contatto confortevole.
Il resto della scena contiene alcuni modelli 3D e un XrItem che reagisce agli eventi tattili.
© 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.