File: main.qml

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

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

Item {
    id: root
    property url modelSrc: "meshes/cube.mesh"

    Rectangle {
        color: "lightGray"
        width: parent.width / 2
        height: parent.height

        View3D {
            id: lightmapSource
            renderMode: View3D.Offscreen // so a texture provider as well
            anchors.fill: parent
            anchors.margins: 4

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

            DirectionalLight {
            }

            PerspectiveCamera {
                id: camera1
                z: 200
            }

            Texture {
                id: baseTex
                source: "hskin.png"
                generateMipmaps: true
                mipFilter: Texture.Linear
            }

            PrincipledMaterial {
                id: principledMat
                roughness: 0.4
                baseColorMap: baseTex
            }

            CustomMaterial {
                id: customMat
                shadingMode: CustomMaterial.Shaded
                fragmentShader: "lightmapgen.frag"
                property TextureInput tex: TextureInput {
                    texture: baseTex
                }
            }

            CustomMaterial {
                id: lightmapGenMat
                shadingMode: CustomMaterial.Shaded
                vertexShader: "lightmapgen.vert"
                fragmentShader: "lightmapgen.frag"
                property bool useUV1: radioLightmapGen1.checked
                property TextureInput tex: TextureInput {
                    texture: baseTex
                }
                cullMode: Material.NoCulling
                // sourceBlend: CustomMaterial.SrcAlpha
                // destinationBlend: CustomMaterial.OneMinusSrcAlpha
            }

            Model {
                source: root.modelSrc
                scale: Qt.vector3d(10, 10, 10)
                NumberAnimation on eulerRotation.y {
                    running: cbRotate.checked
                    from: 0; to: 360; duration: 10000; loops: -1
                }
                materials: radioPrincipled.checked
                    ? principledMat
                    : (radioCustom.checked ? customMat : lightmapGenMat)
            }

            WasdController {
                controlledObject: camera1
            }
        }
    }

    View3D {
        width: parent.width / 2
        height: parent.height
        x: parent.width / 2

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

        camera: camera1

        Model {
            source: root.modelSrc
            scale: Qt.vector3d(10, 10, 10)
            NumberAnimation on eulerRotation.y {
                running: cbRotate.checked
                from: 0; to: 360; duration: 10000; loops: -1
            }
            materials: CustomMaterial {
                shadingMode: CustomMaterial.Unshaded
                vertexShader: "lightmapuse.vert"
                fragmentShader: "lightmapuse.frag"
                property TextureInput tex: TextureInput {
                    texture: Texture {
                        sourceItem: lightmapSource
                        minFilter: Texture.Nearest
                        magFilter: Texture.Nearest
                    }
                }
            }
        }
    }

    Rectangle {
        id: controlPanelBackground
        x: 4
        y: 4
        color: "white"
        opacity: 0.5
        width: controlPanel.implicitWidth
        height: controlPanel.implicitHeight
    }
    Button {
        anchors.top: controlPanelBackground.top
        anchors.right: controlPanelBackground.right
        text: "Toggle"
        onClicked: {
            controlPanelBackground.visible = !controlPanelBackground.visible;
            controlPanel.visible = !controlPanel.visible;
        }
        focusPolicy: Qt.NoFocus
    }
    ColumnLayout {
        id: controlPanel
        x: 4
        y: 4
        RowLayout {
            Label {
                text: "Model"
            }
            ComboBox {
                model: [ "Cube",
                         "Sphere",
                         "Torus",
                         "Suzanne",
                         "Animal" ]
                onCurrentIndexChanged: {
                    var meshes = [ "meshes/cube.mesh",
                                   "meshes/sphere.mesh",
                                   "meshes/torus.mesh",
                                   "meshes/suzanne.mesh",
                                   "meshes/animal.mesh" ];
                    root.modelSrc = meshes[currentIndex]
                }
                focusPolicy: Qt.NoFocus
            }
        }
        RadioButton {
            id: radioPrincipled
            text: "PrincipledMaterial"
            checked: true
            focusPolicy: Qt.NoFocus
        }
        RadioButton {
            id: radioCustom
            text: "Custom material\nJust for verification; should look identical to Principled"
            checked: false
            focusPolicy: Qt.NoFocus
        }
        RadioButton {
            id: radioLightmapGen0
            text: "Lightmap generator custom material\nusing UV0"
            checked: false
            focusPolicy: Qt.NoFocus
        }
        RadioButton {
            id: radioLightmapGen1
            text: "Lightmap generator custom material\nusing UV1"
            checked: false
            focusPolicy: Qt.NoFocus
        }
        CheckBox {
            id: cbRotate
            text: "Rotate model\nto demo how the lightmap changes"
            checked: false
            focusPolicy: Qt.NoFocus
        }
    }

    Text {
        x: parent.width / 2
        text: "DefaultMaterial with no lighting,\njust a diffuseMap with UV1 and the texture of the left view.\nCamera is the same (use WASD to move)."
    }

    Text {
        anchors.right: parent.right
        anchors.margins: 10
        color: "red"
        style: Text.Outline
        font.pixelSize: 28
        text: {
            if (GraphicsInfo.api === GraphicsInfo.OpenGL)
                "OpenGL";
            else if (GraphicsInfo.api === GraphicsInfo.Direct3D11)
                "D3D11";
            else if (GraphicsInfo.api === GraphicsInfo.Vulkan)
                "Vulkan";
            else if (GraphicsInfo.api === GraphicsInfo.Metal)
                "Metal";
            else if (GraphicsInfo.api === GraphicsInfo.Null)
                "Null";
            else
                "Unknown";
        }
    }
}