Apple Engine

Apple, iPhone, iOS, その周辺のことについて

iOS で SceneKit を試す(Swift 3) その31 - ビルトインジオメトリ SCNShape(パスからの図形)

SCNText はフォントからジオメトリを作成するが、こちらは UIBezierPath で作成するジオメトリ。

設定値は SCNText 同様にイニシャライズや個別でパスと押出し設定を行う。
面取り(chamfer) も SCNText と同様の設定を行うことができる。

Scene Editor で UIBezierPath をつくることができないため、 SCNShape はコードでしか作成することができない。

 

設定値

プロパティ名 機能
path UIBezierPath を指定
extrusionDepth 押出しの奥行きの値
chamferRadius 面取りの半径
chamferProfile 面取りの形状 UIBezierPath を指定
chamferMode 面取りをする面の設定。front は前面のみ、back は後面のみ、both 両面で初期値はこれが設定されている

 

サンプル

幅、高さ 2 の中に治る星型のパスで、
押出し 0.5、表面のみ面取りし 0.1 で表示している

f:id:x67x6fx74x6f:20170713144731p:plain

 

コード

// シーン設定
let scnView = self.view as! SCNView
scnView.showsStatistics = true
scnView.allowsCameraControl = true
scnView.debugOptions = .showWireframe // ワイヤーフレーム表示

let scene = SCNScene()
scene.background.contents = UIColor.white
scnView.scene = scene

// カメラ設定
let cameraNode = SCNNode()
cameraNode.camera = SCNCamera()
cameraNode.position = SCNVector3(2.372,1.473,2.399)
cameraNode.eulerAngles = SCNVector3(-Float.pi * 0.125,Float.pi * 0.25,0)
scene.rootNode.addChildNode(cameraNode)

// ライト設定
let lightNode = SCNNode()
let omniLight = SCNLight()
omniLight.type = .omni
lightNode.light = omniLight
lightNode.position = SCNVector3(0,5,5)
scene.rootNode.addChildNode(lightNode)

// SCNShape
let shape = SCNShape()

let path = UIBezierPath()
path.move(to: CGPoint(x: 0, y: 0))
path.addLine(to: CGPoint(x: 0.2361, y: 0.7265))
path.addLine(to: CGPoint(x: 1.0, y: 0.7265))
path.addLine(to: CGPoint(x: 0.382, y: 1.1756))
path.addLine(to: CGPoint(x: 0.618, y: 1.9021))
path.addLine(to: CGPoint(x: 0.0, y: 1.4531))
path.addLine(to: CGPoint(x: -0.618, y: 1.9021))
path.addLine(to: CGPoint(x: -0.382, y: 1.1756))
path.addLine(to: CGPoint(x: -1.0, y: 0.7265))
path.addLine(to: CGPoint(x: -0.2361, y: 0.7265))

shape.path = path
shape.extrusionDepth = 0.5

shape.chamferRadius = 0.1
shape.chamferMode = .front

let node = SCNNode(geometry: shape)
node.position = SCNVector3Zero
scene.rootNode.addChildNode(node)

 

今回はここまで。