FurnitureApp / Furniture App / ARView / FurnitureARView.swift
FurnitureARView.swift
Raw
//
//  FurnitureARView.swift
//  Furniture App
//
//  Created by Milos Ilic on 11.3.23..
//

import ARKit
import SwiftUI
import RealityKit

class FurnitureARView: ARView {
    
    required init(frame frameRect: CGRect) {
        super.init(frame: frameRect)
    }
    
    required init?(coder decoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    convenience init(item: FurnitureItem) {
        self.init(frame: UIScreen.main.bounds)
        
        let anchor = AnchorEntity(.plane(.horizontal, classification: .floor, minimumBounds: .zero))
        guard let entity = try? Entity.load(named: item.entityName) else {
            fatalError("Failed to load entity named \(item.entityName)!")
        }
        
        let parentEntity = ModelEntity()
        parentEntity.addChild(entity)
        
        anchor.addChild(parentEntity)
        scene.addAnchor(anchor)
        
        let bounds = entity.visualBounds(relativeTo: parentEntity)
        parentEntity.collision = CollisionComponent(shapes: [
            ShapeResource.generateBox(size: bounds.extents).offsetBy(translation: bounds.center)
        ])
        
        installGestures([.rotation, .scale, .translation], for: parentEntity)
        
    }
}