Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // ViewController.swift
- // FlipSignBoard
- //
- // Created by MizushimaYusuke on 2018/01/08.
- // Copyright © 2018 MizushimaYusuke. All rights reserved.
- //
- import UIKit
- import SceneKit
- import ARKit
- class ViewController: UIViewController, ARSCNViewDelegate {
- @IBOutlet var sceneView: ARSCNView!
- override func viewDidLoad() {
- super.viewDidLoad()
- // Set the view's delegate
- sceneView.delegate = self
- // Show statistics such as fps and timing information
- sceneView.showsStatistics = true
- sceneView.autoenablesDefaultLighting = true
- // Create a new scene
- let scene = SCNScene()
- // Set the scene to the view
- sceneView.scene = scene
- }
- override func viewWillAppear(_ animated: Bool) {
- super.viewWillAppear(animated)
- // Create a session configuration
- let configuration = ARWorldTrackingConfiguration()
- // Run the view's session
- sceneView.session.run(configuration)
- }
- override func viewWillDisappear(_ animated: Bool) {
- super.viewWillDisappear(animated)
- // Pause the view's session
- sceneView.session.pause()
- }
- override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
- if let p = touches.first?.location(in: sceneView) {
- if let hit = sceneView.hitTest(p, types: .featurePoint).first {
- let position = SCNVector3(hit.worldTransform.columns.3.x, hit.worldTransform.columns.3.y + 0.02, hit.worldTransform.columns.3.z)
- let signBoard = SCNNode()
- signBoard.position = position
- sceneView.scene.rootNode.addChildNode(signBoard)
- stride(from: 0, to: 100, by: 5).forEach { x in
- UIGraphicsBeginImageContextWithOptions(CGSize(width: 5, height: 50), true, 0)
- let paragraphStyle = NSMutableParagraphStyle()
- paragraphStyle.alignment = .center
- let attrs = [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 30, weight: UIFont.Weight.black),
- NSAttributedStringKey.paragraphStyle: paragraphStyle,
- NSAttributedStringKey.foregroundColor: UIColor.white]
- "Hello".draw(with: CGRect(x: -CGFloat(x) + 10, y: 10, width: 0, height: 0), options: .usesLineFragmentOrigin, attributes: attrs, context: nil)
- let img = UIGraphicsGetImageFromCurrentImageContext()!
- UIGraphicsEndImageContext()
- let part = SCNBox(width: 0.004, height: 0.04, length: 0.0001, chamferRadius: 0)
- part.firstMaterial?.diffuse.contents = img
- let partNode = SCNNode(geometry: part)
- partNode.position = SCNVector3(CGFloat(x) * 0.0010, 0, 0)
- signBoard.addChildNode(partNode)
- partNode.runAction(.rotateBy(x: 0, y: .pi * 5.0, z: 0, duration: 3.0))
- }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment