Debian Codegraph @

tests/manual/compute-manual/ParticlesScene.qml

raw
1 // Copyright (C) 2019 Klaralvdalens Datakonsult AB (KDAB).
2 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4 import Qt3D.Core 2.14
5 import Qt3D.Render 2.14
6 import Qt3D.Extras 2.14
7 import Qt3D.Input 2.14
8
9 Entity {
10
11 property alias computeMode: particlesComputeJob.runType
12
13 function triggerCompute(frames) {
14 particlesComputeJob.trigger(frames)
15 }
16
17 components: [
18 RenderSettings {
19 ComputeFrameGraph {
20 camera: sceneCamera
21 }
22 // explicitly set RenderingPolicy to AlwaysRender, as changes in the
23 // scene won't be reflected in actual Qt scene-graph changes (due to
24 // GPU compute calls)
25 renderPolicy: RenderSettings.Always
26 },
27 InputSettings {}
28 ]
29
30 FirstPersonCameraController { camera: sceneCamera }
31
32 Camera {
33 id: sceneCamera
34 projectionType: CameraLens.PerspectiveProjection
35 viewCenter: Qt.vector3d(0, 0, 0)
36 position: Qt.vector3d(0, 20, -800)
37 nearPlane: 0.1
38 farPlane: 1000
39 fieldOfView: 60
40 }
41
42 property int particlesCount: 1024 * 1024
43 readonly property int floatSize: 4
44
45 function buildParticlesBuffer() {
46 var byteSizeOfParticleData = 8;
47 var bufferData = new Float32Array(particlesCount * byteSizeOfParticleData);
48 for (var x = 0; x < 1024; ++x) {
49 for (var z = 0; z < 1024; ++z) {
50 var i = x * 1024 + z;
51 var positionIdx = i * byteSizeOfParticleData;
52 var colorIdx = i * byteSizeOfParticleData + 8;
53
54 bufferData[positionIdx] = -1024 + x * 2;
55 bufferData[positionIdx + 1] = 0;
56 bufferData[positionIdx + 2] = -1024 + z * 2;
57 bufferData[positionIdx + 3] = 1.0;
58
59 bufferData[colorIdx] = 0.25 + Math.sin(x / 1024) * 0.75;
60 bufferData[colorIdx + 1] = 0.25 + Math.sin(z / 1024) * 0.75;
61 bufferData[colorIdx + 2] = 0.25 + Math.sin(x / 1024) * 0.75;
62 bufferData[colorIdx + 3] = 1.0;
63 }
64 }
65 return bufferData
66 }
67
68 Buffer {
69 id: particleBuffer
70 // struct ParticleData
71 // {
72 // vec3 position; // Aligned to 4 floats
73 // vec3 color; // Aligned to 4 floats
74 // };
75 data: buildParticlesBuffer()
76 }
77
78 Attribute {
79 id: particlePositionDataAttribute
80 name: "particlePosition"
81 attributeType: Attribute.VertexAttribute
82 vertexBaseType: Attribute.Float
83 vertexSize: 3
84 divisor: 1
85 byteStride: 8 * floatSize
86 buffer: particleBuffer
87 }
88
89 Attribute {
90 id: particleColorDataAttribute
91 name: "particleColor"
92 attributeType: Attribute.VertexAttribute
93 vertexBaseType: Attribute.Float
94 vertexSize: 3
95 divisor: 1
96 byteOffset: 4 * floatSize
97 byteStride: 8 * floatSize
98 buffer: particleBuffer
99 }
100
101 ComputeMaterial {
102 id: computeMaterial
103 dataBuffer: particleBuffer
104 }
105
106 ComputeCommand {
107 id: particlesComputeJob
108 onRunTypeChanged: {
109 // Don't forget to re-enable the command
110 // when switching back to continuous
111 if (runType === ComputeCommand.Continuous)
112 enabled = true
113 }
114 }
115
116 Entity {
117 id: particleComputeEntity
118 components: [
119 particlesComputeJob,
120 computeMaterial
121 ]
122 }
123
124 SphereGeometry {
125 id: sphereGeometry
126 rings: 10
127 slices: 10
128 radius: 1
129 // Additional Attributes
130 attributes: [
131 particlePositionDataAttribute,
132 particleColorDataAttribute
133 ]
134 }
135
136 Entity {
137 id: particleRenderEntity
138 readonly property GeometryRenderer particlesRenderer: GeometryRenderer {
139 instanceCount: particlesCount
140 indexOffset: 0
141 firstInstance: 0
142 primitiveType: GeometryRenderer.Triangles
143 geometry: sphereGeometry
144 }
145
146 components: [
147 particlesRenderer,
148 computeMaterial
149 ]
150 }
151 }
152