RealityKit–使用`.rcproject` vs` .usdz'加载模型之间的区别

发布于 2025-01-26 01:59:05 字数 2347 浏览 5 评论 0原文

我正在构建一个简单的应用程序,该应用在用户脸上添加了帽子。我已经看到了两种不同方法的示例:

  1. 将对象作为场景添加到experience.rcproject
  2. 直接从捆绑包中读取对象的.usdz file> file

方法#1

struct ARViewContainer: UIViewRepresentable {
    
    func makeUIView(context: Context) -> ARView {
        arView = ARView(frame: .zero)
        arView.automaticallyConfigureSession = false
        return arView
    }
    
    func updateUIView(_ uiView: ARView, context: Context) {
        let arConfiguration = ARFaceTrackingConfiguration()
        
        uiView.session.run(arConfiguration,
                           options:[.resetTracking, .removeExistingAnchors])
        
        let arAnchor = try! Experience.loadHat()
        
        uiView.scene.anchors.append(arAnchor)
    }
}

方法#2

struct ARViewContainer: UIViewRepresentable {
    
    func makeUIView(context: Context) -> ARView {
        let arView = ARView(frame: .zero)
        let modelEntity = try! ModelEntity.load(named: "hat.usdz")
        
        modelEntity.position = SIMD3(0, 0, -8)
        modelEntity.orientation = simd_quatf.init(angle: 0, axis: SIMD3(-90, 0, 0))
        modelEntity.scale = SIMD3(0.02, 0.02, 0.02)
        
        arView.session.run(ARFaceTrackingConfiguration())
        
        let anchor = AnchorEntity(.face)
        anchor.position.y += 0.25
        anchor.addChild(modelEntity)
        arView.scene.addAnchor(anchor)
        return arView
    }
    
    func updateUIView(_ uiView: ARView, context: Context) {
        let arConfiguration = ARFaceTrackingConfiguration()
        
        uiView.session.run(arConfiguration,
                           options:[.resetTracking, .removeExistingAnchors])
        
        let fileName = "hat.usdz"
        let modelEntity = try! ModelEntity.loadModel(named: fileName)
        
        modelEntity.position = SIMD3(0, 0, -8)
        modelEntity.orientation = simd_quatf.init(angle: 0, axis: SIMD3(-90, 0, 0))
        modelEntity.scale = SIMD3(0.02, 0.02, 0.02)
        
        let arAnchor = AnchorEntity(.face)
        arAnchor.addChild(modelEntity)
                
        uiView.scene.anchors.append(arAnchor)
    }
}

这些方法之间的主要区别是什么?方法1有效,但问题是方法#2甚至对我不起作用 - 该对象根本不会加载到场景中。谁能解释一下?

谢谢!

I'm building a simple app that adds a hat on top of the user's face. I've seen examples of 2 different approaches:

  1. Adding the object as a scene to Experience.rcproject
  2. Reading the object from the bundle directly as a .usdz file

Approach #1

struct ARViewContainer: UIViewRepresentable {
    
    func makeUIView(context: Context) -> ARView {
        arView = ARView(frame: .zero)
        arView.automaticallyConfigureSession = false
        return arView
    }
    
    func updateUIView(_ uiView: ARView, context: Context) {
        let arConfiguration = ARFaceTrackingConfiguration()
        
        uiView.session.run(arConfiguration,
                           options:[.resetTracking, .removeExistingAnchors])
        
        let arAnchor = try! Experience.loadHat()
        
        uiView.scene.anchors.append(arAnchor)
    }
}

Approach #2

struct ARViewContainer: UIViewRepresentable {
    
    func makeUIView(context: Context) -> ARView {
        let arView = ARView(frame: .zero)
        let modelEntity = try! ModelEntity.load(named: "hat.usdz")
        
        modelEntity.position = SIMD3(0, 0, -8)
        modelEntity.orientation = simd_quatf.init(angle: 0, axis: SIMD3(-90, 0, 0))
        modelEntity.scale = SIMD3(0.02, 0.02, 0.02)
        
        arView.session.run(ARFaceTrackingConfiguration())
        
        let anchor = AnchorEntity(.face)
        anchor.position.y += 0.25
        anchor.addChild(modelEntity)
        arView.scene.addAnchor(anchor)
        return arView
    }
    
    func updateUIView(_ uiView: ARView, context: Context) {
        let arConfiguration = ARFaceTrackingConfiguration()
        
        uiView.session.run(arConfiguration,
                           options:[.resetTracking, .removeExistingAnchors])
        
        let fileName = "hat.usdz"
        let modelEntity = try! ModelEntity.loadModel(named: fileName)
        
        modelEntity.position = SIMD3(0, 0, -8)
        modelEntity.orientation = simd_quatf.init(angle: 0, axis: SIMD3(-90, 0, 0))
        modelEntity.scale = SIMD3(0.02, 0.02, 0.02)
        
        let arAnchor = AnchorEntity(.face)
        arAnchor.addChild(modelEntity)
                
        uiView.scene.anchors.append(arAnchor)
    }
}

What is the main difference between these approaches? Approach #1 works, but the issue is that approach #2 doesn't even work for me - the object simply doesn't load into the scene. Could anyone explain a bit?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

荒芜了季节 2025-02-02 01:59:05

.rcproject.usdz之间的差异很明显:现实作曲家文件已经具有模型的锚点(并且它位于层次结构的顶部)。当您在现实作曲家中原型原型时,您可以在视觉上控制模型的规模。 .usdz模型通常具有巨大的规模,您需要减少100次。

通常,.usdz模型没有地板,而.rcproject默认情况下具有地板,并且该地板充当阴影捕捉器。另外,请注意,.rcproject文件大于.usdz文件。

let scene = try! Experience.loadHat()
arView.scene.anchors.append(scene)

print(scene)

.usdz加载到场景中时,您必须以编程方式创建一个锚点( pythonaly)。使用.reality文件,因为它们被优化以用于更快的加载,也很有意义。

let model = try! ModelEntity.load(named: "hat.usdz")
let anchor = AnchorEntity(.face)
anchor.addChild(model)
arView.scene.anchors.append(anchor)

print(model)

另外,在makeuiview方法中放置面部跟踪配置:

import SwiftUI
import RealityKit
import ARKit 

func makeUIView(context: Context) -> ARView {

    let arView = ARView(frame: .zero)
    let model = try! ModelEntity.load(named: "hat.usdz")
    arView.session.run(ARFaceTrackingConfiguration())
    
    let anchor = AnchorEntity(.face)
    anchor.position.y += 0.25
    anchor.addChild(model)
    arView.scene.addAnchor(anchor)
    return arView
}

另外,检查以下渲染选项是否已禁用。

arView.renderOptions = [.disableFaceMesh, .disablePersonOcclusion]

并检查帽子模型中枢轴点的位置。

The difference between .rcproject and .usdz is quite obvious: the Reality Composer file already has an anchor for the model (and it's at the top of the hierarchy). When you prototype in Reality Composer, you have the ability to visually control the scale of your models. .usdz models very often have a huge scale, which you need to reduce by 100 times.

As a rule, .usdz model doesn't have a floor, while .rcproject has a floor by default and this floor acts as a shadow catcher. Also, note that the .rcproject file is larger than the .usdz file.

let scene = try! Experience.loadHat()
arView.scene.anchors.append(scene)

print(scene)

When loading .usdz into a scene, you have to programmatically create an anchor (either swiftly or pythonically). It also makes sense to use .reality files as they are optimized for faster loading.

let model = try! ModelEntity.load(named: "hat.usdz")
let anchor = AnchorEntity(.face)
anchor.addChild(model)
arView.scene.anchors.append(anchor)

print(model)

Also, put a face tracking config inside makeUIView method:

import SwiftUI
import RealityKit
import ARKit 

func makeUIView(context: Context) -> ARView {

    let arView = ARView(frame: .zero)
    let model = try! ModelEntity.load(named: "hat.usdz")
    arView.session.run(ARFaceTrackingConfiguration())
    
    let anchor = AnchorEntity(.face)
    anchor.position.y += 0.25
    anchor.addChild(model)
    arView.scene.addAnchor(anchor)
    return arView
}

Also, check if the following render options are disabled.

arView.renderOptions = [.disableFaceMesh, .disablePersonOcclusion]

And check a position of pivot point in hat model.

临风闻羌笛 2025-02-02 01:59:05

对于2号方法,请尝试删除模型性的位置。您提供的位置为0,-4.9和11.8。这些位置为米。因此,尝试将其删除,看看是否出现。

For approach number 2, try removing the the position for the modelEntity. You provided position as 0, -4.9 and 11.8. Those positions are in meters. So try to remove it and see if appears.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文