-
Notifications
You must be signed in to change notification settings - Fork 0
/
ViewCotroller.swift
101 lines (72 loc) · 2.86 KB
/
ViewCotroller.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import UIKit
import SceneKit
import ARKit
class ViewController: UIViewController, ARSCNViewDelegate {
let tapLoc : CGPoint = CGPointZero
@IBOutlet var sceneView: ARSCNView!
override func viewDidLoad() {
super.viewDidLoad()
sceneView.showsStatistics = true
sceneView.delegate = self
sceneView.scene = SCNScene()
sceneView.debugOptions = .showWireframe
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let configuration = ARWorldTrackingConfiguration()
configuration.planeDetection = .horizontal
sceneView.session.run(configuration)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
sceneView.session.pause()
}
@IBAction func tapSceneView(_ sender: UITapGestureRecognizer) {
let tapLoc = sender.location(in: sceneView)
let hitTestResult = sceneView.hitTest(tapLoc, types: .existingPlaneUsingExtent)
if let result = hitTestResult.first {
//赤か青の箱がランダムで落ちてくる
var x : Int = arc4random()%2
switch x{
case 0:
let boxNode = RedBoxNode(hitTestResult: result)
sceneView.scene.rootNode.addChildNode(boxNode)
case 1:
let boxNode = BlueBoxNode(hitTestResult: result)
sceneView.scene.rootNode.addChildNode(boxNode)
default:
break
}
}
// 繰り返し落ちてくる
Timer.scheduledTimer(
timeInterval: 1,
target: self,
selecter: #selecter(self.setupbox),
userInfo: nil,
repeats: true
)
}
@objc func setupbox(){
let tapLoc = sender.location(in: sceneView)
let hitTestResult = sceneView.hitTest(tapLoc, types: .existingPlaneUsingExtent)
if let result = hitTestResult.first {
var x : Int = arc4random()%2
switch x{
case 0:
let boxNode = RedBoxNode(hitTestResult: result!)
sceneView.scene.rootNode.addChildNode(boxNode)
case 1:
let boxNode = BlueBoxNode(hitTestResult: result!)
sceneView.scene.rootNode.addChildNode(boxNode)
default:
break
}
}
func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
guard let planeAnchor = anchor as? ARPlaneAnchor else {
return
}
node.addChildNode(Plane1_Node(anchor: planeAnchor))
node.addChildNode(Plane2_Node(anchor: planeAnchor))
}