Guest User

Untitled

a guest
Dec 13th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. //
  2. // ViewController.swift
  3. // dot3D
  4. //
  5. // Created by MizushimaYusuke on 2017/12/11.
  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. // Create a new scene
  27. let scene = SCNScene()
  28.  
  29. // Set the scene to the view
  30. sceneView.scene = scene
  31. }
  32.  
  33. override func viewWillAppear(_ animated: Bool) {
  34. super.viewWillAppear(animated)
  35.  
  36. // Create a session configuration
  37. let configuration = ARWorldTrackingConfiguration()
  38.  
  39. // Run the view's session
  40. sceneView.session.run(configuration)
  41. }
  42.  
  43. override func viewWillDisappear(_ animated: Bool) {
  44. super.viewWillDisappear(animated)
  45.  
  46. // Pause the view's session
  47. sceneView.session.pause()
  48. }
  49.  
  50. var ready = 0
  51. var points:[SCNVector3] = []
  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, options: [.sortResults : true]).first?.node {
  56. if hit.geometry is SCNSphere {
  57. hit.geometry?.firstMaterial?.diffuse.contents = UIColor.yellow
  58. }
  59. }
  60.  
  61. if ready == 0 {
  62. ready += 1
  63. if let hit = sceneView.hitTest(p, types: .featurePoint).first {
  64. let position = SCNVector3(hit.worldTransform.columns.3.x, hit.worldTransform.columns.3.y + 0.05, hit.worldTransform.columns.3.z - 0.02)
  65. stride(from: 0, to: 0.06, by: 0.02).forEach { x in
  66. stride(from: 0, to: 0.06, by: 0.02).forEach { y in
  67. stride(from: 0, to: 0.06, by: 0.02).forEach { z in
  68. let dot = SCNSphere(radius: 0.005)
  69. dot.firstMaterial?.diffuse.contents = UIColor.black
  70. let dotNode = SCNNode(geometry: dot)
  71. dotNode.position = SCNVector3(position.x + Float(x), position.y + Float(y), position.z + Float(z))
  72. sceneView.scene.rootNode.addChildNode(dotNode)
  73. }
  74. }
  75. }
  76. }
  77. }
  78. }
  79. }
  80.  
  81. }
Add Comment
Please, Sign In to add comment