Проблемы со ссылками на модели из одного файла в другой - PullRequest
0 голосов
/ 05 мая 2020

Я пытаюсь загрузить конкретную модель c на свой экран AR из другого файла, но это не работает. Как я могу это исправить?

       //MARK: OBJECT PLACEMENT
    @objc
    func handleTap(recognizer: UITapGestureRecognizer)
    {
        let obj = Object(uid: "", name: "", images: [UIImage].init(), price: Double.init(), description: "", detail: "", anchorName: "")
        let location = recognizer.location(in: arView)
        let results = arView.raycast(from: location, allowing: .estimatedPlane, alignment: .horizontal)
        if let firstResult = results.first
        {
            let anchor = ARAnchor(name: obj.anchorName, transform: firstResult.worldTransform)
            arView.session.add(anchor: anchor)
        }
        else
        {
            print("Object placement failed - couldn`t find surface")
        }

    }

    func placeObject(named entityName: String, for anchor: ARAnchor)
    {
        let entity = try! ModelEntity.loadModel(named: entityName)

        entity.generateCollisionShapes(recursive: true)
        arView.installGestures([.rotation, .translation], for: entity)

        let anchorEntity = AnchorEntity(anchor: anchor)
        anchorEntity.addChild(entity)
        arView.scene.addAnchor(anchorEntity)
    }

}

extension ARViewController: ARSessionDelegate
{
    func session(_ session: ARSession, didAdd anchors: [ARAnchor])
    {
        for anchor in anchors
        {
            let obj = Object(uid: "", name: "", images: [UIImage].init(), price: Double.init(), description: "", detail: "", anchorName: "")
            if let anchorName = anchor.name, anchorName == obj.anchorName
            {
                placeObject(named: anchorName, for: anchor)
            }
        }
    }
}

В объектном файле, на который я ссылаюсь, объявлено anchorName.

class Object
{
    var uid: String?
    var name: String?
    var images: [UIImage]?
    var price: Double?
    var description: String?
    var detail: String?
    var anchorName: String

    init(uid: String, name: String, images: [UIImage], price: Double, description: String, detail: String, anchorName: String)
    {
        self.uid = uid
        self.name = name
        self.images = images
        self.price = price
        self.description = description
        self.detail = detail
        self.anchorName = anchorName
    }

    class func fetchObjects() -> [Object]
    {
        var objects = [Object]()

        // 1
        var object1Images = [UIImage]()
        for i in 1...2 {
            object1Images.append(UIImage(named: "s\(i)")!)
        }
        let object1 = Object(uid: "875942-100", name: "SOFA", images: object1Images, price: 180, description: "Here is some sample text", anchorName: "Stool1")
        objects.append(object1)
...