RealityKit – 获取 ModelEntity 大小为 SIMD3<Float>

RealityKit – Get ModelEntity Size as SIMD3<Float>

有没有办法知道 SIMD3 模型实体的大小?一次,我得到尺寸,我想将它传递给 CollisionComponent。

 let box = ModelEntity(mesh: MeshResource.generateBox(size: 0.2), 
                  materials: [SimpleMaterial(color: .green, isMetallic: true)])
 box.position.y = 0.3 
 box.generateCollisionShapes(recursive: true)
 box.physicsBody = PhysicsBodyComponent(massProperties: .default, 
                                              material: .default, 
                                                  mode: .dynamic)
            
 // I know I can use the following
 box.collision = CollisionComponent(shapes: [.generateBox(size: [0.2,0.2,0.2])], 
                                                          mode: .trigger, 
                                                        filter: .sensor)
            
 // but why not just pass the size of the box I created earlier. 
 box.collision = CollisionComponent(shapes: [PASS BOX SIZE HERE], 
                                      mode: .trigger, 
                                    filter: .sensor)

要计算基元的大小,请使用其 bounding boxmaxmin 值。

let box = ModelEntity(mesh: MeshResource.generateBox(width: 0.11, 
                                                    height: 0.23, 
                                                     depth: 0.45))
       
// "reverse engineering"

let width = (box.model?.mesh.bounds.max.x)! - (box.model?.mesh.bounds.min.x)!
let height = (box.model?.mesh.bounds.max.y)! - (box.model?.mesh.bounds.min.y)!
let depth = (box.model?.mesh.bounds.max.z)! - (box.model?.mesh.bounds.min.z)!
    
let boxSize: SIMD3<Float> = [width, height, depth]
    
print(boxSize)

// Result:

// SIMD3<Float>(0.11, 0.23, 0.45)

要计算 .usdz 模型任何部分的边界框大小,请使用以下方法:

let car = try! ModelEntity.load(named: "car.usdz")

let carBody = car.children[0]..........children[0] as! ModelEntity

print((carBody.model?.mesh.bounds)!)