aboutsummaryrefslogtreecommitdiffstats
path: root/examples/quick3d/xr_input/main.qml
blob: 52501dd7bbef7f2d74c5fbeebf1ba34c22fd0e7e (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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause

import QtQuick
import QtQuick.Layouts
import QtQuick.Controls

import QtQuick3D
import QtQuick3D.Helpers
import QtQuick3D.Xr

XrView {
    id: xrView
    referenceSpace: XrView.ReferenceSpaceLocalFloor

    depthSubmissionEnabled: true

    xrOrigin: theOrigin

    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 {
        controller: XrHapticFeedback.RightController
        condition: XrHapticFeedback.RisingEdge
        trigger: pickRay.hit
        hapticEffect: XrSimpleHapticEffect {
            amplitude: 0.5
            duration: 30
            frequency: 3000
        }
    }
    //! [haptics]

    XrOrigin {
        id: theOrigin
        z: 100
        //! [picking]
        XrController {
            id: rightController
            controller: XrController.RightController
            poseSpace: XrController.AimPose

            property QtObject hitObject

            onRotationChanged: {
                const pickResult = xrView.rayPick(scenePosition, forward)
                if (pickResult.hitType !== PickResult.Null) {
                    pickRay.hit = true
                    pickRay.length = pickResult.distance
                    hitObject = pickResult.objectHit
                    cursorSphere.position = pickResult.scenePosition
                    cursorSphere.visible = true
                } else {
                    pickRay.hit = false
                    pickRay.length = 50
                    hitObject = null
                    cursorSphere.visible = false
                }
            }

            Node {
                id: pickRay
                property real length: 50
                property bool hit: false

                Model {
                    eulerRotation.x: -90
                    scale: Qt.vector3d(0.005, pickRay.length/100, 0.005)
                    source: "#Cone"
                    materials: PrincipledMaterial {
                        baseColor: rightTrigger.pressed ? "#99aaff" : "#cccccc"
                        lighting: PrincipledMaterial.NoLighting
                    }
                    opacity: 0.8
                }
            }

            Node {
                z: 5
                Model {
                    eulerRotation.x: 90
                    scale: Qt.vector3d(0.05, 0.10, 0.05)
                    source: "#Cylinder"
                    materials: PrincipledMaterial {
                        baseColor: "black"
                        roughness: 0.2
                    }
                }
            }
        }
        //! [picking]
    }

    Model {
        id: cursorSphere
        source: "#Sphere"
        materials: PrincipledMaterial {
            baseColor: "#777777"
            lighting: PrincipledMaterial.NoLighting
        }
        opacity: 0.4
        scale: Qt.vector3d(0.05, 0.05, 0.05)
        visible: false
    }

    //! [trigger input]
    XrInputAction {
        id: rightTrigger
        controller: XrInputAction.RightController
        actionId: [XrInputAction.TriggerPressed, XrInputAction.TriggerValue, XrInputAction.IndexFingerPinch]
        onTriggered: {
            const button = rightController.hitObject as ExampleButton
            if (button && button !== panel.activeButton) {
                panel.activeButton = button
            }
        }
    }
    //! [trigger input]

    //! [mouse input]
    XrInputAction {
        id: rightThumbstickX
        controller: XrInputAction.RightController
        actionId: [XrInputAction.ThumbstickX]
    }
    XrInputAction {
        id: rightThumbstickY
        controller: XrInputAction.RightController
        actionId: [XrInputAction.ThumbstickY]
    }

    XrVirtualMouse {
        view: xrView
        source: rightController
        leftMouseButton: rightTrigger.pressed
        scrollWheelX: rightThumbstickX.value
        scrollWheelY: rightThumbstickY.value
    }
    //! [mouse input]

    Model {
        id: floor
        source: "#Rectangle"
        eulerRotation.x: -90
        scale: Qt.vector3d(5,5,5)
        materials: [ PrincipledMaterial {
                baseColor: "green"
            }
        ]
    }

    Model {
        id: table
        property real height: 70
        position: Qt.vector3d(0, height / 2, 0)
        source: "#Cube"
        scale: Qt.vector3d(3, height / 100, 1)
        materials: PrincipledMaterial {
            baseColor: "#554433"
        }
    }

    Model {
        y: table.height + 2.5
        source: "#Cylinder"
        scale: Qt.vector3d(0.5, 0.05, 0.5)
        materials: PrincipledMaterial {
            baseColor: "black"
            roughness: 0.7
        }
    }

    Model {
        id: teapot
        y: table.height + 5
        source: "meshes/teapot.mesh"
        scale: Qt.vector3d(10, 10, 10)
        property color color: "#cdad52"
        materials: [
            PrincipledMaterial {
                baseColor: teapot.color
                roughness: 0.1
                clearcoatAmount: clearcoatSlider.value
                clearcoatRoughnessAmount: 0.1
                metalness: metalnessCheckBox.checked ? 1.0 : 0.0
            }
        ]
        property real speedFactor: speedSlider.value
        FrameAnimation {
            running: true
            onTriggered: {
                const speed = panel.activeButton?.rotationSpeed * teapot.speedFactor
                teapot.eulerRotation.y += speed * frameTime
            }
        }
    }

    Teacup {
        x: 50
        y: table.height
        scale: Qt.vector3d(1.5, 1.5, 1.5)
        color: colorView.selectedColor
    }

    Node {
        id: panel
        y: table.height
        z: 50
        eulerRotation.x: -45
        Model {
            pickable: true
            scale: Qt.vector3d(1, 0.2, 0.02)
            source: "#Cube"
            materials: PrincipledMaterial {
                baseColor: "lightgray"
            }
        }
        property ExampleButton activeButton: midButton
        ExampleButton {
            id: leftButton
            x: -35
            on: panel.activeButton === this
            objectName: "Button 1"
            rotationSpeed: -45
        }
        ExampleButton {
            id: midButton
            x: 0
            on: panel.activeButton === this
            objectName: "Button 2"
            rotationSpeed: 0
        }
        ExampleButton {
            id: rightButton
            x: 35
            on: panel.activeButton === this
            objectName: "Button 3"
            rotationSpeed: 45
        }
    }

    //! [xritem start]
    XrItem {
        width: 75
        height: 100
        x: -100
        y: height + table.height + 5
        z: 40
        eulerRotation.y: 45
        color: "transparent"
        contentItem: Rectangle {
            color: Qt.rgba(1, 1, 1, 0.5)
            border.width: 5
            border.color: "lightblue"
            height: 400
            width: 300
            radius: 25
    //! [xritem start]

            ColumnLayout {
                anchors.fill: parent
                anchors.margins: 10
                Button {
                    text: "Red"
                    onClicked: teapot.color = "#dd3311"
                    Layout.alignment: Qt.AlignHCenter
                    Layout.preferredWidth: parent.width * 2 / 3
                }
                Button {
                    text: "Green"
                    onClicked: teapot.color = "#41cd52"
                    Layout.alignment: Qt.AlignHCenter
                    Layout.preferredWidth: parent.width * 2 / 3
                }
                Button {
                    text: "Blue"
                    onClicked: teapot.color = "#103f8b"
                    Layout.alignment: Qt.AlignHCenter
                    Layout.preferredWidth: parent.width * 2 / 3
                }
                Slider {
                    id: speedSlider
                    from: 0.5
                    value: 1
                    to: 5
                    wheelEnabled: true
                    Layout.fillWidth: true
                }
                Text {
                    text: "speed: " + speedSlider.value.toFixed(1)
                }
                Slider {
                    id: clearcoatSlider
                    from: 0.0
                    value: 0.0
                    to: 1.0
                    wheelEnabled: true
                    Layout.fillWidth: true
                }
                Text {
                    text: "clearcoat: " + clearcoatSlider.value.toFixed(1)
                }
                CheckBox {
                    id: metalnessCheckBox
                    text: "Metallic"
                    checked: false
                }
    //! [xritem end]
            }
        }
    //! [xritem end]
    }

    Node {
        x: 150
        y: listviewItem.height + table.height + 5
        z: 40
        eulerRotation.y: -45
        XrItem {
            id: listviewItem
            width: 75
            height: 100
            x: -width
            color: "transparent"

            contentItem: Rectangle {
                color: Qt.rgba(1, 1, 1, 0.5)
                border.width: 5
                border.color: "lightblue"
                height: 400
                width: 300
                radius: 25

                ColorView {
                    id: colorView
                    anchors.fill: parent
                    anchors.margins: 25
                }
            }
        }
    }
}