_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
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