首页 > 解决方案 > 在 ARKit 水平面上绘制填充多边形

问题描述

我将如何在 ARKit 世界中检测到的水平面上绘制用户触摸的填充多边形连接点。

我继续通过将生命值保持在数组上来做到这一点......

let position = SCNVector3.positionFrom(matrix: result.worldTransform)
let sphere = SphereNode(position: position)
nodes.append(position)

...然后尝试绘制贝塞尔路径

let bezierPath = UIBezierPath()
bezierPath.lineWidth = 0.1

for index in 0..<nodes.count {

    let node = nodes[index] as SphereNode
    if (index == 0) {

       let point = CGPoint(x: CGFloat(node.position.x), y: CGFloat(node.position.z))
       bezeierPath.move(to: point)


    } else {
       let point = CGPoint(x: CGFloat(node.position.x), y: CGFloat(node.position.z))
        bezeierPath.addLine(to: point)

     }

 }

bezeierPath.close()
bezeierPath.fill()
bezeierPath.stroke()

let shape = SCNShape(path: bezeierPath, extrusionDepth: 0.03)
shape.firstMaterial?.diffuse.contents = UIColor.red
let node = SCNNode.init(geometry: shape)
sceneView.scene.rootNode.addChildNode(node)

但这并没有在正确的位置绘制多边形。它以垂直角度将其绘制在相机后面。

标签: iosswiftpolygonarkituibezierpath

解决方案


像和的2D 几何形状本质SCNPlane上是垂直的,因为它们在 xy 平面上工作。在您的示例中,您使用 z 3D 坐标作为 y 2X 坐标。SCNTextSCNShape

尝试设置node.eulerAngles.x-.pi / 2(如果不起作用,请尝试不使用-),您的节点将是水平的而不是垂直的。


推荐阅读