Skip to content

Commit

Permalink
Merge pull request #2 from maxxfrazer/bold-material
Browse files Browse the repository at this point in the history
Bold Materials, iOS 15 required.
  • Loading branch information
maxxfrazer committed May 14, 2023
2 parents 8991f33 + 55ccf93 commit 89d8a44
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 36 deletions.
45 changes: 45 additions & 0 deletions .github/workflows/build-docc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Simple workflow for deploying static content to GitHub Pages
name: Deploy DocC

on:
# Runs on pushes targeting the default branch
push:
branches: ["main"]

# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages
permissions:
contents: read
pages: write
id-token: write

# Allow one concurrent deployment
concurrency:
group: "pages"
cancel-in-progress: true

jobs:
# Single deploy job since we're just deploying
deploy:
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
runs-on: macos-13
steps:
- name: Checkout 🛎️
uses: actions/checkout@v3
- name: Build DocC
run: |
xcodebuild docbuild -scheme RealityToolkit -derivedDataPath /tmp/docbuild -destination 'generic/platform=iOS';
$(xcrun --find docc) process-archive \
transform-for-static-hosting /tmp/docbuild/Build/Products/Debug-iphoneos/RealityToolkit.doccarchive \
--hosting-base-path RealityToolkit \
--output-path docs;
echo "<script>window.location.href += \"/documentation/realitytoolkit\"</script>" > docs/index.html
- name: Upload artifact
uses: actions/upload-pages-artifact@v1
with:
# Upload docs directory
path: 'docs'
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v1
4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// swift-tools-version:5.3
// swift-tools-version:5.5
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "RealityToolkit",
platforms: [.iOS(.v14), .macOS(.v11)],
platforms: [.iOS(.v15), .macOS(.v12)],
products: [
.library(name: "RealityToolkit", targets: ["RealityToolkit"])
],
Expand Down
44 changes: 44 additions & 0 deletions Sources/RealityToolkit/BoldMaterial.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//
// BoldMaterial.swift
//
//
// Created by Max Cobb on 06/05/2023.
//

import RealityKit
#if canImport(AppKit)
import AppKit.NSColor
#elseif canImport(UIKit.UIColor)
import UIKit.UIColor
#endif

typealias BoldMaterial = PhysicallyBasedMaterial

extension BoldMaterial {
/// An initialiser for making a ``BoldMaterial``. BoldMaterial is essentially just a PhysicallyBasedMaterial,
/// but with some properties that make non-responsive to lighting, and the colours pop more vividly than an UnlitMaterial.
/// - Parameters:
/// - color: Color to set for the Bold material.
public init(color: Material.Color) {
self.init(color: color, texture: nil)
}

/// An initialiser for making a ``BoldMaterial``. BoldMaterial is essentially just a PhysicallyBasedMaterial,
/// but with some properties that make non-responsive to lighting, and the colours pop more vividly than an UnlitMaterial.
/// - Parameters:
/// - texture: Texture to set for the Bold material.
public init(texture: MaterialParameters.Texture) {
self.init(color: nil, texture: texture)
}
internal init(color: Material.Color?, texture: MaterialParameters.Texture?) {
self.init()
self.baseColor = .init(tint: .black)
self.sheen = .init(tint: .black)
if let color = color {
self.emissiveColor = .init(color: color, texture: texture)
} else {
self.emissiveColor = .init(texture: texture)
}
self.emissiveIntensity = 2
}
}
4 changes: 1 addition & 3 deletions Sources/RealityToolkit/RealityToolkit+LoadingAssets.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ public extension RealityToolkit {
internal static func loadResourceCompletion(
contentsOf url: URL
) async throws -> TextureResource {
if Thread.isMainThread { return try TextureResource.load(contentsOf: url) }
return try await MainActor.run { try TextureResource.load(contentsOf: url) }
}
}
Expand All @@ -61,12 +60,11 @@ public extension RealityToolkit {
static func loadEntity(
contentsOf url: URL, withName resourceName: String? = nil,
saveTo destination: URL? = nil, useCache: Bool = true,
using loadMethod: @escaping ((_ contentsOf: URL, _: String?) throws -> Entity) = Entity.load
using loadMethod: @escaping (@MainActor(_ contentsOf: URL, _: String?) throws -> Entity) = Entity.load
) async throws -> Entity {
let localUrl = try await RealityToolkit.downloadRemoteFile(
contentsOf: url, saveTo: destination, useCache: useCache
)
if Thread.isMainThread { return try loadMethod(localUrl, resourceName) }
return try await MainActor.run { try loadMethod(localUrl, resourceName) }
}

Expand Down
55 changes: 25 additions & 30 deletions Sources/RealityToolkit/RealityToolkit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,37 +35,32 @@ import OSLog
public static func downloadRemoteFile(
contentsOf url: URL, saveTo destination: URL? = nil, useCache: Bool = true
) async throws -> URL {
if !url.absoluteString.hasPrefix("http") {
RealityToolkit.RUIPrint("URL is already local. To move the file, call `FileManager.default.moveItem`")
if FileManager.default.fileExists(atPath: url.path) { return url }
RealityToolkit.RUIPrint("File at local URL does not exist.")
throw LoadRemoteError.localURLProvided
}
let filename = url.lastPathComponent
var endLocation = destination ?? FileManager.default.temporaryDirectory
if endLocation.hasDirectoryPath {
endLocation.appendPathComponent(filename)
}
if FileManager.default.fileExists(atPath: endLocation.path) {
if useCache {
return endLocation
}
do {
try FileManager.default.removeItem(atPath: endLocation.path)
} catch let err {
RealityToolkit.RUIPrint("Could not remove item: \(err)")
throw LoadRemoteError.cannotDelete
}
if !url.absoluteString.hasPrefix("http") {
RealityToolkit.RUIPrint("URL is already local. To move the file, call `FileManager.default.moveItem`")
if FileManager.default.fileExists(atPath: url.path) { return url }
RealityToolkit.RUIPrint("File at local URL does not exist.")
throw LoadRemoteError.localURLProvided
}
let filename = url.lastPathComponent
var endLocation = destination ?? FileManager.default.temporaryDirectory
if endLocation.hasDirectoryPath {
endLocation.appendPathComponent(filename)
}
if FileManager.default.fileExists(atPath: endLocation.path) {
if useCache {
return endLocation
}
if #available(iOS 15.0, macOS 12.0, *) {
let (moveFrom, _) = try await URLSession.shared.download(from: url)
try FileManager.default.moveItem(
atPath: moveFrom.path, toPath: endLocation.path
)
} else {
let data = try Data(contentsOf: url)
try data.write(to: endLocation)
do {
try FileManager.default.removeItem(atPath: endLocation.path)
} catch let err {
RealityToolkit.RUIPrint("Could not remove item: \(err)")
throw LoadRemoteError.cannotDelete
}
return endLocation
}
let (moveFrom, _) = try await URLSession.shared.download(from: url)
try FileManager.default.moveItem(
atPath: moveFrom.path, toPath: endLocation.path
)
return endLocation
}
}
3 changes: 2 additions & 1 deletion Tests/RealityToolkitTests/DownloadTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import RealityKit
import RealityFoundation
import Combine

@available(iOS 15.0, macOS 12.0, *)
final class Download_Tests: XCTestCase {

var downloadsFolder: URL = {
Expand All @@ -31,6 +30,7 @@ final class Download_Tests: XCTestCase {
return folder
}()

@MainActor
func testDownloadImg() async throws {
let saveToParam = self.downloadsFolder.appendingPathComponent("test_image.png")
let testImg = "https://www.freepnglogos.com/uploads/google-logo-png/" +
Expand Down Expand Up @@ -69,6 +69,7 @@ final class Download_Tests: XCTestCase {
XCTFail("The download should have failed")
}

@MainActor
func testDownloadUsdz() async throws {
let testImg = "https://developer.apple.com/augmented-reality/quick-look/models/retrotv/tv_retro.usdz"
let remoteFileLoaded = try await RealityToolkit.downloadRemoteFile(
Expand Down

0 comments on commit 89d8a44

Please sign in to comment.