Guest User

Untitled

a guest
Dec 17th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.39 KB | None | 0 0
  1. //
  2. // ViewController.swift
  3. // flipThrough
  4. //
  5. // Created by MizushimaYusuke on 2017/12/14.
  6. // Copyright © 2017 MizushimaYusuke. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10. import SceneKit
  11. import ARKit
  12.  
  13. class ViewController: UIViewController, ARSCNViewDelegate {
  14.  
  15. @IBOutlet var sceneView: ARSCNView!
  16.  
  17. override func viewDidLoad() {
  18. super.viewDidLoad()
  19.  
  20. // Set the view's delegate
  21. sceneView.delegate = self
  22.  
  23. // Show statistics such as fps and timing information
  24. sceneView.showsStatistics = true
  25.  
  26. sceneView.autoenablesDefaultLighting = true
  27.  
  28. // Create a new scene
  29. let scene = SCNScene()
  30.  
  31. // Set the scene to the view
  32. sceneView.scene = scene
  33. }
  34.  
  35. override func viewWillAppear(_ animated: Bool) {
  36. super.viewWillAppear(animated)
  37.  
  38. // Create a session configuration
  39. let configuration = ARWorldTrackingConfiguration()
  40.  
  41. // Run the view's session
  42. sceneView.session.run(configuration)
  43. }
  44.  
  45. override func viewWillDisappear(_ animated: Bool) {
  46. super.viewWillDisappear(animated)
  47.  
  48. // Pause the view's session
  49. sceneView.session.pause()
  50. }
  51.  
  52. override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
  53.  
  54. if let p = touches.first?.location(in: sceneView) {
  55. if let hit = sceneView.hitTest(p, types: .featurePoint).first {
  56. let position = SCNVector3(hit.worldTransform.columns.3.x, hit.worldTransform.columns.3.y, hit.worldTransform.columns.3.z)
  57. self.createPage(position, 50)
  58. }
  59. }
  60. }
  61.  
  62. func createPage(_ position: SCNVector3, _ cnt: Int) {
  63.  
  64. if cnt < 0 {
  65. return
  66. }
  67.  
  68. let page = SCNBox(width: 0.02, height: 0.001, length: 0.02, chamferRadius: 0.0)
  69. page.firstMaterial?.diffuse.contents = UIColor(hue: 0.13, saturation: 0.1, brightness: 0.8, alpha: 1)
  70. let pageNode = SCNNode(geometry: page)
  71. pageNode.pivot = SCNMatrix4MakeTranslation(0.01, 0, 0)
  72. pageNode.position = position
  73. sceneView.scene.rootNode.addChildNode(pageNode)
  74.  
  75. pageNode.runAction(.rotateBy(x: 0, y: 0, z: -.pi, duration: 0.3))
  76. DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
  77. self.createPage(position, cnt - 1)
  78. }
  79.  
  80. }
  81. }
Add Comment
Please, Sign In to add comment