gpt4 book ai didi

swift - RealityKit – 获取 ModelEntity 大小作为 SIMD3

转载 作者:行者123 更新时间:2023-12-02 18:12:17 24 4
gpt4 key购买 nike

有没有办法知道 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)

最佳答案

要计算图元的大小,请使用其边界框maxmin 值。

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)!)

关于swift - RealityKit – 获取 ModelEntity 大小作为 SIMD3<Float>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72131489/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com