From 2a7a83c59515f6a3901d4a80b00e496dcc02e62d Mon Sep 17 00:00:00 2001 From: Hexagons Date: Sun, 28 Jun 2020 15:48:09 +0200 Subject: [PATCH] SwiftUI PIXs --- .../PIX/PIXs/Content/Generator/NoisePIX.swift | 116 ++++++++++++++++++ Source/PIX/PIXs/Effects/Merger/BlendPIX.swift | 28 ++++- 2 files changed, 142 insertions(+), 2 deletions(-) diff --git a/Source/PIX/PIXs/Content/Generator/NoisePIX.swift b/Source/PIX/PIXs/Content/Generator/NoisePIX.swift index f54466b2..74f17833 100644 --- a/Source/PIX/PIXs/Content/Generator/NoisePIX.swift +++ b/Source/PIX/PIXs/Content/Generator/NoisePIX.swift @@ -8,6 +8,122 @@ import LiveValues import RenderKit +#if canImport(SwiftUI) +import SwiftUI +#endif + +@available(iOS 13.0.0, *) +@available(OSX 10.15, *) +@available(tvOS 13.0.0, *) +public struct PixNoise: View, PIXUI { + public var node: NODE { pix } + + public let pix: PIX + let noisepix: NoisePIX + public var body: some View { + NODERepView(node: pix) + } + + public init(octaves: Binding = .constant(10), + colored: Binding = .constant(false), + random: Binding = .constant(false), + zPosition: Binding = .constant(0.0), + zoom: Binding = .constant(1.0), + position: Binding = .constant(.zero), + resolution: Resolution = .auto(render: PixelKit.main.render)) { + noisepix = NoisePIX(at: resolution) + noisepix.octaves = LiveInt({ octaves.wrappedValue }) + noisepix.colored = LiveBool({ colored.wrappedValue }) + noisepix.random = LiveBool({ random.wrappedValue }) + noisepix.zPosition = LiveFloat({ zPosition.wrappedValue }) + noisepix.zoom = LiveFloat({ zoom.wrappedValue }) + noisepix.position = LivePoint({ position.wrappedValue }) + pix = noisepix + } + // Parent Property Funcs + public func bgColor(_ bgColor: Binding<_Color>) -> PixNoise { + noisepix.bgColor = LiveColor({ bgColor.wrappedValue }) + return self + } + public func bgColor(_ bgColor: LiveColor) -> PixNoise { + noisepix.bgColor = bgColor + return self + } + public func color(_ color: Binding<_Color>) -> PixNoise { + noisepix.color = LiveColor({ color.wrappedValue }) + return self + } + public func color(_ color: LiveColor) -> PixNoise { + noisepix.color = color + return self + } + // General Property Funcs + public func colored(_ colored: Binding) -> PixNoise { + noisepix.colored = LiveBool({ colored.wrappedValue }) + return self + } + public func colored(_ colored: LiveBool) -> PixNoise { + noisepix.colored = colored + return self + } + public func random(_ random: Binding) -> PixNoise { + noisepix.random = LiveBool({ random.wrappedValue }) + return self + } + public func random(_ random: LiveBool) -> PixNoise { + noisepix.random = random + return self + } + public func includeAlpha(_ includeAlpha: Binding) -> PixNoise { + noisepix.includeAlpha = LiveBool({ includeAlpha.wrappedValue }) + return self + } + public func includeAlpha(_ includeAlpha: LiveBool) -> PixNoise { + noisepix.includeAlpha = includeAlpha + return self + } + public func zPosition(_ zPosition: Binding) -> PixNoise { + noisepix.zPosition = LiveFloat({ zPosition.wrappedValue }) + return self + } + public func zPosition(_ zPosition: LiveFloat) -> PixNoise { + noisepix.zPosition = zPosition + return self + } + public func zoom(_ zoom: Binding) -> PixNoise { + noisepix.zoom = LiveFloat({ zoom.wrappedValue }) + return self + } + public func zoom(_ zoom: LiveFloat) -> PixNoise { + noisepix.zoom = zoom + return self + } + public func seed(_ seed: Binding) -> PixNoise { + noisepix.seed = LiveInt({ seed.wrappedValue }) + return self + } + public func seed(_ seed: LiveInt) -> PixNoise { + noisepix.seed = seed + return self + } + public func octaves(_ octaves: Binding) -> PixNoise { + noisepix.octaves = LiveInt({ octaves.wrappedValue }) + return self + } + public func octaves(_ octaves: LiveInt) -> PixNoise { + noisepix.octaves = octaves + return self + } + public func position(_ position: Binding) -> PixNoise { + noisepix.position = LivePoint({ position.wrappedValue }) + return self + } + public func position(_ position: LivePoint) -> PixNoise { + noisepix.position = position + return self + } + // Enum Property Funcs +} public class NoisePIX: PIXGenerator, PIXAuto { diff --git a/Source/PIX/PIXs/Effects/Merger/BlendPIX.swift b/Source/PIX/PIXs/Effects/Merger/BlendPIX.swift index 332fb9c0..287f4c8f 100644 --- a/Source/PIX/PIXs/Effects/Merger/BlendPIX.swift +++ b/Source/PIX/PIXs/Effects/Merger/BlendPIX.swift @@ -13,6 +13,30 @@ import MetalKit //#if !os(tvOS) && !targetEnvironment(simulator) //import MetalPerformanceShaders //#endif +#if canImport(SwiftUI) +import SwiftUI +#endif + +@available(iOS 13.0.0, *) +@available(OSX 10.15, *) +@available(tvOS 13.0.0, *) +public struct PixBlend: View, PIXUI { + public var node: NODE { pix } + + public let pix: PIX + let blendpix: BlendPIX + public var body: some View { + NODERepView(node: pix) + } + + public init(mode: RenderKit.BlendMode, _ uiPixA: () -> (PIXUI), with uiPixB: () -> (PIXUI)) { + blendpix = BlendPIX() + blendpix.blendMode = mode + pix = blendpix + blendpix.inputA = uiPixA().pix as? (PIX & NODEOut) + blendpix.inputB = uiPixB().pix as? (PIX & NODEOut) + } +} public class BlendPIX: PIXMergerEffect, Layoutable, PIXAuto/*, PixelCustomMergerRenderDelegate*/ { @@ -20,7 +44,7 @@ public class BlendPIX: PIXMergerEffect, Layoutable, PIXAuto/*, PixelCustomMerger // MARK: - Public Properties - public var blendMode: BlendMode = .add { didSet { setNeedsRender() } } + public var blendMode: RenderKit.BlendMode = .add { didSet { setNeedsRender() } } public var bypassTransform: LiveBool = false public var position: LivePoint = .zero public var rotation: LiveFloat = LiveFloat(0.0, min: -0.5, max: 0.5) @@ -140,7 +164,7 @@ public class BlendPIX: PIXMergerEffect, Layoutable, PIXAuto/*, PixelCustomMerger } -public func blend(_ mode: BlendMode, _ pixA: PIX & NODEOut, _ pixB: PIX & NODEOut) -> BlendPIX { +public func blend(_ mode: RenderKit.BlendMode, _ pixA: PIX & NODEOut, _ pixB: PIX & NODEOut) -> BlendPIX { let blendPix = BlendPIX() blendPix.inputA = pixA blendPix.inputB = pixB