Skip to content

Commit

Permalink
Update to latest changes in XRViewer develop branch (d631bfb)
Browse files Browse the repository at this point in the history
tzachari committed Sep 12, 2019

Verified

This commit was signed with the committer’s verified signature.
torkelrogstad Torkel Rogstad
1 parent 507ea40 commit 072ec55
Showing 24 changed files with 172 additions and 165 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -67,4 +67,4 @@ This plugin will be updated as the API stabilizes.

## Credits

This plugin is based on the source for Mozilla's WebXR Viewer ([webxr-ios@c91decb](https://github.com/mozilla-mobile/webxr-ios/tree/c91decbecd11b0691f974fb5edc80a06b62cec11))
This plugin is based on the source for Mozilla's WebXR Viewer ([webxr-ios@d631bfb](https://github.com/mozilla-mobile/webxr-ios/tree/d631bfb0a65573c26efe34f53e012427b75da847))
7 changes: 6 additions & 1 deletion XRViewer/ARKController/ARKController+ARSessionDelegate.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import ARKit

extension ARKController: ARSessionDelegate {

// Tony: Per SO, a bug that's been around for 3+ years necessitates these @objc calls
@@ -56,7 +58,8 @@ extension ARKController: ARSessionDelegate {
}

if usingMetal,
let controller = controller as? ARKMetalController
let controller = controller as? ARKMetalController,
addedAnchor is ARPlaneAnchor
{
let node = Node()
controller.planes[addedAnchor.identifier] = node
@@ -110,6 +113,7 @@ extension ARKController: ARSessionDelegate {

if usingMetal,
let controller = controller as? ARKMetalController,
updatedAnchor is ARPlaneAnchor,
let node = controller.planes[updatedAnchor.identifier]
{
node.transform = Transform(from: updatedAnchor.transform)
@@ -126,6 +130,7 @@ extension ARKController: ARSessionDelegate {

if usingMetal,
let controller = controller as? ARKMetalController,
removedAnchor is ARPlaneAnchor,
let node = controller.planes[removedAnchor.identifier]
{
node.removeFromParentNode()
2 changes: 2 additions & 0 deletions XRViewer/ARKController/ARKController+ARSessionObserver.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import ARKit

extension ARKController: ARSessionObserver {

@objc(session:cameraDidChangeTrackingState:)
28 changes: 15 additions & 13 deletions XRViewer/ARKController/ARKController+Anchors.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
@objc extension ARKController {
import ARKit

extension ARKController {

// MARK: - Anchor Dictionaries

@@ -272,11 +274,11 @@
for anchorIDToDelete in anchorIDsToDelete as? [String] ?? [] {
var anchorToDelete: ARAnchor? = getAnchorFromUserAnchorID(anchorIDToDelete)
if let anchorToDelete = anchorToDelete {
session?.remove(anchor: anchorToDelete)
session.remove(anchor: anchorToDelete)
} else {
anchorToDelete = getAnchorFromARKitAnchorID(anchorIDToDelete)
if let anchorToDelete = anchorToDelete {
session?.remove(anchor: anchorToDelete)
session.remove(anchor: anchorToDelete)
}
}
}
@@ -287,7 +289,7 @@
key "distantAnchorsDistanceKey"
*/
func removeDistantAnchors() {
guard let currentFrame = session?.currentFrame else { return }
guard let currentFrame = session.currentFrame else { return }
let cameraTransform = currentFrame.camera.transform
let distanceThreshold: Float = UserDefaults.standard.float(forKey: Constant.distantAnchorsDistanceKey())

@@ -316,13 +318,13 @@
(center2 + extent2) < -distanceThreshold {

print("\n\n*********\n\nRemoving distant plane \(anchor.identifier.uuidString)\n\n*********")
session?.remove(anchor: anchor)
session.remove(anchor: anchor)
}
} else {
let distance = simd_distance(anchor.transform.columns.3, cameraTransform.columns.3)
if distance >= distanceThreshold {
print("\n\n*********\n\nRemoving distant anchor \(anchor.identifier.uuidString)\n\n*********")
session?.remove(anchor: anchor)
session.remove(anchor: anchor)
}
}
}
@@ -331,21 +333,21 @@
func removeAllAnchors() {
clearImageDetectionDictionaries()

guard let currentFrame = session?.currentFrame else { return }
guard let currentFrame = session.currentFrame else { return }

for anchor in currentFrame.anchors {
session?.remove(anchor: anchor)
session.remove(anchor: anchor)
}
}

func removeAllAnchorsExceptPlanes() {
clearImageDetectionDictionaries()

guard let currentFrame = session?.currentFrame else { return }
guard let currentFrame = session.currentFrame else { return }

for anchor in currentFrame.anchors {
if !(anchor is ARPlaneAnchor) {
session?.remove(anchor: anchor)
session.remove(anchor: anchor)
}
}
}
@@ -354,7 +356,7 @@

func getAnchorFromARKitAnchorID(_ arkitAnchorID: String) -> ARAnchor? {
var anchor: ARAnchor? = nil
guard let currentFrame: ARFrame = session?.currentFrame else { return nil }
guard let currentFrame: ARFrame = session.currentFrame else { return nil }
for currentAnchor in currentFrame.anchors {
if currentAnchor.identifier.uuidString == arkitAnchorID {
anchor = currentAnchor
@@ -370,7 +372,7 @@
guard let userID = userID as? String else { return }
guard let arkitID = arkitID as? String else { return }
if userID == userAnchorID {
guard let currentFrame = self.session?.currentFrame else { return }
guard let currentFrame = self.session.currentFrame else { return }
let anchors = currentFrame.anchors
for currentAnchor in anchors {
if currentAnchor.identifier.uuidString == arkitID {
@@ -463,7 +465,7 @@
var matrix = matrix_float4x4()
matrix = transform.matrix()
let anchor = ARAnchor(name: userGeneratedAnchorID ?? "", transform: matrix)
session?.add(anchor: anchor)
session.add(anchor: anchor)
arkitGeneratedAnchorIDUserAnchorIDMap[anchor.identifier.uuidString] = userGeneratedAnchorID ?? ""

return true
20 changes: 10 additions & 10 deletions XRViewer/ARKController/ARKController+AppState.swift
Original file line number Diff line number Diff line change
@@ -12,7 +12,7 @@ extension ARKController {
*/
func startSession(with state: AppState) {
updateARConfiguration(with: state)
session?.run(configuration, options: [.resetTracking, .removeExistingAnchors])
session.run(configuration, options: [.resetTracking, .removeExistingAnchors])
arSessionState = .arkSessionRunning

// if we are removing anchors, clear the user map
@@ -43,21 +43,21 @@ extension ARKController {
*/
func runSession(with state: AppState) {
updateARConfiguration(with: state)
session?.run(configuration, options: [])
session.run(configuration, options: [])
arSessionState = .arkSessionRunning
}

func runSessionRemovingAnchors(with state: AppState) {
updateARConfiguration(with: state)
session?.run(configuration, options: .removeExistingAnchors)
session.run(configuration, options: .removeExistingAnchors)
// If we are removing anchors, clear the user map
arkitGeneratedAnchorIDUserAnchorIDMap = NSMutableDictionary()
arSessionState = .arkSessionRunning
}

func runSessionResettingTrackingAndRemovingAnchors(with state: AppState) {
updateARConfiguration(with: state)
session?.run(configuration, options: [.resetTracking, .removeExistingAnchors])
session.run(configuration, options: [.resetTracking, .removeExistingAnchors])
// If we are removing anchors, clear the user map
arkitGeneratedAnchorIDUserAnchorIDMap = NSMutableDictionary()
arSessionState = .arkSessionRunning
@@ -90,7 +90,7 @@ extension ARKController {
} else {
appDelegate().logger.error("resume session on a face-tracking camera")
}
session?.run(configuration, options: [])
session.run(configuration, options: [])
arSessionState = .arkSessionRunning
setupDeviceCamera()
setShowMode(state.showMode)
@@ -123,15 +123,15 @@ extension ARKController {
} else {
appDelegate().logger.error("resume session on a face-tracking camera")
}
session?.run(configuration, options: [])
session.run(configuration, options: [])
arSessionState = .arkSessionRunning
}

/**
Pauses the AR session and sets the arSessionState to paused
*/
func pauseSession() {
session?.pause()
session.pause()
arSessionState = .arkSessionPaused
}

@@ -187,19 +187,19 @@ extension ARKController {
// MARK: - Helpers

func currentFrameTimeInMilliseconds() -> TimeInterval {
return TimeInterval((session?.currentFrame?.timestamp ?? 0.0) * 1000)
return TimeInterval((session.currentFrame?.timestamp ?? 0.0) * 1000)
}

func trackingStateNormal() -> Bool {
guard let ts = session?.currentFrame?.camera.trackingState.presentationString else {
guard let ts = session.currentFrame?.camera.trackingState.presentationString else {
print("Unable to check if camera trackingState presentationString is normal")
return false
}
return ts == ARCamera.TrackingState.normal.presentationString
}

func trackingStateRelocalizing() -> Bool {
guard let ts = session?.currentFrame?.camera.trackingState.presentationString else {
guard let ts = session.currentFrame?.camera.trackingState.presentationString else {
print("Unable to check if camera trackingState presentationString is relocalizing")
return false
}
8 changes: 5 additions & 3 deletions XRViewer/ARKController/ARKController+Camera.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
@objc extension ARKController {
import ARKit

extension ARKController {

// MARK: - Camera Device

@@ -47,9 +49,9 @@
in the previous ARWorldTrackingConfiguration session, and run the session.
*/
func switchCameraButtonTapped(_ state: AppState) { // numberOfTrackedImages: Int) {
guard let currentFrame = session?.currentFrame else { return }
guard let currentFrame = session.currentFrame else { return }
for anchor in currentFrame.anchors {
session?.remove(anchor: anchor)
session.remove(anchor: anchor)
}

if !(configuration is ARFaceTrackingConfiguration) {
3 changes: 2 additions & 1 deletion XRViewer/ARKController/ARKController+Frame.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import Accelerate
import ARKit

extension ARKController {

@objc func updateARKData(with frame: ARFrame) {
func updateARKData(with frame: ARFrame) {

synced(self) {

15 changes: 8 additions & 7 deletions XRViewer/ARKController/ARKController+Images.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Accelerate
import ARKit

extension ARKController {

@@ -117,7 +118,7 @@ extension ARKController {
}

detectionImageActivationPromises[referenceImage.name ?? ""] = completion
session?.run(configuration, options: [])
session.run(configuration, options: [])
} else {
if detectionImageActivationPromises[referenceImage.name ?? ""] != nil {
// Trying to reactivate an active image that hasn't been found yet, return an error on the first promise, keep the second
@@ -128,7 +129,7 @@ extension ARKController {
} else {
// Activating an already activated and found image, remove the anchor from the scene
// so it can be detected again
guard let anchors = session?.currentFrame?.anchors else { return }
guard let anchors = session.currentFrame?.anchors else { return }
for anchor in anchors {
if let imageAnchor = anchor as? ARImageAnchor,
imageAnchor.referenceImage.name == imageName
@@ -138,13 +139,13 @@ extension ARKController {
if let currentDetectionImages = currentDetectionImages as? Set<ARReferenceImage> {
worldTrackingConfiguration?.detectionImages = currentDetectionImages
}
session?.run(configuration, options: [])
session.run(configuration, options: [])

// When the anchor is removed and didRemoveAnchor callback gets called, look in this map
// and see if there is a promise for the recently removed image anchor. If so, call
// activateDetectionImage again with the image name of the removed anchor, and the completion set here
detectionImageActivationAfterRemovalPromises[referenceImage.name ?? ""] = completion
session?.remove(anchor: anchor)
session.remove(anchor: anchor)
return
}
}
@@ -190,7 +191,7 @@ extension ARKController {
if let currentDetectionImages = currentDetectionImages as? Set<ARReferenceImage> {
worldTrackingConfiguration?.detectionImages = currentDetectionImages
}
session?.run(configuration, options: [])
session.run(configuration, options: [])
completion(true, nil)
} else {
completion(false, "The image attempting to be deactivated doesn't exist")
@@ -230,7 +231,7 @@ extension ARKController {
if let currentDetectionImages = currentDetectionImages as? Set<ARReferenceImage> {
worldTrackingConfiguration?.detectionImages = currentDetectionImages
}
session?.run(configuration, options: [])
session.run(configuration, options: [])
}
}
referenceImageMap[imageName] = nil
@@ -440,7 +441,7 @@ extension ARKController {
func setNumberOfTrackedImages(_ numberOfTrackedImages: Int) {
if let trackingConfiguration = configuration as? ARWorldTrackingConfiguration {
trackingConfiguration.maximumNumberOfTrackedImages = numberOfTrackedImages
session?.run(trackingConfiguration, options: [])
session.run(trackingConfiguration, options: [])
} else {
print("Error: Cannot set tracked images on an ARFaceTrackingConfiguration session.")
}
15 changes: 8 additions & 7 deletions XRViewer/ARKController/ARKController+WorldMap.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Compression
import ARKit

@objc extension ARKController {
extension ARKController {

// MARK: - Saving

@@ -20,7 +21,7 @@ import Compression
return
}

session?.getCurrentWorldMap(completionHandler: { worldMap, error in
session.getCurrentWorldMap(completionHandler: { worldMap, error in
if let worldMap = worldMap {
appDelegate().logger.error("saving WorldMap to local storage")
self._save(worldMap)
@@ -56,7 +57,7 @@ import Compression
return
}

session?.getCurrentWorldMap(completionHandler: { worldMap, error in
session.getCurrentWorldMap(completionHandler: { worldMap, error in
if worldMap != nil {
appDelegate().logger.error("saving WorldMap as we transition to background")
self.backgroundWorldMap = worldMap
@@ -154,7 +155,7 @@ import Compression
return
}

session?.getCurrentWorldMap(completionHandler: { worldMap, error in
session.getCurrentWorldMap(completionHandler: { worldMap, error in
if let worldMap = worldMap {
if let completion = completion {
var mapData = [AnyHashable: Any]()
@@ -262,14 +263,14 @@ import Compression
worldTrackingConfiguration?.initialWorldMap = map
printWorldMapInfo(map)

session?.run(configuration, options: [.resetTracking, .removeExistingAnchors])
session.run(configuration, options: [.resetTracking, .removeExistingAnchors])

// if we are removing anchors, clear the user map
arkitGeneratedAnchorIDUserAnchorIDMap = NSMutableDictionary.init()
print("Restarted, loading map.")

for anchor in map.anchors {
session?.add(anchor: anchor)
session.add(anchor: anchor)
arkitGeneratedAnchorIDUserAnchorIDMap[anchor.identifier.uuidString] = anchor.name ?? ""
print("WorldMap loaded anchor: \(anchor.name ?? "nameless anchor")")
}
@@ -385,7 +386,7 @@ import Compression
}

func worldMappingAvailable() -> Bool {
guard let ws = session?.currentFrame?.worldMappingStatus else { return false }
guard let ws = session.currentFrame?.worldMappingStatus else { return false }
return ws != .notAvailable
}

Loading

0 comments on commit 072ec55

Please sign in to comment.