maxxfrazer / FocusEntity

Bringing the scanning box from SceneKit to RealityKit

Home Page:https://maxxfrazer.github.io/FocusEntity/documentation/focusentity/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

i need help!

LA35NE opened this issue · comments

_Hi Max,

thank you for your unbelievable nice codes! I'm new in this topic, actually studying architecture, I watched this tutorial https://www.youtube.com/watch?v=9R_G0EI-UoI and tried to use your code as it is shown in the video, but I don't get it work.

here is my code: please help me out. Thank you so much

Bildschirmfoto 2020-10-25 um 21 45 29

It always says "Cannot find 'FESquare' in scope, Cannot find'FEDelegate' in scope

//
// ContentView.swift
// Model Picker
//
// Created by Niko Endres on 12.10.20.
//

import SwiftUI
import RealityKit
import ARKit
import FocusEntity

struct ContentView: View {
@State private var isPlacementEnabled = false
@State private var selectedModel: Model?
@State private var modelConfirmedForPlacement: Model?

private var models: [Model] = {
    // dynamically get file names
    let filemanager = FileManager.default
    
    guard let path = Bundle.main.resourcePath, let files = try?
            filemanager.contentsOfDirectory(atPath: path) else {
        return []
    }
    
    var availableModels: [Model] = []
    for filename in files where filename.hasSuffix("usdz") {
        let modelName = filename.replacingOccurrences(of: ".usdz", with: "")
        let model = Model(modelName: modelName)
        
        availableModels.append(model)
    }
    
    return availableModels
}()

var body: some View {
    ZStack(alignment: .bottom) {
        
        ARViewContainer(modelConfirmedForPlacement: self.$modelConfirmedForPlacement)
    
        if self.isPlacementEnabled {
            PlacementButtonsView(isPlacementEnabled: self.$isPlacementEnabled, selectedModel: self.$selectedModel, modelConformedForPlacement: self.$modelConfirmedForPlacement)
        } else {
            ModelPickerView(isPlacementEnabled: self.$isPlacementEnabled, selectedModel: self.$selectedModel, models: self.models)
    
        }
    }

}
}

struct ARViewContainer: UIViewRepresentable {
@binding var modelConfirmedForPlacement: Model?

func makeUIView(context: Context) -> ARView {
    
    let arView = CustomARView(frame: .zero)
    
    return arView
    
}

func updateUIView(_ uiView: ARView, context: Context) {
    if let model = self.modelConfirmedForPlacement{
        
        if let modelEntity = model.modelEntity {
            print("DEBUG: adding model to scene - \(model.modelName)")
            let anchorEntity = AnchorEntity(plane:.any)
            anchorEntity.addChild(modelEntity.clone(recursive: true))
            
            uiView.scene.addAnchor(anchorEntity)
        } else {
            print("DEBUG: Unable to load modelEntity for \(model.modelName)")
        }
        
        
        
        
        DispatchQueue.main.async {
            self.modelConfirmedForPlacement = nil}
    }
    
    
}

}

class CustomARView: ARView {
    let focusSquare = FESquare()

    required init(frame frameRect: CGRect) {
    super.init(frame: frameRect)
    
    focusSquare.viewDelegate = self
    focusSquare.delegate = self
    focusSquare.setAutoUpdate(to: true)
        
        self.setupARView()
    
    }
    
    @objc required dynamic init?(coder decoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func setupARView(){
        let config = ARWorldTrackingConfiguration()
        config.planeDetection = [.horizontal, .vertical]
        config.environmentTexturing = .automatic
        
   //     if ARWorldTrackingConfiguration.supportsSceneReconstruction(.mesh) {
  //          config.sceneReconstruction = .mesh
      //  }
        
        self.session.run(config)
        
    }

}

extension CustomARView: FEDelegate {
func toTrackingState () {
print("tracking")
}

func toInitializingState() {
    print("initializing")
}
}

struct ModelPickerView: View{
@binding var isPlacementEnabled: Bool
@binding var selectedModel: Model?

var models: [Model]
var body: some View{
    ScrollView(.horizontal, showsIndicators: false) {
        HStack(spacing: 30){
            ForEach(0..<self.models.count)  {
                index in
                Button(action: {
                    print("DEBUG: selected model with name: \(self.models[index].modelName)")
                    
                    self.selectedModel = self.models[index]
                    
                    self.isPlacementEnabled = true
                }) {
                    Image(uiImage:
                           
                                        self.models[index].image)
                        .resizable()
                        .frame(height: 80)
                        .aspectRatio(1/1, contentMode: .fit)
                        .background(Color.white)
                        .cornerRadius(4)
                }.buttonStyle(PlainButtonStyle())
        }
        }
    }.padding(20)
    .background(Color.black.opacity(0.5))
}

}
struct PlacementButtonsView: View {
@binding var isPlacementEnabled: Bool
@binding var selectedModel: Model?
@binding var modelConformedForPlacement: Model?

var body: some View {
    HStack {
        // Cancel Button
        Button(action: { print("DEBUG: model placement canceled")
            
            self.resetPlacementParameters()
        }) {
            Image(systemName: "xmark")
                .frame(width: 60, height: 60)
                .font(.title)
                .background(Color.white.opacity(0.75))
                .cornerRadius(30)
                .padding(20)
        }
        // Confirm Button
        Button(action: { print("DEBUG: model placemend confirmed")
            self.modelConformedForPlacement = self.selectedModel
            
            self.resetPlacementParameters()
        }) {
            Image(systemName: "checkmark")
                .frame(width: 60, height: 60)
                .font(.title)
                .background(Color.white.opacity(0.75))
                .cornerRadius(30)
                .padding(20)
        }
    }
    
        
        }
func resetPlacementParameters() {
    self.isPlacementEnabled = false
    self.selectedModel = nil
    }

}

#if DEBUG
struct ContentView_Previews : PreviewProvider {
static var previews: some View {
ContentView()
}
}

#endif

Hi @LA35NE, as mentioned in Ryan's video, he uses package version 1.1.1; however the latest version is v2.0.1.

If you want to use the same version as Ryan does, please tell Xcode to use version 1.1.1, otherwise review the provided documentation to use the latest updates to the package.

hi Max,
thank you for your quick response. I installed the 1.1.1 Version like Ryan did in the video. In Ryans video after installing he changing some other things as well. would this changes been needed if I install the latest Version of FocusEntity?

More informations about the system: Xcode Version 12.0.1, using an iPhone X with ios14

thank you

I haven't tested this with iOS 14 since the GM, but everything should work very similarly, I think just initialising the object was the main change.
Otherwise there's a full example in this repo if you clone it, showing how to use the latest version of FocusEntity.

holy shit, it works, I don't know why but that's ok haha
thank you so much.

by the way I'm really impressed by the work you are doing!

stay healthy!
n

Cheers! Glad it's working for you now!!