From 0aab90b693101578c85c603055d2ce926f249bf2 Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Tue, 1 Aug 2017 19:13:20 -0700 Subject: [PATCH] [MetaballsKit] Implement sampling kernel --- MetaballsKit/SamplingKernel.metal | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/MetaballsKit/SamplingKernel.metal b/MetaballsKit/SamplingKernel.metal index 90a2de8..a8b09ca 100644 --- a/MetaballsKit/SamplingKernel.metal +++ b/MetaballsKit/SamplingKernel.metal @@ -16,9 +16,18 @@ typedef struct { } Ball; kernel void -sampleFieldKernel(const device Ball* metaballs [[buffer(0)]], - texture2d [[texture(1)]], - uint2 gid [[thread_position_in_grid]]) +sampleFieldKernel(constant Ball* balls [[buffer(0)]], + texture2d samples [[texture(1)]], + uint2 gid [[thread_position_in_grid]]) { - // TODO: Compute a sample for this pixel given the field data, and write it to the out texture. + float sample = 0.0; + // TODO: Get number of metaballs. + for (int i = 0; i < 2; i++) { + constant Ball& ball = metaballs[i]; + float r2 = ball.radius * ball.radius; + float xDiff = gid[0] - ball.position[0]; + float yDiff = gid[1] - ball.position[1]; + sample += r2 / ((xDiff * xDiff) + (yDiff * yDiff)); + } + samples.write(sample, gid); }