如何在SceneKit Swift中使用Two Points,Start Point Vector和End Point Vector绘制所有方向的箭头?

mnemlml8  于 2023-04-19  发布在  Swift
关注(0)|答案(1)|浏览(104)

如何使用两个点(起点矢量和终点矢量)在sceneview中绘制所有方向的箭头参见附件绘制箭头,如向上,向下,左侧上下,右侧上下enter image description here。我曾尝试使用UIBezierPath绘制箭头,但箭头形状看起来不完全相同,箭头图像也不适用于所有方向
检查下面的代码,我已经应用,任何解决方案,我如何修复形状相同的图像或有任何解决方案,使用两个点如何绘制方向箭头在sceneview

let path = UIBezierPath.arrow(from: CGPoint(x: arrowstartx, y: arrowstarty), to: CGPoint(x: arrowendx, y: arrowendy), tailWidth: 0.02, headWidth: 0.02, headLength: 0.02)
    path.close()
    let arrowShape = SCNShape(path: path, extrusionDepth: 0.001)
    let arrowNode = SCNNode(geometry: arrowShape)
    arrowNode.geometry?.firstMaterial?.diffuse.contents = UIColor(hexString: selectedItem ?? myMarkupDefaultColor)
 
    arrowNode.position = SCNVector3((vector1.x+vector3.x)/2.0, (vector1.y+vector3.y)/2.0, (vector1.z+vector3.z)/2.0)
    
    arrowNode.scale = SCNVector3(1.0,1.0, 1.0)

    
    self.sceneViewARMarkup.scene.rootNode.addChildNode(arrowNode)

class func arrow(from start: CGPoint, to end: CGPoint, tailWidth: CGFloat, headWidth: CGFloat, headLength: CGFloat) -> Self {
    let length = hypot(end.x - start.x, end.y - start.y)
    let tailLength = length - headLength
    
    func p(_ x: CGFloat, _ y: CGFloat) -> CGPoint { return CGPoint(x: x, y: y) }
    let points: [CGPoint] = [
        p(0, tailWidth / 2),
        p(tailLength, tailWidth / 2),
        p(tailLength, headWidth / 2),
        p(length, 0),
        p(tailLength, -headWidth / 2),
        p(tailLength, -tailWidth / 2),
        p(0, -tailWidth / 2)
    ]
    
    let cosine = (end.x - start.x) / length
    let sine = (end.y - start.y) / length
    let transform = CGAffineTransform(a: cosine, b: sine, c: -sine, d: cosine, tx: start.x, ty: start.y)
    
    let path = CGMutablePath()
    path.addLines(between: points, transform: transform )
    path.closeSubpath()
    return self.init(cgPath: path)
dvtswwa3

dvtswwa31#

一种方法是使用Blender创建箭头或找到一个箭头模型。在Blender中定位模型如下:堆栈O:[56183036],加载它,并将其指向目标。

func setTarget()
    {
        node.constraints = []
        let vConstraint = SCNLookAtConstraint(target: targetNode)
        vConstraint.isGimbalLockEnabled = true
        node.constraints = [vConstraint]
    }

相关问题