我想从 USDZ 文件/Swift 中的场景中获取 3D 模型的大小。我该怎么做?目前我有一个用 swift 导入的 USDZ 文件,然后用代码转换为场景:
class ViewController: UIViewController, ARSCNViewDelegate {
@IBOutlet var sceneView: ARSCNView!
override func viewDidLoad() {
super.viewDidLoad()
sceneView.delegate = self
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let configuration = ARImageTrackingConfiguration()
guard let trackedImages = ARReferenceImage.referenceImages(inGroupNamed: "Photos", bundle: Bundle.main) else {
print("No images available")
return
}
configuration.trackingImages = trackedImages
configuration.maximumNumberOfTrackedImages = 7
sceneView.session.run(configuration)
}
\MARK-: WHERE I CONVERT THE USDZ FILE INTO A SCENE TO DISPLAY
func renderer(_ renderer: SCNSceneRenderer, nodeFor anchor: ARAnchor) -> SCNNode? {
let node = SCNNode()
if let imageAnchor = anchor as? ARImageAnchor {
let plane = SCNPlane(width: imageAnchor.referenceImage.physicalSize.width, height: imageAnchor.referenceImage.physicalSize.height)
plane.firstMaterial?.diffuse.contents = UIColor(white: 1, alpha: 0.8)
let planeNode = SCNNode(geometry: plane)
planeNode.eulerAngles.x = -.pi / 2
guard let url = Bundle.main.url(forResource: "shipScene", withExtension: "usdz") else { fatalError() }
let mdlAsset = MDLAsset(url: url)
let shipScene = SCNScene(mdlAsset: mdlAsset)
let shipNode = shipScene.rootNode.childNodes.first!
shipNode.position = SCNVector3Zero
shipNode.position.z = 0.15
planeNode.addChildNode(shipNode)
node.addChildNode(planeNode)
}
return node
}
}
是要使用的正确代码
shipNode.boundingBox.max
但是我有点困惑如何使用边界框,因为有一个最大值/最小值?我使用哪一个?或者我什至如何使用它?
来自文档
例如,如果几何图形的边界框具有最小拐角
{-1, 0, 2}
和最大拐角{3, 4, 5}
,则几何体顶点数据中的所有点的 x 坐标值介于-1.0
和3.0
之间(包括 和 (。
等等
let width = boundingBox.max.x - boundingBox.min.x
let height = boundingBox.max.y - boundingBox.min.y
let depth = boundingBox.max.z - boundingBox.min.z
找到了解决方案:
// For width:
shipNode.boundingBox.max.x
// For height:
shipNode.boundingBox.max.y