mirror of
https://github.com/ilyakooo0/metal-metaballs.git
synced 2024-10-26 10:22:52 +03:00
Merged feature-edge-animation into dev
This commit is contained in:
commit
95887c9896
@ -12,6 +12,7 @@
|
||||
2426868D1C63B72A00D62456 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 2426868B1C63B72A00D62456 /* Main.storyboard */; };
|
||||
2426868F1C63B72A00D62456 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 2426868E1C63B72A00D62456 /* Assets.xcassets */; };
|
||||
242686921C63B72A00D62456 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 242686901C63B72A00D62456 /* LaunchScreen.storyboard */; };
|
||||
2461651B1C6DA4F400A913C8 /* Interpolation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2461651A1C6DA4F400A913C8 /* Interpolation.swift */; };
|
||||
249CF84C1C6D113C008B0C78 /* Matrix.swift in Sources */ = {isa = PBXBuildFile; fileRef = 249CF84B1C6D113C008B0C78 /* Matrix.swift */; };
|
||||
24E1E7D71C67C8B200ECF1C4 /* UIKit.swift in Sources */ = {isa = PBXBuildFile; fileRef = 24E1E7D61C67C8B200ECF1C4 /* UIKit.swift */; };
|
||||
24E1E7E21C67D37F00ECF1C4 /* Metal.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 24E1E7E11C67D37F00ECF1C4 /* Metal.framework */; };
|
||||
@ -58,6 +59,7 @@
|
||||
2426868E1C63B72A00D62456 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
|
||||
242686911C63B72A00D62456 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
|
||||
242686931C63B72A00D62456 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
|
||||
2461651A1C6DA4F400A913C8 /* Interpolation.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Interpolation.swift; path = PixelImage/Interpolation.swift; sourceTree = SOURCE_ROOT; };
|
||||
249CF84B1C6D113C008B0C78 /* Matrix.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Matrix.swift; sourceTree = "<group>"; };
|
||||
24E1E7D61C67C8B200ECF1C4 /* UIKit.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIKit.swift; sourceTree = "<group>"; };
|
||||
24E1E7E11C67D37F00ECF1C4 /* Metal.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Metal.framework; path = System/Library/Frameworks/Metal.framework; sourceTree = SDKROOT; };
|
||||
@ -151,6 +153,7 @@
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
24FFA84C1C6BA038005ACD60 /* ApproximationOperator.swift */,
|
||||
2461651A1C6DA4F400A913C8 /* Interpolation.swift */,
|
||||
24FFA84D1C6BA038005ACD60 /* Collections.swift */,
|
||||
24FFA84E1C6BA038005ACD60 /* ComparatorChains.swift */,
|
||||
24FFA84F1C6BA038005ACD60 /* Dictionary.swift */,
|
||||
@ -286,6 +289,7 @@
|
||||
24FFA8841C6BA038005ACD60 /* TakeDrop.swift in Sources */,
|
||||
24FFA8771C6BA038005ACD60 /* ChunkWindowSplit.swift in Sources */,
|
||||
24FFA86F1C6BA038005ACD60 /* NSNumber.swift in Sources */,
|
||||
2461651B1C6DA4F400A913C8 /* Interpolation.swift in Sources */,
|
||||
24FFA8861C6BA038005ACD60 /* TestCommons.swift in Sources */,
|
||||
24FFA8741C6BA038005ACD60 /* SignOperator.swift in Sources */,
|
||||
24FFA8821C6BA038005ACD60 /* ScanReduce.swift in Sources */,
|
||||
|
17
PixelImage/Interpolation.swift
Normal file
17
PixelImage/Interpolation.swift
Normal file
@ -0,0 +1,17 @@
|
||||
|
||||
func interpolateCubeEaseIn<T: Numeric>(linear: T) -> T {
|
||||
return linear ^ 3
|
||||
}
|
||||
|
||||
func interpolateSquareEaseIn<T: Numeric>(linear: T) -> T {
|
||||
return linear ^ 2
|
||||
}
|
||||
|
||||
func interpolateSquareEaseOut<T: Numeric>(linear: T) -> T {
|
||||
return 1 - (1 - linear) * (1 - linear)
|
||||
}
|
||||
|
||||
func interpolateSmooth<T: Numeric>(linear: T) -> T {
|
||||
return ((linear) * (linear) * (linear) * ((linear) * ((linear) * 6 - 15) + 10))
|
||||
}
|
||||
|
@ -70,3 +70,23 @@ struct Matrix: CustomStringConvertible {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class EdgeAnimationParameters {
|
||||
let startDate: NSDate
|
||||
let duration: Float
|
||||
let fadeIn: Bool
|
||||
let i: Int
|
||||
let j: Int
|
||||
|
||||
func unpack() -> (startDate: NSDate, duration: Float, fadeIn: Bool, i: Int, j: Int) {
|
||||
return (startDate, duration, fadeIn, i, j)
|
||||
}
|
||||
|
||||
init(startDate: NSDate, duration: Float, fadeIn: Bool, i: Int, j: Int) {
|
||||
self.startDate = startDate
|
||||
self.duration = duration
|
||||
self.fadeIn = fadeIn
|
||||
self.i = i
|
||||
self.j = j
|
||||
}
|
||||
}
|
||||
|
@ -14,6 +14,8 @@ class ViewController: UIViewController, MetaballDataSource {
|
||||
|
||||
var selectedMetaball: Metaball?
|
||||
|
||||
let edgeAnimationDuration: Float = 1
|
||||
|
||||
override func viewDidLoad() {
|
||||
super.viewDidLoad()
|
||||
|
||||
@ -28,8 +30,6 @@ class ViewController: UIViewController, MetaballDataSource {
|
||||
metaballGraph.addEdge(0, 3)
|
||||
metaballGraph.addEdge(1, 2)
|
||||
metaballGraph.addEdge(2, 3)
|
||||
metaballGraph.removeEdge(2, 3)
|
||||
print(metaballGraph.adjacencyMatrix)
|
||||
|
||||
let border = 20
|
||||
let metaballViewFrame = CGRect(x: border/2, y: border/2, width: width, height: height)
|
||||
@ -45,6 +45,41 @@ class ViewController: UIViewController, MetaballDataSource {
|
||||
metaballView.addSubview(metaball)
|
||||
}
|
||||
|
||||
addEdge(0, 2)
|
||||
|
||||
renderer.state = .Running
|
||||
}
|
||||
|
||||
func addEdge(i: Int, _ j: Int) {
|
||||
animateEdge(i, j, fadeIn: true)
|
||||
}
|
||||
|
||||
func removeEdge(i: Int, _ j: Int) {
|
||||
animateEdge(i, j, fadeIn: false)
|
||||
}
|
||||
|
||||
func animateEdge(i: Int, _ j: Int, fadeIn: Bool) {
|
||||
let parameters = EdgeAnimationParameters(startDate: NSDate(), duration: edgeAnimationDuration, fadeIn: fadeIn, i: i, j: j)
|
||||
NSTimer.scheduledTimerWithTimeInterval(1.0/60.0, target: self, selector: "animateEdgeWithTimer:", userInfo: parameters, repeats: true)
|
||||
}
|
||||
|
||||
func animateEdge(withTimer timer: NSTimer) {
|
||||
let (animationStart, duration, fadeIn, i, j) = (timer.userInfo as! EdgeAnimationParameters).unpack()
|
||||
|
||||
let now = NSDate()
|
||||
var timeElapsed = Float(now.timeIntervalSinceDate(animationStart))
|
||||
|
||||
if timeElapsed > duration {
|
||||
timer.invalidate()
|
||||
timeElapsed = duration
|
||||
}
|
||||
|
||||
let linearValue = timeElapsed / duration
|
||||
let interpolatedValue = fadeIn ? interpolateSquareEaseOut(linearValue) : (1 - interpolateSquareEaseIn(linearValue))
|
||||
|
||||
metaballGraph.adjacencyMatrix.set(i, j, value: interpolatedValue)
|
||||
metaballGraph.adjacencyMatrix.set(j, i, value: interpolatedValue)
|
||||
|
||||
renderer.state = .Running
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user