aboutsummaryrefslogtreecommitdiffstats
path: root/tests/manual/multicubetest/main.qml
blob: c2c70981ecfbfc1301a5cfe1780496fd39f708c9 (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
import QtQuick
import QtQuick.Window
import QtQuick3D
import QtQuick3D.Helpers

Window {
    id: window
    visible: true
    width: 640
    height: 480
    title: qsTr("10,000 Cubes (draw call overhead)")

    property int countX: 25
    property int countY: 20
    property int countZ: 20

    property vector3d cubeRotation: Qt.vector3d(0, 0, 0)

    // Uncomment to see performance when animating 10,000 cubes individually
//    NumberAnimation {
//        target: window
//        property: "cubeRotation.x"
//        duration: 1000
//        from: 0
//        to: 360
//        running: true
//        loops: -1
//    }

    Rectangle {
        anchors.fill: parent
        gradient: Gradient {
                GradientStop { position: 0.0; color: "#d2ff52" }
                GradientStop { position: 1.0; color: "#91e842" }
        }
        Text {
            anchors.fill: parent
            text: countX * countY * countZ
            fontSizeMode: Text.Fit
            color: "white"
            minimumPointSize: 128
            font.pointSize: 512
            horizontalAlignment: Text.AlignHCenter
            verticalAlignment: Text.AlignVCenter
            renderType: Text.NativeRendering
        }
    }

    Component {
        id: simpleCube
        Model {
            source: "#Cube"
            scale: Qt.vector3d(0.1, 0.1, 0.1)
            eulerRotation: window.cubeRotation
            materials: PrincipledMaterial {
                baseColor: "white"
            }
        }
    }


    View3D {
        id: view
        anchors.fill: parent

        DirectionalLight {
            color: "red"

        }

        DirectionalLight {
            color: "green"
            eulerRotation: Qt.vector3d(0, 180, 0)
        }

        Node {
            id: cameraSpinner
            position: Qt.vector3d(0, 0, 0);


            PerspectiveCamera {
                id: camera
                position: Qt.vector3d(0, 0, 700)
            }

            eulerRotation: Qt.vector3d(0, 90, 0)


            SequentialAnimation on eulerRotation.y {
                loops: Animation.Infinite
                PropertyAnimation {
                    duration: 10000;
                    from: 0
                    to: 360
                }
            }
        }

        Keys.onPressed: (event)=> {
            switch (event.key) {
                case Qt.Key_C:
                    camera.frustumCullingEnabled = !camera.frustumCullingEnabled
                    break;
                case Qt.Key_Up:
                    camera.position.z -= 100
                    break;
                case Qt.Key_Down:
                    camera.position.z += 100
                    break;
            }
       }
    }

    Timer {
        running: true
        onTriggered: {
            let spacing = 20.0;
            let offsetX = -spacing * countX * 0.5;
            let offsetY = -spacing * countY * 0.5;
            let offsetZ = spacing * countZ * 0.5;

            frustumCullingComponent.createObject(debugView.layout)

            for(var x = 0; x < countX; ++x) {
                for(var y = 0; y < countY; ++y) {
                    for(var z = 0; z < countZ; ++z) {
                        let opacity = (z % 2) ? 1.0 : 0.5;
                        let posX = offsetX + x * spacing;
                        let posY = offsetY + y * spacing;
                        let posZ = offsetZ - z * spacing;

                        simpleCube.createObject(view.scene, {"x": posX, "y": posY, "z": posZ, "opacity": opacity})
                    }
                }
            }
        }
    }

    Component {
        id: frustumCullingComponent
        Text {
           text: "Frustum culling: " + camera.frustumCullingEnabled
           font.pointSize: 9
           color: "white"
       }
    }

    DebugView {
        id: debugView
        source: view
    }
}