Skip to content

Commit

Permalink
web and metal
Browse files Browse the repository at this point in the history
  • Loading branch information
heestand-xyz committed Jul 3, 2019
1 parent 3905df7 commit 7c9b263
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 0 deletions.
6 changes: 6 additions & 0 deletions PixelKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
3A70A64222859EDC00C71B44 /* GradientPIX.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A70A63F22859EDC00C71B44 /* GradientPIX.swift */; };
3A70A64322859EDC00C71B44 /* GradientPIX.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A70A63F22859EDC00C71B44 /* GradientPIX.swift */; };
3A7BFA81223BA75400140C35 /* LiveMouseView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A7BFA80223BA75400140C35 /* LiveMouseView.swift */; };
3A84A0B122CCA958001F3D90 /* WebPIX.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A84A0B022CCA958001F3D90 /* WebPIX.swift */; };
3A84A0B422CCAA00001F3D90 /* WebPIX.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A84A0B022CCA958001F3D90 /* WebPIX.swift */; };
3A8558A62292C55E002B0983 /* Links.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A8558A32292C55E002B0983 /* Links.swift */; };
3A8558A72292C55E002B0983 /* Links.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A8558A32292C55E002B0983 /* Links.swift */; };
3AA4AC20224CFB5D00E894B2 /* ArcPIX.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3AA4AC1F224CFB5D00E894B2 /* ArcPIX.swift */; };
Expand Down Expand Up @@ -283,6 +285,7 @@
3A6E9D2E2254A946008F36D8 /* PIXAuto.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PIXAuto.swift; sourceTree = "<group>"; };
3A70A63F22859EDC00C71B44 /* GradientPIX.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GradientPIX.swift; sourceTree = "<group>"; };
3A7BFA80223BA75400140C35 /* LiveMouseView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LiveMouseView.swift; sourceTree = "<group>"; };
3A84A0B022CCA958001F3D90 /* WebPIX.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WebPIX.swift; sourceTree = "<group>"; };
3A8558A32292C55E002B0983 /* Links.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Links.swift; path = "Source/PIX/PIX Extensions/Links.swift"; sourceTree = SOURCE_ROOT; };
3A8AC3482277282C00A142AA /* PixelKit.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = PixelKit.entitlements; sourceTree = "<group>"; };
3A8AC3492277292C00A142AA /* SyphonInPIX.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SyphonInPIX.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -669,6 +672,7 @@
74BF327D224165A500F039F1 /* ScreenCapturePIX.swift */,
3A8AC3492277292C00A142AA /* SyphonInPIX.swift */,
74BF5E8F22A43978009CB791 /* VectorPIX.swift */,
3A84A0B022CCA958001F3D90 /* WebPIX.swift */,
);
path = Resource;
sourceTree = "<group>";
Expand Down Expand Up @@ -1064,6 +1068,7 @@
3AF28C0E219312F70079FC7A /* PIXEffect.swift in Sources */,
3AF28C18219313050079FC7A /* NoisePIX.swift in Sources */,
3AF28C05219312F50079FC7A /* HueSaturationPIX.swift in Sources */,
3A84A0B422CCAA00001F3D90 /* WebPIX.swift in Sources */,
3A6E9D302254A97F008F36D8 /* PIXAuto.swift in Sources */,
3AF28C12219313050079FC7A /* PIXGenerator.swift in Sources */,
74013307224D29E800826395 /* Layout.swift in Sources */,
Expand Down Expand Up @@ -1099,6 +1104,7 @@
3ADB0D5621AC411000AEEEFF /* LiveValue.swift in Sources */,
74764BB3211EEEDB00ED0FBB /* TwirlPIX.swift in Sources */,
74A40E1B210A11200085041D /* PIXView.swift in Sources */,
3A84A0B122CCA958001F3D90 /* WebPIX.swift in Sources */,
741D2CF921AC9CC0007259E3 /* String.swift in Sources */,
746F7472213FF82700C2AE62 /* CropPIX.swift in Sources */,
7421ED3F2231782E00E60A0E /* FreezePIX.swift in Sources */,
Expand Down
5 changes: 5 additions & 0 deletions Resources/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,10 @@
<string>0.6.5</string>
<key>CFBundleVersion</key>
<string>777</string>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
</dict>
</plist>
2 changes: 2 additions & 0 deletions Source/PIX/PIX Extensions/Resolution.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ extension PIX {
if let imagePix = pixResource as? ImagePIX {
guard let size = imagePix.image?.size else { return nil }
return Res.cgSize(size) * Res.scale
} else if let webPix = pixResource as? WebPIX {
return webPix.res
} else {
guard let pixelBuffer = pixResource.pixelBuffer else { return nil }
var bufferRes = Res(pixelBuffer: pixelBuffer)
Expand Down
112 changes: 112 additions & 0 deletions Source/PIX/PIXs/Content/Resource/WebPIX.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
//
// WebPIX.swift
// PixelKit
//
// Created by Anton Heestand on 2019-07-03.
// Copyright © 2019 Hexagons. All rights reserved.
//

#if os(iOS)
import UIKit
#elseif os(macOS)
import AppKit
#endif
import WebKit

public class WebPIX: PIXResource, PIXRes {

override open var shader: String { return "contentResourcePIX" }

// MARK: - Private Properties

let helper: WebHelper

// MARK: - Public Properties

public var res: Res { didSet { /*setFrame();*/ applyRes { self.setNeedsBuffer() } } }

public var url: URL = URL(string: "http://pixelkit.net/")! { didSet { refresh() } }
public var webView: WKWebView

// MARK: - Life Cycle

public required init(res: PIX.Res) {

self.res = res

helper = WebHelper()

webView = WKWebView()

super.init()

name = "web"

webView.navigationDelegate = helper
helper.refreshCallback = {
self.pixelKit.log(pix: self, .info, .resource, "Web refreshed!")
self.setNeedsBuffer()
}

refresh()

// setFrame()

applyRes { self.setNeedsRender() }

}

// func setFrame() {
// webView.frame = CGRect(origin: .zero, size: res.size.cg)
// }

// MARK: - Load

public func refresh() {
pixelKit.log(pix: self, .info, .resource, "Web refresh: \(url)")
let request = URLRequest(url: url)
webView.load(request)
}

// MARK: - Buffer

func setNeedsBuffer() {
let config = WKSnapshotConfiguration()
if #available(OSX 10.15, *) {
if #available(iOS 13.0, *) {
config.afterScreenUpdates = false
}
}
config.rect = CGRect(origin: .zero, size: res.size.cg)
webView.takeSnapshot(with: config) { image, error in
guard error == nil else {
self.pixelKit.log(pix: self, .error, .resource, "Web snapshot failed.", e: error)
return
}
guard let image = image else {
self.pixelKit.log(pix: self, .error, .resource, "Web snapshot image not avalible.")
return
}
guard let buffer = self.pixelKit.buffer(from: image) else {
self.pixelKit.log(pix: self, .error, .resource, "Pixel Buffer creation failed.")
return
}
self.pixelBuffer = buffer
self.pixelKit.log(pix: self, .info, .resource, "Image Loaded.")
self.setNeedsRender()
}
}

}

// MARK: - Web Helper

class WebHelper: NSObject, WKNavigationDelegate {

var refreshCallback: (() -> ())?

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
refreshCallback?()
}

}
6 changes: 6 additions & 0 deletions Source/PIX/PIXs/Effects/Merger/MetalMergerEffectPIX.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
// Open Source - MIT License
//

#if os(iOS) && targetEnvironment(simulator)
import MetalPerformanceShadersProxy
#else
import Metal
#endif

/// Metal Shader (Merger Effect)
///
/// vars: pi, u, v, uv, wA, hA, wuA, hvA, inPixA, wB, hB, wuB, hvB, inPixB
Expand Down
6 changes: 6 additions & 0 deletions Source/PIX/PIXs/Effects/Multi/MetalMultiEffectPIX.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
// Open Source - MIT License
//

#if os(iOS) && targetEnvironment(simulator)
import MetalPerformanceShadersProxy
#else
import Metal
#endif

/// Metal Shader (Multi Effect)
///
/// vars: pi, u, v, uv, pixCount
Expand Down
6 changes: 6 additions & 0 deletions Source/PIX/PIXs/Effects/Single/MetalEffectPIX.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
// Open Source - MIT License
//

#if os(iOS) && targetEnvironment(simulator)
import MetalPerformanceShadersProxy
#else
import Metal
#endif

/// Metal Shader (Effect)
///
/// vars: pi, u, v, uv, w, h, wu, hv, inPix
Expand Down

0 comments on commit 7c9b263

Please sign in to comment.