aboutsummaryrefslogtreecommitdiffstats
path: root/examples/quick3dphysics/customshapes/main.qml
blob: 64c12165b4746b5adfdef9bbf51c2ce506dd1a99 (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) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
import QtQuick
import QtQuick3D
import QtQuick3D.Physics
import QtQuick3D.Helpers
import QtQuick.Controls
import QtQuick.Layouts

//! [window]
Window {
    width: 1280
    height: 720
    visible: true
    title: qsTr("Qt Quick 3D Physics - Custom Shapes")

    //! [world]
    PhysicsWorld {
        id: physicsWorld
        running: true
        typicalLength: 2
        enableCCD: true
        maximumTimestep: 20
        scene: viewport.scene
    }
    //! [world]

    View3D {
        id: viewport
        anchors.fill: parent

        //! [environment]
        environment: SceneEnvironment {
            clearColor: "white"
            backgroundMode: SceneEnvironment.SkyBox
            antialiasingMode: SceneEnvironment.MSAA
            antialiasingQuality: SceneEnvironment.High
            lightProbe: proceduralSky
        }
        //! [environment]

        //! [textures]
        Texture {
            id: proceduralSky
            textureData: ProceduralSkyTextureData {
                sunLongitude: -115
            }
        }

        Texture {
            id: weaveNormal
            source: "maps/weave.png"
            scaleU: 200
            scaleV: 200
            generateMipmaps: true
            mipFilter: Texture.Linear
        }

        Texture {
            id: numberNormal
            source: "maps/numbers-normal.png"
        }

        Texture {
            id: numberFill
            source: "maps/numbers.png"
            generateMipmaps: true
            mipFilter: Texture.Linear
        }
        //! [textures]

        Node {
            //! [scene]
            id: scene
            scale: Qt.vector3d(2, 2, 2)
            PerspectiveCamera {
                id: camera
                position: Qt.vector3d(-45, 25, 60)
                eulerRotation: Qt.vector3d(-6, -33, 0)
                clipFar: 1000
                clipNear: 0.1
            }

            DirectionalLight {
                eulerRotation: Qt.vector3d(-45, 25, 0)
                castsShadow: true
                brightness: 1
                shadowMapQuality: Light.ShadowMapQualityHigh
                pcfFactor: 0.1
                shadowBias: 1
            }
            //! [scene]

            //! [cloth]
            StaticRigidBody {
                position: Qt.vector3d(-15, -8, 0)
                id: tablecloth

                Model {
                    geometry: HeightFieldGeometry {
                        id: tableclothGeometry
                        extents: Qt.vector3d(150, 20, 150)
                        source: "maps/cloth-heightmap.png"
                        smoothShading: false
                    }
                    materials: PrincipledMaterial {
                        baseColor: "#447722"
                        roughness: 0.8
                        normalMap: weaveNormal
                        normalStrength: 0.7
                    }
                }

                collisionShapes: HeightFieldShape {
                    id: hfShape
                    extents: tableclothGeometry.extents
                    source: "maps/cloth-heightmap.png"
                }
            }
            //! [cloth]

            //! [cup]
            DynamicRigidBody {
                id: diceCup
                isKinematic: true
                mass: 0
                property vector3d bottomPos: Qt.vector3d(11, 6, 0)
                property vector3d topPos: Qt.vector3d(11, 45, 0)
                property vector3d unloadPos: Qt.vector3d(0, 45, 0)
                position: bottomPos
                kinematicPivot: Qt.vector3d(0, 6, 0)
                kinematicPosition: bottomPos
                collisionShapes: TriangleMeshShape {
                    id: cupShape
                    source: "meshes/simpleCup.mesh"
                }
                Model {
                    source: "meshes/cup.mesh"
                    materials: PrincipledMaterial {
                        baseColor: "#cc9988"
                        roughness: 0.3
                        metalness: 1
                    }
                }
            }
            //! [cup]

            //! [tower]
            StaticRigidBody {
                id: diceTower
                x: -4
                Model {
                    id: testModel
                    source: "meshes/tower.mesh"
                    materials: [
                        PrincipledMaterial {
                            baseColor: "#ccccce"
                            roughness: 0.3
                        },
                        PrincipledMaterial {
                            id: glassMaterial
                            baseColor: "#aaaacc"
                            transmissionFactor: 0.95
                            thicknessFactor: 1
                            roughness: 0.05
                        }
                    ]
                }
                collisionShapes: TriangleMeshShape {
                    id: triShape
                    source: "meshes/tower.mesh"
                }
            }
            //! [tower]

            //! [dice]
            Component {
                id: diceComponent

                DynamicRigidBody {
                    id: thisBody
                    function randomInRange(min, max) {
                        return Math.random() * (max - min) + min
                    }

                    function restore() {
                        reset(initialPosition, eulerRotation)
                    }

                    scale: Qt.vector3d(scaleFactor, scaleFactor, scaleFactor)
                    eulerRotation: Qt.vector3d(randomInRange(0, 360),
                                               randomInRange(0, 360),
                                               randomInRange(0, 360))

                    property vector3d initialPosition: Qt.vector3d(11 + 1.5 * Math.cos(index/(Math.PI/4)),
                                                                   diceCup.bottomPos.y + index * 1.5,
                                                                   0)
                    position: initialPosition

                    property real scaleFactor: randomInRange(0.8, 1.4)
                    property color baseCol: Qt.hsla(randomInRange(0, 1),
                                                    randomInRange(0.6, 1.0),
                                                    randomInRange(0.4, 0.7),
                                                    1.0)

                    collisionShapes: ConvexMeshShape {
                        id: diceShape
                        source: Math.random() < 0.25 ? "meshes/icosahedron.mesh"
                              : Math.random() < 0.5 ? "meshes/dodecahedron.mesh"
                              : Math.random() < 0.75 ? "meshes/octahedron.mesh"
                                                     : "meshes/tetrahedron.mesh"
                    }

                    Model {
                        id: thisModel
                        source: diceShape.source
                        receivesShadows: false
                        materials: PrincipledMaterial {
                            metalness: 1.0
                            roughness: randomInRange(0.2, 0.6)
                            baseColor: baseCol
                            emissiveMap: numberFill
                            emissiveFactor: Qt.vector3d(1, 1, 1)
                            normalMap: numberNormal
                            normalStrength: 0.75
                        }
                    }
                }
            }

            Repeater3D {
                id: dicePool
                model: 25
                delegate: diceComponent
                function restore() {
                    for (var i = 0; i < count; i++) {
                        objectAt(i).restore()
                    }
                }
            }
            //! [dice]

            //! [animation]
            Connections {
                target: physicsWorld
                property real totalAnimationTime: 7500
                function onFrameDone(timeStep) {
                    let progressStep = timeStep / totalAnimationTime
                    animationController.progress += progressStep
                    if (animationController.progress >= 1) {
                        animationController.completeToEnd()
                        animationController.reload()
                        animationController.progress = 0
                    }
                }
            }

            AnimationController {
                id: animationController
                animation: SequentialAnimation {
                    PauseAnimation { duration: 2500 }
                    PropertyAnimation {
                        target: diceCup
                        property: "kinematicPosition"
                        to: diceCup.topPos
                        duration: 2500
                    }
                    ParallelAnimation {
                        PropertyAnimation {
                            target: diceCup
                            property: "kinematicEulerRotation.z"
                            to: 130
                            duration: 1500
                        }
                        PropertyAnimation {
                            target: diceCup
                            property: "kinematicPosition"
                            to: diceCup.unloadPos
                            duration: 1500
                        }
                    }
                    PauseAnimation { duration: 1000 }
                    ParallelAnimation {
                        PropertyAnimation {
                            target: diceCup
                            property: "kinematicEulerRotation.z"
                            to: 0
                            duration: 1500
                        }
                        PropertyAnimation {
                            target: diceCup
                            property: "kinematicPosition"
                            to: diceCup.topPos
                            duration: 1500
                        }
                    }
                    PropertyAnimation { target: diceCup; property: "kinematicPosition"; to: diceCup.bottomPos; duration: 1500 }
                    PauseAnimation { duration: 2000 }
                    ScriptAction { script: dicePool.restore() }
                }
            }
            //! [animation]
        } // scene
    } // View3D

    //! [controller]
    WasdController {
        keysEnabled: true
        controlledObject: camera
        speed: 0.2
    }
    //! [controller]
}
//! [window]