{"id":2149,"date":"2021-07-09T21:00:46","date_gmt":"2021-07-09T21:00:46","guid":{"rendered":"https:\/\/sezeromer.com\/?p=2149"},"modified":"2023-03-04T13:46:44","modified_gmt":"2023-03-04T10:46:44","slug":"arkit-ile-cizim-islemleri","status":"publish","type":"post","link":"https:\/\/sezeromer.com\/en\/arkit-ile-cizim-islemleri\/","title":{"rendered":"Drawing with ARKit"},"content":{"rendered":"<p>Hello friends, in this article, we will talk about how to draw with Swift ARKit. You can find my previous post <a href=\"https:\/\/sezeromer.com\/arkit-ile-nodeun-acisini-degistirmek\/\">here<\/a>.<\/p>\n<p>We need to create a project and do the same things we did in our previous articles. After adding SceneKit in the Design section, let&#8217;s put a button to start drawing and the drawing process will start after the user presses this button. After we&#8217;re done with the design, we need to bind it to the ViewController.<\/p>\n<p>Then, every time the ARKit scene is rendered, we need to process it here. We need to bind its delegate so that we can catch it when it renders. I&#8217;ve been providing this with an Extension. Then I need to get the coordinates of the midpoint of the screen. After obtaining this information, if the user presses the button, I will put a dot every time he renders to the screen. I will put a pink pointer to show the middle point of the screen to the user if the button is not pressed. When the user presses the button, this pointer will disappear and drawing will begin.<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;swift&quot;,&quot;mime&quot;:&quot;text\/x-swift&quot;,&quot;theme&quot;:&quot;default&quot;,&quot;lineNumbers&quot;:true,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:true,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">\/\/\r\n\/\/  ViewController.swift\r\n\/\/  arkit-drawing\r\n\/\/\r\n\/\/  Created by Omer Sezer on 9.07.2021.\r\n\/\/\r\n\r\nimport UIKit\r\nimport ARKit\r\n\r\nclass ViewController: UIViewController {\r\n\r\n    @IBOutlet weak var sceneView: ARSCNView!\r\n    @IBOutlet weak var btnDraw: UIButton!\r\n    let configuration = ARWorldTrackingConfiguration()\r\n    \r\n    override func viewDidLoad() {\r\n        super.viewDidLoad()\r\n        setUI()\r\n    }\r\n\r\n    func setUI() {\r\n        \/\/ MARK: sceneView\r\n        sceneView.debugOptions = [.showWorldOrigin, .showFeaturePoints]\r\n        sceneView.showsStatistics = true\r\n        sceneView.delegate = self\r\n        sceneView.session.run(configuration)\r\n    }\r\n}\r\n\r\nextension ViewController: ARSCNViewDelegate {\r\n    func renderer(_ renderer: SCNSceneRenderer, willRenderScene scene: SCNScene, atTime time: TimeInterval) {\r\n        guard let pointOfView = sceneView.pointOfView else { return }\r\n        let transform = pointOfView.transform\r\n        let orientation = SCNVector3(-transform.m31, -transform.m32, -transform.m33)\r\n        let location = SCNVector3(transform.m41, transform.m42, transform.m43)\r\n        let currentPositionOfCamera = orientation + location\r\n        DispatchQueue.main.async {\r\n            if self.btnDraw.isHighlighted {\r\n                let nodeSphere = SCNNode(geometry: SCNSphere(radius: 0.02))\r\n                nodeSphere.position = currentPositionOfCamera\r\n                self.sceneView.scene.rootNode.addChildNode(nodeSphere)\r\n                nodeSphere.geometry?.firstMaterial?.diffuse.contents = UIColor.green\r\n            } else {\r\n                let pointer = SCNNode(geometry: SCNSphere(radius: 0.01))\r\n                pointer.name = \"pointer\"\r\n                pointer.position = currentPositionOfCamera\r\n                self.sceneView.scene.rootNode.enumerateChildNodes { node, _ in\r\n                    if node.name == \"pointer\" {\r\n                        node.removeFromParentNode()\r\n                        \r\n                    }\r\n                }\r\n                self.sceneView.scene.rootNode.addChildNode(pointer)\r\n                pointer.geometry?.firstMaterial?.diffuse.contents = UIColor.systemPink\r\n            }\r\n        }\r\n    }\r\n}\r\n\r\nfunc +(left: SCNVector3, right: SCNVector3) -&gt; SCNVector3 {\r\n    return SCNVector3(x: left.x + right.x, y: left.y + right.y, z: left.z + right.z)\r\n}<\/pre>\n<\/div>\n<p>The screen output of the project is as follows.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-2151\" src=\"https:\/\/sezeromer.com\/wp-content\/uploads\/2021\/07\/ezgif-2-802977a580d7-473x1024.gif\" alt=\"\" width=\"473\" height=\"1024\" srcset=\"https:\/\/sezeromer.com\/wp-content\/uploads\/2021\/07\/ezgif-2-802977a580d7-473x1024.gif 473w, https:\/\/sezeromer.com\/wp-content\/uploads\/2021\/07\/ezgif-2-802977a580d7-139x300.gif 139w, https:\/\/sezeromer.com\/wp-content\/uploads\/2021\/07\/ezgif-2-802977a580d7-500x1083.gif 500w\" sizes=\"(max-width: 473px) 100vw, 473px\" \/><\/p>\n<p>You can access the project <a href=\"https:\/\/github.com\/omersezer\/arkit-samples\">here<\/a>. If you have questions, you can reach us by sending an e-mail or comment. Good work.<\/p>\n<p>&nbsp;<\/p>","protected":false},"excerpt":{"rendered":"<p>Hello friends, in this article, we will talk about how to draw with Swift ARKit. You can find my previous post here. We need to create a project and do the same things we did in our previous articles. After adding SceneKit in the Design section, let&#8217;s put a button to start drawing and the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2118,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[824,327,706],"tags":[813,812,818,822,8,7,819,440,328,707],"class_list":["post-2149","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-arkit","category-swift","category-swiftui","tag-ar","tag-arkit","tag-augmented","tag-augmented-reality","tag-omer","tag-omer-sezer","tag-reality","tag-sezer","tag-swift","tag-swiftui"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/posts\/2149"}],"collection":[{"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/comments?post=2149"}],"version-history":[{"count":4,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/posts\/2149\/revisions"}],"predecessor-version":[{"id":2675,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/posts\/2149\/revisions\/2675"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/media\/2118"}],"wp:attachment":[{"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/media?parent=2149"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/categories?post=2149"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/tags?post=2149"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}