Guest User

Untitled

a guest
Jan 16th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 KB | None | 0 0
  1. //
  2. // ViewController.swift
  3. // FlipSignBoard
  4. //
  5. // Created by MizushimaYusuke on 2018/01/08.
  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. let position = SCNVector3(hit.worldTransform.columns.3.x, hit.worldTransform.columns.3.y + 0.02, hit.worldTransform.columns.3.z)
  55.  
  56. let signBoard = SCNNode()
  57. signBoard.position = position
  58. sceneView.scene.rootNode.addChildNode(signBoard)
  59.  
  60. stride(from: 0, to: 100, by: 5).forEach { x in
  61. UIGraphicsBeginImageContextWithOptions(CGSize(width: 5, height: 50), true, 0)
  62. let paragraphStyle = NSMutableParagraphStyle()
  63. paragraphStyle.alignment = .center
  64. let attrs = [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 30, weight: UIFont.Weight.black),
  65. NSAttributedStringKey.paragraphStyle: paragraphStyle,
  66. NSAttributedStringKey.foregroundColor: UIColor.white]
  67. "Hello".draw(with: CGRect(x: -CGFloat(x) + 10, y: 10, width: 0, height: 0), options: .usesLineFragmentOrigin, attributes: attrs, context: nil)
  68.  
  69. let img = UIGraphicsGetImageFromCurrentImageContext()!
  70. UIGraphicsEndImageContext()
  71.  
  72. let part = SCNBox(width: 0.004, height: 0.04, length: 0.0001, chamferRadius: 0)
  73. part.firstMaterial?.diffuse.contents = img
  74. let partNode = SCNNode(geometry: part)
  75. partNode.position = SCNVector3(CGFloat(x) * 0.0010, 0, 0)
  76. signBoard.addChildNode(partNode)
  77.  
  78. partNode.runAction(.rotateBy(x: 0, y: .pi * 5.0, z: 0, duration: 3.0))
  79. }
  80. }
  81. }
  82. }
  83. }
Add Comment
Please, Sign In to add comment