git.iko.soy/metal-metaballs
on master help sign in
Merged feature-edge-animation into dev
commit95887c9896c0f3d4c829726cc0b0c955c97ee132 authorvinivendra <vinivendra@gmail.com> dateFri 12 Feb 2016, 00:47 (11 years ago) parentdb76528731abff
4 files changed+78−2
PixelImage.xcodeproj/project.pbxproj+4 PixelImage/Interpolation.swift+17 PixelImage/Matrix.swift+20 PixelImage/ViewController.swift+37−2
PixelImage.xcodeproj/project.pbxproj+4
@@ -12,6 +12,7 @@
1212 2426868D1C63B72A00D62456 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 2426868B1C63B72A00D62456 /* Main.storyboard */; };
1313 2426868F1C63B72A00D62456 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 2426868E1C63B72A00D62456 /* Assets.xcassets */; };
1414 242686921C63B72A00D62456 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 242686901C63B72A00D62456 /* LaunchScreen.storyboard */; };
15+ 2461651B1C6DA4F400A913C8 /* Interpolation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2461651A1C6DA4F400A913C8 /* Interpolation.swift */; };
1516 249CF84C1C6D113C008B0C78 /* Matrix.swift in Sources */ = {isa = PBXBuildFile; fileRef = 249CF84B1C6D113C008B0C78 /* Matrix.swift */; };
1617 24E1E7D71C67C8B200ECF1C4 /* UIKit.swift in Sources */ = {isa = PBXBuildFile; fileRef = 24E1E7D61C67C8B200ECF1C4 /* UIKit.swift */; };
1718 24E1E7E21C67D37F00ECF1C4 /* Metal.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 24E1E7E11C67D37F00ECF1C4 /* Metal.framework */; };
@@ -58,6 +59,7 @@
5859 2426868E1C63B72A00D62456 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
5960 242686911C63B72A00D62456 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
6061 242686931C63B72A00D62456 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
62+ 2461651A1C6DA4F400A913C8 /* Interpolation.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Interpolation.swift; path = PixelImage/Interpolation.swift; sourceTree = SOURCE_ROOT; };
6163 249CF84B1C6D113C008B0C78 /* Matrix.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Matrix.swift; sourceTree = "<group>"; };
6264 24E1E7D61C67C8B200ECF1C4 /* UIKit.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIKit.swift; sourceTree = "<group>"; };
6365 24E1E7E11C67D37F00ECF1C4 /* Metal.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Metal.framework; path = System/Library/Frameworks/Metal.framework; sourceTree = SDKROOT; };
@@ -151,6 +153,7 @@
151153 isa = PBXGroup;
152154 children = (
153155 24FFA84C1C6BA038005ACD60 /* ApproximationOperator.swift */,
156+ 2461651A1C6DA4F400A913C8 /* Interpolation.swift */,
154157 24FFA84D1C6BA038005ACD60 /* Collections.swift */,
155158 24FFA84E1C6BA038005ACD60 /* ComparatorChains.swift */,
156159 24FFA84F1C6BA038005ACD60 /* Dictionary.swift */,
@@ -286,6 +289,7 @@
286289 24FFA8841C6BA038005ACD60 /* TakeDrop.swift in Sources */,
287290 24FFA8771C6BA038005ACD60 /* ChunkWindowSplit.swift in Sources */,
288291 24FFA86F1C6BA038005ACD60 /* NSNumber.swift in Sources */,
292+ 2461651B1C6DA4F400A913C8 /* Interpolation.swift in Sources */,
289293 24FFA8861C6BA038005ACD60 /* TestCommons.swift in Sources */,
290294 24FFA8741C6BA038005ACD60 /* SignOperator.swift in Sources */,
291295 24FFA8821C6BA038005ACD60 /* ScanReduce.swift in Sources */,
PixelImage/Interpolation.swiftnew file+17
@@ -0,0 +1,17 @@
1+
2+func interpolateCubeEaseIn<T: Numeric>(linear: T) -> T {
3+ return linear ^ 3
4+}
5+
6+func interpolateSquareEaseIn<T: Numeric>(linear: T) -> T {
7+ return linear ^ 2
8+}
9+
10+func interpolateSquareEaseOut<T: Numeric>(linear: T) -> T {
11+ return 1 - (1 - linear) * (1 - linear)
12+}
13+
14+func interpolateSmooth<T: Numeric>(linear: T) -> T {
15+ return ((linear) * (linear) * (linear) * ((linear) * ((linear) * 6 - 15) + 10))
16+}
17+
PixelImage/Matrix.swift+20
@@ -70,3 +70,23 @@ struct Matrix: CustomStringConvertible {
7070 }
7171 }
7272}
73+
74+class EdgeAnimationParameters {
75+ let startDate: NSDate
76+ let duration: Float
77+ let fadeIn: Bool
78+ let i: Int
79+ let j: Int
80+
81+ func unpack() -> (startDate: NSDate, duration: Float, fadeIn: Bool, i: Int, j: Int) {
82+ return (startDate, duration, fadeIn, i, j)
83+ }
84+
85+ init(startDate: NSDate, duration: Float, fadeIn: Bool, i: Int, j: Int) {
86+ self.startDate = startDate
87+ self.duration = duration
88+ self.fadeIn = fadeIn
89+ self.i = i
90+ self.j = j
91+ }
92+}
PixelImage/ViewController.swift+37−2
@@ -14,6 +14,8 @@ class ViewController: UIViewController, MetaballDataSource {
1414
1515 var selectedMetaball: Metaball?
1616
17+ let edgeAnimationDuration: Float = 1
18+
1719 override func viewDidLoad() {
1820 super.viewDidLoad()
1921
@@ -28,8 +30,6 @@ class ViewController: UIViewController, MetaballDataSource {
2830 metaballGraph.addEdge(0, 3)
2931 metaballGraph.addEdge(1, 2)
3032 metaballGraph.addEdge(2, 3)
31− metaballGraph.removeEdge(2, 3)
32− print(metaballGraph.adjacencyMatrix)
3333
3434 let border = 20
3535 let metaballViewFrame = CGRect(x: border/2, y: border/2, width: width, height: height)
@@ -45,6 +45,41 @@ class ViewController: UIViewController, MetaballDataSource {
4545 metaballView.addSubview(metaball)
4646 }
4747
48+ addEdge(0, 2)
49+
50+ renderer.state = .Running
51+ }
52+
53+ func addEdge(i: Int, _ j: Int) {
54+ animateEdge(i, j, fadeIn: true)
55+ }
56+
57+ func removeEdge(i: Int, _ j: Int) {
58+ animateEdge(i, j, fadeIn: false)
59+ }
60+
61+ func animateEdge(i: Int, _ j: Int, fadeIn: Bool) {
62+ let parameters = EdgeAnimationParameters(startDate: NSDate(), duration: edgeAnimationDuration, fadeIn: fadeIn, i: i, j: j)
63+ NSTimer.scheduledTimerWithTimeInterval(1.0/60.0, target: self, selector: "animateEdgeWithTimer:", userInfo: parameters, repeats: true)
64+ }
65+
66+ func animateEdge(withTimer timer: NSTimer) {
67+ let (animationStart, duration, fadeIn, i, j) = (timer.userInfo as! EdgeAnimationParameters).unpack()
68+
69+ let now = NSDate()
70+ var timeElapsed = Float(now.timeIntervalSinceDate(animationStart))
71+
72+ if timeElapsed > duration {
73+ timer.invalidate()
74+ timeElapsed = duration
75+ }
76+
77+ let linearValue = timeElapsed / duration
78+ let interpolatedValue = fadeIn ? interpolateSquareEaseOut(linearValue) : (1 - interpolateSquareEaseIn(linearValue))
79+
80+ metaballGraph.adjacencyMatrix.set(i, j, value: interpolatedValue)
81+ metaballGraph.adjacencyMatrix.set(j, i, value: interpolatedValue)
82+
4883 renderer.state = .Running
4984 }
5085