RealityKit:无法从 USDZ 文件加载实体

问题描述 投票:0回答:1

我最近开始在 iOS 中学习 AR。我的第一次尝试是将

usdz
文件中的眼镜模型导入现实编辑器并将它们固定在脸上。效果很好。然后我尝试使用相同的文件以编程方式执行此操作,但没有任何反应。

因为它是第一次工作,当我以编程方式加载文件时,我能够打印模型的描述(见下文),我认为问题不在文件中。

这是代码

import UIKit
import ARKit
import RealityKit

class ViewController: UIViewController {
    @IBOutlet var arView: ARView!
    
    // this one does not show
    var glassesEntity: Entity {
        guard let fileUrl = Bundle.main.url(forResource: "glasses", withExtension: "usdz"), 
              let entity = try? Entity.loadModel(contentsOf: fileUrl) else {
            fatalError("could not load entity")
        }
        
        entity.name = "glasses"
        print(entity)
        return entity
    }
    
    // this one works perfectly fine
    var boxEntity: Entity {
        let box = MeshResource.generateBox(size: 0.3)
        return ModelEntity(mesh: box)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let arConfiguration = ARFaceTrackingConfiguration()
        
        arView.session.run(arConfiguration, options: [])
        
        let faceAnchor = AnchorEntity(AnchoringComponent.Target.face)
        arView.scene.addAnchor(faceAnchor)
        
        faceAnchor.addChild(glassesEntity)
        // If I uncomments this line, I see the box on my face 
        // faceAnchor.addChild(boxEntity)
    }
}

这是终端输出。无论如何都会出现前 3 行,即使我不尝试加载眼镜模型。

2021-04-05 10:52:39.589170+0300 FaceTracking[27470:2201299] Metal GPU Frame Capture Enabled
2021-04-05 10:52:39.589627+0300 FaceTracking[27470:2201299] Metal API Validation Enabled
Json Parse Error line 22: Json Deserialization; unknown member 'EnableGuidedFilterOcclusion' - skipping.
Warning: in AppendProperty at line 859 of sdf/path.cpp -- Can only append a property 'preliminary:anchoring:type' to a prim path (/)
Warning: in AppendProperty at line 859 of sdf/path.cpp -- Can only append a property 'triggers' to a prim path (/)
▿ 'glasses' : ModelEntity
  ⟐ ModelComponent
  ⟐ SynchronizationComponent
  ⟐ Transform

2021-04-05 10:52:40.549771+0300 FaceTracking[27470:2201340] [Graphics] Failed to find reflection for buffer clusterIndexTable
swift load entity realitykit usdz
1个回答
1
投票

有几件事你可以尝试......

首先,像这样加载模型:

guard let entity = try? Entity.load(
  named: "glasses"
) else { fatalError("") }

然后,锚定之后,剩下的改成这样:

val glEnt = self.glassesEntity
faceAnchor.addChild(glEnt)
let relNil = glEnt.visualBounds(relativeTo: nil)
let relAnch = glEnt.visualBounds(relativeTo: faceAnchor)

print("centre: \(relNil.center)\n radius:\(relNil.boundingRadius)")
print("centre: \(relAnch.center)\n radius:\(relAnch.boundingRadius)")

看看这些中心值和半径值是否大致符合您的预期。第一个将在世界空间中,第二个是相对于面部锚点。

© www.soinside.com 2019 - 2024. All rights reserved.