在 RealityKit 中查找特定模型的 findEntity(named:) 方法的替代方法?

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

所以我几天前问了一个问题,Andy Jazz 好心地回答了。所以我一直在研究答案,虽然我能够实现一个解决方案,但它是不完整的,因为他使用的方法是

findEntity(named:)
使用 RealityKit。虽然我在自己的答案中使用了 Reality Composer。

问题 #1:当您有多个对象时,

findEntity(named:)
方法在 Reality Composer 上不起作用。 所以我意识到使用 findEntity(named:) 方法不起作用。因为场景内的所有物体都命名为
"simpBld_root"
,所以触发不了我想触发的

问题 #2:当使用 parenting notation 时,我正在试错,因为我并不真正理解什么是什么。 我怎样才能调用每个对象?这也是我自己无法正确理解从 Reality Composer 调用什么的结果,因为我不认为我真的理解 parenting notation 如何应用于 reality composer。下图是控制台打印出来的我正在工作的场景

下面是我做的一个例子,我用 children[0] 代替 Andy 的 findEntity(named:) 解决方案

这是我在使用不起作用的 findEntity() 方法时尝试的代码:

let redBoardModelEntity =  cubesAnchor.redBoard?.findEntity(named: "simpBld_root") as! ModelEntity
let greenCubeModelEntity =  cubesAnchor.greenCube?.findEntity(named: "simpBld_root") as! ModelEntity
let blueCubeModelEntity =  cubesAnchor.blueCube?.findEntity(named: "simpBld_root") as! ModelEntity

这就是我最终得到的,它起作用了,意思是,它显示了所有对象和整个东西被拖在一起。 [见下图,后面是代码]

import UIKit
import RealityKit

class ViewController: UIViewController {
    @IBOutlet var arView: ARView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
                
        let boxAnchor = try! Experience.loadBox()
        arView.scene.anchors.append(boxAnchor)            
    
        let cubesAnchor = try! Experience.loadCubes()
        arView.scene.anchors.append(cubesAnchor)
        print (cubesAnchor)
        cubesAnchor.actions.behavior.onAction = handleTapOnEntity(_:)            
        
        let redModel =  cubesAnchor.children[0].children[0]
                  
        let group = ModelEntity() as ModelEntity & HasCollision
        group.addChild(redModel)
        // group.addChild(blueCubeModelEntity)
        // group.addChild(greenCubeModelEntity)
        
        group.generateCollisionShapes(recursive: false)
        self.arView.installGestures(.all, for: group)
        
        let shape = ShapeResource.generateBox(width: 1, height: 1, depth: 1)
        let collision = CollisionComponent(shapes: [shape],
                                             mode: .trigger,                                               
                                           filter: .sensor)
        group.components.set(collision)
        
        let anchor = AnchorEntity()
        anchor.addChild(group)
        anchor.scale = [5,5,5]
        arView.scene.anchors.append(anchor)            
        
        DispatchQueue.main.asyncAfter(deadline: .now() + 2.0){
            // self.arView.scene.anchors.removeAll()
            let coneAnchor = try! Experience.loadCone()
            self.arView.scene.anchors.append(coneAnchor)
            coneAnchor.actions.behavior.onAction = handleTapOnEntity(_:)                    
            // print(cubesAnchor)                
        }                          
        
        boxAnchor.actions.behavior.onAction = handleTapOnEntity(_:)
            
        func handleTapOnEntity(_ entity: Entity?){
            guard let entity = entity else { return }
            
           // self.myEntity = entity
           // self.myEntity?.isEnabled = false    
        }            
    }
}
swift augmented-reality arkit realitykit reality-composer
1个回答
0
投票

遍历子实体(节点)

如我所见,您的模型被命名为

greenCube
redBoard
blueCube
。因此,从 Reality Composer 的场景层次结构中获取任何 ModelEntity 的最简单方法是使用
findEntity(named:)
及其子项。

let model = cubesAnchor.findEntity(named: "greenCube")?.children[0] as! ModelEntity

这是它的样子:

import UIKit
import RealityKit
    
class ViewController: UIViewController {
    
    @IBOutlet var arView: ARView!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        let scene = try! Experience.loadTwoBoxes()
        
        let model001 = scene.findEntity(named: "redCube")?.children[0] as! ModelEntity & HasPhysicsBody
        model001.position.y = 1.0
        model001.physicsBody = .init()
        model001.generateCollisionShapes(recursive: false)
        
        let model002 = scene.findEntity(named: "blueCube")?.children[0] as! ModelEntity & HasPhysicsBody
        model002.position.y = 1.0
        model002.physicsBody = .init()
        model002.generateCollisionShapes(recursive: false)

        arView.scene.anchors.append(scene)
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.