Skip to content

Instantly share code, notes, and snippets.

@but80
Created May 13, 2015 15:41
Show Gist options
  • Save but80/8c5f9fc872d56a3a4673 to your computer and use it in GitHub Desktop.
Save but80/8c5f9fc872d56a3a4673 to your computer and use it in GitHub Desktop.
#pragma kernel ComputeForce
struct OceanParticle {
half3 position;
half velocity;
half force;
};
half2 Tension;
half Friction;
half Mass;
half DeltaTime;
half2 Pitch;
uint2 Division;
half FadeRadius;
half FadeCurve;
uint XCount;
RWStructuredBuffer<OceanParticle> Particles;
[numthreads(32, 1, 1)]
void ComputeForce(uint3 id : SV_DispatchThreadID) {
uint k = id.x;
uint j = k % XCount;
uint i = k / XCount;
half h = Particles[k].position.y;
half2 force = half2(.0, .0);
if (0u < j) force.x += Particles[k - 1].position.y - h;
if (j < Division.x) force.x += Particles[k + 1].position.y - h;
if (0u < i) force.y += Particles[k - XCount].position.y - h;
if (i < Division.y) force.y += Particles[k + XCount].position.y - h;
force *= Tension / Pitch;
half fade = length(Particles[k].position.xz) / FadeRadius;
fade = min(1.0, pow(abs(fade), FadeCurve));
Particles[k].force = (force.x + force.y) - lerp(Friction, 1.0, fade) * Particles[k].velocity;
Particles[k].velocity += Particles[k].force / Mass * DeltaTime;
Particles[k].position += half3(.0, Particles[k].velocity * DeltaTime, .0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment