mirror of
https://github.com/ilyakooo0/metal-metaballs.git
synced 2024-10-26 10:22:52 +03:00
Refactored metaballs.
This commit is contained in:
parent
f6914176c4
commit
8b54ecfa71
20
PixelImage/Metaball.swift
Normal file
20
PixelImage/Metaball.swift
Normal file
@ -0,0 +1,20 @@
|
||||
|
||||
import UIKit
|
||||
|
||||
class Metaball: UIView {
|
||||
|
||||
var color: UIColor
|
||||
|
||||
init(position: CGPoint, color: UIColor = UIColor(randomFlatColorOfShadeStyle: .Light)) {
|
||||
self.color = color
|
||||
super.init(frame: CGRect.zero)
|
||||
|
||||
size = CGSize(50)
|
||||
middle = position
|
||||
}
|
||||
|
||||
required init?(coder aDecoder: NSCoder) {
|
||||
color = UIColor(randomFlatColorOfShadeStyle: .Light)
|
||||
super.init(coder: aDecoder)
|
||||
}
|
||||
}
|
@ -179,7 +179,7 @@ class MetalMetaballRenderer {
|
||||
array.append(Float(x))
|
||||
array.append(Float(y))
|
||||
|
||||
let color = metaball.backgroundColor!
|
||||
let color = metaball.color
|
||||
let components = color.components
|
||||
array.append(Float(components.red))
|
||||
array.append(Float(components.green))
|
||||
|
@ -7,3 +7,9 @@ extension UIColor {
|
||||
return (r, g, b, a)
|
||||
}
|
||||
}
|
||||
|
||||
extension UIColor {
|
||||
convenience init(red255 red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat) {
|
||||
self.init(red: red / 255, green: green / 255, blue: blue / 255, alpha: alpha / 255)
|
||||
}
|
||||
}
|
||||
|
@ -23,9 +23,22 @@ class ViewController: UIViewController, MetaballDataSource {
|
||||
let recognizer = UIPanGestureRecognizer(target: self, action: "handlePan:")
|
||||
view.addGestureRecognizer(recognizer)
|
||||
|
||||
let metaballs = [CGPoint(x: 70, y: 70), CGPoint(x: 270, y: 70), CGPoint(x: 270, y: 470), CGPoint(x: 70, y: 470)].map {
|
||||
Metaball(position: $0)
|
||||
}
|
||||
let lowX = screenWidth / 3
|
||||
let highX = screenWidth - lowX
|
||||
let lowY = screenHeight / 4
|
||||
let highY = screenHeight - lowY
|
||||
let positions =
|
||||
[CGPoint(x: lowX, y: lowY),
|
||||
CGPoint(x: highX, y: lowY),
|
||||
CGPoint(x: highX, y: highY),
|
||||
CGPoint(x: lowX, y: highY)]
|
||||
let colors =
|
||||
[UIColor(red255: 90, green: 170, blue: 170, alpha: 1.0),
|
||||
UIColor(red255: 90, green: 170, blue: 170, alpha: 1.0),
|
||||
UIColor(red255: 110, green: 220, blue: 220, alpha: 1.0),
|
||||
UIColor(red255: 250, green: 235, blue: 50, alpha: 1.0)]
|
||||
let metaballs = zip(positions, colors).map { Metaball(position: $0.0, color: $0.1) }
|
||||
|
||||
metaballGraph = Graph(vertices: metaballs)
|
||||
metaballGraph.addEdge(0, 1)
|
||||
metaballGraph.addEdge(0, 3)
|
||||
@ -104,18 +117,3 @@ class ViewController: UIViewController, MetaballDataSource {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class Metaball: UIView {
|
||||
|
||||
init(position: CGPoint) {
|
||||
super.init(frame: CGRect.zero)
|
||||
|
||||
size = CGSize(50)
|
||||
middle = position
|
||||
backgroundColor = UIColor(randomFlatColorOfShadeStyle: .Light)
|
||||
}
|
||||
|
||||
required init?(coder aDecoder: NSCoder) {
|
||||
super.init(coder: aDecoder)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user