1
private func lineFrom(vector vector1: SCNVector3, toVector vector2: SCNVector3) -> SCNGeometry {

        let indices: [Int32] = [0,1]
        let source = SCNGeometrySource(vertices: [vector1, vector2])
        let element = SCNGeometryElement(indices: indices, primitiveType: .line)
        return SCNGeometry(sources: [source], elements: [element])
    }


@objc private func tapped(rec: UITapGestureRecognizer) {

    let hitTransform = SCNMatrix4(hit.worldTransform)

                let hitPoint = SCNVector3Make(hitTransform.m41 , hitTransform.m42, hitTransform.m43)

                if points.start == nil{
                    print("start touch")
                    points.start = hitPoint
                    print(points.start!.x, points.start!.y, points.start!.z)

                } 

        else{
                    points.end = hitPoint
                    print(points.end!.x, points.end!.y, points.end!.z

                    line.geometry = lineFrom(vector: points.start!, toVector: points.end!)

                    line.geometry?.firstMaterial?.diffuse.contents = UIColor.red
                    line.geometry?.firstMaterial?.isDoubleSided = true

                    sceneView.scene.rootNode.addChildNode(line)
                    func renderer(_ renderer: SCNSceneRenderer, willRenderScene scene: SCNScene, atTime time: TimeInterval) {
                        glLineWidth(80)
                     }
                    canPlacePoint = false
                }
            }
        }
}

I touch two points to save the position and try to draw a line connecting the two points. Although the line is drawn, the position of the touch point is not correct. I also used glLineWidth () because I wanted to change the thickness of the thickness of the drawn line, but it does not change. What should I do?

Emre Önder
  • 2,408
  • 2
  • 23
  • 73
함혜진
  • 11
  • 2
  • Possible duplicate of [Stroke Width with a SceneKit line primitive type](https://stackoverflow.com/questions/26705321/stroke-width-with-a-scenekit-line-primitive-type) – Enea Dume May 03 '18 at 10:25
  • https://stackoverflow.com/questions/46912921/wider-and-smoother-lines-in-metal-shader – Xartec Jun 28 '18 at 14:28

2 Answers2

0

SCeneKit prefers use Metal over OpenGl, and in Metal there is no way to draw thick lines; in addition, OpenGl is now deprecated. You need use triangles to draw fat lines.

Menio
  • 61
  • 7
0

Two ideas worth to try:

  • A stright line can be drawn with a rotated SCNCapsule or SCNBox
  • fill the material.diffuse.contents with an image file, using transparent images you can draw many interesting forms
Jose Paredes
  • 329
  • 1
  • 8