0

I'm trying to code with my iPad and the Swift Playgrounds 4.0. I tried to do Image Tracking with SwiftUI example 1 or example 2. It's possible to create a folder in the iPad App, but you can't put an Image in here... So the following code doesn't work:

func makeUIView(context: Context) -> ARView {

    guard let referenceImages = ARReferenceImage.referenceImages(
                                                  inGroupNamed: "AR Resources",
                                                        bundle: nil) 
    else {
        fatalError("Missing expected asset catalog resources.")
    }
}

Here an Image from the Swift Playgrounds App

Is it possible to use reference images from the root / main like you do with the .usdz models?

if let usdzModel = try? Entity.load(named: "drummer") {
    anchor.addChild(usdzModel)
}

Here is the complete Code:

import ARKit
import SwiftUI
import RealityKit

struct RealityKitView: UIViewRepresentable {
    func makeUIView(context: Context) -> ARView {
        let view = ARView()
        
        // Start AR session
        let session = view.session
        let config = ARWorldTrackingConfiguration()
        config.planeDetection = [.horizontal]
        session.run(config)
        
        // Add coaching overlay
        let coachingOverlay = ARCoachingOverlayView()
        coachingOverlay.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        coachingOverlay.session = session
        coachingOverlay.goal = .horizontalPlane
        view.addSubview(coachingOverlay)
        
        // Set debug options
#if DEBUG
        view.debugOptions = [.showFeaturePoints, .showAnchorOrigins, .showAnchorGeometry]
#endif
        
        //AnchorEntity Bild
        
        let anchor = AnchorEntity(.image(group: "AR Resources", name: "Test"))
        // Create an image anchor by specifying the group name and image name of the AR resource
        let box = ModelEntity(mesh: .generateBox(size: simd_make_float3(0.1, 0.03, 0.05)))
        anchor.addChild(box)
        
        view.scene.anchors.append(anchor)
        
        //End AnchorEntity
        
        return view
    }
    
    func updateUIView(_ view: ARView, context: Context) {
    }
    
}

Andx
  • 51
  • 4
  • The [documentation for `ARReferenceImage` defines an initializer](https://developer.apple.com/documentation/arkit/arreferenceimage/2942252-init) you can use when you want to use an image that is not in the assets folder. – EmilioPelaez Jan 04 '22 at 23:10
  • @EmilioPelaez thank you for the fast response! I'm new to Swift / AR development, I tried several things with `init(_ image: CGImage, orientation: CGImagePropertyOrientation, physicalWidth: CGFloat)` but nothing works... (I think it was wrong?) – Andx Jan 05 '22 at 12:08

1 Answers1

0

It's a code snippet from my Medium story.

This is done differently from Xcode, but no more complicated than in Xcode. Use + button in the upper right corner of your Swift Playgrounds app to place your reference images there.

Then use this code.

import ARKit

let sceneView = ARSCNView(frame: .zero)
var trackingImages = Set<ARReferenceImage>()

fileprivate func feedSession() {
    
    let imageFromWeb = #imageLiteral(resourceName: "Photo.png")
    
    let image = ARReferenceImage(imageFromWeb.cgImage!, orientation: .up, 
                                                      physicalWidth: 0.5)
    
    self.trackingImages.insert(image)
    
    let config = ARImageTrackingConfiguration()
    config.trackingImages = trackingImages
    self.sceneView.session.run(config)
}
Andy Jazz
  • 49,178
  • 17
  • 136
  • 220
  • 1
    I edited the complete Code of my Swift-File. I don´t get it working.. But thanks for your help :) – Andx Jan 06 '22 at 13:19
  • 1
    Great Medium story.. but I dont get it to work for me :( i think the problem is that i work with SwiftUI and not UIKit and ViewController / Playgrounds. – Andx Jan 10 '22 at 14:40
  • Have you implemented `coordinator`? – Andy Jazz Jan 10 '22 at 14:44
  • I dont think so, but I´m new in Swift / AR Development.. i think I need more experience. In the first Post is the complete Code. If I try to implemented the [Coordinator](https://stackoverflow.com/questions/60582392/swiftui-passing-data-from-swiftuiview-to-scenekit/60590065#60590065) i get errors.. – Andx Jan 13 '22 at 08:33