diff --git a/Metaballs-macOS/ViewController.swift b/Metaballs-macOS/ViewController.swift index 5adaa2d..31c5987 100644 --- a/Metaballs-macOS/ViewController.swift +++ b/Metaballs-macOS/ViewController.swift @@ -13,7 +13,7 @@ class ViewController: NSViewController, RendererDelegate { private static func defaultParameters() -> Parameters { var p = Parameters() let defaults = UserDefaults.standard - let style = defaults.colorStyle ?? .gradient2Horizontal + let style = defaults.colorStyle ?? .gradient2 p.colorStyle = style let color0 = defaults.color0 ?? Float4(0.50, 0.79, 1, 1) p.color0 = color0 diff --git a/MetaballsKit/Metaballs.swift b/MetaballsKit/Metaballs.swift index a679234..128ced6 100644 --- a/MetaballsKit/Metaballs.swift +++ b/MetaballsKit/Metaballs.swift @@ -16,14 +16,10 @@ public enum MetaballsError: Error { public enum ColorStyle: UInt32 { /// Single flat color case singleColor = 1 - /// Two color horizontal gradient - case gradient2Horizontal = 2 - /// Two color vertical gradient - case gradient2Vertical = 3 - /// Four color gradient from corners - case gradient4Corners = 4 - /// Four color gradient from middle of sides - case gradient4Sides = 5 + /// Two color gradient + case gradient2 = 2 + /// Four color gradient + case gradient4 = 4 } public struct Parameters { diff --git a/MetaballsKit/PreferencesViewController.swift b/MetaballsKit/PreferencesViewController.swift index d91a5f6..074f1f0 100644 --- a/MetaballsKit/PreferencesViewController.swift +++ b/MetaballsKit/PreferencesViewController.swift @@ -25,15 +25,17 @@ public class PreferencesViewController: NSViewController { return [ StyleItem(name: NSLocalizedString("Single Color", comment: "single color menu item"), tag: Int(ColorStyle.singleColor.rawValue), - colorNames: [NSLocalizedString("Color", comment: "single color name")]), - StyleItem(name: NSLocalizedString("Two Color Gradient — Horizontal", comment: "two color horizontal gradient menu item"), - tag: Int(ColorStyle.gradient2Horizontal.rawValue), - colorNames: [NSLocalizedString("Right", comment: "two color horizontal gradient, color 1"), - NSLocalizedString("Left", comment: "two color horizontal gradient, color 2")]), - StyleItem(name: NSLocalizedString("Two Color Gradient — Vertical", comment: "two color vertical gradient menu item"), - tag: Int(ColorStyle.gradient2Vertical.rawValue), - colorNames: [NSLocalizedString("Top", comment: "two color vertical gradient, color 1"), - NSLocalizedString("Bottom", comment: "two color vertical gradient, color 2")]), + colorNames: [NSLocalizedString("A", comment: "first color")]), + StyleItem(name: NSLocalizedString("Two Color Gradient", comment: "two color gradient menu item"), + tag: Int(ColorStyle.gradient2.rawValue), + colorNames: [NSLocalizedString("A", comment: "first color"), + NSLocalizedString("B", comment: "second color")]), + StyleItem(name: NSLocalizedString("Four Color Gradient", comment: "four color gradient menu item"), + tag: Int(ColorStyle.gradient4.rawValue), + colorNames: [NSLocalizedString("A", comment: "first color"), + NSLocalizedString("B", comment: "second color"), + NSLocalizedString("C", comment: "third color"), + NSLocalizedString("D", comment: "fourth color")]), ] } diff --git a/MetaballsKit/Shaders.metal b/MetaballsKit/Shaders.metal index b1ab61e..21b344f 100644 --- a/MetaballsKit/Shaders.metal +++ b/MetaballsKit/Shaders.metal @@ -27,14 +27,10 @@ typedef struct { typedef enum { /// Single flat color SingleColor = 1, - /// Two color horizontal gradient - Gradient2Horizontal = 2, - /// Two color vertical gradient - Gradient2Vertical = 3, - /// Four color gradient from corners - Gradient4Corners = 4, - /// Four color gradient from middle of sides - Gradient4Sides = 5, + /// Two color gradient + Gradient2 = 2, + /// Four color gradient + Gradient4 = 4, } ColorStyle; typedef struct { @@ -88,19 +84,26 @@ sampleToColorShader(RasterizerData in [[stage_in]], case SingleColor: out = singleColor(sample, target, feather, parameters.colors[0]); break; - case Gradient2Horizontal: { - const float3 transformedColor = (parameters.colorTransform * float3(in.position.xy, 1.0)); + case Gradient2: { + const float3 transformedColor = parameters.colorTransform * float3(in.position.xy, 1.0); const float blend = transformedColor.x / parameters.size[0]; - out = gradient2(sample, target, feather, blend, parameters.colors[0], parameters.colors[1]); + const float4 color = mix(parameters.colors[0], parameters.colors[1], blend); + out = singleColor(sample, target, feather, color); break; } - case Gradient2Vertical: { - const float blend = in.position.y / parameters.size[1]; - out = gradient2(sample, target, feather, blend, parameters.colors[0], parameters.colors[1]); + case Gradient4: { + const float3 transformedColorCoords = parameters.colorTransform * float3(in.position.xy, 1.0); + const float2 blend = float2(transformedColorCoords.x / parameters.size[0], + transformedColorCoords.y / parameters.size[1]); + const float4 color = mix(mix(parameters.colors[0], parameters.colors[2], blend.y), + mix(parameters.colors[1], parameters.colors[3], blend.y), + blend.x); + out = singleColor(sample, target, feather, color); break; } } + return out; } @@ -147,7 +150,7 @@ gradient2(float sample, float4 fromColor, float4 toColor) { - float4 blendedColor = averageTwoColors(normalizedBlend, fromColor, toColor); + float4 blendedColor = mix(fromColor, toColor, normalizedBlend); float4 out = singleColor(sample, target, feather, blendedColor); return out; } diff --git a/MetaballsSaver/MetaballsSaverView.swift b/MetaballsSaver/MetaballsSaverView.swift index dc83a52..6b93725 100644 --- a/MetaballsSaver/MetaballsSaverView.swift +++ b/MetaballsSaver/MetaballsSaverView.swift @@ -14,7 +14,7 @@ import ScreenSaver private static func defaultParameters() -> Parameters { var p = Parameters() let defaults = UserDefaults.standard - let style = defaults.colorStyle ?? .gradient2Horizontal + let style = defaults.colorStyle ?? .gradient2 p.colorStyle = style let color0 = defaults.color0 ?? Float4(0.50, 0.79, 1, 1) p.color0 = color0