我最近开始在iOS中学习AR。我的第一个尝试是将眼镜模型从usdz
文件导入到reality composer中,并将它们锚定到一张脸上。这样做效果很好。然后,我尝试使用相同的文件以编程方式完成此操作,但什么也没有发生。
由于它第一次工作,并且当我以编程方式加载文件时,我能够打印模型的描述(见下文),我不认为问题出在文件中。
下面是代码
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
发布于 2021-04-07 16:13:45
有几件事你可以试试...
首先,像这样加载模型:
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)")
看看这些中心值和半径值是否与您预期的大致相同。第一个是在世界空间中,第二个是相对于面锚点。
https://stackoverflow.com/questions/66949918
复制相似问题