| 1 |
// Copyright (C) 2015 Klaralvdalens Datakonsult AB (KDAB). |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 |
|
| 4 |
import Qt3D.Core 2.0 |
| 5 |
import Qt3D.Render 2.0 |
| 6 |
import Qt3D.Extras 2.0 |
| 7 |
|
| 8 |
Entity { |
| 9 |
property alias particleStep: computeMaterial.particleStep |
| 10 |
property alias finalCollisionFactor: computeMaterial.finalCollisionFactor |
| 11 |
|
| 12 |
readonly property int _SPHERE: 0 |
| 13 |
readonly property int _CUBE: 1 |
| 14 |
readonly property int _CYLINDER: 2 |
| 15 |
readonly property int _TORUS: 3 |
| 16 |
|
| 17 |
property int particlesShape: _SPHERE |
| 18 |
|
| 19 |
signal reset() |
| 20 |
|
| 21 |
components: [ |
| 22 |
RenderSettings { |
| 23 |
ComputeFrameGraph { |
| 24 |
camera: sceneCamera |
| 25 |
} |
| 26 |
// explicitly set RenderingPolicy to AlwaysRender, as changes in the |
| 27 |
// scene won't be reflected in actual Qt scene-graph changes (due to |
| 28 |
// GPU compute calls) |
| 29 |
renderPolicy: RenderSettings.Always |
| 30 |
} |
| 31 |
] |
| 32 |
|
| 33 |
FirstPersonCameraController { camera: sceneCamera } |
| 34 |
|
| 35 |
Camera { |
| 36 |
id: sceneCamera |
| 37 |
projectionType: CameraLens.PerspectiveProjection |
| 38 |
viewCenter: Qt.vector3d(0, 0, 0) |
| 39 |
position: Qt.vector3d(0, 0, -800) |
| 40 |
nearPlane: 0.1 |
| 41 |
farPlane: 1000 |
| 42 |
fieldOfView: 25 |
| 43 |
aspectRatio: 1.33 |
| 44 |
} |
| 45 |
|
| 46 |
property int particlesCount: 50 * 1024 |
| 47 |
readonly property int floatSize: 4 |
| 48 |
|
| 49 |
function buildParticlesBuffer() { |
| 50 |
var byteSizeOfParticleData = 12; |
| 51 |
var bufferData = new Float32Array(particlesCount * byteSizeOfParticleData); |
| 52 |
var factor = 500.0; |
| 53 |
for (var i = 0; i < particlesCount; ++i) { |
| 54 |
var positionIdx = i * byteSizeOfParticleData; |
| 55 |
var velocityIdx = i * byteSizeOfParticleData + 4; |
| 56 |
var colorIdx = i * byteSizeOfParticleData + 8; |
| 57 |
|
| 58 |
for (var j = 0; j < 3; ++j) { |
| 59 |
bufferData[positionIdx + j] = (Math.random() - 0.5) * factor; |
| 60 |
bufferData[velocityIdx + j] = Math.random() * 2.0; |
| 61 |
bufferData[colorIdx + j] = 0.75 + Math.sin(((i / 1024.0) + j * 0.333) * 6.0) * 0.25; |
| 62 |
} |
| 63 |
|
| 64 |
bufferData[positionIdx + 3] = 1.0; |
| 65 |
bufferData[velocityIdx + 3] = 0.0; |
| 66 |
bufferData[colorIdx + 3] = 1.0; |
| 67 |
} |
| 68 |
return bufferData |
| 69 |
} |
| 70 |
|
| 71 |
Buffer { |
| 72 |
id: particleBuffer |
| 73 |
// struct ParticleData |
| 74 |
// { |
| 75 |
// vec3 position; // Aligned to 4 floats |
| 76 |
// vec3 velocity; // Aligned to 4 floats |
| 77 |
// vec3 color; // Aligned to 4 floats |
| 78 |
// }; |
| 79 |
data: buildParticlesBuffer() |
| 80 |
} |
| 81 |
|
| 82 |
onReset : { |
| 83 |
particleBuffer.data = buildParticlesBuffer() |
| 84 |
} |
| 85 |
|
| 86 |
Attribute { |
| 87 |
id: particlePositionDataAttribute |
| 88 |
name: "particlePosition" |
| 89 |
attributeType: Attribute.VertexAttribute |
| 90 |
vertexBaseType: Attribute.Float |
| 91 |
vertexSize: 3 |
| 92 |
divisor: 1 |
| 93 |
byteStride: 12 * floatSize |
| 94 |
buffer: particleBuffer |
| 95 |
} |
| 96 |
|
| 97 |
Attribute { |
| 98 |
id: particleColorDataAttribute |
| 99 |
name: "particleColor" |
| 100 |
attributeType: Attribute.VertexAttribute |
| 101 |
vertexBaseType: Attribute.Float |
| 102 |
vertexSize: 3 |
| 103 |
divisor: 1 |
| 104 |
byteOffset: 8 * floatSize |
| 105 |
byteStride: 12 * floatSize |
| 106 |
buffer: particleBuffer |
| 107 |
} |
| 108 |
|
| 109 |
ComputeMaterial { |
| 110 |
id: computeMaterial |
| 111 |
dataBuffer: particleBuffer |
| 112 |
} |
| 113 |
|
| 114 |
Entity { |
| 115 |
id: particleComputeEntity |
| 116 |
readonly property ComputeCommand particlesComputeJob: ComputeCommand {} |
| 117 |
components: [ |
| 118 |
particlesComputeJob, |
| 119 |
computeMaterial |
| 120 |
] |
| 121 |
} |
| 122 |
|
| 123 |
SphereGeometry { |
| 124 |
id: sphereGeometry |
| 125 |
rings: 10 |
| 126 |
slices: 10 |
| 127 |
radius: 1 |
| 128 |
// Additional Attributes |
| 129 |
attributes: [ |
| 130 |
particlePositionDataAttribute, |
| 131 |
particleColorDataAttribute |
| 132 |
] |
| 133 |
} |
| 134 |
|
| 135 |
CuboidGeometry { |
| 136 |
id: cubeGeometry |
| 137 |
yzMeshResolution: Qt.size(2, 2) |
| 138 |
xzMeshResolution: Qt.size(2, 2) |
| 139 |
xyMeshResolution: Qt.size(2, 2) |
| 140 |
// Additional Attributes |
| 141 |
attributes: [ |
| 142 |
particlePositionDataAttribute, |
| 143 |
particleColorDataAttribute |
| 144 |
] |
| 145 |
} |
| 146 |
|
| 147 |
CylinderGeometry { |
| 148 |
id: cylinderGeometry |
| 149 |
rings: 10 |
| 150 |
slices: 10 |
| 151 |
radius: 1 |
| 152 |
length: 1.5 |
| 153 |
// Additional Attributes |
| 154 |
attributes: [ |
| 155 |
particlePositionDataAttribute, |
| 156 |
particleColorDataAttribute |
| 157 |
] |
| 158 |
} |
| 159 |
|
| 160 |
TorusGeometry { |
| 161 |
id: torusGeometry |
| 162 |
rings: 10 |
| 163 |
slices: 10 |
| 164 |
radius: 1 |
| 165 |
minorRadius: 0.5 |
| 166 |
// Additional Attributes |
| 167 |
attributes: [ |
| 168 |
particlePositionDataAttribute, |
| 169 |
particleColorDataAttribute |
| 170 |
] |
| 171 |
} |
| 172 |
|
| 173 |
Entity { |
| 174 |
id: particleRenderEntity |
| 175 |
readonly property GeometryRenderer particlesRenderer: GeometryRenderer { |
| 176 |
instanceCount: particlesCount |
| 177 |
indexOffset: 0 |
| 178 |
firstInstance: 0 |
| 179 |
primitiveType: GeometryRenderer.Triangles |
| 180 |
geometry: { |
| 181 |
switch (particlesShape) { |
| 182 |
case _SPHERE: |
| 183 |
return sphereGeometry; |
| 184 |
case _CUBE: |
| 185 |
return cubeGeometry; |
| 186 |
case _CYLINDER: |
| 187 |
return cylinderGeometry; |
| 188 |
case _TORUS: |
| 189 |
return torusGeometry; |
| 190 |
} |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
components: [ |
| 195 |
particlesRenderer, |
| 196 |
computeMaterial |
| 197 |
] |
| 198 |
} |
| 199 |
} |
| 200 |
|