【问题标题】:RealityKit – Getting runtime warning when placing a model in ARViewRealityKit - 在 ARView 中放置模型时收到运行时警告
【发布时间】:2021-04-29 23:01:48
【问题描述】:

当我在 ARView 中点击以加载模型时,我在运行时收到以下警告:警告(辅助线程):在 sdf/path.cpp 第 859 行的 AppendProperty 中 -- 只能附加一个属性'preliminary:anchoring:type ' 到一个原始路径 (/) 警告(辅助线程):在 sdf/path.cpp 第 859 行的 AppendProperty 中 -- 只能将属性“触发器”附加到主路径 (/)

有人知道为什么我会收到此警告吗?所有的逻辑都在我的 ViewController 中:

import UIKit
import ARKit
import RealityKit
import SwiftUI
import Combine

class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate  {

private var modelArray: [String] = []
var selectedModel = ""

@IBOutlet weak var arView: ARView!

@IBOutlet weak var modelCollectionView: UICollectionView!

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    
    loadModelName()
    arView.session.delegate = self
    setupARView()
    //modelArray = getModelNames()
    self.modelCollectionView.dataSource = self
    self.modelCollectionView.delegate = self
    arView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleTap(recognizer:))))
    
}
 // Dynamically load available files from directory
func loadModelName() {
    
    let fm = FileManager.default
    let path = Bundle.main.resourcePath!

    do {
        let items = try fm.contentsOfDirectory(atPath: path)

        for item in items where item.hasSuffix("usdz"){
            let modelName = item.replacingOccurrences(of: ".usdz", with: "")
            print("Found \(item)")
            modelArray.append(modelName)
        }
    } catch {
        // failed to read directory – bad permissions, perhaps?
    }
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return modelArray.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "item", for: indexPath) as! itemCell
        cell.itemImage.image = UIImage(named: self.modelArray[indexPath.row])
        return cell
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    let cell = collectionView.cellForItem(at: indexPath)
    cell?.backgroundColor = UIColor.lightGray
    selectedModel = self.modelArray[indexPath.row] + ".usdz"
}
func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
    let cell = collectionView.cellForItem(at: indexPath)
    cell?.backgroundColor = UIColor.white
}



 func setupARView() {
        arView.automaticallyConfigureSession = false
        let configuration  = ARWorldTrackingConfiguration()
        configuration.planeDetection = [.horizontal, .vertical]
        configuration.environmentTexturing = .automatic
        if ARWorldTrackingConfiguration.supportsSceneReconstruction(.mesh) {
            print("scene reconstruction supported")
            configuration.sceneReconstruction = .mesh
            configuration.sceneReconstruction = .meshWithClassification
            arView.debugOptions.insert(.showSceneUnderstanding)
        }
        arView.session.run(configuration)
        print("plane detection")
    }



 @objc
    func handleTap(recognizer: UITapGestureRecognizer) {
        //gets the location in the arView
        let location = recognizer.location(in: arView)
        //grab the results of the tap -location: the location of the tap -allowing: tye type of surface to calculate the location -alignment: the type of surface
        let reults = arView.raycast(from: location, allowing: .estimatedPlane, alignment: .any)
        
    //check to see if the raycast returned a result, did it actually hit a horizontal surface
    if let firstResult = reults.first {
        //returns an array of all the things it finds so grab the first
        //in order to add objects into a scene, we have to add objects to anchors, firstResult.worldTransform - add anchor at the orientation and position
        print("tap gesture recognized")
        print("DEBUG: the model is \(selectedModel)")
        let anchor = ARAnchor(name: selectedModel, 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)

    //add collision to have physiscs for manipulations
    entity.generateCollisionShapes(recursive: true)
    arView.installGestures([.rotation, .translation], for: entity)

    //create an anchor entity
    let anchorEntity = AnchorEntity(anchor: anchor)
    // add the entity to anchor
    anchorEntity.addChild(entity.clone(recursive: true))
    //add the anchor with the entity to the scene
    arView.scene.addAnchor(anchorEntity)
}
}

extension ViewController: ARSessionDelegate {
    func session(_ session: ARSession, didAdd anchors: [ARAnchor]) {
        for anchor in anchors {
            if let anchorName = anchor.name, anchorName == selectedModel {
                placeObject(named: anchorName, for: anchor)
            }
        }
    }
}

【问题讨论】:

  • 您添加的模型是否是有效的usdz文件?
  • 是的,它们是 Apple 的 usdz 型号
  • 嗨@Myoung,你试过我的方法吗(我的意思是初步锚定)?
  • 嗨@AndyJazz 不,我还没有尝试过,因为我正在从事其他一些项目,但有机会我会尝试一下,谢谢你的帮助!

标签: swift augmented-reality arkit realitykit


【解决方案1】:

简而言之,答案听起来是这样的:如果您声明了 USDZ 模型的初步锚定,那么 Xcode 将不会打印此类警告

现在让我们更详细地讨论这个问题。正如您之前所说,在调试模式下运行您的 AR 应用时,Xcode 的控制台中有两个警告:

// 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 (/)

您可以在 Pixar/Apple path.cpp 文件中轻松找到生成这些警告的 C++ 语句(只需在 Spotlight 搜索字符串中键入文件名)。这里是:

SdfPath::AppendProperty(TfToken const &propName) const {
    if (ARCH_UNLIKELY(_propPart)) {
        TF_WARN("Can only append a property '%s' to a prim path (%s)",
                propName.GetText(), GetText());
        return EmptyPath();
    }
}

那么,有什么意义呢?让我引用 Apple 文档:

Preliminary_AnchoringAPI 将锚点的中心指定为 prim 的原点,将锚点的顶部指定为其法向量点。运行时需要资产来为此属性提供值。


您可以在 USDZ Schemas 故事中了解更多关于 preliminary:anchoring:type 的信息。


换句话说,如果您在场景中使用带有初步生成锚点的文件,无论它们是在 Reality Composer 中分配的锚点,还是为.usdz 场景通过 Python 定义的锚点,您都不会收到此类消息。

Reality Composer 也使用这些模式在其 USDZ 导出中描述了 AR 功能...

【讨论】:

    猜你喜欢
    • 2020-05-11
    • 1970-01-01
    • 2017-02-01
    • 1970-01-01
    • 2016-10-25
    • 2021-08-28
    • 1970-01-01
    • 2020-06-15
    • 1970-01-01
    相关资源
    最近更新 更多