如何获取3D模型的大小?来自 USDZ file/scene?
How to get the size of 3D model? From a USDZ file/scene?
我想从 Swift 中的 USDZ 文件/场景中获取 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
但是我对如何使用 boundingBox 有点困惑,因为有一个 max/min 值?我用哪一个?或者我该如何使用它?
找到解决方案。
//For width:
shipNode.boundingBox.max.x
//For height:
shipNode.boundingBox.max.y
For example, if a geometry’s bounding box has the minimum corner {-1, 0, 2}
and the maximum corner {3, 4, 5}
, all points in the geometry’s vertex data have an x-coordinate value between -1.0
and 3.0
, inclusive.
等等
let width = boundingBox.max.x - boundingBox.min.x
let height = boundingBox.max.y - boundingBox.min.y
let depth = boundingBox.max.z - boundingBox.min.z
我想从 Swift 中的 USDZ 文件/场景中获取 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
但是我对如何使用 boundingBox 有点困惑,因为有一个 max/min 值?我用哪一个?或者我该如何使用它?
找到解决方案。
//For width:
shipNode.boundingBox.max.x
//For height:
shipNode.boundingBox.max.y
For example, if a geometry’s bounding box has the minimum corner
{-1, 0, 2}
and the maximum corner{3, 4, 5}
, all points in the geometry’s vertex data have an x-coordinate value between-1.0
and3.0
, inclusive.
等等
let width = boundingBox.max.x - boundingBox.min.x
let height = boundingBox.max.y - boundingBox.min.y
let depth = boundingBox.max.z - boundingBox.min.z