@@ -21,6 +21,65 @@ class ViewController: UIViewController {
2121
2222 override func viewDidLoad( ) {
2323 super. viewDidLoad ( )
24+ // Load assets
25+ guard let imagePath = NSBundle . mainBundle ( ) . pathForResource ( " Hellbrunn25 " , ofType: " jpg " ) else {
26+ fatalError ( " Failed to find path for panaromic file. " )
27+ }
28+ guard let image = UIImage ( contentsOfFile: imagePath) else {
29+ fatalError ( " Failed to load panoramic image " )
30+ }
31+
32+ // Set the scene
33+ let scene = SCNScene ( )
34+ sceneView. scene = scene
35+ sceneView. showsStatistics = true
36+ sceneView. allowsCameraControl = true
37+
38+ //Create node, containing a sphere, using the panoramic image as a texture
39+ let sphere = SCNSphere ( radius: 20.0 )
40+ sphere. firstMaterial!. doubleSided = true
41+ sphere. firstMaterial!. diffuse. contents = image
42+ let sphereNode = SCNNode ( geometry: sphere)
43+ sphereNode. position = SCNVector3Make ( 0 , 0 , 0 )
44+ scene. rootNode. addChildNode ( sphereNode)
45+
46+ // Lights, ...
47+ let ambientLightNode = SCNNode ( )
48+ ambientLightNode. light = SCNLight ( )
49+ ambientLightNode. light!. type = SCNLightTypeAmbient
50+ ambientLightNode. light!. color = UIColor ( white: 0.67 , alpha: 1.0 )
51+ scene. rootNode. addChildNode ( ambientLightNode)
52+
53+ // Camera, ...
54+ cameraNode. camera = SCNCamera ( )
55+ cameraNode. position = SCNVector3Make ( 0 , 0 , 0 )
56+ scene. rootNode. addChildNode ( cameraNode)
57+
58+ // Action (Sorry! I could not help myself)
59+ if motionManager. deviceMotionAvailable {
60+ motionManager. deviceMotionUpdateInterval = 1.0 / 60.0
61+ motionManager. startDeviceMotionUpdatesToQueue ( NSOperationQueue . mainQueue ( ) ) {
62+ [ weak self] ( data: CMDeviceMotion ? , error: NSError ? ) in
63+
64+ guard let data = data else {
65+ NSLog ( " Error in deviceMotionUpdate \( error) " )
66+ return
67+ }
68+ guard let strongSelf = self else {
69+ NSLog ( " Captured weak self was nil in deviceMotionUpdate " )
70+ return
71+ }
72+
73+ let attitude : CMAttitude = data. attitude
74+
75+ strongSelf. cameraNode. eulerAngles = SCNVector3Make ( Float ( attitude. roll - M_PI/ 2.0 ) , Float ( attitude. yaw) , Float ( attitude. pitch) )
76+ }
77+
78+ }
79+ else {
80+ fatalError ( " Device motion is not available " )
81+ }
82+
2483
2584 }
2685
0 commit comments