4 Copyright (c) 2022 Sascha Willems
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
13 The above copyright notice and this permission notice shall be included in all
14 copies or substantial portions of the Software.
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
35 layout(std430, binding = 0) buffer ParticleIn {
36 Particle particleIn[ ];
39 layout(std430, binding = 1) buffer ParticleOut {
40 Particle particleOut[ ];
43 // todo: use shared memory to speed up calculation
45 layout (local_size_x = 10, local_size_y = 10) in;
47 layout (binding = 2) uniform UBO
51 float springStiffness;
62 layout (push_constant) uniform PushConsts {
63 uint calculateNormals;
66 vec3 springForce(vec3 p0, vec3 p1, float restDist)
69 return normalize(dist) * params.springStiffness * (length(dist) - restDist);
74 uvec3 id = gl_GlobalInvocationID;
76 uint index = id.y * params.particleCount.x + id.x;
77 if (index > params.particleCount.x * params.particleCount.y)
81 if (particleIn[index].pinned == 1.0) {
82 particleOut[index].pos = particleOut[index].pos;
83 particleOut[index].vel = vec4(0.0);
87 // Initial force from gravity
88 vec3 force = params.gravity.xyz * params.particleMass;
90 vec3 pos = particleIn[index].pos.xyz;
91 vec3 vel = particleIn[index].vel.xyz;
93 // Spring forces from neighboring particles
96 force += springForce(particleIn[index-1].pos.xyz, pos, params.restDistH);
99 if (id.x < params.particleCount.x - 1) {
100 force += springForce(particleIn[index + 1].pos.xyz, pos, params.restDistH);
103 if (id.y < params.particleCount.y - 1) {
104 force += springForce(particleIn[index + params.particleCount.x].pos.xyz, pos, params.restDistV);
108 force += springForce(particleIn[index - params.particleCount.x].pos.xyz, pos, params.restDistV);
111 if ((id.x > 0) && (id.y < params.particleCount.y - 1)) {
112 force += springForce(particleIn[index + params.particleCount.x - 1].pos.xyz, pos, params.restDistD);
115 if ((id.x > 0) && (id.y > 0)) {
116 force += springForce(particleIn[index - params.particleCount.x - 1].pos.xyz, pos, params.restDistD);
119 if ((id.x < params.particleCount.x - 1) && (id.y < params.particleCount.y - 1)) {
120 force += springForce(particleIn[index + params.particleCount.x + 1].pos.xyz, pos, params.restDistD);
123 if ((id.x < params.particleCount.x - 1) && (id.y > 0)) {
124 force += springForce(particleIn[index - params.particleCount.x + 1].pos.xyz, pos, params.restDistD);
127 force += (-params.damping * vel);
130 vec3 f = force * (1.0 / params.particleMass);
131 particleOut[index].pos = vec4(pos + vel * params.deltaT + 0.5 * f * params.deltaT * params.deltaT, 1.0);
132 particleOut[index].vel = vec4(vel + f * params.deltaT, 0.0);
135 vec3 sphereDist = particleOut[index].pos.xyz - params.spherePos.xyz;
136 if (length(sphereDist) < params.sphereRadius + 0.01) {
137 // If the particle is inside the sphere, push it to the outer radius
138 particleOut[index].pos.xyz = params.spherePos.xyz + normalize(sphereDist) * (params.sphereRadius + 0.01);
139 // Cancel out velocity
140 particleOut[index].vel = vec4(0.0);
144 if (pushConsts.calculateNormals == 1) {
145 vec3 normal = vec3(0.0);
149 a = particleIn[index - 1].pos.xyz - pos;
150 b = particleIn[index - params.particleCount.x - 1].pos.xyz - pos;
151 c = particleIn[index - params.particleCount.x].pos.xyz - pos;
152 normal += cross(a,b) + cross(b,c);
154 if (id.x < params.particleCount.x - 1) {
155 a = particleIn[index - params.particleCount.x].pos.xyz - pos;
156 b = particleIn[index - params.particleCount.x + 1].pos.xyz - pos;
157 c = particleIn[index + 1].pos.xyz - pos;
158 normal += cross(a,b) + cross(b,c);
161 if (id.y < params.particleCount.y - 1) {
163 a = particleIn[index + params.particleCount.x].pos.xyz - pos;
164 b = particleIn[index + params.particleCount.x - 1].pos.xyz - pos;
165 c = particleIn[index - 1].pos.xyz - pos;
166 normal += cross(a,b) + cross(b,c);
168 if (id.x < params.particleCount.x - 1) {
169 a = particleIn[index + 1].pos.xyz - pos;
170 b = particleIn[index + params.particleCount.x + 1].pos.xyz - pos;
171 c = particleIn[index + params.particleCount.x].pos.xyz - pos;
172 normal += cross(a,b) + cross(b,c);
175 particleOut[index].normal = vec4(normalize(normal), 0.0f);