我对ARKit很新(准确地说是2天),我在点击时尝试在我的ARSCNView上放置一个节点,并且我能够在50%的时间内成功完成此操作。另一半时间,屏幕上没有任何内容。我已将其缩小到以下代码行:
func getCameraTransform(for camera: ARCamera) -> MDLTransform {
return MDLTransform(matrix: camera.transform)
}
当屏幕上没有显示任何内容时,上述功能将返回 float3(0.0,0.0,0.0)的 transform.translation
我已经检查过,camera.transform提供的值似乎是 simd_float4x4([[0.98134,0.0198354,-0.191256,0.0)],[ - 0.0129627,0.998227,0.0371193,0.0]] ,[0.191844,-0.0339474,0.980838,0.0)],[ - 0.482363,-0.00405288,-0.768801,1.0)]]) transform.translation为0.
为什么这有时会返回0但是在我点击的时候工作完全正常?
以下是完整代码:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
guard touches.first!.tapCount == 1 else { return }
let camera = sceneView.session.currentFrame!.camera
let transform = getCameraTransform(for: camera)
let boxGeometry = SCNBox(width: 0.1, height: 0.1, length: 0.1, chamferRadius: 0)
let cube = SCNNode(geometry: boxGeometry)
// SOMETIMES TRANSFORM.TRANSLATION IS JUST 0s
let position = SCNVector3(
transform.translation.x,
transform.translation.y,
transform.translation.z
)
cube.position = position
sceneView.scene.rootNode.addChildNode(cube)
}
func getCameraTransform(for camera: ARCamera) -> MDLTransform {
return MDLTransform(matrix: camera.transform) // this is returning the 0 translation every other time
}
答案 0 :(得分:0)
I think it may be better to look at a different way to approach the issue. I haven't seen that example before and from having a test, it doesn't seem to be what you need.
As such, I will provide you with a few examples which should help you get started.
(1) Place An Object At A Certain Position Based On The Current Frame Of The Camera:
/// Adds An SCNNode 3m Away From The Current Frame Of The Camera
func addNodeInFrontOfCamera(){
//1. Get The Current Frame Of The Camera
guard let currentTransform = augmentedRealitySession.currentFrame?.camera.transform else { return }
//2. Create An SCNNode
let nodeToAdd = SCNNode()
let boxGeometry = SCNBox(width: 0.1, height: 0.1, length: 0.1, chamferRadius: 0)
boxGeometry.firstMaterial?.diffuse.contents = UIColor.red
nodeToAdd.geometry = boxGeometry
// Create A Transform With A Translation 1m In Front Of The Camera
var translation = matrix_identity_float4x4
//Change The X Value
translation.columns.3.x = 0
//Change The Y Value
translation.columns.3.y = 0
//Change The Z Value
translation.columns.3.z = -1
nodeToAdd.simdTransform = matrix_multiply(currentTransform, translation)
augmentedRealityView?.scene.rootNode.addChildNode(nodeToAdd)
}
(2) Place an SCNNode Based On Any Available featurePoints In The Scene:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
//1. Get The Current Touch Point
guard let currentTouchPoint = touches.first,
//2. Perform A Hit Test Against Any Feature Points
let result = augmentedRealityView.hitTest(currentTouchPoint.location(in: augmentedRealityView), types: .featurePoint).last else { return }
//3. Get The World Coordinates Of The Result
let transform = SCNMatrix4(result.worldTransform)
//4. Get The Positional Data From The Transform
let vectorFromTransform = SCNVector3(transform.m41, transform.m42, transform.m43)
//5. Create An SCNBox At The Point
let boxNode = SCNNode()
let boxGeometry = SCNBox(width: 0.1, height: 0.1, length: 0.1, chamferRadius: 0)
boxNode.geometry = boxGeometry
boxNode.position = vectorFromTransform
augmentedRealityView.scene.rootNode.addChildNode(boxNode)
}
Apologies if it doesn't explain the results your are experiencing, but nonetheless, I hope it helps...