<?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 Daniel Fortes on Medium]]></title>
        <description><![CDATA[Stories by Daniel Fortes on Medium]]></description>
        <link>https://medium.com/@daniel.mbfm?source=rss-4e878d7d9e45------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*3dj_63K5FuyWkiFftOIIMg.png</url>
            <title>Stories by Daniel Fortes on Medium</title>
            <link>https://medium.com/@daniel.mbfm?source=rss-4e878d7d9e45------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 03 Dec 2026 22:36:12 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@daniel.mbfm/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[Create a Google Maps Live View-like application with Unity3D in under 10 minutes!]]></title>
            <link>https://medium.com/@daniel.mbfm/create-a-google-maps-live-view-like-application-in-unity3d-in-10-minutes-3265d6fc6b5d?source=rss-4e878d7d9e45------2</link>
            <guid isPermaLink="false">https://medium.com/p/3265d6fc6b5d</guid>
            <category><![CDATA[unity]]></category>
            <category><![CDATA[unity3d]]></category>
            <category><![CDATA[metaverse]]></category>
            <category><![CDATA[ar]]></category>
            <category><![CDATA[augmented-reality]]></category>
            <dc:creator><![CDATA[Daniel Fortes]]></dc:creator>
            <pubDate>Wed, 12 Jan 2022 22:26:35 GMT</pubDate>
            <atom:updated>2022-01-12T23:54:43.990Z</atom:updated>
            <content:encoded><![CDATA[<p>Location-based augmented-reality experiences with routes and navigation in Unity using the “Unity AR+GPS Location” plugin.</p><p>A few years ago Google started rolling the experimental augmented-reality view for its <strong>Google Maps</strong> app. At first it was available in a few cities around the world, but now it is available everywhere and it is called <strong>Live View </strong>for <strong>Google Maps</strong>. Apple soon followed suite and introduced the <strong>3D View</strong> for their <strong>Maps</strong> app which provides a similar experience — but only works on a few selected cities.</p><p>The <a href="https://assetstore.unity.com/packages/tools/integration/ar-gps-location-134882"><strong>Unity AR+GPS Location</strong></a> plugin is an asset for the Unity3D engine which allows you to easily create <strong>location-based</strong> AR experiences in Unity. With it you can assign geographical coordinates to <em>Game Objects </em>inside the Unity editor, and, after you deploy the application to your device, go to the real-world location, fire up the app, and see your object being placed right on the spot.</p><p>In the latest release (v3.6.0) of our <a href="https://assetstore.unity.com/packages/tools/integration/ar-gps-location-134882"><strong>Unity AR+GPS Location</strong></a> plugin we are excited bring an exciting feature inspired by the Google Maps <strong>Live View </strong>and powered by the <a href="https://docs.mapbox.com/help/glossary/directions-api/">Mapbox <strong>Directions API</strong></a>: the <strong>Routes and Navigation </strong>feature.</p><p>With the new <strong>Routes and Navigation</strong> feature you will be able to quickly create applications that aid users in navigating through cities, parks, event venues and other places using <strong>augmented-reality</strong>.</p><p>Now I will show you the step-by-step guide on how to prototype such application in a few minutes!</p><h3>Let’s do it</h3><p>First open up a new project on the Unity3D editor (we will be using Unity 2020.3, but you can use Unity 2019.4 or newer). Now let’s open up the <strong>Package Manager</strong> window and install all the packages we need for our application.</p><p>On the top-left of the window select <strong>Packages: Unity Registry</strong> and install the <strong>AR Foundation</strong> package. If you are building for Android, also install the <strong>AR Core XR Plugin</strong>, and if you are building for iOS, install the <strong>ARKit XR Plugin</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yd60MdrSNPYkEFb7wdwszw.png" /></figure><p>Again on the top-left of the Package Manager window select <strong>Packages: My Assets </strong>on the drop-down. This will display all the assets owned by you on the Unity Asset Store. Here you should install the <strong>AR+GPS Location</strong> asset.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5vW5DKXxY67pQuwc5X3xOg.png" /></figure><p>The next step is setting up the scene. Go ahead and create a new scene on your project. Just right-click on the <strong>Project</strong> window and select <em>Create-&gt;Scene</em>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MAOXhKXKmNaOFUyv6imXeA.png" /></figure><p>Open your new scene, right-click on the <strong>Hirarchy</strong> window, and then on <em>AR+GPS-&gt;Create Basic Scene Structure </em>to automatically create all the elements of a basic <strong>AR+GPS Location</strong> scene.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*blH2C981WxfDLEBn5qdZog.png" /></figure><p>Your scene should like the one in the image bellow. You can delete the <strong>GPS Stage Object</strong> since we won’t be using it for now.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XdsnJNRwoofv7qF2zsFxnA.png" /></figure><p>Next, right click again on the <strong>Hirarchy</strong> window and select <em>AR+GPS-&gt;Mapbox Route</em>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OW0kRPuhVic5Y2rfrUaOgw.png" /></figure><p>This will create a new Game Object named <strong>Mapbox Route</strong> with all the necessary components and basic configuration for building a AR Navigation experience. Still, there are a few things that we need to setup so that things will work properly.</p><p>First, paste your <strong>Mapbox Token</strong>. This it the mapbox API token you can get form your account page on <a href="http://mapbox.com/">mapbox.com</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*r2fwWA01wk7Sa6PZpvDvzA.png" /></figure><p>Next, go to the <strong>Route</strong> section of the <em>Mapbox Route</em> component&#39;s settings. This section is where you configure the start and ending points of your route.</p><p>We want the route to start at the user&#39;s location when he opens the app, so we will set the <strong>type</strong><em> </em>of the <strong>From </strong>property to <strong>User Location</strong>. For the destiation, we will set to <strong>Type</strong> of the <strong>To</strong> property to <strong>Query</strong>. This will show another property called <strong>Query — </strong>a string that will be used to search for a location using the Mapbox Search API. Here you should write an exact address that will be the first result of the Mapbox Search, e.g., <em>&quot;405 Lexington Ave, New York, NY 10174, United States&quot;. </em>You can use <a href="https://docs.mapbox.com/playground/geocoding/">this mapbox playground</a> to test <strong>Query</strong> strings before inputting them.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QDmVIKMVLjVzbaz5ItijjQ.png" /></figure><p>Of course the locations don&#39;t need to be hard-coded on your app. For instance you can create an UI to let users search and select locations themselves — but in this case you need to create UI and scripts, and for this simple example we will stick to hard-coded queries.</p><p>Now we are very near the finishing line!</p><p>To facilitate in-editor testing, we will add the <strong>ARLocationDevCamera </strong>prefab into the scene — this allows us visualize our results before building and deploying the application to our device. Go ahead and search for &quot;ARLocationDevCamera&quot; in the project&#39;s assets.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mhDzmam5BlPGYNVwdFbKaA.png" /></figure><p>Then drag the <strong>ARLocationDevCamera</strong> prefab to be a child of the <strong>AR Session Origin</strong> Game Object.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/540/1*uEnQ0aznsIkx_p5yyrIYYQ.png" /></figure><p>We will also add a <strong>Mock Location </strong>which is a geo-location we can use for in-editor testing. Create a new Location asset:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZFF3tWOJg9s08A-EFn33Fw.png" /></figure><p>And, on the inspector panel, set the Latitude and Longitude to the testing location you want. This should be near the Query string and around the location you will want to run the app in the real world.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aKqfQUOJZxXOWzJDGhrRYA.png" /></figure><p>Finally, select the <strong>AR Location Root</strong> game object on the <strong>Hierarchy</strong> window, and scroll to the<strong> AR Location Provider</strong> component in the <strong>Inspector</strong> panel. Drag the location you just created to the <strong>Mock Location Data</strong> property.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*K_tho6HbzAWCbaSCnCVaOw.png" /></figure><p>Now we are ready to press the <strong>Play Button</strong>!</p><p>But wait, we get this message instead of our beautiful route! What is happening? Well, it happens that we use <strong>Text Mesh Pro</strong> for rendering text, and it wants to install some things into our project so it works correctly. Press the <strong>Play Button</strong> again to stop the <strong>Play Mode</strong>, and click on <strong>Import TMP Essentials</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mPUsJ4YqExln4n5iHkZbVA.png" /></figure><p>Wait for the installation to finish and press the <strong>Play Button </strong>again. Finally you should see something like this!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Mwgio7l-i0M1tpKxhXbr9Q.png" /></figure><p>You can look around using your mouse and move using the WASD keys on your keyboard.</p><p>Now we are ready to build the app and deploy it to our device!</p><p>Don&#39;t forget to go into your Project Settings window and to configure your <strong>XR Plug-in Provider</strong>. Sing I am building for iOS I will select ARKit. If you are building for android select ARCore.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*F-jBstHoF7-AI9OPT5UXSg.png" /></figure><p>Open up the <strong>Build Settings</strong> window, and add your scene to the build.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PQHR0MZfaJ1Xog-0YJJNDA.png" /></figure><p>Go into the <strong>Player Settings</strong> and set your Company and Product names.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YUpQKfbyrq4cGMfp0mPGPw.png" /></figure><p>And on <strong>Other Settings</strong> set platform specific settings. For instance on iOS you must set the <strong>Camera</strong> and <strong>Location Usage Description</strong> strings.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*anrbJ2fF9m5pAA6J8CHx-g.png" /></figure><p>Now go ahead, click on <strong>Build And Run</strong> and deploy your app to your device!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/750/1*NI-NVt-ryG1L8k3lwCjNiA.png" /></figure><p>Now go and explore your own <em>Metaverse</em> route!</p><p>This is just a small sample of what you can do with the <strong>Routes and Navigation</strong> feature of the <strong>Unity AR+GPS Plugin</strong>. With the wave of the <em>Metaverse</em> just coming there are many possibilities for you to develop!</p><p>For more detailed information on this feature please go through our <a href="http://docs.unity-ar-gps-location.com/">documentation</a>. You can also contact us with any questions, issues or suggestions at our <a href="https://argpslocation.freshdesk.com/">support page</a>.</p><p>And, if you haven&#39;t already, get <a href="https://assetstore.unity.com/packages/tools/integration/ar-gps-location-134882">our asset at the Unity Asset Store</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3265d6fc6b5d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Creating a Location-Based Christmas AR Experience in Less Than 10 Minutes]]></title>
            <link>https://medium.com/ar-vr-journey/creating-a-location-based-christmas-ar-experience-in-less-than-10-minutes-3f900bd35e27?source=rss-4e878d7d9e45------2</link>
            <guid isPermaLink="false">https://medium.com/p/3f900bd35e27</guid>
            <category><![CDATA[unity]]></category>
            <category><![CDATA[mixed-reality]]></category>
            <category><![CDATA[unity3d]]></category>
            <category><![CDATA[ar]]></category>
            <category><![CDATA[augmented-reality]]></category>
            <dc:creator><![CDATA[Daniel Fortes]]></dc:creator>
            <pubDate>Mon, 24 Dec 2018 22:00:15 GMT</pubDate>
            <atom:updated>2018-12-24T22:00:15.085Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*T_X-Q0FrA3XgoIguGFB1Ww.png" /></figure><p>There is certainly a lot of hype around AR nowadays, specially with the advent of the ARKit/ARCore duo — which allows you to easily build augmented-reality experiences on iOS and Android devices.</p><p>Still, the directions one should take to build successful AR products is less clear — as it usually is with novel technologies. Gaming? Education? Business? Technical Materials? All Of The Above?? If I were to guess today I would say education, training and technical-focused corporate software is were it’s at.</p><p>In any case, there is still a lot to be done so that AR experiences are ever so integrated in the real world. Work on 3D object occlusion, realist lighting, better and more intuitive AR user interfaces, are all essential aspects of this development.</p><p>One other, in which I am passionate about making a contribution, is integrating AR with real-world locations via their geographic (or GPS) coordinates¹. This is often called <em>Location-Based</em> Augmented Reality.</p><p>Anyone who has to deal with location data from mobile devices knows how hard it can be to work with such data, and cam imagine the challenges that there are in trying to continuously synchronize the real and the virtual world using it (I will go in depth about them in a future post). Luckily, we can get some help from the <a href="https://en.wikipedia.org/wiki/Visual_odometry#Visual_Inertial_Odometry">Visual-Intertial-Odometry</a> implemented in modern AR systems ; )</p><p>While working on these issues I developed and recently release an Unity3D scripting plugin called <strong>AR+GPS Location</strong>. You can check it at the Unity Asset Store <a href="https://assetstore.unity.com/packages/templates/systems/ar-gps-location-134882">here</a>, and it’s full documentation <a href="https://docs.unity-ar-gps-location.com/">here</a>.</p><p>Since it’s Christmas time, I wanted to build something cool to show the kids in our family. So I got myself some Christmas-themed 3D assets from cgtrader, fired-up the Unity3D editor and, using the <strong>AR+GPS Location </strong>package, created a AR Christmas experience in about 10 minutes. Here is how.</p><h3>First</h3><p>Get some cool models you want to see in the real-world : )</p><p>I got <a href="https://www.cgtrader.com/3d-models/various/various-models/christmas-assets-cubic">this</a> pack at cgtrader at a good price and I really liked it. Best of all, it contained a pre-packaged unity project, so I had to do was import it and all the 3D assets were almost done!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*i151hYCUAkZKH7-mD4eYhA.png" /></figure><p>All that is need to do is to move around the objects to build what you’d like to. In my case, I built a Santa Claus riding his Sled, dragged on by his loyal reindeer. What I got was this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_GDY5azfAp7Nwq_oBr3zLw.png" /></figure><p>And, adding some quick-and-dirty particle systems, this was the final result:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*ZLqBMu2sGwbpbJdwcXSmkA.gif" /></figure><h3>Second</h3><p>Add some motion.</p><p>So far it looks OK, but it’s a little boring. Let’s put some more life into it.</p><p>Let’s say the reindeer do some type of wavy motion while the sled flies about it’s way. The simplest thing we can do is to make them oscillate in the y-direction, using a sinusoidal function.</p><p>To do that, I created a MoveDeers script component with the following code</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bcdf01c25111d2ac2779eef60a53e9b0/href">https://medium.com/media/bcdf01c25111d2ac2779eef60a53e9b0/href</a></iframe><p>Just add the component, and here we have it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/636/1*Cq_NaNUvwpVanmtidMynpA.gif" /></figure><h3>Third</h3><p>Setup the scene for AR Foundation.</p><p>Start by importing the AR Foundation and the ARKit or ARCore (depending on the platform you are targeting) packages via the package manager.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-XfYJwZMFamwJZ-cJHPQLw.png" /></figure><p>And the <strong>AR+GPS Location</strong> package from the Unity Asset Store.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cx6mtWWzaxtbh-6XAoKa8g.png" /></figure><p>Now let’s create the basic structure for a AR Foundation scene. That means we need to have two objects at the root of the scene: one AR Session object, with a AR Session component, and one AR Session Origin object, with an AR Session Origin component.</p><p>We also need to parent the main camera to the AR Session Origin object, and two components to it: A TrackedPoseDriver component, and a AR Camera Background component. Don’t forget to set the Pose Source in the TrackedPoseDriver component to Color Camera.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zRT8yZxeQuUcubxm8Of_wA.png" /></figure><h3>Fourth</h3><p>Setup the scene for <strong>AR+GPS Location</strong>!</p><p>We’ll also need some additional objects and components.</p><p>First, a LocationProvider object with a LocationProvider component:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SZLErlo7y6enP2iqY8QWCg.png" /></figure><p>This component manages location data and controls how often we update the data and how accurate we (ideally) want it to be. Play around with the values to see which ones work better for your project.</p><p>Next, we’ll create an object named ARLocationRoot under the AR Session Origin object and add two components to it, a ARLocationOrientation and a ARLocationManager.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BaUvjjIUgGZ9mxFiEGap8w.png" /></figure><p>The ARLocationOrientation component makes sure that the coordinate system of the ARLocationRoot object is in synch with the cardinal directions, so that to z-axis will match the true north, while the x-axis will point to the east. And the ARLocationManager makes sure all placed objects in the scene are in their correct positions.</p><h3>Fifth</h3><p>Let’s make it fly!</p><p>We’ll be making santa claus and his sledge move along a predefined geographical route using the ARLocationMoveAlongPath component. With it we can select a set of points we want our object to pass trough in it’s way, choose it’s velocity along it, and that’s it!</p><p>To do that we first create a ARLocationPath, which is a ScriptableObject that holds the data describing the path, that is, the geographical coordinates of the set of points.</p><p>So let’s get our coordinates! One easy way to do this is to login to Google Maps, go to ‘your places’, then ‘Maps’, and click on ‘Create Map’. Then you can just add draw your path by placing markers, like I did below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HqWfSM8Sz6vtmMgGWJ8Sew.png" /></figure><p>That way you can see the basic shape of the path, and then just click on the points to copy the latitude/longitude coordinates.</p><p>Now go back to the Unity editor, and click on Create/ARLocation/Path to create a new Path. Set the number of locations and copy/paste the coordinates from the map. Adjust the altitudes as you wish (they’ll usually be relative to the device’s initial height. We can do absolute altitude from the device’s sensors, but this is usually wildly inaccurate).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9rGiEk9Tcl7yt40RMdVIxA.png" /></figure><p>Finally, assign the path to the ARLocationMoveAlongPath component in Stanta’s Sled game object and we are ready to go!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1pLd63yne2YGG3sg4bcBrw.png" /></figure><h3>Sixth</h3><p>Compile and enjoy!</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FpOxNSYEVBnA%3Ffeature%3Doembed&amp;url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DpOxNSYEVBnA&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FpOxNSYEVBnA%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/4d699c15910522110b12b9c8bb72ae48/href">https://medium.com/media/4d699c15910522110b12b9c8bb72ae48/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FveuOweojDC8%3Ffeature%3Doembed&amp;url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DveuOweojDC8&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FveuOweojDC8%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="640" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/26cf7ef9c1e76bfafe6e43c042259688/href">https://medium.com/media/26cf7ef9c1e76bfafe6e43c042259688/href</a></iframe><p>You can get the AR+GPS Location package here: <a href="https://assetstore.unity.com/packages/templates/systems/ar-gps-location-134882">https://assetstore.unity.com/packages/templates/systems/ar-gps-location-134882</a></p><p>Full docs here: <a href="https://docs.unity-ar-gps-location.com/">https://docs.unity-ar-gps-location.com</a></p><p>If you have any questions, just e-mail me at daniel.mbfm@gmail.com, or at twitter <a href="https://twitter.com/daniel_mbfm">@daniel_mbfm</a>.</p><p>Thanks for reading this, Merry Christmas and a Happy New Year!</p><h4>Notes</h4><p>[1] Of course, we are referring to the <a href="https://en.wikipedia.org/wiki/World_Geodetic_System">WGS 84</a> geographic system, which is to most widely used <a href="https://en.wikipedia.org/wiki/Geographic_coordinate_system">geographic<em> </em>coordinate system</a> today. But there are <a href="https://en.wikipedia.org/wiki/Geodetic_datum">other </a>possible systems one can use.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3f900bd35e27" width="1" height="1" alt=""><hr><p><a href="https://medium.com/ar-vr-journey/creating-a-location-based-christmas-ar-experience-in-less-than-10-minutes-3f900bd35e27">Creating a Location-Based Christmas AR Experience in Less Than 10 Minutes</a> was originally published in <a href="https://medium.com/ar-vr-journey">AR/VR Journey: Augmented &amp; Virtual Reality Magazine</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[csg: subtracting solids in webgl]]></title>
            <link>https://medium.com/@daniel.mbfm/csg-subtracting-solids-in-webgl-dadc6126c041?source=rss-4e878d7d9e45------2</link>
            <guid isPermaLink="false">https://medium.com/p/dadc6126c041</guid>
            <category><![CDATA[computer-graphics]]></category>
            <category><![CDATA[threejs]]></category>
            <category><![CDATA[csg]]></category>
            <category><![CDATA[opencsg]]></category>
            <category><![CDATA[webgl]]></category>
            <dc:creator><![CDATA[Daniel Fortes]]></dc:creator>
            <pubDate>Sat, 10 Nov 2018 00:15:14 GMT</pubDate>
            <atom:updated>2018-11-10T00:15:14.146Z</atom:updated>
            <content:encoded><![CDATA[<p>Suppose that, having two 3D objects, we need to render the mesh that is obtained by subtracting one from the other. That is, we take one object, and remove from it the points belonging intersection between itself and the other object. For concretness, if we have a rectangular mesh B, and a shpere A, as in the drawing below, then the red outline corresponds to the set of points B - A.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/912/0*aDf8jSiyCxthLH-I.png" /></figure><p>This way of obtaining new solids via a sequence of set-operations on other solids is usually called <strong>constructive solid geometry</strong>, or csg. Such sequence is usually represented by a hierarchical tree. But let’s keep things very simple here and consider only one operation, subtraction, on a pair of solids.</p><p>I’m not really interested here in obtaining the solid’s set of points, or even it’s representative data structure, but only in rendering a representation of the final solid obtained by the operations.</p><p>In other words, given solids A and B, how do we render the resulting solid B - A?</p><p>Well, as one would imagine, a lot of different methods to achieve this were developed along the years. Some more, some less suited for real-time rendering; on top of that, webgl does not have the same tools as more advanced OpenGL specifications, which restricts our range even more. Let’s then take a look at a couple of CSG algorithms and implement a small demonstration — the simple case of CSG subtraction above — using <a href="https://threejs.org/">threejs</a> to make things easier (maybe not much easier tough…).</p><h3>The goldfeather algorithm</h3><p>So, let’s say we have a primitive A and a sequence of other primitives P₁, P₂,..., Pₙ which we would like to subtract from A. The <em>goldfeather algorithm</em> for this subtraction proceeds roughly by rendering the front surface of A (into a temporary depth buffer), clipping out of A all intersecting back-surfaces of all the P, and saving the results in the final depth-buffer; then, for each one of the subtracting primitives, draw the relevant back-surface parts into the temporary depth-buffer, and composite them into the final depth buffer; finally, we can render the front of A and the back of all the P using something like gl.depthFunc(gl.EQUAL) and have the final result.</p><p>In pseudo code:</p><pre><strong>begin</strong> renderGoldfeatherSubtract(A, P₁, P₂,…, Pₙ) <br><strong>do</strong> <br>    for each primitive P in (A, P₁, P₂,…, Pₙ)<br>        if P == A<br>            draw front of P into Zₜₑₘₚ<br>        else<br>            draw back of P into Zₜₑₘₚ<br>        for each Q ≠ P in A, P₁, P₂,…, Pₙ<br>            parityClip(Q, Zₜₑₘₚ, P ≠ A)<br>        merge Zₜₑₘₚ buffer into Zₒᵤₜ (where Zₜₑₘₚ &lt; Zₒᵤₜ)<br>        set depthFunc to EQUAL<br>        draw front of A<br>        draw back of P₁, P₂,…, Pₙ</pre><pre><strong>begin</strong> parityClip(S, Z, isSubtracted) <br><strong>do</strong>  <br>    clear stencil to 0<br>    disable face culling<br>    for all fragments of S<br>        if (Zₛ &lt; Z)<br>            stencil += 1<br>    for all pixels<br>        if (isSubtracted)<br>            if stencil is odd<br>                Z = Zfₐᵣ<br>        else<br>            if stencil is even<br>                Z = Zfₐᵣ</pre><p>It’s a little convoluted, but maybe the drawing below can give a better idea of the process:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*V_JgCsjNEuYycBTp.jpg" /></figure><p>As implementation goes, it’s hard to do this in webgl (vanilla WebGL 1 at least), since it requires two depth buffers and there is no way to do that, as far as I know, without WebGL2 or some extension like EXT_frag_depth which, sadly only has a 22% availability according to <a href="https://webglstats.com/webgl/extension/EXT_frag_depth">WebGL Stats</a>.</p><p>Luckly, there is a method which allows us to implement CSG operations without the use of two depth buffers. In fact, it’s the method used by the popular <a href="http://www.opencsg.org/">OpenCSG</a> library, the <strong>SCS CSG Rendering Algorithm</strong>.</p><h3>The SCS algorithm</h3><p>The slightly modified SCS algorithm utilized in the OpenCSG library utilizes a numeric ID encoded in the alpha channel instead of a second depth buffer, to transfer the depth information used to create the final depth buffer output.</p><p>Basically what we do is assign each surface an integer ID; calculate the visibility of each surface at each pixel utilizing the stencil buffer and depth testing, and store the ID of the visible surface at a given pixel in the alpha channel; and, at the end, we draw each surface again (the front of A and backs of all the P₁, P₂,…, Pₙ), but only where the alpha value corresponds to that surface’s ID.</p><p>The pseudo-code is:</p><pre><strong>begin</strong> renderSCSSubtract(A, P₁, P₂,..., Pₙ) <br><strong>do</strong> <br>    draw front of A to Z buffer<br>    stencilRef = 0<br>    id = 1 <br>    for each P in P₁, P₂,..., Pₙ<br>        stencilRef++<br>        id++<br>        for all front fragments of P<br>            if Zₚ &lt; Z<br>                stencil = stencilRef<br>        <br>        for all back fragments of P<br>            if stencil == stencilRef and Zₚ &gt; Z<br>                Z = Zₚ<br>    for all back fragments of A<br>        if Zₐ &lt; Z<br>            alpha = 0<br>    <br>    id = 1<br>    for all front fragments of A<br>        if alpha == id<br>            draw fragment<br>    for each P in P₁, P₂,..., Pₙ<br>        id++<br>        for all back fragments of P<br>            if alpha == id<br>                draw fragment</pre><p>This can be implemented on webgl by using to framebuffers, one for the passes where we store the alpha channel IDs, and the other for the rendering passes where we read the alpha channel IDs.</p><p>Bellow a schematic of the alpha values for a scene where a sphere is subtracted from a cuboid solid.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1016/0*_e-kktFa4YEt_xuk.jpg" /></figure><p>You can see the demo that I wrote using threejs running and it’s code bellow. We have a plane, three moving spheres that are subtracted to it, and two other spheres that just sit there. I warn you that it’s quite messy and un-optimized still : )</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodesandbox.io%2Fembed%2Fjv5q27022y&amp;url=https%3A%2F%2Fcodesandbox.io%2Fs%2Fjv5q27022y&amp;image=https%3A%2F%2Fcodesandbox.io%2Fapi%2Fv1%2Fsandboxes%2Fjv5q27022y%2Fscreenshot.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codesandbox" width="1000" height="500" frameborder="0" scrolling="no"><a href="https://medium.com/media/819fb3759502fecd8b7e71ce687a86b9/href">https://medium.com/media/819fb3759502fecd8b7e71ce687a86b9/href</a></iframe><p>With this I also found out that threejs is maybe not the best tool to use if you need fine-tuned access to the webgl state and other low-level stuff… It’s not very easy for instance to render a single mesh that is inside a given scene.</p><p><em>Originally published at </em><a href="https://www.danielfortes.com/2018/09/csg-subtracting-slolids-in-webgl/"><em>www.danielfortes.com</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=dadc6126c041" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>