git.iko.soy/metal-metaballs
on master help sign in
Added color information to the metaball buffer.
commit131fb3168e2af3a3660f7fdeae7deb20e46d5f00 authorvinivendra <vinivendra@gmail.com> dateSun 14 Feb 2016, 02:32 (11 years ago) parent292d19c
3 files changed+15−3
PixelImage.xcodeproj/project.pbxproj+4 PixelImage/MetalMetaballRenderer.swift+9−1 PixelImage/shaders.metal+2−2
PixelImage.xcodeproj/project.pbxproj+4
@@ -13,6 +13,7 @@
1313 2426868F1C63B72A00D62456 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 2426868E1C63B72A00D62456 /* Assets.xcassets */; };
1414 242686921C63B72A00D62456 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 242686901C63B72A00D62456 /* LaunchScreen.storyboard */; };
1515 2461651B1C6DA4F400A913C8 /* Interpolation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2461651A1C6DA4F400A913C8 /* Interpolation.swift */; };
16+ 246B0F831C70635D00A71D05 /* UIColor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 246B0F821C70635D00A71D05 /* UIColor.swift */; };
1617 249CF84C1C6D113C008B0C78 /* Matrix.swift in Sources */ = {isa = PBXBuildFile; fileRef = 249CF84B1C6D113C008B0C78 /* Matrix.swift */; };
1718 24E1E7D71C67C8B200ECF1C4 /* UIKit.swift in Sources */ = {isa = PBXBuildFile; fileRef = 24E1E7D61C67C8B200ECF1C4 /* UIKit.swift */; };
1819 24E1E7E21C67D37F00ECF1C4 /* Metal.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 24E1E7E11C67D37F00ECF1C4 /* Metal.framework */; };
@@ -61,6 +62,7 @@
6162 242686911C63B72A00D62456 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
6263 242686931C63B72A00D62456 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
6364 2461651A1C6DA4F400A913C8 /* Interpolation.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Interpolation.swift; path = PixelImage/Interpolation.swift; sourceTree = SOURCE_ROOT; };
65+ 246B0F821C70635D00A71D05 /* UIColor.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIColor.swift; sourceTree = "<group>"; };
6466 249CF84B1C6D113C008B0C78 /* Matrix.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Matrix.swift; sourceTree = "<group>"; };
6567 24E1E7D61C67C8B200ECF1C4 /* UIKit.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIKit.swift; sourceTree = "<group>"; };
6668 24E1E7E11C67D37F00ECF1C4 /* Metal.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Metal.framework; path = System/Library/Frameworks/Metal.framework; sourceTree = SDKROOT; };
@@ -143,6 +145,7 @@
143145 242686871C63B72A00D62456 /* AppDelegate.swift */,
144146 242686891C63B72A00D62456 /* ViewController.swift */,
145147 24E1E7D61C67C8B200ECF1C4 /* UIKit.swift */,
148+ 246B0F821C70635D00A71D05 /* UIColor.swift */,
146149 24ED21CC1C6C2397009C40FC /* MetaballDataSource.swift */,
147150 24E1E7EA1C67D70300ECF1C4 /* MetalMetaballRenderer.swift */,
148151 24E1E7EE1C67D74300ECF1C4 /* Metal.swift */,
@@ -383,6 +386,7 @@
383386 24FFA86E1C6BA038005ACD60 /* Dictionary.swift in Sources */,
384387 24FFA87D1C6BA038005ACD60 /* Hopping.swift in Sources */,
385388 24E1E7EB1C67D70300ECF1C4 /* MetalMetaballRenderer.swift in Sources */,
389+ 246B0F831C70635D00A71D05 /* UIColor.swift in Sources */,
386390 24FFA87A1C6BA038005ACD60 /* Enumerate.swift in Sources */,
387391 242686881C63B72A00D62456 /* AppDelegate.swift in Sources */,
388392 24E1E7F01C67D74300ECF1C4 /* Metal.swift in Sources */,
PixelImage/MetalMetaballRenderer.swift+9−1
@@ -1,5 +1,6 @@
11import Metal
22import UIKit
3+import ChameleonFramework
34
45class MetalMetaballRenderer {
56
@@ -181,11 +182,18 @@ class MetalMetaballRenderer {
181182 -border < y ≤ targetView.height + border {
182183 array.append(Float(x))
183184 array.append(Float(y))
185+
186+ let color = UIColor.randomFlatColor()
187+ let components = color.components
188+ array.append(Float(components.blue))
189+ array.append(Float(components.green))
190+ array.append(Float(components.red))
184191 }
185192 return array
186193 }
194+
187195 // Add array size to start of array so metal knows how far to go
188− floats[0] = Float(floats.count) / 2
196+ floats[0] = Float(floats.count) / 5
189197
190198 // Create buffer
191199 let buffer = context.device.newBufferWithBytes(floats, length: floats.count * sizeof(Float), options: .StorageModeShared)
PixelImage/shaders.metal+2−2
@@ -6,7 +6,7 @@ using namespace metal;
66kernel void drawMetaballs(texture2d<float, access::write> outTexture[[texture(0)]], constant float *edgesBuffer[[buffer(1)]], constant float *metaballBuffer[[buffer(0)]], uint2 gid[[thread_position_in_grid]]) {
77
88 char numberOfMetaballs = metaballBuffer[0];
9− char metaballArraySize = numberOfMetaballs * 2 + 1;
9+ char metaballArraySize = numberOfMetaballs * 5 + 1;
1010
1111 float sum = 0;
1212
@@ -15,7 +15,7 @@ kernel void drawMetaballs(texture2d<float, access::write> outTexture[[texture(0)
1515 float metaballDistances[10];
1616 float2 metaballDirections[10];
1717
18− for (y = 1, x = 0; y < metaballArraySize; y += 2, x += 1) {
18+ for (y = 1, x = 0; y < metaballArraySize; y += 5, x += 1) {
1919 float2 metaball = float2(metaballBuffer[y], metaballBuffer[y + 1]);
2020 float2 vector = float2(metaball.x - gid.x, metaball.y - gid.y);
2121 float squaredDistance = dot(vector, vector);