【问题标题】:Problem loading .usdz into a custom Entity class将 .usdz 加载到自定义实体类时出现问题
【发布时间】:2020-09-04 04:04:22
【问题描述】:

有没有办法将usdz模型加载到自定义实体类中?

我尝试将返回的 ModelEntity 强制转换为我的自定义类,但没有成功。

let entity: CustomEntity = try! CustomEntity.load(named: name) as! CustomEntity

【问题讨论】:

标签: swift augmented-reality arkit realitykit


【解决方案1】:

UIKit 版本

import UIKit
import RealityKit

class CustomClass: Entity, HasModel {
    
    let modelName: String? = "gramophone"
    let myAnchor = AnchorEntity()
    
    func loader() -> AnchorEntity {
        
        if let name = self.modelName {
            let modelEntity = try! CustomClass.loadModel(named: name)
            myAnchor.addChild(modelEntity)
        }
        return myAnchor
    }
}

class ViewController: UIViewController {
    
    @IBOutlet var arView: ARView!
    let modelName: String? = "gramophone"
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let usdz = CustomClass().loader()
        arView.scene.anchors.append(usdz)
    }
}

SwiftUI 版本:

import SwiftUI
import RealityKit

class CustomClass: Entity, HasModel {
    
    func printer() {
        print("I'm inside CustomClass...")
    }
}

struct ARViewContainer: UIViewRepresentable {

    let modelName: String? = "gramophone"
    
    func makeUIView(context: Context) -> ARView {
        
        let arView = ARView(frame: .zero)
        typealias CustomEntity = ModelEntity
        var modelEntity = CustomEntity()
       
        if let name = self.modelName {
            
            modelEntity = try! CustomClass.loadModel(named: name)    
            let anchor = AnchorEntity()
            anchor.addChild(modelEntity)
            arView.scene.anchors.append(anchor)
        }
        return arView
    }
    func updateUIView(_ uiView: ARView, context: Context) {
        CustomClass().printer()
    }
}

【讨论】:

  • 感谢您的回答。我想将转换信息(通过安装的手势)附加到我的实体以进行协作会话。我想这种模式不会以那种方式起作用。
  • 请将其作为另一个问题发布。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-09
  • 1970-01-01
  • 2012-03-24
  • 1970-01-01
  • 2017-09-02
相关资源
最近更新 更多