Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // ViewController.swift
- // BambooCopter
- //
- // Created by MizushimaYusuke on 2017/11/20.
- // Copyright © 2017 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
- // 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()
- }
- func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
- renderer.autoenablesDefaultLighting = true
- let fan = SCNBox(width: 0.04, height: 0.001, length: 0.005, chamferRadius: 0.0001)
- fan.firstMaterial?.diffuse.contents = UIColor.green
- let fanNode = SCNNode(geometry: fan)
- fanNode.position = SCNVector3(0, 0.02, 0)
- let bar = SCNCylinder(radius: 0.0005, height: 0.04)
- bar.firstMaterial?.diffuse.contents = UIColor.brown
- let barNode = SCNNode(geometry: bar)
- let copter = SCNNode()
- copter.addChildNode(fanNode)
- copter.addChildNode(barNode)
- node.addChildNode(copter)
- copter.runAction(.repeatForever(.rotateBy(x: 0, y: .pi, z: 0, duration: 0.25)))
- copter.runAction(.sequence([
- .moveBy(x: 0, y: 0.05, z: 0, duration: 1.0),
- .moveBy(x: 0, y: -0.05, z: 0, duration: 1.0),
- ]))
- }
- 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 {
- sceneView.session.add(anchor: ARAnchor(transform: hit.worldTransform))
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment