Swift 如何获取三维模型的大小?来自USDZ文件/场景?
我想从Swift中的USDZ文件/场景中获取3D模型的大小。我怎样才能做到?目前,我在swift中导入了一个USDZ文件,然后在代码中转换为场景:Swift 如何获取三维模型的大小?来自USDZ文件/场景?,swift,scenekit,arkit,scene,Swift,Scenekit,Arkit,Scene,我想从Swift中的USDZ文件/场景中获取3D模型的大小。我怎样才能做到?目前,我在swift中导入了一个USDZ文件,然后在代码中转换为场景: class ViewController: UIViewController, ARSCNViewDelegate { @IBOutlet var sceneView: ARSCNView! override func viewDidLoad() { super.viewDidLoad()
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
但是我有点困惑如何使用边界框,因为有一个最大/最小值?我用哪一个?或者我该如何使用它?找到了解决方案
//For width:
shipNode.boundingBox.max.x
//For height:
shipNode.boundingBox.max.y
从
例如,如果几何体的边界框的最小角点{1,0,2}和最大角点{3,4,5},则几何体顶点数据中的所有点的x坐标值都在-1.0和3.0之间(包括-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
boundingBox.max.x用于宽度,boundingBox.max.y用于高度: