Guest User

Untitled

a guest
Jan 22nd, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1. //
  2. // ViewController.swift
  3. // Shoji
  4. //
  5. // Created by MizushimaYusuke on 2018/01/12.
  6. // Copyright © 2018 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. sceneView.autoenablesDefaultLighting = true
  26.  
  27. // Create a new scene
  28. let scene = SCNScene()
  29.  
  30. // Set the scene to the view
  31. sceneView.scene = scene
  32. }
  33.  
  34. override func viewWillAppear(_ animated: Bool) {
  35. super.viewWillAppear(animated)
  36.  
  37. // Create a session configuration
  38. let configuration = ARWorldTrackingConfiguration()
  39.  
  40. // Run the view's session
  41. sceneView.session.run(configuration)
  42. }
  43.  
  44. override func viewWillDisappear(_ animated: Bool) {
  45. super.viewWillDisappear(animated)
  46.  
  47. // Pause the view's session
  48. sceneView.session.pause()
  49. }
  50.  
  51. override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
  52. if let p = touches.first?.location(in: sceneView) {
  53. if let hit = sceneView.hitTest(p, types: .featurePoint).first {
  54.  
  55. let position = SCNVector3(hit.worldTransform.columns.3.x, hit.worldTransform.columns.3.y + 0.03, hit.worldTransform.columns.3.z)
  56.  
  57. let paper = SCNBox(width: 0.03, height: 0.06, length: 0.001, chamferRadius: 0)
  58. let paperNode = SCNNode(geometry: paper)
  59. paperNode.position = position
  60. sceneView.scene.rootNode.addChildNode(paperNode)
  61.  
  62. stride(from: -0.015, through: 0.015, by: 0.005).forEach { x in
  63. let barv = SCNBox(width: 0.001, height: 0.06, length: 0.0015, chamferRadius: 0)
  64. barv.firstMaterial?.diffuse.contents = UIColor.brown
  65. let barvNode = SCNNode(geometry: barv)
  66. barvNode.position = SCNVector3(x, 0, 0)
  67. paperNode.addChildNode(barvNode)
  68. }
  69.  
  70. stride(from: -0.03, through: 0.03, by: 0.005).forEach { y in
  71. let barh = SCNBox(width: 0.03, height: 0.001, length: 0.0015, chamferRadius: 0)
  72. barh.firstMaterial?.diffuse.contents = UIColor.brown
  73. let barhNode = SCNNode(geometry: barh)
  74. barhNode.position = SCNVector3(0, y, 0)
  75. paperNode.addChildNode(barhNode)
  76. }
  77. }
  78. }
  79. }
  80.  
  81. }
Add Comment
Please, Sign In to add comment