0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

SCNConstraintを使って必ず上から光が当たる様にする

Posted at

はじめに

ARKitで飛行機を飛ばしたときに光の当て方(SCNLightの配置方法)に困ったのでその対処方法を残しておく。

何も考えず下記の様に飛行機のSCNNodeのchildNodeとしてライトをaddすると絵の様に飛行機の上から光を当てることができる。

if let ship = SCNScene(named: "art.scnassets/ship.scn")!.rootNode.childNode(withName: "ship", recursively: true) {
	let light = SCNLight()
	light.type = .omni
	
	let lightNode = SCNNode()
	lightNode.light = light
	lightNode.position = SCNVector3(ship.position.x,ship.position.y + 2,ship.position.z)
	
	ship.addChildNode(lightNode)
}

light_above_ship.png

しかし、単純にライトを飛行機の子ノードとして追加しているので飛行機をグルっと回転させたときにライトも同様に回転してしまう。
kaiten_before.png

SCNConstraintを使う

SCNConstraintとはSCNNode間の位置関係に制約を与えるための設定であり、StoryBoadを使ってUI部品を配置するときに設定するConstraintのSCNKit版に相当する物。
この場合、ライトは必ず飛行機のY座標の+0.5に位置することという制約を設ければ、飛行機の向きに関わらず上から光を当てる事ができる。

if let ship = SCNScene(named: "art.scnassets/ship.scn")!.rootNode.childNode(withName: "ship", recursively: true) {
	let light = SCNLight()
	light.type = .omni
	
	let lightNode = SCNNode()
	lightNode.light = light
            
	let positionConstraint = SCNTransformConstraint(inWorldSpace: true, with: { (node, transform) -> SCNMatrix4 in
		let pos = node.position
		return SCNMatrix4MakeTranslation(pos.x, pos.y + 2, pos.z)
	})
            
	lightNode.constraints = [positionConstraint]
            
	ship.addChildNode(lightNode)
}

kaiten_after.png

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?