import UIKit import SceneKit import ARKit class ViewController: UIViewController, ARSCNViewDelegate { @IBOutlet var sceneView: ARSCNView! @IBOutlet weak var switchDraw: UISwitch! 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.debugOptions = [ARSCNDebugOptions.showFeaturePoints, .showWorldOrigin] } func renderer(_ renderer: SCNSceneRenderer, willRenderScene scene: SCNScene, atTime time: TimeInterval) { guard let pov = sceneView.pointOfView else { return } let transform = pov.transform let rotation = SCNVector3(-transform.m31, -transform.m32, -transform.m33) let location = SCNVector3(transform.m41, transform.m42, transform.m43) let currentPosition = SCNVector3(rotation.x + location.x, rotation.y + location.y, rotation.z + location.z) DispatchQueue.main.async { if self.switchDraw.isOn { let drawNode = SCNNode() drawNode.geometry = SCNSphere(radius: 0.01) drawNode.geometry?.firstMaterial?.diffuse.contents = UIColor.green drawNode.position = currentPosition self.sceneView.scene.rootNode.addChildNode(drawNode) } else { let point = SCNNode() point.name = "çizim noktası" point.geometry = SCNSphere(radius: 0.005) point.position = currentPosition point.geometry?.firstMaterial?.diffuse.contents = UIColor.red self.sceneView.scene.rootNode.enumerateChildNodes( {(node, _) in if node.name == "çizim noktası" { node.removeFromParentNode() } }) self.sceneView.scene.rootNode.addChildNode(point) } } } 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() } }