last lesson we set up our project architecture so today is all about the foundational models of arcade anchors and entities hey Code Crew this is Flo with our project architecture ready from the first lesson today we'll be all about how you can place content in the scene we will start with the theoretical foundations and dive into an example in the end so first of all before you can place any content in your scene you need to run a ar configuration there are a bunch of different ones and i've already prepared some example code here you
will want to create this configuration for example in your initializer of your ar view now with our architecture we're not using the air view directly because we have created a custom a review which inherits from AR view so in its initializer you would call or you would create a configuration and then always call ar session or session dot run with set configuration now which configuration you need depends on what you want to do in your ar view if you only want to place content at coordinates relative to your iPhone then all you need is the
basic ar world tracking configuration in ios 15 and as far as i know also in ios 16 this is the default so if you only want to place objects relative to the iphone you don't actually need to create a configuration but you of course can and it will be it will not be hurting you and will just be more explicit so i encourage you to always create a configuration even if you're just placing objects relative to your iphone's coordinates now there's a very cool configuration which is called geotracking this one allows you to place object
with respect to real-life coordinates so latitude and longitude coordinates the only downside is that this configuration is only supported in certain regions as of the date of this recording it's only supported in some major cities in the us this can of course change with time and the reason why this is not supported is because it needs the street view and apple maps this basically does sensor fusion if you want to hear the fancy name so it looks at your gps coordinates and at your camera stream and then matches that camera with what it knows from
street view and that way it can very precisely position you in a yeah on the globe basically that's why this is not available everywhere so i would personally hold back with the geo tracking configuration until more cities worldwide are also supported otherwise users from abroad will not be able to use this the next configuration is needed if you want to track faces so for example if you want to place a virtual mask on a user's face then you will need a face tracking configuration and very similarly if you want to track the user's body you
will need a ar body tracking configuration just one more time to repeat when you want to use one of these configurations that will tell a arcade what you're going to do what it has to track and you will have to create this configuration just a single time and run it on your current ar session which is just the session property of an ar view so very simple now that you've probably created a configuration it's time to dive into anchors let me first explain to you how air kit works under the hood so there are entities
which are basically your 3d objects that you want to place in the scene and then there are anchors anchors tie your objects to a certain place in the scene so for example an anchor could be tied to a coordinate in the scene the coordinates in a r kit are always relative to your iphone's initial position when you first opened the app so for example you could place an anchor three meters in front of the iphone or a meter above the iphone even and then these anchors are sitting there still even when the user moves around
the anchor is still at the same position and then you can attach entities to those anchors now which configuration you need highly depends on which type of anchor you want to use so there are a bunch of very simple anchors and also some more advanced ones i've prepared a few here so let's dive into them the first one is just the anchor entity with world coordinates so this gets passed in a matrix with your word coordinates let's have a look at the dot zero here what this actually is so this is a zoomed three which
is a r kit term for a three by three matrix of a scalar value so for example a double now i went the easy route here and use the zero filled matrix dot zero so this would just place an entity at zero zero zero so basically exactly where the iphone was when the session started of course you can pass in any coordinates here and then your entity or your anchor will be placed at that specific coordinate this is the easiest way to use ar kit in my opinion next up you can do plane tracking this
is a bit more fancy so for example imagine you want to place a figurine on the floor now what you want to do is create an anchor entity on a horizontal plane this tells ar kit to track surfaces in this case horizontal surfaces and place an entity there once it found one of course the same works for vertical surfaces so just create an anchor entity with a plane of dot vertical you can also allow to track any kind of surface next and this will need one of the configurations from before in specific the ar face
tracking configuration you can of course also place anchors on a user's face and the same thing for the user's body for example another very fancy anchor is for images so for example if you want to track some markers like a visual code or a printed image or an iphone or whatever it may be you can track images in the ar scene using the dot image syntax here for your anchor entity i have never used this myself so i can't give you give you much insight on how it actually works under the hood but this is
one of the more fancy ways to create an anchor and then lastly whichever kind of anchor you created doesn't really matter but you will have to add it to the ar scene our air view has a property called scene which is an ar scene i believe oh it's just a scene actually not from ar kit and this one has a function called add anchor and then you just pass in your anchor so for example you would create an anchor at 0 0 0 using the syntax up here and then you can add it to the
current scene so you can later on add entities to that anchor okay so now that you have created your configuration and your anchor it's time to place some 3d objects there of course there are a bunch of different ways to load assets into your app i will just go over a few of them the most basic example is to load an entity from a usdz file that's included in your project a usdz file is just a file type that is created by apple actually and that's used for 3d models with textures so very basic there's
a library of usdz files online on the apple developer website where you can just grab a few of course that's very limited so you would probably want to create your own usdz files but loading them is super simple just say entity.load and pass in the name of your usd file the same syntax can also be used to load a reality file for example from reality creator or reality converter i'm not exactly sure which of the two is the ipad app but you can use that to create reality files which are actually interactive so you could
also make them do some animations for example which is very fancy not covered in this lesson though but loading entities that you already have an asset of in your project super easy just call entity.load named and pass in a string of your file name now there's one thing to note and that's that the load function is available so you need to try to do it so you either have to put it inside of a to catch block or just use the try optional operator here and then another very cool method to create entities is to
do it by code so you just use the mesh resource dot generate box function and you can pass in a size here as i said it's a cube so this is the size of the edges in this case one meter then you create a entity from your mesh resource so just a model entity from our box and then you're already done basically of course now you could add colors or textures to this entity this is the way that you can generate entities by code if you only need something very simple lastly as always you need
to add it to the scene somehow so you will have to have an anchor of some sorts now i just have a dummy anchor here obviously in reality you will have one of the anchors that i showed you in the previous examples and then you just call anchor dot add child of your new entity okay lastly let's have a look at all of this in action so as i've told you about the ar world tracking configuration is free basically it just runs by default so i have not created a specific configuration here what i have
done is created a function called place blue block and i've called that function in our initializer which we are actually using so if you remember from the last lesson we have two required initializers but we're not actually using them we're just using this convenience initializer here and in there i'm calling the place blue block function now in this function i'm using code to generate an entity let's have a look at it so i'm creating a meshresource.generatebox with a edge size of one meter basically exactly the same as in the entity example beforehand then i create
a simple material which is a way to color that box so i chose the color blue and i've said is metallic to false if you set this to true it will of course be metallic as the name suggests so so it will reflect light from the actual environment which looks really cool then i will create an entity same as before so an model entity passing in our block mesh but also passing in a list of materials in this case we just have our simple material which is a blue color that's not metallic then i create
an anchor entity that's anchored at the horizontal plane so when you open the app arcade will look for any horizontal plane that it can detect for example your desk or your floor and then i will add our entities or blue block to that anchor so now it's attached to that anchor and will always stay at this exact position that we've specified with the anchor and lastly i've added the anchor to our scene in the air view since i've called this method in the initializer it will run once the app opens so let's have a look
at that right now okay so in this lesson you learned how objects can be placed in the ar scene which kinds of anchors and configurations are available and how you can load your different assets