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 sceneView.debugOptions = [ARSCNDebugOptions.showWorldOrigin, ARSCNDebugOptions.showFeaturePoints] let tiklamaYakala = UITapGestureRecognizer(target: self, action: #selector(tiklandi)) sceneView.addGestureRecognizer(tiklamaYakala) } @objc func tiklandi(cevap:UITapGestureRecognizer) { let ekran = cevap.view as! ARSCNView let tiklamaBolgesi = cevap.location(in: ekran) let tiklamaTest = ekran.hitTest(tiklamaBolgesi, types: .existingPlaneUsingExtent) if tiklamaTest.isEmpty { print("Yüzeye tiklanmadı.") } else { print("Yüzeye tikladiniz") guard let tiklamaSonucu = tiklamaTest.first else { return} objeEkle(tiklamaSonucu:tiklamaSonucu) } } func objeEkle(tiklamaSonucu:ARHitTestResult) { let obje = SCNNode() obje.geometry = SCNPyramid(width: 0.1, height: 0.2, length: 0.1) obje.geometry?.firstMaterial?.diffuse.contents = UIColor.orange obje.position = SCNVector3(tiklamaSonucu.worldTransform.columns.3.x, tiklamaSonucu.worldTransform.columns.3.y+0.5, tiklamaSonucu.worldTransform.columns.3.z) obje.physicsBody = SCNPhysicsBody(type: .dynamic, shape: nil) sceneView.scene.rootNode.addChildNode(obje) } func zeminOlustur(anchor:ARPlaneAnchor) -> SCNNode{ let zeminDugum = SCNNode() zeminDugum.geometry = SCNPlane(width: CGFloat(anchor.extent.x), height: CGFloat(anchor.extent.z)) zeminDugum.geometry?.firstMaterial?.diffuse.contents = UIImage(named: "izgara.png") zeminDugum.position = SCNVector3(anchor.center.x,anchor.center.y,anchor.center.z) let doksanDerece = GLKMathDegreesToRadians(90) zeminDugum.eulerAngles = SCNVector3(doksanDerece,0,0) zeminDugum.geometry?.firstMaterial?.isDoubleSided = true zeminDugum.physicsBody = SCNPhysicsBody(type: .static, shape: nil) return zeminDugum } func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) { guard anchor is ARPlaneAnchor else { return } let yuzeyDugum = zeminOlustur(anchor: anchor as! ARPlaneAnchor) node.addChildNode(yuzeyDugum) print("Yüzey tespit edildi.") } func renderer(_ renderer: SCNSceneRenderer, didUpdate node: SCNNode, for anchor: ARAnchor) { guard anchor is ARPlaneAnchor else { return } node.enumerateChildNodes { (childNode, _) in childNode.removeFromParentNode() let yuzeyDugum = zeminOlustur(anchor: anchor as! ARPlaneAnchor) node.addChildNode(yuzeyDugum) } print("Yüzey tespiti güncellendi.") } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) // Create a session configuration let configuration = ARWorldTrackingConfiguration() configuration.planeDetection = .horizontal // 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() } }