在 ARView 中放置对象时,ARKit RealityKit 会在运行时发出警告

Myo*_*ung 7 ios swift arkit realitykit

当我点击在 ARView 中加载模型时,我在运行时收到以下警告:警告(辅助线程):在 sdf/path.cpp 的第 859 行的 AppendProperty -- Can only append a property 'preliminary:anchoring:type' to a主要路径 (/) 警告(辅助线程):在 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)
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

ARG*_*Geo 2

关于警告

\n

我简单说一下,答案听起来是这样的:如果你为 USDZ 模型设置了初步锚定,那么 Xcode 将不会打印此类警告。这些警告来自 USD C++ 库。

\n

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

\n
// Warning: in AppendProperty at line 859 of sdf/path.cpp - Can only append a \n// property \'preliminary:anchoring:type\' to a prim path (/)\n\n// Warning: in AppendProperty at line 859 of sdf/path.cpp - Can only append a\n// property \'triggers\' to a prim path (/)\n
Run Code Online (Sandbox Code Playgroud)\n

你可以很容易地在Pixar/Apple中找到C++语句path.cpp(只需在 Spotlight 搜索字符串中键入文件名)。这里是:

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

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

\n
\n

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

\n
\n
\n

preliminary:anchoring:type您可以在USDZ Schemas故事中阅读更多信息。

\n
\n

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

\n
\n

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

\n
\n

关闭警告

\n

如果这些消息确实让您烦恼,请将其关闭。

\n

转到 Xcode 的菜单 Product \xe2\x80\x93 Scheme \xe2\x80\x93 Edit Scheme 并添加环境变量。

\n
OS_ACTIVITY_MODE = disable\n
Run Code Online (Sandbox Code Playgroud)\n

在此输入图像描述

\n