Skip to content

Commit

Permalink
Bug Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Lakr233 committed Jul 4, 2024
1 parent 3ed67df commit 03f1ab6
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 212 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/swift.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,3 @@ jobs:
- uses: actions/checkout@v4
- name: Run Test Build
run: ./Scripts/test.build.sh
- name: Run Tests
run: pushd Example && xcodebuild test -workspace ColorfulApp.xcworkspace -scheme ShaderTest && popd
4 changes: 0 additions & 4 deletions Example/ColorfulApp/ColorfulAppApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ import SwiftUI

@main
struct ColorfulAppApp: App {
init() {
setenv("MTL_HUD_ENABLED", "1", 1)
}

var body: some Scene {
WindowGroup {
ContentView()
Expand Down
5 changes: 5 additions & 0 deletions Example/ColorfulApp/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ struct ContentView: View {
@AppStorage("duration") var duration: TimeInterval = 3.5
@AppStorage("ColorSpace") var colorSpace: ColorSpace = .lab

@State var controlPanelVisible: Bool = true

var body: some View {
ZStack {
ForEach([colorSpace.rawValue], id: \.self) { _ in
Expand All @@ -35,6 +37,8 @@ struct ContentView: View {
.animation(.interactiveSpring, value: colorSpace.rawValue)
VStack {
controlPanel
.opacity(controlPanelVisible ? 1 : 0)
.animation(.spring, value: controlPanelVisible)
#if os(tvOS)
Button {
preset = ColorfulPreset.allCases.randomElement()!
Expand All @@ -51,6 +55,7 @@ struct ContentView: View {
#else
.font(.system(size: 12, weight: .semibold, design: .rounded))
#endif
.onTapGesture { controlPanelVisible.toggle() }

Check failure on line 58 in Example/ColorfulApp/ContentView.swift

View workflow job for this annotation

GitHub Actions / build

'onTapGesture(count:perform:)' is only available in tvOS 16.0 or newer
.frame(maxHeight: .infinity, alignment: .bottom)
.padding()
}
Expand Down
9 changes: 9 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
{
"pins" : [
{
"identity" : "colorvector",
"kind" : "remoteSourceControl",
"location" : "https://github.com/Lakr233/ColorVector.git",
"state" : {
"revision" : "031ce6ce65c0b347fb192302aa8ec4a17cc1494f",
"version" : "1.0.3"
}
},
{
"identity" : "springinterpolation",
"kind" : "remoteSourceControl",
Expand Down
15 changes: 6 additions & 9 deletions Sources/ColorfulX/AnimatedMulticolorGradientView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import ColorVector
import MetalKit
import SpringInterpolation

private let COLOR_SLOT = 8
private let SPRING_CONFIG = SpringInterpolation.Configuration(
angularFrequency: 1.5,
dampingRatio: 0.2
Expand All @@ -28,7 +27,7 @@ open class AnimatedMulticolorGradientView: MulticolorGradientView {
public var frameLimit: Int = 0

override public init(colorSpace: ColorSpace = .lab) {
colorElements = .init(repeating: .init(position: SPRING_ENGINE), count: COLOR_SLOT)
colorElements = .init(repeating: .init(position: SPRING_ENGINE), count: Uniforms.COLOR_SLOT)

super.init(colorSpace: colorSpace)

Expand Down Expand Up @@ -70,17 +69,15 @@ open class AnimatedMulticolorGradientView: MulticolorGradientView {

public func setColors(_ colors: [ColorVector], interpolationEnabled: Bool = true) {
var colors = colors
if colors.isEmpty { colors.append(.init(v: .zero, space: .rgb)) }

if let targetSpace = colors.first?.space,
targetSpace != colorSpace
{
colors = colors.map { $0.color(in: targetSpace) }
}
{ colors = colors.map { $0.color(in: targetSpace) } }

for idx in 0 ..< COLOR_SLOT {
for idx in 0 ..< Uniforms.COLOR_SLOT {
var read = colorElements[idx]
let color: ColorVector = colors.isEmpty
? ColorVector(space: .rgb)
: colors[idx % colors.count]
let color: ColorVector = colors[idx % colors.count]
guard read.targetColor != color else { continue }
let interpolationEnabled = interpolationEnabled && read.enabled
let currentColor = computeSpeckleColor(read)
Expand Down
2 changes: 1 addition & 1 deletion Sources/ColorfulX/Color.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public typealias ColorSpace = ColorVector.Space
#endif
#endif

extension ColorVector {
public extension ColorVector {
init(_ color: ColorElement, usingSpace space: Space = .rgb) {
let cgColor = color.cgColor
let color = cgColor.converted(
Expand Down
62 changes: 59 additions & 3 deletions Sources/ColorfulX/MulticolorGradientView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,22 @@ open class MulticolorGradientView: MetalView {
simd_float2(0.0, 0.0),
simd_float4(0.0, 0.0, 0.0, 0.0)
),
count: 8
count: Uniforms.COLOR_SLOT
)

let parms = parameters

for i in 0 ..< parms.points.count {
let point = parms.points[i]
let color = point.color.color(in: .rgb)
shaderPoints[i] = (
simd_float2(Float(point.position.x), Float(point.position.y)),
simd_float4(point.color.v / 255)
simd_float4(
Float(color.rgba.r / 255),
Float(color.rgba.g / 255),
Float(color.rgba.b / 255),
Float(color.rgba.a)
)
)
}

Expand All @@ -82,6 +88,7 @@ open class MulticolorGradientView: MetalView {
bias: simd_float1(parms.bias),
power: simd_float1(parms.power),
noise: simd_float1(parms.noise),

point0: shaderPoints[0].0,
point1: shaderPoints[1].0,
point2: shaderPoints[2].0,
Expand All @@ -90,14 +97,63 @@ open class MulticolorGradientView: MetalView {
point5: shaderPoints[5].0,
point6: shaderPoints[6].0,
point7: shaderPoints[7].0,
// point8: shaderPoints[8].0,
// point9: shaderPoints[9].0,
// point10: shaderPoints[10].0,
// point11: shaderPoints[11].0,
// point12: shaderPoints[12].0,
// point13: shaderPoints[13].0,
// point14: shaderPoints[14].0,
// point15: shaderPoints[15].0,
// point16: shaderPoints[16].0,
// point17: shaderPoints[17].0,
// point18: shaderPoints[18].0,
// point19: shaderPoints[19].0,
// point20: shaderPoints[20].0,
// point21: shaderPoints[21].0,
// point22: shaderPoints[22].0,
// point23: shaderPoints[23].0,
// point24: shaderPoints[24].0,
// point25: shaderPoints[25].0,
// point26: shaderPoints[26].0,
// point27: shaderPoints[27].0,
// point28: shaderPoints[28].0,
// point29: shaderPoints[29].0,
// point30: shaderPoints[20].0,
// point31: shaderPoints[31].0,

color0: shaderPoints[0].1,
color1: shaderPoints[1].1,
color2: shaderPoints[2].1,
color3: shaderPoints[3].1,
color4: shaderPoints[4].1,
color5: shaderPoints[5].1,
color6: shaderPoints[6].1,
color7: shaderPoints[7].1
color7: shaderPoints[7].1 // ,
// color8:shaderPoints[8].1,
// color9:shaderPoints[9].1,
// color10:shaderPoints[10].1,
// color11:shaderPoints[11].1,
// color12:shaderPoints[12].1,
// color13:shaderPoints[13].1,
// color14:shaderPoints[14].1,
// color15:shaderPoints[15].1,
// color16:shaderPoints[16].1,
// color17:shaderPoints[17].1,
// color18:shaderPoints[18].1,
// color19:shaderPoints[19].1,
// color20:shaderPoints[20].1,
// color21:shaderPoints[21].1,
// color22:shaderPoints[22].1,
// color23:shaderPoints[23].1,
// color24:shaderPoints[24].1,
// color25:shaderPoints[25].1,
// color26:shaderPoints[26].1,
// color27:shaderPoints[27].1,
// color28:shaderPoints[28].1,
// color29:shaderPoints[29].1,
// color30:shaderPoints[20].1,
// color31:shaderPoints[31].1
)

commandEncoder.setComputePipelineState(computePipelineState)
Expand Down
13 changes: 7 additions & 6 deletions Sources/ColorfulX/Shaders/Main.metal
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@
using namespace metal;

#define M_PI 3.1415926535897932384626433832795
#define COLOR_SOLT 8

typedef struct {
int32_t pointCount;
float bias;
float power;
float noise;
float2 points[8];
float4 colors[8];
float2 points[COLOR_SOLT];
float4 colors[COLOR_SOLT];
} Uniforms;

typedef struct {
Expand Down Expand Up @@ -299,7 +300,7 @@ kernel void gradientWithRGB(texture2d<float, access::write> output [[texture(4)]
float2 uv = (float2(gid) + float2(sin(noise.x * 2 * M_PI_F), sin(noise.y * 2 * M_PI_F)) * uniforms.noise) / float2(width, width);

float totalContribution = 0.0;
float contribution[8];
float contribution[COLOR_SOLT];

for (int i = 0; i < uniforms.pointCount; i++)
{
Expand Down Expand Up @@ -332,7 +333,7 @@ kernel void gradientWithXYZ(texture2d<float, access::write> output [[texture(4)]
float2 uv = (float2(gid) + float2(sin(noise.x * 2 * M_PI_F), sin(noise.y * 2 * M_PI_F)) * uniforms.noise) / float2(width, width);

float totalContribution = 0.0;
float contribution[8];
float contribution[COLOR_SOLT];

for (int i = 0; i < uniforms.pointCount; i++)
{
Expand Down Expand Up @@ -368,7 +369,7 @@ kernel void gradientWithLAB(texture2d<float, access::write> output [[texture(4)]
float2 uv = (float2(gid) + float2(sin(noise.x * 2 * M_PI_F), sin(noise.y * 2 * M_PI_F)) * uniforms.noise) / float2(width, width);

float totalContribution = 0.0;
float contribution[8];
float contribution[COLOR_SOLT];

for (int i = 0; i < uniforms.pointCount; i++)
{
Expand Down Expand Up @@ -404,7 +405,7 @@ kernel void gradientWithLCH(texture2d<float, access::write> output [[texture(4)]
float2 uv = (float2(gid) + float2(sin(noise.x * 2 * M_PI_F), sin(noise.y * 2 * M_PI_F)) * uniforms.noise) / float2(width, width);

float totalContribution = 0.0;
float contribution[8];
float contribution[COLOR_SOLT];

for (int i = 0; i < uniforms.pointCount; i++)
{
Expand Down
50 changes: 50 additions & 0 deletions Sources/ColorfulX/Uniforms.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import MetalKit

struct Uniforms {
static let COLOR_SLOT = 8

let pointCount: simd_int1

let bias: simd_float1
Expand All @@ -22,6 +24,30 @@ struct Uniforms {
let point5: simd_float2
let point6: simd_float2
let point7: simd_float2
// let point8: simd_float2
// let point9: simd_float2
// let point10: simd_float2
// let point11: simd_float2
// let point12: simd_float2
// let point13: simd_float2
// let point14: simd_float2
// let point15: simd_float2
// let point16: simd_float2
// let point17: simd_float2
// let point18: simd_float2
// let point19: simd_float2
// let point20: simd_float2
// let point21: simd_float2
// let point22: simd_float2
// let point23: simd_float2
// let point24: simd_float2
// let point25: simd_float2
// let point26: simd_float2
// let point27: simd_float2
// let point28: simd_float2
// let point29: simd_float2
// let point30: simd_float2
// let point31: simd_float2

let color0: simd_float4
let color1: simd_float4
Expand All @@ -31,4 +57,28 @@ struct Uniforms {
let color5: simd_float4
let color6: simd_float4
let color7: simd_float4
// let color8: simd_float4
// let color9: simd_float4
// let color10: simd_float4
// let color11: simd_float4
// let color12: simd_float4
// let color13: simd_float4
// let color14: simd_float4
// let color15: simd_float4
// let color16: simd_float4
// let color17: simd_float4
// let color18: simd_float4
// let color19: simd_float4
// let color20: simd_float4
// let color21: simd_float4
// let color22: simd_float4
// let color23: simd_float4
// let color24: simd_float4
// let color25: simd_float4
// let color26: simd_float4
// let color27: simd_float4
// let color28: simd_float4
// let color29: simd_float4
// let color30: simd_float4
// let color31: simd_float4
}
Loading

0 comments on commit 03f1ab6

Please sign in to comment.