File: main.qml

package info (click to toggle)
qt6-quick3d 6.8.2-5
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 140,860 kB
  • sloc: cpp: 380,464; ansic: 36,078; xml: 252; sh: 241; makefile: 29
file content (351 lines) | stat: -rw-r--r-- 13,140 bytes parent folder | download
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
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause

import QtCore

import QtQuick.Window
import QtQuick.Controls
import QtQuick.Layouts
import QtQuick.Dialogs

import QtQuick3D
import QtQuick3D.Helpers
import QtQuick3D.AssetUtils

ApplicationWindow {
    id: rootWindow
    visible: true
    width: 1280
    height: 720

    property url importUrl

    header: ToolBar {
        id: pane
        RowLayout {
            id: controlsLayout
            Button {
                id: importButton
                text: "Import..."
                onClicked: fileDialog.open()
            }
            Button {
                id: resetModel
                text: "ResetModel"
                onClicked: view3D.resetModel()
            }
            Button {
                id: resetButton
                text: "Reset view"
                onClicked: view3D.resetView()
            }
            Button {
                id: visualizeButton
                checkable: true
                text: "Visualize bounds"
            }
            Button {
                id: instancingButton
                checkable: true
                text: "Instancing"
            }
            Button {
                id: gridButton
                text: "Show grid"
                checkable: true
                checked: false
            }
            Button {
                id: controllerButton
                text: helper.orbitControllerEnabled ? "Orbit" : "WASD"
                onClicked: helper.switchController(!helper.orbitControllerEnabled)
            }
            RowLayout {
                Label {
                    text: "Material Override"
                }
                ComboBox {
                    id: materialOverrideComboBox
                    textRole: "text"
                    valueRole: "value"
                    implicitContentWidthPolicy: ComboBox.WidestText
                    onActivated: env.debugSettings.materialOverride = currentValue

                    Component.onCompleted: materialOverrideComboBox.currentIndex = materialOverrideComboBox.indexOfValue(env.debugSettings.materialOverride)

                    model: [
                        { value: DebugSettings.None, text: "None"},
                        { value: DebugSettings.BaseColor, text: "Base Color"},
                        { value: DebugSettings.Roughness, text: "Roughness"},
                        { value: DebugSettings.Metalness, text: "Metalness"},
                        { value: DebugSettings.Diffuse, text: "Diffuse"},
                        { value: DebugSettings.Specular, text: "Specular"},
                        { value: DebugSettings.ShadowOcclusion, text: "Shadow Occlusion"},
                        { value: DebugSettings.Emission, text: "Emission"},
                        { value: DebugSettings.AmbientOcclusion, text: "Ambient Occlusion"},
                        { value: DebugSettings.Normals, text: "Normals"},
                        { value: DebugSettings.Tangents, text: "Tangents"},
                        { value: DebugSettings.Binormals, text: "Binormals"},
                        { value: DebugSettings.F0, text: "F0"}
                    ]
                }
            }
            CheckBox {
                text: "Wireframe"
                checked: env.debugSettings.wireframeEnabled
                onCheckedChanged: {
                    env.debugSettings.wireframeEnabled = checked
                }
            }
            CheckBox {
                text: "Debug View"
                checked: debugView.visible
                onCheckedChanged: {
                    debugView.visible = checked
                }
            }
        }
    }

    SplitView {
        anchors.fill: parent

        SettingsPage {
            id: settingsPage
            camera: view3D.camera as PerspectiveCamera
            sceneEnvironment: env
            SplitView.preferredWidth: 400
            SplitView.minimumWidth: 350
            SplitView.fillHeight: true
            lutTexture: lutSourceTexture
        }

        View3D {
            id: view3D
            SplitView.fillWidth: true
            SplitView.fillHeight: true
            environment: ExtendedSceneEnvironment {
                id: env
                backgroundMode: SceneEnvironment.SkyBox
                lightProbe: Texture {
                    textureData: ProceduralSkyTextureData{}
                }
                InfiniteGrid {
                    visible: helper.gridEnabled
                    gridInterval: helper.gridInterval
                }
                skyboxBlurAmount: 0.1
                exposure: 1.0
                lensFlareBloomBias: 2.75
                lensFlareApplyDirtTexture: true
                lensFlareApplyStarburstTexture: true
                lensFlareCameraDirection: view3D.camera.forward
                lutTexture: lutSourceTexture

                Texture {
                    id: lutSourceTexture
                    source: "qrc:/luts/identity.png"
                }

                fog: Fog {
                }
            }

            camera: helper.orbitControllerEnabled ? orbitCamera : wasdCamera

            DirectionalLight {
                eulerRotation.x: -35
                eulerRotation.y: -90
                castsShadow: true
            }

            Node {
                id: orbitCameraNode
                PerspectiveCamera {
                    id: orbitCamera
                }
            }

            PerspectiveCamera {
                id: wasdCamera
                onPositionChanged: {
                    // Near/far logic copied from OrbitController
                    let distance = position.length()
                    if (distance < 1) {
                        clipNear = 0.01
                        clipFar = 100
                    } else if (distance < 100) {
                        clipNear = 0.1
                        clipFar = 1000
                    } else {
                        clipNear = 1
                        clipFar = 10000
                    }
                }
            }

            function resetView() {
                if (importNode.status !== RuntimeLoader.Error) {
                    helper.resetController()
                }
            }

            function resetModel() {
                importUrl = ""
                helper.updateBounds(defaultModel.bounds)
                resetView()
            }

            RandomInstancing {
                id: instancing
                instanceCount: 30
                position: InstanceRange {
                    property alias boundsDiameter: helper.boundsDiameter
                    from: Qt.vector3d(-3*boundsDiameter, -3*boundsDiameter, -3*boundsDiameter);
                    to: Qt.vector3d(3*boundsDiameter, 3*boundsDiameter, 3*boundsDiameter)
                }
                color: InstanceRange { from: "black"; to: "white" }
            }

            QtObject {
                id: helper
                property real boundsDiameter: 0
                property vector3d boundsCenter
                property vector3d boundsSize
                property bool orbitControllerEnabled: true
                property bool gridEnabled: gridButton.checked
                property real cameraDistance: orbitControllerEnabled ? orbitCamera.z : wasdCamera.position.length()
                property real gridInterval: Math.pow(10, Math.round(Math.log10(cameraDistance)) - 1)

                function updateBounds(bounds) {
                    boundsSize = Qt.vector3d(bounds.maximum.x - bounds.minimum.x,
                                             bounds.maximum.y - bounds.minimum.y,
                                             bounds.maximum.z - bounds.minimum.z)
                    boundsDiameter = Math.max(boundsSize.x, boundsSize.y, boundsSize.z)
                    boundsCenter = Qt.vector3d((bounds.maximum.x + bounds.minimum.x) / 2,
                                               (bounds.maximum.y + bounds.minimum.y) / 2,
                                               (bounds.maximum.z + bounds.minimum.z) / 2 )

                    wasdController.speed = boundsDiameter / 1000.0
                    wasdController.shiftSpeed = 3 * wasdController.speed
                    wasdCamera.clipNear = boundsDiameter / 100
                    wasdCamera.clipFar = boundsDiameter * 10
                    view3D.resetView()
                }

                function resetController() {
                    orbitCameraNode.eulerRotation = Qt.vector3d(-5, 0, 0)
                    orbitCameraNode.position = boundsCenter
                    orbitCamera.position = Qt.vector3d(0, 0, 2 * helper.boundsDiameter)
                    orbitCamera.eulerRotation = Qt.vector3d(0, 0, 0)
                    orbitControllerEnabled = true
                }

                function switchController(useOrbitController) {
                    if (useOrbitController) {
                        let wasdOffset = wasdCamera.position.minus(boundsCenter)
                        let wasdDistance = wasdOffset.length()
                        let wasdDistanceInPlane = Qt.vector3d(wasdOffset.x, 0, wasdOffset.z).length()
                        let yAngle = Math.atan2(wasdOffset.x, wasdOffset.z) * 180 / Math.PI
                        let xAngle = -Math.atan2(wasdOffset.y, wasdDistanceInPlane) * 180 / Math.PI

                        orbitCameraNode.position = boundsCenter
                        orbitCameraNode.eulerRotation = Qt.vector3d(xAngle, yAngle, 0)
                        orbitCamera.position = Qt.vector3d(0, 0, wasdDistance)

                        orbitCamera.eulerRotation = Qt.vector3d(0, 0, 0)
                    } else {
                        wasdCamera.position = orbitCamera.scenePosition
                        wasdCamera.rotation = orbitCamera.sceneRotation
                        wasdController.focus = true
                    }
                    orbitControllerEnabled = useOrbitController
                }
            }

            RuntimeLoader {
                id: importNode
                source: rootWindow.importUrl
                instancing: instancingButton.checked ? instancing : null
                onBoundsChanged: helper.updateBounds(bounds)
            }

            Model {
                id: defaultModel
                source: "#Sphere"
                visible: importNode.status === RuntimeLoader.Empty
                instancing: instancingButton.checked ? instancing : null
                onBoundsChanged: helper.updateBounds(bounds)
                materials: PrincipledMaterial {
                    baseColor: "green"
                }

                scale: Qt.vector3d(helper.boundsSize.x / 100,
                                   helper.boundsSize.y / 100,
                                   helper.boundsSize.z / 100)
            }

            Model {
                parent: importNode
                source: "#Cube"
                materials: PrincipledMaterial {
                    baseColor: "red"
                }
                opacity: 0.2
                visible: visualizeButton.checked && importNode.status !== RuntimeLoader.Error
            }

            Rectangle {
                id: messageBox
                visible: importNode.status === RuntimeLoader.Error
                color: "red"
                width: parent.width * 0.8
                height: parent.height * 0.8
                anchors.centerIn: parent
                radius: Math.min(width, height) / 10
                opacity: 0.6
                Text {
                    anchors.fill: parent
                    font.pixelSize: 36
                    text: "Status: " + importNode.errorString + "\nPress \"Import...\" to import a model"
                    color: "white"
                    wrapMode: Text.Wrap
                    horizontalAlignment: Text.AlignHCenter
                    verticalAlignment: Text.AlignVCenter
                }
            }

            OrbitCameraController {
                id: orbitController
                origin: orbitCameraNode
                anchors.fill: parent
                camera: orbitCamera
                enabled: helper.orbitControllerEnabled
            }
            WasdController {
                id: wasdController
                anchors.fill: parent
                controlledObject: wasdCamera
                enabled: !helper.orbitControllerEnabled
            }
            DebugView {
                id: debugView
                anchors.top: parent.top
                anchors.right: parent.right
                source: view3D
                visible: false
            }
        }
    }

    FileDialog {
        id: fileDialog
        nameFilters: ["glTF files (*.gltf *.glb)", "All files (*)"]
        onAccepted: importUrl = selectedFile
        Settings {
            id: fileDialogSettings
            category: "QtQuick3D.Examples.RuntimeLoader"
            property alias currentFolder: fileDialog.currentFolder
        }
    }
}