aboutsummaryrefslogtreecommitdiffstats
path: root/tools/lightmapviewer/MeshViewer.qml
blob: 5a3acbe4da8d3ef93c83f2c6a3d39f6a2cef5964 (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
// Copyright (C) 2025 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
import QtQuick.Window
import QtQuick.Dialogs
import QtQuick3D
import QtQuick3D.Helpers

import QtQuick3D.lightmapviewer
import LightmapFile 1.0

ColumnLayout {
    id: root
    anchors.fill: parent

    property string selectedModelCandidate: ""

    onSelectedModelCandidateChanged: {
        if (!selectedModelCandidate || selectedModelCandidate === "") {
            view.lmTextureCandidates = []
            lview.mSelectedTextureCandidate = -1
            return
        }
        view.lmTextureCandidates = LightmapFile.texturesAvailableFor(
                    selectedModelCandidate)
        view.lmSelectedTextureCandidate
                = view.lmTextureCandidates.length ? view.lmTextureCandidates[0].value : -1
    }

    Pane {
        Layout.fillWidth: true
        padding: 8

        ColumnLayout {
            width: parent.width
            spacing: 6

            ColumnLayout {
                RowLayout {
                    Label {
                        text: "Key:"
                    }
                    ComboBox {
                        id: comboLmModelCandidate
                        Layout.preferredWidth: 220
                        model: view.lmModelCandidates
                        onActivated: root.selectedModelCandidate = currentText
                        currentIndex: {
                            const i = view.lmModelCandidates.indexOf(
                                        root.selectedModelCandidate)
                            return (i >= 0 ? i : -1)
                        }
                        enabled: !!selectedEntry
                                 && selectedEntry.kind === "mesh"
                    }
                    Label {
                        text: "Texture:"
                    }
                    ComboBox {
                        id: comboLmTextureCandidate
                        Layout.preferredWidth: 220
                        model: view.lmTextureCandidates
                        textRole: "name"
                        valueRole: "value"

                        function indexForValue(val) {
                            for (var i = 0; i < view.lmTextureCandidates.length; ++i)
                                if (view.lmTextureCandidates[i].value === val)
                                    return i
                            return -1
                        }

                        onActivated: view.lmSelectedTextureCandidate = Number(
                                         currentValue)
                        currentIndex: indexForValue(
                                          view.lmSelectedTextureCandidate)
                        enabled: !!selectedEntry
                                 && selectedEntry.kind === "mesh"
                    }
                }

                RowLayout {
                    CheckBox {
                        id: checkboxBfCull
                        text: "Backface Culling"
                        checked: true
                    }
                    CheckBox {
                        id: checkboxDebugUV
                        text: "Debug UV"
                        checked: false
                    }
                }
            }
        }
    }

    View3D {
        id: view
        Layout.fillWidth: true
        Layout.fillHeight: true
        enabled: meshLoader.visible
        visible: meshLoader.visible

        property var lmModelCandidates: []
        property var lmTextureCandidates: []
        property int lmSelectedTextureCandidate: -1

        property real boundsDiameter: 0
        property vector3d boundsCenter
        property vector3d boundsSize

        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)
            model.position = Qt.vector3d(-boundsCenter.x, -boundsCenter.y,
                                         -boundsCenter.z)
            cameraNode.clipNear = boundsDiameter / 100
            cameraNode.clipFar = boundsDiameter * 10
            resetCamera()
        }

        function resetCamera() {
            cameraNode.position = boundsCenter
            cameraNode.position = Qt.vector3d(0, 0, 2 * boundsDiameter)
            cameraNode.eulerRotation = Qt.vector3d(0, 0, 0)
        }

        function refreshLightmapSelection() {
            if (!selectedEntry) {
                lmModelCandidates = []
                root.selectedModelCandidate = ""
                lmTextureCandidates = []
                lmSelectedTextureCandidate = -1
                return
            }

            if (selectedEntry.kind === "image") {
                lmModelCandidates = [selectedEntry.key]
                root.selectedModelCandidate = selectedEntry.key
            } else if (selectedEntry.kind === "mesh") {
                lmModelCandidates = LightmapFile.keysReferencingMesh(
                            selectedEntry.key)
                root.selectedModelCandidate = lmModelCandidates.length ? lmModelCandidates[0] : ""
            } else {
                lmModelCandidates = []
                root.selectedModelCandidate = ""
                lmTextureCandidates = []
                lmSelectedTextureCandidate = -1
            }
        }

        Component.onCompleted: refreshLightmapSelection()
        Connections {
            target: window
            function onSelectedEntryChanged() {
                view.refreshLightmapSelection()
            }
        }

        environment: SceneEnvironment {
            backgroundMode: SceneEnvironment.Color
            clearColor: "black"
        }

        PerspectiveCamera {
            id: cameraNode
            z: 300
        }

        Node {
            id: modelNode

            Model {
                id: model
                geometry: LightmapMesh {
                    source: LightmapFile.source
                    key: selectedEntry.key
                    onBoundsChanged: view.updateBounds(bounds)
                }
                materials: CustomMaterial {
                    shadingMode: CustomMaterial.Unshaded
                    cullMode: checkboxBfCull.checked ? Material.BackFaceCulling : Material.NoCulling

                    property TextureInput baseMap: TextureInput {
                        texture: Texture {
                            id: lmTexture
                            minFilter: Texture.Linear
                            magFilter: Texture.Linear
                            mipFilter: Texture.None
                            tilingModeHorizontal: Texture.ClampToEdge
                            tilingModeVertical: Texture.ClampToEdge

                            textureData: LightmapFile.textureDataFor(
                                             root.selectedModelCandidate,
                                             view.lmSelectedTextureCandidate)
                        }
                    }
                    property bool debugUV: checkboxDebugUV.checked

                    vertexShader: "mesh.vert"
                    fragmentShader: "mesh.frag"
                }
            }
        }

        ArcballController {
            id: arcballController
            controlledObject: modelNode

            function jumpToAxis(axis) {
                cameraRotation.from = arcballController.controlledObject.rotation
                cameraRotation.to = originGizmo.quaternionForAxis(
                            axis, arcballController.controlledObject.rotation)
                cameraRotation.duration = 200
                cameraRotation.start()
            }

            function jumpToRotation(qRotation) {
                cameraRotation.from = arcballController.controlledObject.rotation
                cameraRotation.to = qRotation
                cameraRotation.duration = 100
                cameraRotation.start()
            }

            QuaternionAnimation {
                id: cameraRotation
                target: arcballController.controlledObject
                property: "rotation"
                type: QuaternionAnimation.Slerp
                running: false
                loops: 1
            }
        }

        OriginGizmo {
            id: originGizmo
            anchors.top: parent.top
            anchors.right: parent.right
            anchors.margins: 10
            width: 120
            height: 120
            targetNode: modelNode

            onAxisClicked: axis => {
                               arcballController.jumpToAxis(axis)
                           }
        }

        DragHandler {
            id: dragHandler
            target: null
            acceptedModifiers: Qt.NoModifier
            onCentroidChanged: {
                arcballController.mouseMoved(toNDC(centroid.position.x,
                                                   centroid.position.y))
            }

            onActiveChanged: {
                if (active) {
                    view.forceActiveFocus()
                    arcballController.mousePressed(toNDC(centroid.position.x,
                                                         centroid.position.y))
                } else
                    arcballController.mouseReleased(toNDC(centroid.position.x,
                                                          centroid.position.y))
            }

            function toNDC(x, y) {
                return Qt.vector2d((2.0 * x / width) - 1.0,
                                   1.0 - (2.0 * y / height))
            }
        }

        WheelHandler {
            id: wheelHandler
            orientation: Qt.Vertical
            target: null
            acceptedDevices: PointerDevice.Mouse | PointerDevice.TouchPad
            onWheel: event => {
                         let delta = -event.angleDelta.y * 0.01
                         cameraNode.z += cameraNode.z * 0.1 * delta
                     }
        }

        Keys.onPressed: event => {
                            if (event.key === Qt.Key_Space) {
                                let rotation = originGizmo.quaternionAlign(
                                    arcballController.controlledObject.rotation)
                                arcballController.jumpToRotation(rotation)
                            } else if (event.key === Qt.Key_S) {
                                settingsPane.toggleHide()
                            } else if (event.key === Qt.Key_Left
                                       || event.key === Qt.Key_A) {
                                let rotation = originGizmo.quaternionRotateLeft(
                                    arcballController.controlledObject.rotation)
                                arcballController.jumpToRotation(rotation)
                            } else if (event.key === Qt.Key_Right
                                       || event.key === Qt.Key_D) {
                                let rotation = originGizmo.quaternionRotateRight(
                                    arcballController.controlledObject.rotation)
                                arcballController.jumpToRotation(rotation)
                            }
                        }
    }
}