scenekitで写真を立体に貼る

![IMG_0396.PNG](https://qiita-image-store.s3.amazonaws.com/0/176818/3805e30a-96bb-5dac-a0d6-c35438cc7141.png)
scenkitの直方体に写真を貼って、motionセンサと連動させてみました。
よりリアルな感じがします。
写真はiPadの写真を6面用意して貼り付けています。

```python:cube.py
# coding: utf-8
from objc_util import *
import ui
import math
import motion
from scene import *
load_framework('SceneKit')
SCNView, SCNScene, SCNBox, SCNText, SCNNode, SCNLight, SCNMaterial, SCNCamera, SCNAction, SCNTransaction,SCNLookAtConstraint,UIFont= map(ObjCClass, ['SCNView', 'SCNScene', 'SCNBox', 'SCNText', 'SCNNode', 'SCNLight',  'SCNMaterial','SCNCamera', 'SCNAction','SCNTransaction','SCNLookAtConstraint','UIFont'])
class SCNVector3 (Structure):
    _fields_ = [('x', c_float), ('y', c_float), ('z', c_float)]
W=30
L=15
H=2.5

@on_main_thread
class MyScene (Scene):
    def setup(self):
        #motion start
        motion.start_updates()
    def draw(self):
        #motion update
        pitch, roll, yaw =motion.get_attitude()
        self.box_node.runAction_(SCNAction.rotateToX_y_z_duration_(pitch,-roll,yaw,0))
    def make_view(self,mc):
        pitch, roll, yaw =motion.get_attitude()
        main_view_objc = mc
        scene_view = SCNView.alloc().initWithFrame_options_(((0, 0),(100, 100)), None).autorelease()
        scene_view.setAutoresizingMask_(18)
        scene_view.setAllowsCameraControl_(True)
        scene = SCNScene.scene()
        root_node = scene.rootNode()  
        box= SCNBox.boxWithWidth_height_length_chamferRadius_(W, L,H,0)
        
        Material_img1 = SCNMaterial.material()
        Material_img1.contents = UIImage.imageWithContentsOfFile_('Photo1.jpg') #forward
        Material_img2 = SCNMaterial.material()
        Material_img2.contents = UIImage.imageWithContentsOfFile_('Photo2.jpg') #up
        Material_img3 = SCNMaterial.material()
        Material_img3.contents = UIImage.imageWithContentsOfFile_('Photo3.jpg') #back
        Material_img4 = SCNMaterial.material()
        Material_img4.contents = UIImage.imageWithContentsOfFile_('Photo4.jpg') #down
        Material_img5 = SCNMaterial.material()
        Material_img5.contents = UIImage.imageWithContentsOfFile_('Photo5.jpg') #left
        Material_img6 = SCNMaterial.material()
        Material_img6.contents = UIImage.imageWithContentsOfFile_('Photo6.jpg') #right
        box.setMaterials_([Material_img1,Material_img2,Material_img3,Material_img4,Material_img5,Material_img6])
        self.box_node = SCNNode.nodeWithGeometry_(box)
        self.box_node.runAction_(SCNAction.rotateToX_y_z_duration_(pitch,-roll,yaw,0))
        self.box_node.setPosition_((0, 0, 20))

        base= SCNBox.boxWithWidth_height_length_chamferRadius_(80, 80, 4, 0)
        base_node = SCNNode.nodeWithGeometry_(base) 
        base_node.setPosition_((0, 0, 0))

        light_node = SCNNode.node()
        light_node.setPosition_((0, 0, 70))
        light_node.setRotation_((0, 0, 1, -math.pi/2))
        light = SCNLight.light()
        light.setType_('spot')
        light.setCastsShadow_(True)
        light.setColor_(UIColor.whiteColor().CGColor())
        light_node.setLight_(light)

        camera = SCNCamera.camera()
        camera_node = SCNNode.node()
        camera_node.setCamera(camera)
        camera_node.setPosition((15, 15,80))
        
        # Add a constraint to the camera to keep it pointing to the target 
        constraint = SCNLookAtConstraint.lookAtConstraintWithTarget_(self.box_node)
        constraint.gimbalLockEnabled = True
        camera_node.constraints = [constraint]
        camera_node.setRotation_((1, 1, 1, math.pi*1/3))

        root_node.addChildNode_(camera_node)
        root_node.addChildNode_(self.box_node)
        root_node.addChildNode_(base_node)
        root_node.addChildNode_(light_node)
        
        scene_view.setScene_(scene)
        main_view_objc.addSubview_(scene_view) 
if __name__ == "__main__":
    #set view
    main_view = ui.View()
    main_view_objc = ObjCInstance(main_view)
    main_view.name = 'SceneKit Demo'
    #run MyScene
    my_scene = MyScene()
    scene_view = SceneView()
    scene_view.scene = my_scene
    #make scenekit
    main_view_objc.addSubview_(scene_view)
    my_scene.make_view(main_view_objc)
    #present view
    main_view.present()  
```