aboutsummaryrefslogtreecommitdiffstats
path: root/examples/quick3d/xr_interaction/main.qml
blob: 5cad65c88e8fc9a3003bd377ab396de09fedab98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause

import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
import QtQuick3D
import QtQuick3D.Helpers
import QtQuick3D.Xr

import xr_shared

pragma ComponentBehavior: Bound

XrView {
    id: xrView
    referenceSpace: XrView.ReferenceSpaceLocalFloor

    depthSubmissionEnabled: true

    environment: SceneEnvironment {
        id: sceneEnvironment
        lightProbe: Texture {
            textureData: ProceduralSkyTextureData {
            }
        }
        antialiasingMode: SceneEnvironment.MSAA
        antialiasingQuality: SceneEnvironment.High
        backgroundMode: SceneEnvironment.Color
        clearColor: "skyblue"
        probeHorizon: 0.5
    }

    DirectionalLight {
        eulerRotation.x: -30
        eulerRotation.y: -70
    }

    //! [haptics]
    XrHapticFeedback {
        id: hapticFeedback
        controller: XrHapticFeedback.RightController
        hapticEffect: XrSimpleHapticEffect {
            amplitude: 0.5
            duration: 30
            frequency: 3000
        }
        property Model prevObj: null
        function handleHover(obj: Model) {
            if (obj && obj !== prevObj)
                start()
            prevObj = obj
        }
    }
    //! [haptics]

    xrOrigin: XrOrigin {
        id: theOrigin

        //! [connections]
        AimController {
            id: rightAim
            controller: XrController.RightController
            view: xrView
            enableVirtualMouse: true
            enableThumbstickMove: thumbCheckBox.checked

            onObjectPressed: (obj, pos, dir) => {
                gadgetBox.handlePress(obj, pos, dir)
            }
            onHoveredObjectChanged: {
                gadgetBox.handleHover(hoveredObject)
                hapticFeedback.handleHover(hoveredObject)
            }
            onMoved: (pos, dir) => {
                gadgetBox.handleMove(pos, dir)
            }
            onReleased: {
                gadgetBox.handleRelease()
            }
            onObjectGrabbed: (obj) => {
                if (!grabCheckBox.checked)
                    return
                const gadget = obj as XrGadget
                if (!gadget)
                    startGrab(obj)
                else if (gadget.grabbable)
                    startGrab(gadget.controlledObject)
            }
            Model {
                source: "#Cylinder"
                scale: "0.05, 0.1, 0.05"
                z: 5
                eulerRotation.x: 90
                materials: PrincipledMaterial {
                    baseColor: "black"
                }
            }
            xrCursor: cursor
        }
        //! [connections]

        XrCamera {
            id: camera
        }

        XrCursor {
            id: cursor
            cameraNode: camera
            size: cursorSlider.value
            function style() : int {
                if (rightAim.pickStatus === PickResult.Item)
                    return XrCursor.CursorStyle.Flat
                if (rightAim.pickStatus === PickResult.Null)
                    return XrCursor.CursorStyle.Hidden
                const gadget = rightAim.hoveredObject as XrGadget
                if (gadget)
                    return gadget.cursorStyle
                return sphereButton.checked ? XrCursor.CursorStyle.Sphere : XrCursor.CursorStyle.Flat
            }
            cursorStyle: style()
        }
    }

    GadgetBox {
        id: gadgetBox
    }

    Scene {
        z: -100
    }

    XrItem {
        y: height + 75
        x: -125
        z: -75
        width: 60
        height: 80
        eulerRotation.y: 30
        color: "transparent"
        contentItem: Rectangle {
            width: 300
            height: 400
            opacity: 0.99
            color: "#aafff7f0"
            radius: 20
            ColumnLayout {
                anchors.fill: parent
                anchors.margins: 20
                CheckBox {
                    id: grabCheckBox
                    text: "Enable grab"
                    checked: true
                }
                CheckBox {
                    id: thumbCheckBox
                    text: "Thumbstick move"
                    checked: true
                    enabled: grabCheckBox.checked
                }
                Slider {
                    id: cursorSlider
                    from: 0.5
                    value: 2.0
                    to: 5
                }
                Text {
                    text: "Cursor size: " + cursorSlider.value.toFixed(1)
                }
                Frame {
                    ColumnLayout {
                        anchors.fill: parent
                        Text { text: "Cursor style for 3D objects" }
                        RadioButton { id: sphereButton; text: "Sphere"; checked: true }
                        RadioButton { text: "Flat" }
                    }
                    Layout.alignment: Qt.AlignBottom
                }
            }
        }
        XrItemHandle {
            visible: grabCheckBox.checked
        }
    }
}