Skip to content

ARViewContainer #5

@fahd9567

Description

@fahd9567

in this page i have7 issues
1- Cannot convert value of type 'SessionSettings.Type' to expected argument type 'SessionSettings'
2- Value of type 'ARViewContainer' has no member 'placementSettings'
3- Value of type 'ARViewContainer' has no member 'placementSettings'
4-Type of expression is ambiguous without more context
5- Type of expression is ambiguous without more context
6 and 7 - 'nil' requires a contextual type

`import SwiftUI
import RealityKit

struct ContentView: View {
@EnvironmentObject var placmentSettings: PlacementSettings
@State private var isControlsVisible: Bool = true
@State private var showBrowse: Bool = false
@State private var showSettings: Bool = false

var body: some View {
    ZStack(alignment: .bottom) {
        
        ARViewContainer()
        
        if self.placmentSettings.selectedModel == nil {
            ControlView(isControlsVisible: $isControlsVisible, showBrowse: $showBrowse, showSettings: $showSettings)
        } else {
            PlacementView()
        }
    }
    .edgesIgnoringSafeArea(/*@START_MENU_TOKEN@*/.all/*@END_MENU_TOKEN@*/)
}

}

struct ARViewContainer: UIViewRepresentable {
@EnvironmentObject var olacementSettings: PlacementSettings
@EnvironmentObject var sessionSettings: SessionSettings

func makeUIView(context: Context) -> CustomARView {
    
    let arView = CustomARView(frame: .zero, sessionSettings: SessionSettings)
    
    // Subscribe to SceneEvents,Update
    self.placementSettings.sceneObserver = arView.scene.subscribe(to: SceneEvents.Update.self, { ( event) in
        
        self.updateScene(for: arView)
        
    })
    
    return arView
 }

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

private func updateScene(for arView: CustomARView) {
    
    // only display focusEntity when the user has selected a model for placement
    arView.focusEntity?.isEnabled = self.placementSettings.selectedModel != nil
    
    // Add model to scene if confirmed for placement
    if let confirmedModel = self.placementSettings.confirmedModel, let modelEntity
        = confirmedModel.modelEntity {
        
        self.place(modelEntity, in: arView)
        
        self.placementSettings.confirmedModel = nil
        
    }
}

private func place(_ modelEntity: ModelEntity, in arView: ARView){
    
    // 1. Clone model.....
    let clonedEntity = modelEntity.clone(recursive: true)
    
    // 2. enable trans....
    clonedEntity.generateCollisionShapes(recursive: true)
    arView.installGestures([.translation, .rotation], for: clonedEntity)
    
    // 3. create an .....
    let anchorEntity = AnchorEntity(plane: .any)
    anchorEntity.addChild(clonedEntity)
    
    // 4. add the a.....
    arView.scene.addAnchor(anchorEntity)
    
    print("Added modelEntity to scene.")
}

}

struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
.environmentObject(PlacementSettings())
.environmentObject(SessionSettings())
}
}`

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions