• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2The MIT License (MIT)
3
4Copyright (c) 2022 Sascha Willems
5
6Permission is hereby granted, free of charge, to any person obtaining a copy
7of this software and associated documentation files (the "Software"), to deal
8in the Software without restriction, including without limitation the rights
9to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10copies of the Software, and to permit persons to whom the Software is
11furnished to do so, subject to the following conditions:
12
13The above copyright notice and this permission notice shall be included in all
14copies or substantial portions of the Software.
15
16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22SOFTWARE.
23*/
24
25#version 450
26
27struct Particle {
28	vec4 pos;
29	vec4 vel;
30	vec4 uv;
31	vec4 normal;
32	float pinned;
33};
34
35layout(std430, binding = 0) buffer ParticleIn {
36	Particle particleIn[ ];
37};
38
39layout(std430, binding = 1) buffer ParticleOut {
40	Particle particleOut[ ];
41};
42
43// todo: use shared memory to speed up calculation
44
45layout (local_size_x = 10, local_size_y = 10) in;
46
47layout (binding = 2) uniform UBO
48{
49	float deltaT;
50	float particleMass;
51	float springStiffness;
52	float damping;
53	float restDistH;
54	float restDistV;
55	float restDistD;
56	float sphereRadius;
57	vec4 spherePos;
58	vec4 gravity;
59	ivec2 particleCount;
60} params;
61
62layout (push_constant) uniform PushConsts {
63	uint calculateNormals;
64} pushConsts;
65
66vec3 springForce(vec3 p0, vec3 p1, float restDist)
67{
68	vec3 dist = p0 - p1;
69	return normalize(dist) * params.springStiffness * (length(dist) - restDist);
70}
71
72void main()
73{
74	uvec3 id = gl_GlobalInvocationID;
75
76	uint index = id.y * params.particleCount.x + id.x;
77	if (index > params.particleCount.x * params.particleCount.y)
78		return;
79
80	// Pinned?
81	if (particleIn[index].pinned == 1.0) {
82		particleOut[index].pos = particleOut[index].pos;
83		particleOut[index].vel = vec4(0.0);
84		return;
85	}
86
87	// Initial force from gravity
88	vec3 force = params.gravity.xyz * params.particleMass;
89
90	vec3 pos = particleIn[index].pos.xyz;
91	vec3 vel = particleIn[index].vel.xyz;
92
93	// Spring forces from neighboring particles
94	// left
95	if (id.x > 0) {
96		force += springForce(particleIn[index-1].pos.xyz, pos, params.restDistH);
97	}
98	// right
99	if (id.x < params.particleCount.x - 1) {
100		force += springForce(particleIn[index + 1].pos.xyz, pos, params.restDistH);
101	}
102	// upper
103	if (id.y < params.particleCount.y - 1) {
104		force += springForce(particleIn[index + params.particleCount.x].pos.xyz, pos, params.restDistV);
105	}
106	// lower
107	if (id.y > 0) {
108		force += springForce(particleIn[index - params.particleCount.x].pos.xyz, pos, params.restDistV);
109	}
110	// upper-left
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);
113	}
114	// lower-left
115	if ((id.x > 0) && (id.y > 0)) {
116		force += springForce(particleIn[index - params.particleCount.x - 1].pos.xyz, pos, params.restDistD);
117	}
118	// upper-right
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);
121	}
122	// lower-right
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);
125	}
126
127	force += (-params.damping * vel);
128
129	// Integrate
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);
133
134	// Sphere collision
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);
141	}
142
143	// Normals
144	if (pushConsts.calculateNormals == 1) {
145		vec3 normal = vec3(0.0);
146		vec3 a, b, c;
147		if (id.y > 0) {
148			if (id.x > 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);
153			}
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);
159			}
160		}
161		if (id.y < params.particleCount.y - 1) {
162			if (id.x > 0) {
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);
167			}
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);
173			}
174		}
175		particleOut[index].normal = vec4(normalize(normal), 0.0f);
176	}
177}
178