How can I draw a circle in SceneKit using its radius and origin?

Viewed 140

What I am trying to do that draw a circle node using its radius and origin points.

Is there any way to draw a circle node in SceneKit?

How can I draw that?

2 Answers

You could probably do something like this:

let circle = SCNPlane(width: 1.0, height: 1.0)
circle.cornerRadius = circle.width/2

hope, this is, what you are looking for.

another approch might be the SCNCylinder with a very small height or even SCNTorus with a very small pipe radius.

First approach

Use SceneKit SCNCylinder's procedural mesh to create a circle.

func circle(origin: SCNVector3, radius: CGFloat) -> SCNNode {

    let cylinderNode = SCNNode()
    cylinderNode.geometry = SCNCylinder(radius: radius, height: 0.001)
    cylinderNode.geometry?.firstMaterial?.diffuse.contents = UIColor.red
    cylinderNode.geometry?.firstMaterial?.isDoubleSided = true
    cylinderNode.position = origin
    cylinderNode.eulerAngles.x = .pi/2
    return cylinderNode
}

sceneView.scene?.rootNode.addChildNode(circle(
                                           origin: SCNVector3(), radius: 0.5)
                                      )

Second approach

This approach is impractical for your particular case, because you must specify the radius in UIKit units, not in meters like in SceneKit. However, I decided to publish it as option B.

// Path
let circlePath = UIBezierPath(arcCenter: CGPoint(x: 0, y: 0), 
                                 radius: 20, 
                             startAngle: 0.0, 
                               endAngle: .pi * 2, 
                              clockwise: true)
circlePath.flatness = 0.1

// Shape
let material = SCNMaterial()
material.diffuse.contents = UIColor.systemRed
material.isDoubleSided = true

let circleShape = SCNShape(path: circlePath, extrusionDepth: 0.001)
circleShape.materials = [material]

// Node
let circleNode = SCNNode()
circleNode.geometry = circleShape
sceneView.scene?.rootNode.addChildNode(circleNode)
Related