首页 > 解决方案 > 如何在两个地理位置之间创建 SCNPlane(如下图所示)?

问题描述

我正在为 AR 导航使用“Mapbox”框架,我需要在两个地理位置之间添加 SCNPlane,如下图所示

在此处输入图像描述

// 代码 - 添加 SCNNode 和 SCNPlane

func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
    // 1

    guard let planeAnchor = anchor as? ARPlaneAnchor else { return }

    // 2
    let width = CGFloat(planeAnchor.extent.x )
    let height = CGFloat(planeAnchor.extent.z)
    let plane = SCNPlane(width: width, height: height)

    // 3
    plane.materials.first?.diffuse.contents = UIColor.lightText

    // 4
    let planeNode = SCNNode(geometry: plane)

    // 5
    let x = CGFloat(planeAnchor.center.x)
    let y = CGFloat(planeAnchor.center.y)
    let z = CGFloat(planeAnchor.center.z)
    planeNode.position = SCNVector3(x,y,z)
    planeNode.eulerAngles.x = -.pi / 2

    // 6
    node.addChildNode(planeNode)
}

// 更新这里的节点

func renderer(_ renderer: SCNSceneRenderer, didUpdate node: SCNNode, for anchor: ARAnchor) {

    // 1
    guard let planeAnchor = anchor as?  ARPlaneAnchor,
        let planeNode = node.childNodes.first,
        let plane = planeNode.geometry as? SCNPlane
        else { return }

    // 2
    let width = CGFloat(planeAnchor.extent.x + 100)
    let height = CGFloat(planeAnchor.extent.z )
    plane.width = width
    plane.height = height

    // 3
    let x = CGFloat(planeAnchor.center.x)
    let y = CGFloat(planeAnchor.center.y)
    let z = CGFloat(planeAnchor.center.z)
    planeNode.position = SCNVector3(x, y, z)

}

在这里,我使用 ARPlaneAnchor 来检测平面并使用 didAdd /didUpdate 代表添加/更新 SCNNode,但我需要在不使用 ARPlaneAnchor 的情况下在两个地理位置之间绘制 SCNPlane。

提前致谢,

标签: iosswiftmapboxarkitscnnode

解决方案


推荐阅读