Skip to content

Commit

Permalink
Merge pull request #33 from maxxfrazer/no-arkit-bugs
Browse files Browse the repository at this point in the history
Simulator and Delegates
- New delegate method for every anchor change
- Deploy DocC
- Fix simulator breaking issues
  • Loading branch information
maxxfrazer authored Oct 31, 2022
2 parents a3363f3 + 9d83ea6 commit ced786a
Show file tree
Hide file tree
Showing 16 changed files with 279 additions and 202 deletions.
43 changes: 43 additions & 0 deletions .github/workflows/deploy_docs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
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:
deploy:
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
runs-on: macos-12
steps:
- name: Checkout 🛎️
uses: actions/checkout@v3
- name: Build DocC 🛠
run: |
xcodebuild docbuild -scheme FocusEntity -derivedDataPath /tmp/docbuild -destination 'generic/platform=iOS';
$(xcrun --find docc) process-archive \
transform-for-static-hosting /tmp/docbuild/Build/Products/Debug-iphoneos/FocusEntity.doccarchive \
--output-path docs \
--hosting-base-path FocusEntity;
echo "<script>window.location.href += \"/documentation/focusentity\"</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
23 changes: 23 additions & 0 deletions .github/workflows/swift-build-lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: swiftlint

on:
push:
branches:
- "main"
pull_request:
branches:
- "*"

jobs:
build:
runs-on: macos-latest
steps:
- name: Checkout 🛎
uses: actions/checkout@v3
- name: Swift Lint 🧹
run: swiftlint --strict
- name: Test Build 🔨
run: xcodebuild -scheme $SCHEME -destination $DESTINATION
env:
SCHEME: FocusEntity
DESTINATION: generic/platform=iOS
25 changes: 0 additions & 25 deletions .github/workflows/swift-build.yml

This file was deleted.

19 changes: 0 additions & 19 deletions .github/workflows/swift-lint.yml

This file was deleted.

2 changes: 2 additions & 0 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ disabled_rules:
identifier_name:
min_length:
warning: 1
line_length:
ignores_comments: true
28 changes: 14 additions & 14 deletions FocusEntity-Example/FocusEntity-Example/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@ import SwiftUI
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?
var window: UIWindow?

func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {

// Create the SwiftUI view that provides the window contents.
let contentView = ContentView()
// Create the SwiftUI view that provides the window contents.
let contentView = ContentView()

// Use a UIHostingController as window root view controller.
let window = UIWindow(frame: UIScreen.main.bounds)
window.rootViewController = UIHostingController(rootView: contentView)
self.window = window
window.makeKeyAndVisible()
return true
}
// Use a UIHostingController as window root view controller.
let window = UIWindow(frame: UIScreen.main.bounds)
window.rootViewController = UIHostingController(rootView: contentView)
self.window = window
window.makeKeyAndVisible()
return true
}
}
23 changes: 10 additions & 13 deletions FocusEntity-Example/FocusEntity-Example/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,22 @@ import SwiftUI
import RealityKit

struct ContentView: View {
var body: some View {
ARViewContainer().edgesIgnoringSafeArea(.all)
}
var body: some View {
ARViewContainer().edgesIgnoringSafeArea(.all)
}
}

struct ARViewContainer: UIViewRepresentable {

func makeUIView(context: Context) -> FocusARView {
FocusARView(frame: .zero)
}

func updateUIView(_ uiView: FocusARView, context: Context) {}

func makeUIView(context: Context) -> FocusARView {
FocusARView(frame: .zero)
}
func updateUIView(_ uiView: FocusARView, context: Context) {}
}

#if DEBUG
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
static var previews: some View {
ContentView()
}
}
#endif
91 changes: 41 additions & 50 deletions FocusEntity-Example/FocusEntity-Example/FocusARView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,59 +12,50 @@ import Combine
import ARKit

class FocusARView: ARView {
enum FocusStyleChoices {
case classic
case material
case color
}
enum FocusStyleChoices {
case classic
case material
case color
}

/// Style to be displayed in the example
let focusStyle: FocusStyleChoices = .classic
var focusEntity: FocusEntity?
required init(frame frameRect: CGRect) {
super.init(frame: frameRect)
self.setupConfig()
/// Style to be displayed in the example
let focusStyle: FocusStyleChoices = .classic
var focusEntity: FocusEntity?
required init(frame frameRect: CGRect) {
super.init(frame: frameRect)
self.setupConfig()

switch self.focusStyle {
case .color:
self.focusEntity = FocusEntity(on: self, focus: .plane)
case .material:
do {
let onColor: MaterialColorParameter = try .texture(.load(named: "Add"))
let offColor: MaterialColorParameter = try .texture(.load(named: "Open"))
self.focusEntity = FocusEntity(
on: self,
style: .colored(
onColor: onColor, offColor: offColor,
nonTrackingColor: offColor
)
)
} catch {
self.focusEntity = FocusEntity(on: self, focus: .classic)
print("Unable to load plane textures")
print(error.localizedDescription)
}
default:
self.focusEntity = FocusEntity(on: self, focus: .classic)
switch self.focusStyle {
case .color:
self.focusEntity = FocusEntity(on: self, focus: .plane)
case .material:
do {
let onColor: MaterialColorParameter = try .texture(.load(named: "Add"))
let offColor: MaterialColorParameter = try .texture(.load(named: "Open"))
self.focusEntity = FocusEntity(
on: self,
style: .colored(
onColor: onColor, offColor: offColor,
nonTrackingColor: offColor
)
)
} catch {
self.focusEntity = FocusEntity(on: self, focus: .classic)
print("Unable to load plane textures")
print(error.localizedDescription)
}
default:
self.focusEntity = FocusEntity(on: self, focus: .classic)
}
}
}

func setupConfig() {
let config = ARWorldTrackingConfiguration()
config.planeDetection = [.horizontal, .vertical]
session.run(config, options: [])
}

@objc required dynamic init?(coder decoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
func setupConfig() {
let config = ARWorldTrackingConfiguration()
config.planeDetection = [.horizontal, .vertical]
session.run(config)
}

extension FocusARView: FocusEntityDelegate {
func toTrackingState() {
print("tracking")
}
func toInitializingState() {
print("initializing")
}
@objc required dynamic init?(coder decoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
20 changes: 10 additions & 10 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
import PackageDescription

let package = Package(
name: "FocusEntity",
platforms: [.iOS(.v13), .macOS(.v10_15)],
products: [
.library(name: "FocusEntity", targets: ["FocusEntity"])
],
dependencies: [],
targets: [
.target(name: "FocusEntity", dependencies: [])
],
swiftLanguageVersions: [.v5]
name: "FocusEntity",
platforms: [.iOS(.v13), .macOS(.v10_15)],
products: [
.library(name: "FocusEntity", targets: ["FocusEntity"])
],
dependencies: [],
targets: [
.target(name: "FocusEntity", dependencies: [])
],
swiftLanguageVersions: [.v5]
)
11 changes: 11 additions & 0 deletions Sources/FocusEntity/Documentation.docc/FocusEntity.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# ``FocusEntity``

Visualise the camera focus in Augmented Reality.

## Overview

FocusEntity lets you see exactly where the centre of the view will sit in the AR space. To add FocusEntity to your scene:

```swift
let focusSquare = FocusEntity(on: <#ARView#>, focus: .classic)
```
10 changes: 8 additions & 2 deletions Sources/FocusEntity/FocusEntity+Alignment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
// Copyright © 2019 Max Cobb. All rights reserved.
//

#if canImport(ARKit) && !targetEnvironment(simulator)
import RealityKit
#if canImport(ARKit)
import ARKit
#endif
import Combine

extension FocusEntity {
Expand All @@ -27,6 +28,7 @@ extension FocusEntity {
self.position = average
}

#if canImport(ARKit)
/// Update the transform of the focus square to be aligned with the camera.
internal func updateTransform(raycastResult: ARRaycastResult) {
self.updatePosition()
Expand Down Expand Up @@ -90,6 +92,7 @@ extension FocusEntity {
orientation = targetAlignment
}
}
#endif

internal func normalize(_ angle: Float, forMinimalRotationTo ref: Float) -> Float {
// Normalize angle in steps of 90 degrees such that the rotation to the other angle is minimal
Expand All @@ -112,6 +115,7 @@ extension FocusEntity {
return (camTransform.translation, -[camDirection.x, camDirection.y, camDirection.z])
}

#if canImport(ARKit)
/// - Parameters:
/// - Returns: ARRaycastResult if an existing plane geometry or an estimated plane are found, otherwise nil.
internal func smartRaycast() -> ARRaycastResult? {
Expand All @@ -135,6 +139,7 @@ extension FocusEntity {
// 2. As a fallback, check for a result on estimated planes.
return results.first(where: { $0.target == .estimatedPlane })
}
#endif

/// Uses interpolation between orientations to create a smooth `easeOut` orientation adjustment animation.
internal func performAlignmentAnimation(to newOrientation: simd_quatf) {
Expand All @@ -153,6 +158,7 @@ extension FocusEntity {
return vectorsDot < 0.999
}

#if canImport(ARKit)
/**
Reduce visual size change with distance by scaling up when close and down when far away.

Expand All @@ -170,5 +176,5 @@ extension FocusEntity {
return 0.25 * distanceFromCamera + 0.825
}
}
#endif
}
#endif
Loading

0 comments on commit ced786a

Please sign in to comment.