<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Ranadhir Dey on Medium]]></title>
        <description><![CDATA[Stories by Ranadhir Dey on Medium]]></description>
        <link>https://medium.com/@ranadhir.dey82?source=rss-1824678624c------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*diNnangkfqzdvOwl5K1-Og@2x.jpeg</url>
            <title>Stories by Ranadhir Dey on Medium</title>
            <link>https://medium.com/@ranadhir.dey82?source=rss-1824678624c------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 00:24:58 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ranadhir.dey82/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How to get started with augmented reality in Swift by decorating your home]]></title>
            <link>https://medium.com/free-code-camp/how-to-get-started-with-augmented-reality-in-swift-by-decorating-your-home-85671482df3c?source=rss-1824678624c------2</link>
            <guid isPermaLink="false">https://medium.com/p/85671482df3c</guid>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[technology]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[augmented-reality]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Ranadhir Dey]]></dc:creator>
            <pubDate>Wed, 08 Aug 2018 19:03:08 GMT</pubDate>
            <atom:updated>2018-08-08T19:03:24.726Z</atom:updated>
            <content:encoded><![CDATA[<p>If you’ve read my previous post, you already have a beautiful AR floor in your dining room. That was the first thing we built while learning the <a href="https://medium.freecodecamp.org/how-to-get-started-with-ar-in-swift-the-easy-way-7399fe1c82f5">basics of AR</a>. And now, it’s time to decorate the room with some cool virtual furniture. At the end of this tutorial, you will have a dining room just like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/404/1*ANkvfr-d7BmwxS_AxGD6zw.gif" /></figure><h3>Naming SCNNodes</h3><p>Let’s get our hands dirty. Fire up Xcode, and open the <a href="https://github.com/ranadhirdey/Home-Decor">last project</a> where we decorated our floor. Before viewDidLoad, create a constant floorNodeName.</p><pre>let floorNodeName = &quot;FloorNode&quot;</pre><p>We are now going to set the name of the floor node to this constant so that we don’t mix up this node with other furniture nodes. Go to the createFloorNode method and name the floor node.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c1fb72c1f7a9f5d87e02c8e0885ddd31/href">https://medium.com/media/c1fb72c1f7a9f5d87e02c8e0885ddd31/href</a></iframe><p>In line 7 we have just named the floor node — everything else remains the same.</p><p>The plan is that, once launched, the app would recognise the floor first and then the user would see through the screen to determine where they want to place the furniture. They would tap on the location and a piece of furniture would sit just there. To achieve this, we need a correlation between the points on screen and the real-world locations. Thankfully, Apple has made this process quite simple.</p><h3>Gestures and HitTests</h3><p>An active AR session keeps on finding nearby objects/planes. Once a new object/plane is found, it places AR anchors on top of it. To find exactly on which anchors user has tapped, we need the help of <a href="https://developer.apple.com/documentation/arkit/arframe/2875718-hittest">HitTest</a>. HitTest works like this:</p><ul><li>a logical ray is shot from the point of touch to the anchors on the plane</li><li>all the anchors the ray passes through are stored in an array in the format of <a href="https://developer.apple.com/documentation/arkit/arhittestresult">HitTestResult</a>.</li></ul><p>Each HitTestResult contains the information of the real-world surface of an AR anchor. We will use this HitTestResult information to place our furniture.</p><p>Let’s create a method that adds the tap gesture to our sceneView to interact with the user. We’ll call this method from viewDidLoad.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fab530b18c3f79f3ed387434ccb3930e/href">https://medium.com/media/fab530b18c3f79f3ed387434ccb3930e/href</a></iframe><p>Now define the “tapped” method to get the location of the tap and place a furniture there. For now, we print to test if the HitTest is working fine.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/342e379646ce1870d07d4cb16365abf6/href">https://medium.com/media/342e379646ce1870d07d4cb16365abf6/href</a></iframe><p>In the first line, we are casting tap gesture’s view to ARSCNView. As we know that the tap will come from our sceneView itself, we force-unwrap it. Then we get the location on sceneView where user has tapped. Then a HitTest is performed to get all the HitTestResults from the tapped location to the real-world anchors. “.existingPlaneUsingExtent” gives the estimated size of the detected planes. Now, we check if the user actually has tapped on a detected plane or somewhere else, and we print accordingly.</p><p>Now run the app, wait until the world origin loads and the floor is detected. Then tap on the screen to check if we are hitting the planes correctly.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hPQSMPreK6Nv3bypYaaR_Q.png" /></figure><p>If you tap on the places where the tiles are located, it will print “Touched on the plane.” Touch anywhere else, and it will be “Not a plane.” If there’s no output in the console, you haven’t called the addTapGesture method from viewDidLoad (this happed to me!). Now that we have successfully detected the clicked position, it’s time to bring home some furniture.</p><h3>Importing 3D models to the project</h3><p>We need some 3D furniture models. I use <a href="https://www.turbosquid.com/">turbosquid</a>, which is a great repository of 3D models. You need to create a free account there to access their free models. I’ve downloaded a 3D <a href="https://www.turbosquid.com/3d-models/3d-small-dining-table-1161153">table</a> for now (in the GitHub repo — I’ll add some more furniture as well).</p><p><a href="https://developer.apple.com/documentation/scenekit/scnscenesource?changes=_4">Apple advises</a> you to use collada-formatted(.dae) models in SceneKit. I have used other kinds of models as well in the past, and I have faced problems in many cases. Mostly, if there is a glass, SceneKit tends to make it solid. So, let’s find 3D models with a .dae extension.</p><p>Now add an asset folder under the “Home Decor” group.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uZdqGuSoLDUxBwrXlOYxUQ.png" /></figure><p>Press next, and in the save dialog, save it as furnitures.<strong>scnassets </strong>and not xcassets.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xtrw-mhR3k5NKcfRtOUoZw.png" /></figure><h3>Working with Xcode SceneKit Editor</h3><p>All scene files or models that will be used by SceneKit should reside in a scnassets folder. Now drag and drop the table we just downloaded to the scnassets folder and rename it as “Table”. Click on the Table.dae file (if not already opened) to open it in SceneKit Editor. On the bottom left corner there is a button (marked with a red ellipse below) to open Scene Graph View. Click on it and the editor should appear like below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qffmqaKYDb8vMjRCiqB-8g.png" /></figure><p>In the Scene Graph there are 3 nodes: a Camera, a light source (Lamp) and the table (Small_table). ARKit has its default camera, so we don’t need this camera anymore. As we will be using sceneView’s default lighting, we don’t need the Lamp either. Remove both of them.</p><p>Let’s rename “Small_table” to “Table”, same as the file name. Now the scene graph will look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FSabN1vH7ZexHcv6Wmscdw.png" /></figure><p>There is something called point of view (marked with a red ellipse above) in the editor which determines how the object will look from a certain point of view. By default, it’s set to Perspective, but we will be seeing the table from the front. So change the point of view to Front.</p><p>Oops — it looks like we can see only the top view. Clearly, this isn’t how we would like to see it. We want to see the table as it was shown in the perspective view. Let’s fix this.</p><p>Select the Table node, open the Utilities tab (right-top corner button in Xcode) and click on the node inspector(the cube icon). You should see the below window.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vt6XWmh6fsRBmBIbe1Qgaw.png" /></figure><p>I’ve numbered the steps for your reference. To view the complete table from the front, we need to rotate the table on its X axis. If you can recollect, we did same kind of thing in the <a href="https://medium.freecodecamp.org/how-to-get-started-with-ar-in-swift-the-easy-way-7399fe1c82f5">first post</a> where we rotated the capsule on its Z axis by changing its Euler angle.</p><p>If you see the Transforms matrix, the Euler angle for X is already specified to 90 degree radians, which is causing the table to be rotated incorrectly. Make it zero and the rotation will be fixed. But the positioning has a vector of (-0.35,0.348,0). We will make it (0,0,0) to place the table exactly where the user will tap. Now the Transform will look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/522/1*ZnK7C1ACHG-roVzMnNCWsg.png" /></figure><p>Transform matrix editing is kind of a trial and error task. You might need to go through quite a number of iterations before you reach the exact position.</p><p>Now, we change the .dae model to a SceneKit scene file (.scn) which will made it much more efficient for SceneKit to handle. Go to Editor&gt;Convert to SceneKit scene file(.scn).</p><p>And…we are done with the 3D object editing. Head back to the ViewController file. First, since we have removed the light source of the table, we should turn on the default lighting of the scene view in viewDidLoad.</p><p>sceneView.autoenablesDefaultLighting = true</p><h3>Positioning 3D objects</h3><p>Then we create a method to add the table to the scene. It will accept a HitResult as a parameter and place the table based on the position of the HitResult.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b99b7d9db109bb0b8652d9f6b377495e/href">https://medium.com/media/b99b7d9db109bb0b8652d9f6b377495e/href</a></iframe><p>Bear with me — this is the last method which needs some explanation!</p><ol><li>A constant is declared to know what furniture scene needs to be added. We will change it to a variable and declare it on top of the viewcontroller when we have more furniture.</li><li>Then we create a scene from the selected furniture file.</li><li>A furniture node is created from the child node of the furniture scene’s root node with the name of the furniture. As we named the first node the same as the furniture name, it doesn’t need to traverse any further. Hence the recursive option is set to false.</li><li>HitResult’s <a href="https://developer.apple.com/documentation/arkit/arhittestresult/2867907-worldtransform">worldTransform</a> property holds the co-relation transform matrix between the real-world position and the scene anchor/node position. And the 3rd column of the transform matrix holds the position information.</li><li>Now that we have successfully extracted the world coordinate of the tapped location, our job is now just to place the furniture node on that exact same coordinate.</li><li>Then we add the node to the root node of the scene. And thats it!</li></ol><p>Now we just need to call the method whenever the user taps on the screen. Let’s modify the tapped method to accommodate this change.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7c62e774e25ca4f6f9ac5c1cefd84ebb/href">https://medium.com/media/7c62e774e25ca4f6f9ac5c1cefd84ebb/href</a></iframe><p>Here, if we find a plane, we are just calling the method to add the furniture. As HitTest holds all the positions the ray passes through, we are considering the top most result. Let’s run the app, wait until the floor has tiles on it, and then tap on the screen to place a table. And voilà! You have a new table on your floor. Watch your step :)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/750/1*HMeqtcy7IoAzqkiwRoPBkg.png" /></figure><p>I have downloaded some more furniture and have added it into the repo. I’ve also added pinch and rotate gestures to resize and rotate objects. The complete source code will give you an appearance like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/746/1*ycF6WrJkcT0SUg45pM-reg.jpeg" /></figure><p>You can download the full source code from <a href="https://github.com/ranadhirdey/Home-Decor-With-Furniture">GitHub</a>.</p><p>Hope you have enjoyed reading the post as much as I did writing it :)</p><p>See you at the next post. Happy reading!!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=85671482df3c" width="1" height="1" alt=""><hr><p><a href="https://medium.com/free-code-camp/how-to-get-started-with-augmented-reality-in-swift-by-decorating-your-home-85671482df3c">How to get started with augmented reality in Swift by decorating your home</a> was originally published in <a href="https://medium.com/free-code-camp">We’ve moved to freeCodeCamp.org/news</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to get started with augmented reality in Swift, the easy way]]></title>
            <link>https://medium.com/free-code-camp/how-to-get-started-with-ar-in-swift-the-easy-way-7399fe1c82f5?source=rss-1824678624c------2</link>
            <guid isPermaLink="false">https://medium.com/p/7399fe1c82f5</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[technology]]></category>
            <category><![CDATA[augmented-reality]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[swift]]></category>
            <dc:creator><![CDATA[Ranadhir Dey]]></dc:creator>
            <pubDate>Mon, 16 Jul 2018 22:09:53 GMT</pubDate>
            <atom:updated>2018-07-16T22:10:12.328Z</atom:updated>
            <content:encoded><![CDATA[<p>If you look around, this is the golden era of technology. Every keynote adds something new to the existing stack of technologies. It’s exciting to see how these emerging technologies have enhanced the boundaries of our imagination. As a developer, we must be proud that we are the firsthand users of these technologies.</p><p>But every new technology comes with quite a steep learning curve. You just can’t watch a keynote or a video on Youtube and start developing an app. But the good news is that, with AR in Swift, it’s remarkably easy to work with basic AR apps. Apple has done most of the heavy lifting for you. Follow along and you’ll see how easy it can be.</p><h3><strong>Let’s dig in…</strong></h3><p>In this tutorial, we will learn the necessary tools and techniques of AR in Swift that will allow us to create an app that decorates your floor with some cool floor tiles and wooden textures. The finished app will look something like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*PpADDkOgV3voNF_zTYAQtQ.gif" /></figure><p>Let’s start by creating a <strong>single view</strong> application in Xcode and name it Home Decor.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2Kug9HeETVkkx7PZ5eB_bw.png" /></figure><h3><strong>Adding camera permissions</strong></h3><p>Now the very first thing we will do is to navigate to the info.plist file and enable camera usage. Camera capability is the first thing you need for an AR app. Find the Camera Usage Description key, like the below image, and give it a suitable message. This message will show up in the very first launch of the app while asking for the camera permissions from the user.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ro1_pGg6XJKYaN3pO6BkWA.png" /></figure><h3><strong>Adding ARKit Capabilities to the app</strong></h3><p>Go to Main.storyboard. Drag and drop an ARKit SceneKit View on the ViewController and pin the ARSCNView to the edges of the ViewController.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7uTIw_j2_-zclJS9AEBC3A.png" /></figure><p>Create a IBOutlet to the ViewController class, and name it sceneView. As soon as you do that, an error stating <strong>undeclared ARSCNView</strong><em>,</em> will popup, as our view controller doesn’t recognise anything of type ARSCNView. To resolve this, and to use other ARKit features, we need to import ARKit into the view controller.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*g6Pglip2Wc14wyhP9w2mig.png" /></figure><p>Now move from storyboard to the view controller.swift file. Declare a property of type ARWorldTrackingConfiguration before the viewDidLoad() method and name it config. And our view controller will look like this (I’ve removed the didReceiveMemoryWarning method):</p><pre>import UIKit<br>import ARKit</pre><pre>class ViewController: UIViewController {</pre><pre>@IBOutlet weak var sceneView: ARSCNView!<br>let config = ARWorldTrackingConfiguration()</pre><pre>override func viewDidLoad() {<br>super.viewDidLoad()<br>}</pre><h3><strong>Allow debugging</strong></h3><p>This config variable will determine the configurations of the scene session. We will see it’s usage later in the section. Now, in the viewDidLoad method after super.viewDidLoad(), add the following:</p><pre>sceneView.debugOptions = [ARSCNDebugOptions.showFeaturePoints, ARSCNDebugOptions.showWorldOrigin]</pre><p>Here we are enabling debug options for our sceneView, which is nothing but the camera view with the capabilities of AR framework. ARSCNDebugOptions.showWorldOrigin will display world origin on the screen. This will help us find the reference point of all other positions. ARSCNDebugOptions.showFeaturePoints will display all the points on the screen which the AR camera has recognised in the surroundings.</p><p>Now to start the AR session, we need to run a session in our sceneView with the configurations mentioned in the config variable. Just below sceneView.debugOptions line, write:</p><pre>sceneView.session.run(config)</pre><p>Now run the app on your device (not on a simulator, as it doesn’t have the camera). The alert asking for camera permissiosn with the message you wrote will show up, and you need allow it. Wait for a bit while it loads the world origin.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/990/1*Gf3uExB7i8IDfN-s3_tcLw.png" /></figure><p>If you are here, you have already have an AR app running. Congratulations!</p><h3><strong>How AR Axes work</strong></h3><p>The red bar or X axis is used to position objects left or right of the world origin. The green bar or Y axis is used to position objects to the top or bottom of the world origin. And the blue bar or Z axis is used to determine how close or far an object will be placed from the world origin.</p><p>A positive value of X will position an object to the right of the world origin, and negative will place it on the left. Positive for Y will place it on top and negative will place it on the bottom of the world origin. Positive for Z will place it nearer, and negative will place it farther from the world origin.</p><h3><strong>Adding a virtual object</strong></h3><p>Let’s add some virtual objects to the scene. 3D capsule would be a good choice. Declare a capsuleNode of type <a href="https://developer.apple.com/documentation/scenekit/scnnode?changes=_8">SCNNode</a> and give it a geometry of <a href="https://developer.apple.com/documentation/scenekit/scncapsule?changes=_5">capsule</a>. Give it a height of 0.1 meter, and radius of 0.03 meters.</p><pre>let capsuleNode = SCNNode(geometry: SCNCapsule(capRadius: 0.03, height: 0.1</pre><p>Now position it 0.1 meter left of the world origin, 0.1 meter above the world origin, and 0.1 meter away from the world origin:</p><pre>capsuleNode.position = SCNVector3(0.1, 0.1, -0.1)</pre><p>Now, add the node to scene:</p><pre>sceneView.scene.rootNode.addChildNode(capsuleNode)</pre><p>The sceneView contains a scene which is responsible for holding all the 3D objects in SCNNode format that will form the 3D scene. We are adding the capsule to the root node of the scene. Root node’s position is exactly aligned to the position of the world origin. That means its position is (0,0,0).</p><p>Currently, our viewDidLoad method looks like this:</p><pre>override func viewDidLoad() {</pre><pre>super.viewDidLoad()</pre><pre>sceneView.debugOptions = [ARSCNDebugOptions.showFeaturePoints, ARSCNDebugOptions.showWorldOrigin]</pre><pre>sceneView.session.run(config)</pre><pre>let capsuleNode = SCNNode(geometry: SCNCapsule(capRadius: 0.03, height: 0.1))</pre><pre>capsuleNode.position = SCNVector3(0.1, 0.1, -0.1)</pre><pre>sceneView.scene.rootNode.addChildNode(capsuleNode)</pre><pre>}</pre><p>Now run the app.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/750/1*aMftUO6OzWmcWaEsPcYHlw.png" /></figure><p>Cool! We have just positioned a virtual object in the real world. You can play with different positions and different <a href="https://developer.apple.com/documentation/scenekit/scngeometry">geometries</a> to explore more. Now let’s rotate the capsule 90 degree around the Z axis so that it lies flat on the X axis and changes its colour to blue.</p><h3><strong>Euler Angles</strong></h3><p>Euler Angles are responsible for a SCNNode’s display angle. We will see how to use it to rotate the capsule.</p><p>Every SCNGeometry can have materials added to it, which defines the appearance of the geometry. Materials have a diffuse property which, when set, spreads its content all over the geometry.</p><p>In viewDidLoad, add the below lines after you set the position of the capsule.</p><pre>capsuleNode.geometry?.firstMaterial?.diffuse.contents = UIColor.blue //1<br>capsuleNode.eulerAngles = SCNVector3(0,0,Double.pi/2)<br>//2</pre><p>Here, in the first line, we are setting the blue colour to the very first material of the node which will spread across the capsule and will make it look blue. In line 2, we are setting the Z Euler angle to 90 degree radians. Finally, our view loads and looks like this:</p><pre>override func viewDidLoad() {</pre><pre>super.viewDidLoad()</pre><pre>sceneView.debugOptions = [ARSCNDebugOptions.showFeaturePoints, ARSCNDebugOptions.showWorldOrigin]</pre><pre>sceneView.session.run(config)</pre><pre>let capsuleNode = SCNNode(geometry: SCNCapsule(capRadius: 0.03, height: 0.1))</pre><pre>capsuleNode.position = SCNVector3(0.1, 0.1, -0.1)</pre><pre>capsuleNode.geometry?.firstMaterial?.diffuse.contents = UIColor.blue //1</pre><pre>capsuleNode.eulerAngles = SCNVector3(0,0,Double.pi/2)//2</pre><pre>sceneView.scene.rootNode.addChildNode(capsuleNode)</pre><pre>}</pre><p>Now run the app.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/750/1*7hyDkNMaZbQP2TJWBeVyRg.png" /></figure><p>Great! A blue coloured sleeping capsule on the wall! You can even add textures as diffuse contents to make an object look more realistic. We will use that in the next section when we place the tiles’ textures on the floor.</p><p>Now that we have successfully placed virtual objects on the real world, it’s time to decorate our real floor with virtual floor-tiles. To achieve the floor effect, we will use a <a href="https://developer.apple.com/documentation/scenekit/scnplane">SCNPlane</a> geometry. SCNPlane doesn’t have any depth like other 3D geometries, which makes it a perfect fit for our app.</p><h3><strong>ARSCENEView Delegates</strong></h3><p>Before starting the floor detection, we will explore some delegate methods of our sceneView to understand what capabilities we are equipped with to interact with an ongoing AR session.</p><pre>func renderer(SCNSceneRenderer, didAdd: SCNNode, for: ARAnchor)</pre><p>Whenever we move or tilt our device with an AR session on in it, the ARKit tries to find different ARAnchors in the surroundings. An <a href="https://developer.apple.com/documentation/arkit/aranchor">ARAnchor</a> contains information about a real world position and orientation that can be used to place an object.</p><p>Once a different anchor is found, a new node gets added to the scene with the same information to accommodate this newly found anchor. This delegate method will inform us about that. We will be using it to find all the positions on the floor to place the tiles.</p><pre>func renderer(_ renderer: SCNSceneRenderer, didUpdate node: SCNNode, for anchor: ARAnchor)</pre><p>Most of the time, all the nodes that get added from the anchors belong to the same object. Let’s say you are moving around the floor and the device finds a number of anchors at different positions. It tries to add all the nodes for those anchors, as it thinks that all these anchors belong to different objects.</p><p>But ARKit eventually recognises that all of them belong to the same floor, so it updates the very first floor node by appending dimensions of other duplicate nodes. This delegate method will inform us about that.</p><pre>func renderer(SCNSceneRenderer, didRemove: SCNNode, for: ARAnchor)</pre><p>After updating the first unique node with dimensions of all other duplicate nodes, ARKit removes all the duplicate nodes and the delegate method notifies us. We will be using all of the above delegate methods in our app (and their purpose will become clearer).</p><h3><strong>Plane detection</strong></h3><p>Presently, our scene is trying to gather all anchors that it comes across, as that is the default behaviour. But since a floor is a horizontal surface, we are only interested in anchors that are on horizontal planes. So, go back to our viewDidLoad method and write the below code <strong>before </strong>running the session (that is before sceneView.session.run(config)) line.</p><pre>config.planeDetection = .horizontal</pre><p>In the viewDidLoad method, you can remove everything after sceneView.session.run(config) as that was for placing the capsule on screen and we don’t need that anymore. Since we will be using all the above mentioned delegate methods, we need to make our viewController a delegate of the sceneView. Before the closing brace of viewDidLoad() method, add the below line.</p><pre>sceneView.delegate = self</pre><p>You should get an error now, as our view controller still is not conforming the sceneView delegate. To implement this, let’s create an extension of the view controller at the end of the ViewController.swift file.</p><pre>extension ViewController:ARSCNViewDelegate{}</pre><p>The didAdd SCNNode delegate method will get fired every time a part of the floor is discovered and a new node gets added to the scene based on the anchor. Within this method, we will create a floor node and will add it as a child of the recently added node in the position of the anchor.</p><p><a href="https://developer.apple.com/documentation/arkit/aranchor">ARArchor</a> can be of four different types to solve four different purposes. Here we are only interested in ARPlaneAnchor which detects the horizontal or vertical planes.</p><h3><strong>Creating AR floor nodes</strong></h3><p>Let’s create a function that would receive an ARPlaneAnchor as a parameter, create a floor node at the anchor’s position, and return it.</p><pre>func createFloorNode(anchor:ARPlaneAnchor) -&gt;SCNNode{</pre><pre>let floorNode = SCNNode(geometry: SCNPlane(width: CGFloat(anchor.extent.x), height: CGFloat(anchor.extent.z))) //1</pre><pre>floorNode.position=SCNVector3(anchor.center.x,0,anchor.center.z)                                               //2</pre><pre>floorNode.geometry?.firstMaterial?.diffuse.contents = UIColor.blue                                             //3</pre><pre>floorNode.geometry?.firstMaterial?.isDoubleSided = true                                                        //4</pre><pre>floorNode.eulerAngles = SCNVector3(Double.pi/2,0,0)                                                    //5</pre><pre>return floorNode                                                                                               //6</pre><pre>}</pre><p>Let’s go through the function line by line and discuss it in more detail. Please follow each line’s description, as it’s the trickiest part.</p><p>1. We are creating a node with a geometry of SCNPlane which has the size of the anchor. ARPlaneAnchor’s extent holds the position information. The fact that the extent.z has been used as height and not extent.y, might be a little confusing. If you visualise that a 3D cube is placed on a floor and you want to make it flat along a 2D surface, you would change the y to zero and it would go flat. Now, to get the length of this 2D surface, you would consider the z, wouldn’t you? Our floor is flat, so we need a flat node not a cube.</p><p>2. We are setting the position of the node. As we don’t need any elevation, we make y zero.</p><p>3. Set the floor colour to blue.</p><p>4. The material colour will be displayed only on one side unless we specifically mention it is double-sided.</p><p>5. By default, the plane will be placed vertically. To make it horizontal, we need to rotate it by 90 degrees.</p><h3><strong>Implementing the delegate methods</strong></h3><p>Now, let’s implement the didAdd SCNNode delegate method.</p><pre>func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {</pre><pre>guard let planeAnchor = anchor as? ARPlaneAnchor else {return} //1</pre><pre>let planeNode = createFloorNode(anchor: planeAnchor) //2</pre><pre>node.addChildNode(planeNode) //3</pre><pre>}</pre><p>In line 1, we are checking if the anchor is a ARPlaneAnchor, since we would only deal with this type of anchor.</p><p>In line 2, a new node is getting created based on the anchor. In line 3, it’s getting added to the node.</p><p>Now in the didUpdate SCNNode delegate, we will delete all our floor nodes. We’ll do this because the dimensions of the current node have been changed and the old floor nodes won’t match. Then we will again add a fresh floor node to this updated node.</p><pre>func renderer(_ renderer: SCNSceneRenderer, didUpdate node: SCNNode, for anchor: ARAnchor) {</pre><pre>guard let planeAnchor = anchor as? ARPlaneAnchor else {return}</pre><pre>node.enumerateChildNodes { (node, _) in</pre><pre>node.removeFromParentNode()</pre><pre>}</pre><pre>let planeNode = createFloorNode(anchor: planeAnchor)</pre><pre>node.addChildNode(planeNode)</pre><pre>}</pre><p>In didRemove SCNNode delegate method, we want to clean out all our junk nodes in a civilized manner.</p><pre>func renderer(_ renderer: SCNSceneRenderer, didRemove node: SCNNode, for anchor: ARAnchor) {</pre><pre>guard let _ = anchor as? ARPlaneAnchor else {return}</pre><pre>node.enumerateChildNodes { (node, _) in</pre><pre>node.removeFromParentNode()</pre><pre>}</pre><pre>}</pre><p>Phew! Thats it! Run the app.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/750/1*oHsguBirlTY76XeL4MwdOA.png" /></figure><h3><strong>Adding the tile effect</strong></h3><p>Wait, what? A blue floor? No, we are not completely done yet. Just a small change and we will have a stunning floor!</p><p>To change the blue floor to tiles, we need a texture. Let’s google for a floor tile texture. I searched for “wooden floor texture” and found some beautiful texture images. Save any of them on your Mac and drag it to the Assets.xcassets.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9m_HcxyzHfKJFLBdYQAr6g.png" /></figure><p>I named it WoodenFloorTile. You can name it whatever you want. Back to the ViewController.swift file again. In the createFloorNode function, instead of setting UIColor.blue as diffuse content, make it an UIImage with the name you have given to the image in the asset folder.</p><pre>floorNode.geometry?.firstMaterial?.diffuse.contents = UIImage(named: &quot;WoodenFloorTile&quot;)</pre><p>Now run the app, and wait until the world origin loads. Once the floor is detected, move around to update the node information.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/750/1*lxtT8vYWJOW_4NepnNXNag.png" /></figure><p>Wow, You really have a gorgeous floor! You can download multiple textures and place them in a listView. This allows you to change the floor based on selected texture, as it was shown in the first part.</p><p><a href="https://github.com/ranadhirdey/Home-Decor">Download the complete project from GitHub here.</a></p><p>Now that you have a nice floor, you must be missing some nice furnitures to give your room a great look! We will work on that later on.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7399fe1c82f5" width="1" height="1" alt=""><hr><p><a href="https://medium.com/free-code-camp/how-to-get-started-with-ar-in-swift-the-easy-way-7399fe1c82f5">How to get started with augmented reality in Swift, the easy way</a> was originally published in <a href="https://medium.com/free-code-camp">We’ve moved to freeCodeCamp.org/news</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[iOS AR, The easy way — Home Decor]]></title>
            <link>https://medium.com/@ranadhir.dey82/ios-ar-the-easy-way-home-decor-fb655690319a?source=rss-1824678624c------2</link>
            <guid isPermaLink="false">https://medium.com/p/fb655690319a</guid>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[ios-app-development]]></category>
            <category><![CDATA[augmented-reality]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Ranadhir Dey]]></dc:creator>
            <pubDate>Wed, 27 Jun 2018 15:26:43 GMT</pubDate>
            <atom:updated>2018-08-09T09:44:36.610Z</atom:updated>
            <content:encoded><![CDATA[<p>This story has been moved to <a href="https://medium.freecodecamp.org/how-to-get-started-with-augmented-reality-in-swift-by-decorating-your-home-85671482df3c">freecodecamp</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fb655690319a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[AR, The easy way — Floor Decor]]></title>
            <link>https://medium.com/@ranadhir.dey82/ar-the-easy-way-floor-decor-39d3b5cfb639?source=rss-1824678624c------2</link>
            <guid isPermaLink="false">https://medium.com/p/39d3b5cfb639</guid>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[augmented-reality]]></category>
            <category><![CDATA[ios-app-development]]></category>
            <category><![CDATA[ar]]></category>
            <category><![CDATA[ios]]></category>
            <dc:creator><![CDATA[Ranadhir Dey]]></dc:creator>
            <pubDate>Fri, 15 Jun 2018 08:32:27 GMT</pubDate>
            <atom:updated>2018-08-09T09:46:02.772Z</atom:updated>
            <content:encoded><![CDATA[<p>This story has been moved to <a href="https://medium.freecodecamp.org/how-to-get-started-with-ar-in-swift-the-easy-way-7399fe1c82f5">freecodecamp</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=39d3b5cfb639" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[iOS AR, The easy way]]></title>
            <link>https://medium.com/@ranadhir.dey82/ios-ar-the-easy-way-be9fd28bd828?source=rss-1824678624c------2</link>
            <guid isPermaLink="false">https://medium.com/p/be9fd28bd828</guid>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[ar]]></category>
            <category><![CDATA[ios-app-development]]></category>
            <category><![CDATA[augmented-reality]]></category>
            <dc:creator><![CDATA[Ranadhir Dey]]></dc:creator>
            <pubDate>Fri, 15 Jun 2018 08:30:53 GMT</pubDate>
            <atom:updated>2018-08-09T09:46:56.532Z</atom:updated>
            <content:encoded><![CDATA[<p>This story has been moved to <a href="https://medium.freecodecamp.org/how-to-get-started-with-ar-in-swift-the-easy-way-7399fe1c82f5">freecodecamp</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=be9fd28bd828" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>