Compute shader cloth simulation
This commit is contained in:
parent
fd439a59e2
commit
a55759b31b
14 changed files with 1157 additions and 0 deletions
148
data/shaders/computecloth/cloth.comp
Normal file
148
data/shaders/computecloth/cloth.comp
Normal file
|
|
@ -0,0 +1,148 @@
|
|||
#version 450
|
||||
|
||||
struct Particle {
|
||||
vec4 pos;
|
||||
vec4 vel;
|
||||
vec4 uv;
|
||||
vec4 normal;
|
||||
float pinned;
|
||||
};
|
||||
|
||||
layout(std430, binding = 0) buffer ParticleIn {
|
||||
Particle particleIn[ ];
|
||||
};
|
||||
|
||||
layout(std430, binding = 1) buffer ParticleOut {
|
||||
Particle particleOut[ ];
|
||||
};
|
||||
|
||||
// todo: use shared memory to speed up calculation
|
||||
|
||||
layout (local_size_x = 10, local_size_y = 10) in;
|
||||
|
||||
layout (binding = 2) uniform UBO
|
||||
{
|
||||
float deltaT;
|
||||
float particleMass;
|
||||
float springStiffness;
|
||||
float damping;
|
||||
float restDistH;
|
||||
float restDistV;
|
||||
float restDistD;
|
||||
float sphereRadius;
|
||||
vec4 spherePos;
|
||||
vec4 gravity;
|
||||
ivec2 particleCount;
|
||||
} params;
|
||||
|
||||
vec3 springForce(vec3 p0, vec3 p1, float restDist)
|
||||
{
|
||||
vec3 dist = p0 - p1;
|
||||
return normalize(dist) * params.springStiffness * (length(dist) - restDist);
|
||||
}
|
||||
|
||||
void main()
|
||||
{
|
||||
uvec3 id = gl_GlobalInvocationID;
|
||||
|
||||
uint index = id.y * params.particleCount.x + id.x;
|
||||
if (index > params.particleCount.x * params.particleCount.y)
|
||||
return;
|
||||
|
||||
// Pinned?
|
||||
if (particleIn[index].pinned == 1.0) {
|
||||
particleOut[index].pos = particleOut[index].pos;
|
||||
particleOut[index].vel = vec4(0.0);
|
||||
return;
|
||||
}
|
||||
|
||||
// Initial force from gravity
|
||||
vec3 force = params.gravity.xyz * params.particleMass;
|
||||
|
||||
vec3 pos = particleIn[index].pos.xyz;
|
||||
vec3 vel = particleIn[index].vel.xyz;
|
||||
|
||||
// Spring forces from neighboring particles
|
||||
// left
|
||||
if (id.x > 0) {
|
||||
force += springForce(particleIn[index-1].pos.xyz, pos, params.restDistH);
|
||||
}
|
||||
// right
|
||||
if (id.x < params.particleCount.x - 1) {
|
||||
force += springForce(particleIn[index + 1].pos.xyz, pos, params.restDistH);
|
||||
}
|
||||
// upper
|
||||
if (id.y < params.particleCount.y - 1) {
|
||||
force += springForce(particleIn[index + params.particleCount.x].pos.xyz, pos, params.restDistV);
|
||||
}
|
||||
// lower
|
||||
if (id.y > 0) {
|
||||
force += springForce(particleIn[index - params.particleCount.x].pos.xyz, pos, params.restDistV);
|
||||
}
|
||||
// upper-left
|
||||
if ((id.x > 0) && (id.y < params.particleCount.y - 1)) {
|
||||
force += springForce(particleIn[index + params.particleCount.x - 1].pos.xyz, pos, params.restDistD);
|
||||
}
|
||||
// lower-left
|
||||
if ((id.x > 0) && (id.y > 0)) {
|
||||
force += springForce(particleIn[index - params.particleCount.x - 1].pos.xyz, pos, params.restDistD);
|
||||
}
|
||||
// upper-right
|
||||
if ((id.x < params.particleCount.x - 1) && (id.y < params.particleCount.y - 1)) {
|
||||
force += springForce(particleIn[index + params.particleCount.x + 1].pos.xyz, pos, params.restDistD);
|
||||
}
|
||||
// lower-right
|
||||
if ((id.x < params.particleCount.x - 1) && (id.y > 0)) {
|
||||
force += springForce(particleIn[index - params.particleCount.x + 1].pos.xyz, pos, params.restDistD);
|
||||
}
|
||||
|
||||
force += (-params.damping * vel);
|
||||
|
||||
// Integrate
|
||||
vec3 f = force * (1.0 / params.particleMass);
|
||||
particleOut[index].pos = vec4(pos + vel * params.deltaT + 0.5 * f * params.deltaT * params.deltaT, 1.0);
|
||||
particleOut[index].vel = vec4(vel + f * params.deltaT, 0.0);
|
||||
|
||||
// Sphere collision
|
||||
vec3 sphereDist = particleOut[index].pos.xyz - params.spherePos.xyz;
|
||||
if (length(sphereDist) < params.sphereRadius + 0.01) {
|
||||
// If the particle is inside the sphere, push it to the outer radius
|
||||
particleOut[index].pos.xyz = params.spherePos.xyz + normalize(sphereDist) * (params.sphereRadius + 0.01);
|
||||
// Cancel out velocity
|
||||
particleOut[index].vel = vec4(0.0);
|
||||
}
|
||||
|
||||
// Normals
|
||||
// todo: Only once (use push const to check)
|
||||
vec3 normal = vec3(0.0);
|
||||
vec3 a, b, c;
|
||||
if (id.y > 0) {
|
||||
if (id.x > 0) {
|
||||
a = particleIn[index - 1].pos.xyz - pos;
|
||||
b = particleIn[index - params.particleCount.x - 1].pos.xyz - pos;
|
||||
c = particleIn[index - params.particleCount.x].pos.xyz - pos;
|
||||
normal += cross(a,b) + cross(b,c);
|
||||
}
|
||||
if (id.x < params.particleCount.x - 1) {
|
||||
a = particleIn[index - params.particleCount.x].pos.xyz - pos;
|
||||
b = particleIn[index - params.particleCount.x + 1].pos.xyz - pos;
|
||||
c = particleIn[index + 1].pos.xyz - pos;
|
||||
normal += cross(a,b) + cross(b,c);
|
||||
}
|
||||
}
|
||||
if (id.y < params.particleCount.y - 1) {
|
||||
if (id.x > 0) {
|
||||
a = particleIn[index + params.particleCount.x].pos.xyz - pos;
|
||||
b = particleIn[index + params.particleCount.x - 1].pos.xyz - pos;
|
||||
c = particleIn[index - 1].pos.xyz - pos;
|
||||
normal += cross(a,b) + cross(b,c);
|
||||
}
|
||||
if (id.x < params.particleCount.x - 1) {
|
||||
a = particleIn[index + 1].pos.xyz - pos;
|
||||
b = particleIn[index + params.particleCount.x + 1].pos.xyz - pos;
|
||||
c = particleIn[index + params.particleCount.x].pos.xyz - pos;
|
||||
normal += cross(a,b) + cross(b,c);
|
||||
}
|
||||
}
|
||||
particleOut[index].normal = vec4(normalize(normal), 0.0f);
|
||||
}
|
||||
BIN
data/shaders/computecloth/cloth.comp.spv
Normal file
BIN
data/shaders/computecloth/cloth.comp.spv
Normal file
Binary file not shown.
22
data/shaders/computecloth/cloth.frag
Normal file
22
data/shaders/computecloth/cloth.frag
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
#version 450
|
||||
|
||||
layout (binding = 1) uniform sampler2D samplerColor;
|
||||
|
||||
layout (location = 0) in vec2 inUV;
|
||||
layout (location = 1) in vec3 inNormal;
|
||||
layout (location = 2) in vec3 inViewVec;
|
||||
layout (location = 3) in vec3 inLightVec;
|
||||
|
||||
layout (location = 0) out vec4 outFragColor;
|
||||
|
||||
void main ()
|
||||
{
|
||||
vec3 color = texture(samplerColor, inUV).rgb;
|
||||
vec3 N = normalize(inNormal);
|
||||
vec3 L = normalize(inLightVec);
|
||||
vec3 V = normalize(inViewVec);
|
||||
vec3 R = reflect(-L, N);
|
||||
vec3 diffuse = max(dot(N, L), 0.15) * vec3(1.0);
|
||||
vec3 specular = pow(max(dot(R, V), 0.0), 8.0) * vec3(0.2);
|
||||
outFragColor = vec4(diffuse * color.rgb + specular, 1.0);
|
||||
}
|
||||
BIN
data/shaders/computecloth/cloth.frag.spv
Normal file
BIN
data/shaders/computecloth/cloth.frag.spv
Normal file
Binary file not shown.
35
data/shaders/computecloth/cloth.vert
Normal file
35
data/shaders/computecloth/cloth.vert
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
#version 450
|
||||
|
||||
layout (location = 0) in vec3 inPos;
|
||||
layout (location = 1) in vec2 inUV;
|
||||
layout (location = 2) in vec3 inNormal;
|
||||
|
||||
layout (location = 0) out vec2 outUV;
|
||||
layout (location = 1) out vec3 outNormal;
|
||||
layout (location = 2) out vec3 outViewVec;
|
||||
layout (location = 3) out vec3 outLightVec;
|
||||
|
||||
|
||||
layout (binding = 0) uniform UBO
|
||||
{
|
||||
mat4 projection;
|
||||
mat4 modelview;
|
||||
vec4 lightPos;
|
||||
} ubo;
|
||||
|
||||
out gl_PerVertex
|
||||
{
|
||||
vec4 gl_Position;
|
||||
};
|
||||
|
||||
void main ()
|
||||
{
|
||||
outUV = inUV;
|
||||
outNormal = inNormal.xyz;
|
||||
vec4 eyePos = ubo.modelview * vec4(inPos.x, inPos.y, inPos.z, 1.0);
|
||||
gl_Position = ubo.projection * eyePos;
|
||||
vec4 pos = vec4(inPos, 1.0);
|
||||
vec3 lPos = ubo.lightPos.xyz;
|
||||
outLightVec = lPos - pos.xyz;
|
||||
outViewVec = -pos.xyz;
|
||||
}
|
||||
BIN
data/shaders/computecloth/cloth.vert.spv
Normal file
BIN
data/shaders/computecloth/cloth.vert.spv
Normal file
Binary file not shown.
19
data/shaders/computecloth/sphere.frag
Normal file
19
data/shaders/computecloth/sphere.frag
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
#version 450
|
||||
|
||||
layout (location = 0) in vec3 inNormal;
|
||||
layout (location = 1) in vec3 inViewVec;
|
||||
layout (location = 2) in vec3 inLightVec;
|
||||
|
||||
layout (location = 0) out vec4 outFragColor;
|
||||
|
||||
void main ()
|
||||
{
|
||||
vec3 color = vec3(0.5);
|
||||
vec3 N = normalize(inNormal);
|
||||
vec3 L = normalize(inLightVec);
|
||||
vec3 V = normalize(inViewVec);
|
||||
vec3 R = reflect(-L, N);
|
||||
vec3 diffuse = max(dot(N, L), 0.15) * vec3(1.0);
|
||||
vec3 specular = pow(max(dot(R, V), 0.0), 32.0) * vec3(1.0);
|
||||
outFragColor = vec4(diffuse * color.rgb + specular, 1.0);
|
||||
}
|
||||
BIN
data/shaders/computecloth/sphere.frag.spv
Normal file
BIN
data/shaders/computecloth/sphere.frag.spv
Normal file
Binary file not shown.
31
data/shaders/computecloth/sphere.vert
Normal file
31
data/shaders/computecloth/sphere.vert
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
#version 450
|
||||
|
||||
layout (location = 0) in vec3 inPos;
|
||||
layout (location = 2) in vec3 inNormal;
|
||||
|
||||
layout (location = 0) out vec3 outNormal;
|
||||
layout (location = 1) out vec3 outViewVec;
|
||||
layout (location = 2) out vec3 outLightVec;
|
||||
|
||||
layout (binding = 0) uniform UBO
|
||||
{
|
||||
mat4 projection;
|
||||
mat4 modelview;
|
||||
vec4 lightPos;
|
||||
} ubo;
|
||||
|
||||
out gl_PerVertex
|
||||
{
|
||||
vec4 gl_Position;
|
||||
};
|
||||
|
||||
void main ()
|
||||
{
|
||||
vec4 eyePos = ubo.modelview * vec4(inPos.x, inPos.y, inPos.z, 1.0);
|
||||
gl_Position = ubo.projection * eyePos;
|
||||
vec4 pos = vec4(inPos, 1.0);
|
||||
vec3 lPos = ubo.lightPos.xyz;
|
||||
outLightVec = lPos - pos.xyz;
|
||||
outViewVec = -pos.xyz;
|
||||
outNormal = inNormal;
|
||||
}
|
||||
BIN
data/shaders/computecloth/sphere.vert.spv
Normal file
BIN
data/shaders/computecloth/sphere.vert.spv
Normal file
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue