All right. So let's get cracking. So open up your Xcode and click on Create a new X code project. 1
Now, the template that we're going to select this time is the Augmented Reality App. 2
And this already contains a little bit of code that allows us to get started and get going pretty quickly. 3
So go ahead and hit Next and give your product a name. 4
So we're going to call this ARDicee. 5
So you guys have made Dicee before on the iPhone in 2D, 6
and we're going to make it in 3D and in AR, so you can call it aARDicee, or whatever you like. 7
Now, one other thing to note, make sure that the Content Technology has SceneKit selected. 8
So as you can see, there's three options for working with AR on iOS at the moment. 9
You can use SceneKit, SpriteKit, or Metal. So metal is a GPU accelerated graphics API that lets you 10
get pretty close to controlling the GPU which is the graphics processing unit. And you might have heard 11
the term "being close to the metal" or "coding close to the metal." 12
The metal is basically the computer. And this is a good name for the API because it basically lets you push 13
the hardware to the limits and gives you full control over how your game or your graphics would work. 14
It's pretty similar to OpenGL if you've dealt with graphics and with games. You might have heard of 15
that. 16
The other things here are SpriteKit and SceneKit. And SpriteKit and ScreenKit are actually frameworks that 17
rest on top of metal. 18
So the guys at Apple basically created bits of template code and boilerplate code that has a lot of 19
the basic implementation in game development and basically saves you time and frustration when you're 20
creating your games. 21
So SpriteKit was created with mostly 2D games in mind and SceneKit was created for 3D games. 22
So in this ARKit tutorial, we're going to be focusing on using SceneKit with a ARKit. 23
But if you're interested, then you can check out the documentation and learn more about the other content 24
technologies as well, 25
and I'll link the documentation in this lesson. 26
So make sure that SceneKit is checked and hit Next. And then, go ahead and save it any way you'd like. 27
I'm just going to pop it on my desktop for now. 28
All right. 29
So here is our brand-new project and have a look around. 30
You might see some things that look slightly different from what we're used to. 31
The first thing that you'll notice is this art.scnassets folder that is separate from our 32
assets.xcassets folder. 33
So if you have a look inside, you can see that there's a ship.scn file and a texture.png 34
file. 35
So this comes pre-included and it's basically something from Apple that allows you to get going with 36
your ARKit app, and it's going to put the spaceship into the 3D world around you. And the texture map 37
for this object is inside this texture.png and it's just an image that determines how the ship 38
is going to look. 39
So if you have a look at the document outline and select the shipMesh node, then you can see the texture 40
map being implemented under the property called Diffuse. And diffuse shading is basically the amount 41
and color of light that's reflected off this material surface. 42
You can think of it as the base color or the base material for the object and you can change it to a 43
color or you can change it to a image, 44
and that's pretty easy to setup as well. 45
All right. 46
So the art.scnassets folder is a folder that separate from assets.xcassets folder, 47
and this is going to be the folder that you're going to use for all of your 3D graphics, 48
and we're going to see it come into use very soon. 49
I'm just gonna drag it down to here because I don't like it interrupting the flow of my code files, but 50
you can keep it however you like. 51
Now, let's have a look inside ViewController.swift. 52
The first thing you'll notice is that we are importing both SceneKit and ARKit to obviously leverage 53
the code that's inside and use these frameworks that Apple has created, so that we can create augmented 54
reality effects really easily without writing a whole bunch of code. 55
Another thing is that you'll see there's this IBOutlet that links to a sceneView. 56
And if we have a look at the Main.storyboard, we'll see that in the template, Apple is already, by 57
default, implemented a AR. sceneView. 58
And if you search in the object library for AR, you can see that there's two options: AR SceneKit View 59
or AR SpriteKit View, and they're basically a view inside which you can display your SpriteKit 60
or SceneKit content using AR. 61
And this is, of course, linked up to this outlet called sceneView, 62
and we're going to be manipulating it and adding to it inside our code file. 63
So let's go back to the ViewController and have a look at the rest of the template code that Apple 64
has injected. 65
So you can see that we're sending the sceneView's delegate as self, which is this current view controller, 66
and this current ViewController also conforms to the ARSCNViewDelegate, and there's a whole bunch of delegate 67
methods including responding to a whole bunch of things such as session did fail or 68
session interrupted, et cetera. 69
And for now, we're not actually going to be using any of this. 70
I'm actually going to get rid of all of this, make sure that you don't delete the last closing brace, 71
of course. 72
This way we have a slightly cleaner code file. And as we code it up, it's going to get longer and 73
longer, and this will just make it clearer for you to be able to see what's going on. So the next line 74
here is the showStatistics. 75
So this shows the frames per second as well as the timing information. 76
And I'm gonna get rid of that as well because we won't be using it in this project. And then we get to 77
the meat of the code. 78
So here we're creating a new scene by using the ship.scn file that we saw earlier on inside the 79
art.scnassets folder, and then we're putting that scene inside the sceneView. And just before 80
the view shows up on screen, we're creating a configuration of the type ARWorldTrackingSessionConfiguration. 81
So this is really, really important. Do you remember previously how we talked about the fact that you need 82
an A9 chip in order to get augmented reality working properly on your iOS app? 83
This is the reason for it. 84
So if you have a look at Apple's main ARKit Framework Documentation, you can see that on one of the 85
first topics is the Configurations. And there's two types of configurations. 86
One is the ARSessionConfiguration and the other is the ARWorldTrackingSessionConfiguration. 87
So the world tracking session configuration is the one that is immersive AR 88
and this is what you normally associate with augmented reality. 89
What it means is that when you conjure up a virtual object inside your real environment, not only does 90
the object keep its position in the real world when you rotate your device, 91
it also keeps its position when you transversely move your device. 92
So it feels really, really real and it keeps that augmented reality illusion at maximum. 93
Now, let's have a look at the other one which is ARSessionConfiguration. So you can add this type 94
of a AR, I guess, to devices that don't have an A9 chip, so something like an iPhone 5 or an iPhone 6. 95
Although, because ARKit is pretty demanding on the graphical processing of your device, you'll see 96
that if you're using a really old device, it's going to seriously struggle under all of that processing 97
weight. 98
So the main difference between ARSessionConfiguration and ARWorldTrackingSessionConfiguration 99
is that when you move the device, the virtual object will move with your device, 100
and that illusion of augmented reality is kind of broken. Although, in the ARSessionConfiguration, you still 101
get this device rotation effect. And I'm gonna quickly show you what these two types of configurations 102
look like on a device. 103
So I've got an iPhone 6 and one of the latest iPads. 104
So on the iPad, I've got a A9 chip, and on the iPhone 6, I've got an A8 chip. So I'm gonna first run 105
the ARWorldTrackingSessionConfiguration and I'm going to put this ship onto the screen using the 106
iPad. 107
All right. 108
So let's try and find where that airplane--Oh, there we go. Found it. 109
So as you can see, this is ARWorldTrackingSessionConfiguration and you can walk up to it, look above it, 110
look below the airplane, and go really close, 111
go around it, look at the back. 112
It's really, really crazy how easy to implement that. That was--and now, I'm going to delete those two words: 113
"World Tracking," and you're gonna see quite a bit of difference. And we're going to run this on my iPhone 6 with 114
the A8 chip. 115
All right. 116
So here is ARSessionConfiguration. And you can see when I move left and right, 117
my object doesn't move with me. 118
So it's sort of floating in the same place, but it's not really the full AR experience. All right. So you 119
might have noticed a bit of difference. 120
And the other thing to point out is probably quite obvious. 121
But just remember, you can't actually use ARKit in the simulator because it doesn't allow you to tap in 122
to do a live camera session. 123
So that is configuration. And I'm gonna change that back to a ARWorldTrackingSession where most 124
of the interesting ARKit abilities are found. 125
And if you don't know what is the chip inside your phone, you can have a look back at the first lesson 126
in this module where we listed all the devices that are compatible with ARWorldTrackingSessionConfiguration 127
and see if your device is on there. 128
Alternatively, there's actually a property called isSupported on both of these configurations and can 129
print it into the console. 130
So all you have to do is just write ARSessionConfiguation.isSupported or the other one which 131
is ARWorldTrackingSessionConfiguration 132
.isSupported, and we probably should give those two Booleans a descriptive name, so-- 133
All right. 134
So you can go ahead and run that and you'll be able to see whether if your device supports either or both 135
of these configuration types. 136
All right. 137
So that was just a brief demo about the differences between the ARSessionConfiguration and 138
ARWorldTrackingSessionConfiguration. 139
And I hope you can see how much more awesome it is when you are running augmented reality on 140
a ARWorldTrackingSessionConfiguration supported device. 141
Now, how you would use this in your actual product is you would put in a check to see if 142
ARWorldTrackingSessionConfiguration 143
.isSupported. 144
If so, then do all of this, go ahead. 145
But otherwise, you could say, you know, either tell the user that the true AR experience is not supported 146
on their device or the alternative is to give them a configuration that is of the type ARSessionConfiguration, 147
but letting them know that they weren't really experiencing the true AR experience because of device limitations. 148
Basically, you just need an "if else" just to cover those two bases. 149
All right. 150
So I'm gonna delete this "if else" code for now. 151
We're assuming that we have a device that can run ARWorldTrackingSessionConfiguration. And we're going 152
to go ahead in the next lesson to create our first 3D object. 153
So I'll see you there.