From 22fbaed88c94bc670ef8f1158e1bbd6d00b6b937 Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Fri, 4 Aug 2017 21:10:52 -0700 Subject: [PATCH] [MetaballsKit] Fix up didSet responder for Field.size --- MetaballsKit/Metaballs.swift | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/MetaballsKit/Metaballs.swift b/MetaballsKit/Metaballs.swift index 247844a..9f900c8 100644 --- a/MetaballsKit/Metaballs.swift +++ b/MetaballsKit/Metaballs.swift @@ -36,9 +36,19 @@ public struct Ball { public class Field { public var size: CGSize { didSet { - // Remove balls that fall outside the new bounds. - balls = balls.filter { bounds.contains($0.bounds) } - updateThreadgroupSizes(withFieldSize: size) + if size != oldValue { + let numberOfBallsBeforeFilter = balls.count + + // Remove balls that fall outside the new bounds. + balls = balls.filter { bounds.contains($0.bounds) } + + // Update Metal state as needed. + updateThreadgroupSizes(withFieldSize: size) + sampleTexture = nil + if numberOfBallsBeforeFilter != balls.count { + ballBuffer = nil + } + } } }