【问题标题】:How to get accurate CGpoint for drawing UIBezierpath on sceneview from touchesMoved method?如何从 touchesMoved 方法获得准确的 CGpoint 以在场景视图上绘制 UIBezierpath?
【发布时间】:2019-05-07 05:48:37
【问题描述】:

UIBezierpath 显示在现实世界的其他地方

从下面的代码中收集 CGPoint

 override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        guard let location = touches.first?.location(in: self.sceneView) else {
            return
        }
        arrayCGPoints.append(location)
    }

& 使用下面的代码显示 Bazierpath

func updateBazierPath(){        

        if arrayCGPoints.count > 0 {

            let bezierPath = UIBezierPath()

            var i : Int = 0
            for varPoint in arrayCGPoints{

                if i == 0 {
                    bezierPath.move(to: varPoint)
                }
                else{
                    bezierPath.addLine(to: varPoint)
                }
                i += 1
            }
            bezierPath.close()
        }
        // Add shape
        let shape = SCNShape(path: bezierPath, extrusionDepth: 0.2)
        let shapeNode = SCNNode(geometry: shape)
    self.sceneView.scene.rootNode.addChildNode(shapeNode)

        shapeNode.geometry?.firstMaterial?.isDoubleSided = true
        shapeNode.geometry!.firstMaterial?.diffuse.contents = UIColor.blue
        shapeNode.position.z = -0.2
        shapeNode.eulerAngles.x = -.pi / 2
    }

还尝试将 CGPoints 转换为米

arrayCGPoints.append(CGPoint(x: location.x/100, y: location.y/100))

还是不行

【问题讨论】:

    标签: ios scenekit arkit uibezierpath swift4.2


    【解决方案1】:

    这将绘制一个节点@ 0,0,0,其中包含人拖动的轮廓,将屏幕坐标转换为世界。我创建了一个单独的 SCNVector3 数组,它们是 3d 空间中的坐标(您可以在屏幕上的这些点放置一些节点以证明位置)。另请注意附加是 x,z,z(不是 scenepoint.y)。我的相机从 0,15,0.1 看 0,0,0。这至少可以让您直观地了解 z 发生了什么,并希望能回答您提出的问题。

    如果您希望生成的形状与用户触摸的位置保持对齐,这里没有解决。

    var shapeNode = SCNNode()
    
    func updateBazierPath()
    {
        for vPoints in arrayCGPoints
        {
            let projectedPoint = gameScene.projectPoint(SCNVector3(0, 0, 0))
            let scenePoint = gameScene.unprojectPoint(SCNVector3(vPoints.x, vPoints.y, CGFloat(projectedPoint.z)))
            scenePoints.append(SCNVector3(scenePoint.x, scenePoint.z, scenePoint.z))
        }
        let bezierPath = UIBezierPath()
        bezierPath.move(to: CGPoint(x: CGFloat(scenePoints[0].x), y: CGFloat(scenePoints[0].y)))
        for vCount in 1...scenePoints.count - 1
        {
            bezierPath.addLine(to: CGPoint(x: CGFloat(scenePoints[vCount].x), y: CGFloat(scenePoints[vCount].y)))
        }
        bezierPath.close()
    
        shapeNode.removeFromParentNode()
        let shape = SCNShape(path: bezierPath, extrusionDepth: 0.5)
        shapeNode = SCNNode(geometry: shape)
        shapeNode.geometry?.firstMaterial?.isDoubleSided = true
        shapeNode.geometry!.firstMaterial?.diffuse.contents = UIColor.yellow
        //let action = SCNAction.repeatForever(SCNAction.rotate(by: .pi, around: SCNVector3(1, 0, 0), duration: 5))
        //shapeNode.runAction(action)
        shapeNode.position = SCNVector3(0, 0, 0)
        shapeNode.eulerAngles.x = -.pi / 2
        gNodes.gameNodes.addChildNode(shapeNode)
    }
    

    【讨论】:

    • 我尝试了您的解决方案,但行显示如 ibb.co/9gXm622 我的 z 位置对路径的影响有误
    • 场景视图渲染不正确
    猜你喜欢
    • 2011-12-20
    • 1970-01-01
    • 1970-01-01
    • 2020-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多