我可能是错的,但我认为m41
,m42
和m43
可用于获取位置数据,并且与result.worldTransform.columns.3
执行 hitTest 时使用基本相同。
因此,在放置SCNNode
通过执行时,ARSCNHitTest
您可以使用:
let hitTestTransform = SCNMatrix4(result.worldTransform)
let positionFromMatrix4 = SCNVector3(hitTestTransform.m41, hitTestTransform.m42, hitTestTransform.m43)
let positionFromColumns = SCNVector3(result.worldTransform.columns.3.x, result.worldTransform.columns.3.y, result.worldTransform.columns.3.z)
下面的例子应该有助于澄清事情:
/// Places Our Model At The Position Of An Existining ARPlaneAnchor
///
/// - Parameter gesture: UITapGestureRecognizer
@IBAction func placeModel(_ gesture: UITapGestureRecognizer){
//1. Get The Touch Location
let touchLocation = gesture.location(in: self.augmentedRealityView)
//2. Perform An ARSCNHitTest For Any Existing Planes
guard let result = self.augmentedRealityView.hitTest(touchLocation, types: [.existingPlane, .existingPlaneUsingExtent]).first else { return }
//3. Get The World Transform
let hitTestTransform = SCNMatrix4(result.worldTransform)
//4. Initialize Our Position Either From .m41, .m42, .m43 Or From Columns.3
let positionFromMatrix4 = SCNVector3(hitTestTransform.m41, hitTestTransform.m42, hitTestTransform.m43)
let positionFromColumns = SCNVector3(result.worldTransform.columns.3.x, result.worldTransform.columns.3.y, result.worldTransform.columns.3.z)
//5. Log Them To Check I'm Not Being A Moron
print(
"""
Position From Matrix 4 == \(positionFromMatrix4)
Position From Columns == \(positionFromColumns)
""")
/*
Position From Matrix 4 == SCNVector3(x: -0.39050543, y: -0.004766479, z: 0.08107365)
Position From Columns == SCNVector3(x: -0.39050543, y: -0.004766479, z: 0.08107365)
*/
//6. Add A Node At The Position & Add It To The Hierachy
let boxNode = SCNNode(geometry: SCNBox(width: 1, height: 1, length: 1, chamferRadius: 0))
boxNode.geometry?.firstMaterial?.diffuse.contents = UIColor.cyan
boxNode.position = positionFromMatrix4
self.augmentedRealityView.scene.rootNode.addChildNode(boxNode)
}
希望能帮助到你...