メインのViewとサブのViewで違う角度からの立方体を表示するiPhoneアプリのサンプルコードを描いてみます。
import UIKit
import SceneKit
class ViewController: UIViewController {
weak var scene : SCNScene?
override func viewDidLoad() {
super.viewDidLoad()
setupScene()
createSubView()
createGround()
createBox()
}
func setupScene() {
let sv = SCNView(frame: view.bounds)
sv.backgroundColor = UIColor(hue: 0.1, saturation: 0.1, brightness: 0.5, alpha: 1)
sv.scene = SCNScene()
sv.autoenablesDefaultLighting = true
view.addSubview(sv);
scene = sv.scene // common
let camera = SCNNode()
camera.camera = SCNCamera()
camera.position = SCNVector3(x: 0, y: –5, z: 30)
scene?.rootNode.addChildNode(camera)
sv.pointOfView = camera
}
func createSubView() {
let subView = SCNView(frame: CGRectMake(10, CGRectGetMaxY(view.bounds) – 160, 150, 150))
subView.scene = scene // common
subView.backgroundColor = UIColor(hue: 0.1, saturation: 0.2, brightness: 0.6, alpha: 1)
subView.layer.borderColor = UIColor.blackColor().CGColor
subView.layer.borderWidth = 2
subView.autoenablesDefaultLighting = true
view.addSubview(subView)
let subcamera = SCNNode()
subcamera.camera = SCNCamera()
subcamera.position = SCNVector3(x: 0, y: 15, z: 5)
subcamera.rotation = SCNVector4(x: 1, y: 0, z: 0, w: Float(-M_PI) * 0.4)
scene?.rootNode.addChildNode(subcamera)
subView.pointOfView = subcamera
}
func createGround() {
let ground = SCNCylinder(radius: 12, height: 1)
ground.firstMaterial?.diffuse.contents = UIColor(hue: 0.4, saturation: 0.3, brightness: 0.8, alpha: 1)
let groundNode = SCNNode(geometry: ground)
groundNode.position = SCNVector3(x: 0, y: –10, z: 0)
groundNode.physicsBody = SCNPhysicsBody.staticBody()
self.scene?.rootNode.addChildNode(groundNode)
}
func createBox() {
let box = SCNBox(width: 5, height: 5, length: 5, chamferRadius: 0)
box.firstMaterial?.diffuse.contents = UIColor(hue: 0.3, saturation: 0.1, brightness: 1, alpha: 1)
let boxNode = SCNNode(geometry: box)
boxNode.name = “box”
boxNode.physicsBody = SCNPhysicsBody.dynamicBody()
scene?.rootNode.addChildNode(boxNode)
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
let box = scene?.rootNode.childNodeWithName(“box”, recursively: false)
box?.physicsBody?.applyForce(SCNVector3(x: 0, y: 2, z: 0), atPosition: SCNVector3(x: 2, y: 0, z: 2), impulse: true)
}
}