4 Copyright (c) 2022 Google LLC
5 Copyright (c) 2022 Sascha Willems
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
14 The above copyright notice and this permission notice shall be included in all
15 copies or substantial portions of the Software.
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
35 StructuredBuffer<Particle> particleIn;
37 RWStructuredBuffer<Particle> particleOut;
43 float springStiffness;
54 cbuffer ubo : register(b2)
60 layout ( push_constant ) cbuffer PushConstants
62 uint calculateNormals;
67 uint calculateNormals;
71 PushConstants pushConstants;
74 float3 springForce(float3 p0, float3 p1, float restDist)
76 float3 dist = p0 - p1;
77 return normalize(dist) * params.springStiffness * (length(dist) - restDist);
80 [numthreads(10, 10, 1)]
81 void main(uint3 id : SV_DispatchThreadID)
83 uint index = id.y * params.particleCount.x + id.x;
84 if (index > params.particleCount.x * params.particleCount.y)
88 if (particleIn[index].pinned == 1.0) {
89 particleOut[index].pos = particleOut[index].pos;
90 particleOut[index].vel = float4(0, 0, 0, 0);
94 // Initial force from gravity
95 float3 force = params.gravity.xyz * params.particleMass;
97 float3 pos = particleIn[index].pos.xyz;
98 float3 vel = particleIn[index].vel.xyz;
100 // Spring forces from neighboring particles
103 force += springForce(particleIn[index-1].pos.xyz, pos, params.restDistH);
106 if (id.x < params.particleCount.x - 1) {
107 force += springForce(particleIn[index + 1].pos.xyz, pos, params.restDistH);
110 if (id.y < params.particleCount.y - 1) {
111 force += springForce(particleIn[index + params.particleCount.x].pos.xyz, pos, params.restDistV);
115 force += springForce(particleIn[index - params.particleCount.x].pos.xyz, pos, params.restDistV);
118 if ((id.x > 0) && (id.y < params.particleCount.y - 1)) {
119 force += springForce(particleIn[index + params.particleCount.x - 1].pos.xyz, pos, params.restDistD);
122 if ((id.x > 0) && (id.y > 0)) {
123 force += springForce(particleIn[index - params.particleCount.x - 1].pos.xyz, pos, params.restDistD);
126 if ((id.x < params.particleCount.x - 1) && (id.y < params.particleCount.y - 1)) {
127 force += springForce(particleIn[index + params.particleCount.x + 1].pos.xyz, pos, params.restDistD);
130 if ((id.x < params.particleCount.x - 1) && (id.y > 0)) {
131 force += springForce(particleIn[index - params.particleCount.x + 1].pos.xyz, pos, params.restDistD);
134 force += (-params.damping * vel);
137 float3 f = force * (1.0 / params.particleMass);
138 particleOut[index].pos = float4(pos + vel * params.deltaT + 0.5 * f * params.deltaT * params.deltaT, 1.0);
139 particleOut[index].vel = float4(vel + f * params.deltaT, 0.0);
142 float3 sphereDist = particleOut[index].pos.xyz - params.spherePos.xyz;
143 if (length(sphereDist) < params.sphereRadius + 0.01) {
144 // If the particle is inside the sphere, push it to the outer radius
145 particleOut[index].pos.xyz = params.spherePos.xyz + normalize(sphereDist) * (params.sphereRadius + 0.01);
146 // Cancel out velocity
147 particleOut[index].vel = float4(0, 0, 0, 0);
151 if (pushConstants.calculateNormals == 1) {
152 float3 normal = float3(0, 0, 0);
156 a = particleIn[index - 1].pos.xyz - pos;
157 b = particleIn[index - params.particleCount.x - 1].pos.xyz - pos;
158 c = particleIn[index - params.particleCount.x].pos.xyz - pos;
159 normal += cross(a,b) + cross(b,c);
161 if (id.x < params.particleCount.x - 1) {
162 a = particleIn[index - params.particleCount.x].pos.xyz - pos;
163 b = particleIn[index - params.particleCount.x + 1].pos.xyz - pos;
164 c = particleIn[index + 1].pos.xyz - pos;
165 normal += cross(a,b) + cross(b,c);
168 if (id.y < params.particleCount.y - 1) {
170 a = particleIn[index + params.particleCount.x].pos.xyz - pos;
171 b = particleIn[index + params.particleCount.x - 1].pos.xyz - pos;
172 c = particleIn[index - 1].pos.xyz - pos;
173 normal += cross(a,b) + cross(b,c);
175 if (id.x < params.particleCount.x - 1) {
176 a = particleIn[index + 1].pos.xyz - pos;
177 b = particleIn[index + params.particleCount.x + 1].pos.xyz - pos;
178 c = particleIn[index + params.particleCount.x].pos.xyz - pos;
179 normal += cross(a,b) + cross(b,c);
182 particleOut[index].normal = float4(normalize(normal), 0.0f);