我正在开发一个应用程序,用户可以通过使用 ARKit 在 3D 空间上放置一些点来创建一些平面形状,但是似乎我使用这些点创建 UIBezierPath 的部分存在问题。
在我的应用程序中,用户首先通过按下按钮将虚拟透明墙放置在 AR 中与其设备相同的位置:
guard let currentFrame = sceneView.session.currentFrame else {
return
}
let imagePlane = SCNPlane(width: sceneView.bounds.width, height: sceneView.bounds.height)
imagePlane.firstMaterial?.diffuse.contents = UIColor.black
imagePlane.firstMaterial?.lightingModel = .constant
var windowNode = SCNNode()
windowNode.geometry = imagePlane
sceneView.scene.rootNode.addChildNode(windowNode)
windowNode.simdTransform = currentFrame.camera.transform
windowNode.opacity = 0.1
然后,用户在该墙上放置一些点(一些球体节点),以通过按下按钮来确定要创建的平面对象的形状。如果用户指向创建的第一个球体节点,则我关闭该形状,创建它的一个节点并将其放置在与墙相同的位置:
let hitTestResult = sceneView.hitTest(self.view.center, options: nil)
if let firstHit = hitTestResult.first {
if firstHit.node == windowNode {
let x = Double(firstHit.worldCoordinates.x)
let y = Double(firstHit.worldCoordinates.y)
let pointCoordinates = CGPoint(x: x , y: y)
let sphere = SCNSphere(radius: 0.02)
sphere.firstMaterial?.diffuse.contents = UIColor.white
sphere.firstMaterial?.lightingModel = .constant
let sphereNode = SCNNode(geometry: sphere)
sceneView.scene.rootNode.addChildNode(sphereNode)
sphereNode.worldPosition = firstHit.worldCoordinates
if points.isEmpty {
windowPath.move(to: pointCoordinates)
} else {
windowPath.addLine(to: pointCoordinates)
}
points.append(sphereNode)
if undoButton.alpha == 0 {
undoButton.alpha = 1
}
} else if firstHit.node == points.first {
windowPath.close()
let windowShape = SCNShape(path: windowPath, extrusionDepth: 0)
windowShape.firstMaterial?.diffuse.contents = UIColor.white
windowShape.firstMaterial?.lightingModel = .constant
let tintedWindow = SCNNode(geometry: windowShape)
let worldPosition = windowNode.worldPosition
tintedWindow.worldPosition = worldPosition
sceneView.scene.rootNode.addChildNode(tintedWindow)
//removing all the sphere nodes from points and reinitializing the UIBezierPath windowPath
removeAllPoints()
}
}
当我创建第一个不可见的墙和第一个形状时,该代码起作用,但是当我创建第二个墙时,当完成绘制形状时,该形状似乎变形了,并且不在正确的位置,而实际上不在完全正确的地方。所以我认为我的 UIBezierPath 点的坐标缺少某些东西,但是呢?
编辑
好的,经过几次测试,看来这取决于 AR 会话启动时设备的方向。当设备在启动时面对用户将要创建的第一面墙时,将创建形状并按预期放置。但是,例如,如果用户在设备指向一个方向的情况下启动应用程序,然后对自己进行 90 度旋转,放置第一面墙并创建他的形状,则该形状将变形,而不是在正确的位置。
因此,看来这是 3D 坐标的问题,但我仍然不明白。
1 回答
好吧,我刚发现问题!我只是使用了错误的向量和坐标...我从来都不是一个 math/geometry 家伙哈哈
因此,不要使用:
我现在使用:
而不是使用:
我现在使用:
这就是为什么我的形状节点的位置取决于 AR 会话的初始化(我正在使用世界坐标的原因)的原因,这对我来说现在显而易见。