Qt Quick 3D - XR Exemple d'ancrages spatiaux

Démontre comment utiliser les ancres spatiales dans Qt Quick 3D XR.

Boîtes colorées avec étiquette indiquant les informations sur l'ancrage

Cet exemple montre comment utiliser XrSpatialAnchorListModel pour afficher et interagir avec des objets physiques réels dans l'environnement. Il prend en charge à la fois le mode "passthrough" et le mode "fully immersive". La structure de base suit l'exemple xr_simple.

La partie la plus pertinente de l'exemple est un Repeater3D sur un XrSpatialAnchorListModel. Pour chaque ancre, nous créons une boîte qui remplit le volume de l'ancre avec une couleur. Le choix de la couleur dépend de la classification de l'ancre. Cette boîte est invisible en mode passthrough. En outre, nous visualisons la position et l'orientation de l'ancre à l'aide de petites boîtes :

Repeater3D {
    id: spatialAnchors
    model: XrSpatialAnchorListModel {

    }
    delegate: Node {
        id: anchorNode
        required property XrSpatialAnchor anchor
        required property int index
        position: anchor.position
        rotation: anchor.rotation

        Model {
            pickable: true
            z: anchorNode.anchor.has3DBounds ? anchorNode.anchor.offset3D.z / 2 * 100 : 0 // Position is center of 2D surface also for 3D anchors
            scale: anchorNode.anchor.has3DBounds ? anchorNode.anchor.extent3D : Qt.vector3d(anchorNode.anchor.extent2D.x, anchorNode.anchor.extent2D.y, 0.01)
            materials: PrincipledMaterial {
                // Make anchor objects invisible in passthrough mode
                baseColor: xrView.passthroughEnabled ? Qt.rgba(0, 0, 0, 0) : anchorColor(anchor)
                alphaMode: xrView.passthroughEnabled ? PrincipledMaterial.Blend : PrincipledMaterial.Opaque
                roughness: 0.7
            }
            source: anchorNode.anchor.has3DBounds ? "#Cube" : "#Rectangle"
            property string anchorInfo: "anchor #" + anchorNode.index + ", " + anchorNode.anchor.classificationString
        }

        Model {
            // Visualize anchor orientation
            materials: PrincipledMaterial {
                baseColor: anchorNode.anchor.has3DBounds ? anchorNode.anchor.has2DBounds ? "green" : "red" : "blue"
            }
            scale: Qt.vector3d(0.05, 0.05, 0.05)
            source: "#Cube"

            Model {
                materials: PrincipledMaterial {
                    baseColor: "black"
                }
                scale: Qt.vector3d(0.1, 3, 0.1)
                source: "#Cube"
                y: 150
            }
            Model {
                materials: PrincipledMaterial {
                    baseColor: "white"
                }
                scale: Qt.vector3d(3, 0.1, 0.1)
                source: "#Cube"
                x: 150
            }
        }
        visible: anchor.has2DBounds || anchor.has3DBounds
    }
}

La boîte de l'ancre est sélectionnable et possède une propriété de chaîne anchorInfo qui contient le classificationString de l'ancre. Nous effectuons ensuite la sélection en fonction de la position du contrôleur. (Voir l'exemple xr_input pour plus de détails.) Si nous touchons l'une des boîtes de l'ancre, nous affichons une étiquette avec les informations de l'ancre :

Node {
    id: labelNode
    position: rightController.position
    rotation: rightController.rotation

    property int numAnchors: spatialAnchors.count
    property string anchorInfo: "(no anchor)"

    Node {
        y: 15
        x: -15
        scale: Qt.vector3d(0.1, 0.1, 0.1)
        Rectangle {
            width: 300
            height: 100
            color: Qt.rgba(1,0.9,0.8,0.7)
            radius: 10
            border.width: 2
            border.color: "blue"
            Text {
                anchors.fill: parent
                anchors.margins: 10
                textFormat: Text.StyledText
                text: "Total anchors: " + labelNode.numAnchors + "<br>" + "Selected: " + labelNode.anchorInfo + "<br>" + "Press A to Capture/Update anchors"
            }
        }
    }
}

Exemple de projet @ 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.