Home > Back-end >  How can I draw a circle in SceneKit using its radius and origin?
How can I draw a circle in SceneKit using its radius and origin?

Time:06-17

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?

CodePudding user response:

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 SCNTube with a very small height or even SCNTorus with a very small pipe radius.

CodePudding user response:

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(position: SCNVector3(), 
                                                radius: 0.5))

Second approach

This approach isn't practical because you are specifying the radius in the UIKit coordinate grid, not in meters. 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.0)
circleShape.materials = [material]

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