Getting Started With ARFoundation in Unity (ARKit, ARCore)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] welcome to the unity workbench this is Chris in this video I'll walk you through the basics of working with a our foundation Unity's new cross platform augmented reality framework it will let you create content that can run on both iOS and Android the application will be building is simple but feature complete it will contain all the basic functionality you would need when you're executing any augmented reality idea [Music] before we dive in I'm going to assume that you already have some basic level of knowledge for working with unity even if you don't you should find it pretty easy to follow along also I'm going to assume that you've already set up your computer for iOS or Android development if you haven't done this or don't know how I've provided links to unities excellent documentation on this topic in the description of this video and finally if you want to follow along I've provided a download link where you can get the assets that I use in this tutorial to begin we'll open unity and note that I'm using unity 2000 18.3 will create a new project and we'll choose a location to save it and give it a name after a short time the project will open with a standard scene that includes a main camera and a directional light don't be alarmed if your editor looks a little different than mine I've increased the contrast on the grid lines to make them a little more visible and rearrange the panel's a bit since we're going to be deploying to mobile devices we need to configure a few project settings you do this by opening the Edit menu and selecting project settings and then click the player section I'm going to switch over to this icon which represents iOS settings I'm going to go ahead and fill in the company name although this isn't a super important setting then we'll scroll down and look for the bundle identifier traditionally this is a unique identifier that is in the format of a reverse domain name but you can really use any unique identifier that's unlikely to be used by someone else I'm going to choose to use the identifier the unity workbench demo day our demo but you should choose something different I'm also going to turn off automatic signing if you're using an individual Apple Developer account you might want to leave this on because it will make the setup process a little easier in Xcode but because I use multiple developer identities for my iOS work it's better for me to turn this off and configure it explicitly in Xcode later now we'll scroll down and turn on the requires a our kits support checkbox note that when we do this it also fills in a value for the camera usage description this can be any value you choose it just can't be blank you'll also notice that it gives us a warning that iOS 11 or newer is required for a our kits support so let's go ahead and increase the iOS version number in the final change we have to make is to the architecture a our kit only works with arm 64 processors so we'll select that from the drop-down list now I'll copy the bundle identifier because we want to use that same identifier on the Android side switch over to the Android settings section scroll down to the package name and paste the value we copied we also need to turn off multi-threaded rendering because it isn't compatible with AR core currently finally AR core is only supported on Android 7.0 and later so we'll select that as our minimum API level okay those are all the settings we need to change so we can close the project settings window now we have to add the AR foundation library to our project the way we do that is by opening the package manager then we have to open the advanced menu and turn on show preview packages the three packages were concerned with are AR Foundation which is the general cross-platform API the AR core XR plug-in which works in conjunction with AR foundation to allow our app to run on Android devices and the AR kit XR plug-in which allows our app to run on iOS devices so let's install all three of those and we'll close the package manager' now when we right-click in our scene hierarchy you'll notice a new section called XR we'll start by adding an AR session object we won't be interacting directly with the AR session object but it provides a lot of the setup that our scene needs in order to work as an AR scene next we'll add an AR session origin object the a our session origin object is important because it gives us a way to query the physical world around us and also a way to manipulate the scale of our virtual world you'll notice that the a our session origin also includes its own camera so we no longer need the default camera that was included in our scene so we'll delete that and I'll select the AR camera and tag it as our main camera although we don't need this specifically for the app we're going to build it's a good practice to have one of your cameras be tagged as a main camera so if we look at our scene you'll see that the icons for those two AR objects are in the scene I find them a little distracting so I'm going to open the gizmos menu and reduce the size of the 3d icons to nothing so that they disappear so now that we have our AR objects in the scene we just need to create some content for now I'll just create a simple cube as you can tell by looking at the grid in our scene the default cube in unity is one unit by one unit by one unit the way this translates to dimensions in the real world is that each unit in unity is one meter in the real world so a one meter cube is going to be way too big so we'll scale that down to a tenth of the size which would be ten centimeters per side now we'll zoom in to that cube and you can see that it's actually sitting below the ground plane a bit so let's correct that so it looks like it's sitting right on the ground by raising it 0.05 on the y-axis okay now we can try deploying to a device to see how it looks so far for most of this video I'll be deploying to iOS but we'll start by deploying to Android just so you can see how that's done in case that's the platform you're working with select Android from the platform list and click the switch platform button there now that that's complete make sure your Android device is plugged into your computer and select it from the run device drop-down list once your device is selected you can click the build and run button you'll be prompted for a place to save the build I usually create a builds folder in my project folder and we'll call this build Android you after the build completes it should automatically launch the application on your Android device now let's look at the iOS deployment process back in the build window I'll select iOS from the platform list and click switch platform and then this time I'm going to click the build button under my builds folder I'll call this iOS and hit save and you'll get this message telling you that there's a missing file that's required by a our kit just click yes fix and build you at the end of this process you will end up with an Xcode project called unity - iPhone go ahead and open that project in Xcode and with the project navigator icon selected choose the top level unity iPhone node and we'll get a list of general project settings because I've turned off automatic signing I have to manually select my provisioning profile that I want to build the project with you once your project is configured with the proper signing credentials make sure the unity iPhone scheme is selected in the iOS device you have connected to your computer is selected as the deployment target then hit the Run button this will deploy the app to your iPhone you'll initially be asked to give permission to the camera and now notice when I move my camera that cube is sort of floating in space the position of that cube is all determined based on where my phone was in space when a our kit started to stop the application I'll hit the stop button in Xcode so obviously we'll need more control than this over where our AR content sits in the physical world so let's tackle that next instead of placing this cube at a fixed position in our scene we're going to want to place it dynamically but before we delete it from the scene we want to turn it into a prefab since we'll be adding this prefab to our scenes so that it looks like it's sitting on surfaces in the real world we want to make sure that its center point is at the base of the cube one way to do that is by creating an empty game object and we'll call this game piece and notice that that game object appears to be sitting on the floor so we can just drag our cube as a child of the game piece and now it essentially has a registration position that is right where we wanted I'm going to create a new folder to house all of our assets for this project called demo and to turn this into a prefab I'll just drag and drop the game piece object from my scene hierarchy into my assets window now we can delete that prefab from our scene let's refer back to the implementation we're going for you'll notice that there is a visual placement indicator that helps the user understand where an object will be placed in the world that's what we'll create next we'll start by creating an empty game object and we'll call it placement indicator and then within that object will create a quad which is the simplest piece of geometry in unity it's a simple flat plane with one side I'll rotate it 90 degrees on the x axis so that it's parallel with the floor and we'll scale it down to that same size we had scaled the cube down to now we'll import a textured use on that object you you and we'll tell unity that the alpha channel should be used as transparency and hit the apply button now we'll create a new material that we can use that texture in we'll call it placement indicator and with that material selected we want to change the shader to unlit transparent and then we simply select that texture you now we'll drag that material onto our quad and we now have the visual indicator now we'll need to create a script to control the logic in our scene so right click and choose create c-sharp script we'll call this script a our tap to place object double-click it to open it up in your code editor I'm using Visual Studio code as my editor which is not the default so the behavior you see in my editor may be slightly different from what you experienced in your own but as long as you follow along and type the same code in you'll end up with the same result I'm just going to delete the comments to clean up the code a bit and the first thing we need to do is import a couple libraries so I'm going to import Unity engine X raar foundation in unity engine experimental XR I mentioned earlier that interacting with the a our session origin object would be a key and allowing us to interact with the world around us so let's start by creating a reference to that session origin object so I'll create a private variable of a our session origin and we'll call it a our origin and we'll save a reference to that object right when our application starts up now what we want to do is on every frame update we want to check the world around us find out where the cameras pointing and identify if there's a position where we can place a virtual object in order to represent that position in space we're going to use a pose object and we'll call it placement pose a pose object is a simple data structure that describes the position and rotation of a 3d point so it's perfect for our use here within our update method we'll call to a new method that will call update placement pose I get a red squiggly underline indicating that this method hasn't been implemented yet in Visual Studio code I can just press command period and have it auto generate that method for me you you're probably already familiar with the physics raycast method and unity which as it says here allows you to cast a virtual ray from one point in space in this particular direction and determine if it hits any virtual objects along the way well a our foundation provides a similar method on the a our session origin object which allows us to send array from some point on the screen straight out into the real world and it will tell us if it hits any real-world surfaces you'll notice that the first parameter it requires is a screen point we're going to want to shoot that ray from the very center of our screen so I'll create a new variable called screen Center and to determine the screen Center which is described in pixels we will use camera current dot viewport to screen point and we'll provide it a vector that describes the center point of our viewport so that's just new vector3 0.5 0.5 now we'll use that variable as our first parameter the second parameter is a list of array raycast hit objects these hit objects represent any points in physical space where this ray hits a physical surface so we'll just create an empty list called hits and we'll pass that in as our second parameter the final parameter is optional it's a trackable type it defaults to dot all but let's take a look at what the other options are these are all the types of collisions with the physical world that can be detected the most granular and the fastest to respond is feature point featured point can be any distinguishing feature in our camera feed that a our kit or a our core are able to identify but because these features may not be on a flat surface they probably aren't good candidates for us to position objects on top of we also have a number of different ways to detect planes we're just going to choose the final one called planes which will detect all types of planes so after a our origin dot raycast is called will either end up with an empty hit list meaning there is no flat plane in front of the camera currently or we'll have a list with one or more items representing the planes that are in front of the camera to keep track of this outcome we'll create a class variable called placement pose is valid and have it initially set to false you so we'll only consider the placement pose valid if we have actually hit something so in other words if our hits array has at least one item in it so now within our if statement we will set our placement pose by looking up to the first hit result in our hits array and accessing its pose property and that's it so now our placement pose will constantly be updated as our application is running but the placement pose is just a descriptor of position and rotation we haven't actually updated any of the visuals yet so back in our update method let's call a new method called update placement indicator and again we'll use the command period shortcut in Visual Studio code to generate that method so if we're going to update that placement indicator object that we created in our scene we need to have a way to reference it in our code to do that I'll create a public variable of type game object and call it placement indicator so now in our update placement indicator method will again check to make sure the placement pose is valid if it is we want to make that placement indicator object active in other words visible if the pose isn't valid we want to hide the indicator by setting its active state to false so now our indicators visibility is controlled but we also need to control its position and rotation we'll do that by modifying its transform property calling the set position and rotation method and here we simply reference the position and the rotation properties of our placement pose you so let's save our code and switch back over to unity now we've written our code but it doesn't exist in our scene yet so I'm going to create an empty game object and I'll just call it interaction it's a game object with no physical presence but it gives us a place to hang our code so I'll drag and drop our code onto that object and you'll notice it exposes one public parameter called placement indicator so we'll drag and drop our placement indicator object into that field now we can take our code for a spin since we've already built the project once we can just choose build and run and it should automatically launch our project for us without us having to go through the same setup steps we did in xcode previously normally this works pretty seamlessly but I have occasionally found cases where it fails to launch the project on the device if that happens to you just switch over to Xcode and hit the Run button again here I'm intentionally holding the phone very still but once I start moving the phone a little bit it picks up enough information about the world around me to identify those planes and here we see our placement indicator moving just as desired I can even switch to the floor and it detects the floor as a separate plane but one thing you'll notice is that as I turn my phone the placement indicator doesn't turn that's because the rotation of the pose returned by our raycast is always going to be oriented to whatever direction my phone was facing when AR KITT started up it would be better if that placement indicator turned as our phone turned so let's make that adjustment now back in our code editor I'll go down to the section where we're capturing that placement pose and instead of using the default rotation that comes with the pose we want to calculate a new rotation based on the camera direction so first I'll create a variable called camera forward this will be a vector that acts sort of as an arrow that describes the direction the camera is facing along the x y and z axes since we don't really care about how much the camera is pointed toward the sky or toward the ground we only care about its bearing we'll create a new variable called camera bearing and we'll create a new vector3 using the X component of our camera forward object zero for the y component and the Z component of our camera forward object when you're using vectors to represent a direction you should always use the normalized version of the vector so this just gives us the direction as if Y we're perfectly vertical now we'll do placement pose dot rotation and set it to a new value using quaternion dot look rotation and passing that camera bearing let's save the good and run the project again now as I turn the phone you'll see that the placement indicator turns just the way we wanted let's add some user interaction next we want the user to be able to tap on the screen to place an object so to let our script know which objects should be placed will create another public variable of type game object and we'll call it object to place every time the frame updates we want to check and see if the placement pose is currently valid and whether the user has just touched the screen to do that we can do if placement pose is valid and input touch count is greater than zero and input get touch 0 dot phase equals touch phase dot began so let me explain that a little bit placement poses valid is fairly self-explanatory the next section we're checking to see if the user has any fingers currently on the screen then we have to check the phase of one of those fingers here we're choosing the first finger to see if the touch just began if all these conditions are met we can actually place the object so we'll call another new method called place object and generate the body of that method you and this is very simple we just call the instantiate function pass in the object to place and give it a position and rotation based on our placement pose and that's it let's save our code and switch back to unity in our code we added that new public variable called object to place so we're going to set that to the prefab we created earlier now let's run our project again you you that completes the core functionality for our application but let's make the visuals a little bit more interesting by using something besides that cube I'm going to go ahead and import a toy plane prefab that I had created earlier you you let's drag it into our scene to take a look the first thing you'll notice is that it's very large compared to the scale we were working with before in our previous example with the cube we scaled the cube down so that its physical dimensions in the AR experience would be more reasonable you might be tempted to do the same thing with this model however there are some compelling reasons why we don't want to scale things down that small probably the most significant is for any application that includes physics because the unity physics engine is actually tuned to work with larger scale objects if you have anything smaller than say the size of a basketball you're likely to get some very strange results with the physics engine to a lesser extent there are other features like particle systems and shadows that also require a lot of tweaking if you're going to work at very small scales so in a moment I'm going to show you an alternative to how you can change the display scale of your scene in your augmented reality project but first let's talk a little bit about some special concerns with visuals when working with augmented reality I'm gonna switch to a slightly different perspective and I want you to focus on the shadow on this wing notice how it's kind of blocky and fuzzy looking let's see if we can improve that so I'm going to go to my project settings choose the quality section and select medium which as you'll see by the green checkmarks is the default quality setting for our ios and android deployments scroll down to the shadows section and you'll see the shadow distance value is currently set to 20 shadow distance determines how far away the camera must be from the object before the shadowing turns itself off the default value of 20 units seen here is reasonable for a large-scale scene but is way too high when we're talking about augmented reality where the user is unlikely to get more than a couple of yards away from the objects so we can greatly improve quality just by reducing that shadow distance value I'm going to reduce it to eight now you'll notice when we zoom back in the quality looks much better here's the before and here's the after we can improve it even more by changing the shadow resolution from low resolution to high resolution there now our shadow will look great in AR so I mentioned there was an alternate way to change the display scale of your objects besides just scaling down the objects themselves this is another great feature of err foundation you can simply select the a our session origin object and increase its scale to decrease the scale of your scene so in other words if I want to display my objects at half their size I would increase the scale to 2 if I want to display the objects at 1/10 their size I would increase the a our session origin scale to 10 just remember the larger scale value the smaller your objects will appear one other thing I'll point out related to visual quality is that by default your virtual objects will not cast shadows on the real world doing so is possible but it's beyond the scope of this tutorial so to make this model look rounded I've actually created a graphic based shadow that you see beneath it this is just a graphic with transparency that has been applied to a quad ok so now that we're using a much larger scaled object let's increase the scale of our placement indicator I'm going to increase it to a scale of 1 and that looks pretty good with our plane now we can delete the toy plane from our scene select our interaction object and swap out the object to place with our toy plane prefab now let's run the project again now we have our new plane prefab in place the shadows look great and that seems like a great spot to end our tutorial if you enjoyed this introduction to a our foundation hit that like button and consider subscribing I hope to share many more videos on AR creation and unity in the coming weeks until next time thanks for watching [Music]
Info
Channel: The Unity Workbench
Views: 350,646
Rating: undefined out of 5
Keywords: augmented reality, game development, tutorial, Unity3D
Id: Ml2UakwRxjk
Channel Id: undefined
Length: 31min 19sec (1879 seconds)
Published: Fri Jan 04 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.