0
1
00:00:00,420 --> 00:00:08,100
Now, in the last lesson, we've prepared all of our assets, so that we can drag and drop them into our ARKit
1

2
00:00:08,220 --> 00:00:08,790
app.
2

3
00:00:08,820 --> 00:00:14,830
So you should have a video called Harry Potter in the MP4 format.
3

4
00:00:14,910 --> 00:00:21,330
You should have a main image that you're gonna be using to recognize in your newspaper and you should
4

5
00:00:21,330 --> 00:00:26,430
have your newspaper printed and ready for use.
5

6
00:00:26,430 --> 00:00:34,080
Now, let's go ahead and Create a New Xcode Project and make sure you select Augmented Reality app.
6

7
00:00:34,080 --> 00:00:41,840
And I'm just gonna call this MagicPaper and, again, make sure you're using SceneKit, and go ahead and
7

8
00:00:41,840 --> 00:00:42,920
click Next.
8

9
00:00:43,220 --> 00:00:47,990
Then feel free to save it any way you like, but I'm just going to leave it on my desktop for now.
9

10
00:00:50,320 --> 00:00:58,270
So, now that we've got our brand-new ARKit app, first things first, I'm going to delete the ship.scn
10

11
00:00:58,450 --> 00:01:00,330
file and the texture.
11

12
00:01:00,400 --> 00:01:04,600
So let's just hit backspace and move that to trash.
12

13
00:01:04,600 --> 00:01:08,180
Next, we need to add our resources to our project.
13

14
00:01:08,200 --> 00:01:15,430
So the video I'm simply going to drag and drop into my main folder, the yellow one here, and make sure
14

15
00:01:15,430 --> 00:01:22,930
that when you drag and drop it in that you check this part where it says. "Add to targets," namely that
15

16
00:01:22,930 --> 00:01:27,350
the video should be accessible by our app called MagicPaper.
16

17
00:01:27,400 --> 00:01:29,620
Make sure that this box is ticked.
17

18
00:01:29,620 --> 00:01:31,300
This is really important.
18

19
00:01:31,360 --> 00:01:36,650
And it will cause you a lot of headache later on trying to fix this if you didn't tick it.
19

20
00:01:36,670 --> 00:01:43,420
Now, if you forgot to do it, just simply delete the aasset and drag it back on and make sure that box is
20

21
00:01:43,420 --> 00:01:44,830
checked.
21

22
00:01:44,980 --> 00:01:51,640
Now, the next step is to incorporate our images for ARImageRecognition.
22

23
00:01:51,640 --> 00:01:53,380
So you did this in the last module,
23

24
00:01:53,390 --> 00:01:58,390
so I'm going to leave it to you as a challenge and see if you can incorporate the main image that we're
24

25
00:01:58,390 --> 00:02:01,140
going to detect.
25

26
00:02:01,260 --> 00:02:01,560
All right.
26

27
00:02:01,590 --> 00:02:03,560
So this shouldn't be too hard.
27

28
00:02:03,600 --> 00:02:09,120
All we need to do is head into assets.xcassets and we're going to right-click in this white area
28

29
00:02:09,150 --> 00:02:09,610
here,
29

30
00:02:09,630 --> 00:02:13,070
and we're going to create a new AR Resources group.
30

31
00:02:13,410 --> 00:02:23,580
And then, I'm going to rename this AR Resources group to our NewsPaperImages. And this is the area
31

32
00:02:23,580 --> 00:02:28,950
where we need to drag in our image which is this one. It's called harrypotter, remember?
32

33
00:02:29,130 --> 00:02:37,120
And when we drag it in, it will always complain because it doesn't have a physical dimension.
33

34
00:02:37,260 --> 00:02:43,400
Now, the physical dimension depends on the size of the paper that you've printed out the newspaper on.
34

35
00:02:43,410 --> 00:02:49,690
So once you've printed out the newspaper, you can use a ruler to measure the size of this image.
35

36
00:02:50,190 --> 00:02:55,140
So mine turns out to be about 7.6 centimeters in height.
36

37
00:02:55,140 --> 00:03:02,340
So let's put 0.076 and it'll calculate the width automatically.
37

38
00:03:02,340 --> 00:03:09,440
And now that warning goes away and we've successfully incorporated the image that we want to detect.
38

39
00:03:09,450 --> 00:03:14,670
Now, next we can head into our ViewController and we can start writing some code.
39

40
00:03:14,670 --> 00:03:20,460
But before we do that, let's make sure we delete all of this part that relates to that ship.scn which
40

41
00:03:20,460 --> 00:03:21,750
we deleted earlier on
41

42
00:03:21,750 --> 00:03:28,130
and that's the rocket ship that gets included automatically in all the new ARKit templates.
42

43
00:03:28,200 --> 00:03:34,440
Here's another challenge, because I know you've done it before and it's always good to practice, write
43

44
00:03:34,440 --> 00:03:42,300
some code to make our image recognizable so that when you run the app you should be able to print something
44

45
00:03:42,330 --> 00:03:49,740
into the debug console that says, "The image of Harry Potter inside the Assets.xcassets was recognized."
45

46
00:03:50,160 --> 00:03:53,510
So, again, we've done this in the previous module.
46

47
00:03:53,640 --> 00:03:59,630
So if you need to refresh yourself by looking at the previous lessons, then go ahead and do that.
47

48
00:03:59,700 --> 00:04:06,480
But if you really get stuck, then come back and I'll walk you through the solution. Okay.
48

49
00:04:06,480 --> 00:04:08,150
So first things first,
49

50
00:04:08,310 --> 00:04:15,390
we are now tracking an image, so we need to change this to ARImageTrackingConfiguration.
50

51
00:04:15,660 --> 00:04:22,470
And in between creating the configuration and running the configuration, we need to do a few more things.
51

52
00:04:22,650 --> 00:04:25,380
We need to tell it which images to track.
52

53
00:04:25,950 --> 00:04:30,870
So we need to create a object called trackedImages.
53

54
00:04:31,290 --> 00:04:42,610
And this is going to be equal to ARReferenceImage.referenceImages and inGroup Named, so the
54

55
00:04:42,880 --> 00:04:46,750
String that we put in here has to match the name of this folder.
55

56
00:04:46,810 --> 00:04:52,210
So we called it NewsPaperImages, and that all the words are capitalized.
56

57
00:04:52,210 --> 00:04:59,750
"NewsPaperImages." And the bundle is, of course, the main bundle,
57

58
00:04:59,760 --> 00:05:09,950
so the current location of this app. This is, again, if you remember, going to be an optional, because this
58

59
00:05:10,010 --> 00:05:11,100
might not exist,
59

60
00:05:11,150 --> 00:05:12,760
so it might be nil.
60

61
00:05:12,800 --> 00:05:19,790
So we need to either unwrap this or use optional binding to keep ourselves safe. Once we are certain
61

62
00:05:19,820 --> 00:05:26,720
that there definitely are tracked images, then we're going to set our configuration to tell it that the
62

63
00:05:26,720 --> 00:05:35,120
image as it should be tracking are the ones that we specified above. And finally, we need to say that
63

64
00:05:35,120 --> 00:05:43,520
our configuration currently only takes a maximum of 1 images to be tracked simultaneously.
64

65
00:05:43,670 --> 00:05:51,680
And now, we can check to make sure that these NewsPaperImages definitely do exist and they can be used
65

66
00:05:51,800 --> 00:05:57,040
as reference images for tracking. And we're going to do that inside the "if let,"
66

67
00:05:57,130 --> 00:06:02,960
and we're gonna say "Images found," and let's go ahead and hit run.
67

68
00:06:02,960 --> 00:06:09,560
But before we do that, let's make sure that we select a team so that we can successfully sideload our
68

69
00:06:09,560 --> 00:06:16,380
app. And, remember, sideloading all ARKit apps requires a physical device.
69

70
00:06:19,670 --> 00:06:22,060
So down here image is found.
70

71
00:06:22,130 --> 00:06:31,560
That means that this "if let" statement was true and it was able to locate this NewsPaperImages group.
71

72
00:06:31,670 --> 00:06:34,670
So, now that's done, we can delete that.
72

73
00:06:35,090 --> 00:06:45,680
And the next step is we need to write some more code to create a plane to put our ARV content onto
73

74
00:06:45,980 --> 00:06:46,860
the main image.
74

75
00:06:47,360 --> 00:06:49,210
So, again, we've done this before.
75

76
00:06:49,220 --> 00:06:57,140
We just need to generate a white rectangle that shows up on the location of the tracked image.
76

77
00:06:57,140 --> 00:07:02,780
So, again, I'm going to encourage you to pause the video and try to complete this as a challenge.
77

78
00:07:05,560 --> 00:07:05,880
All right.
78

79
00:07:05,900 --> 00:07:15,170
So in order to receive the events from our scene when it detects a new image, we need to add a delegate
79

80
00:07:15,170 --> 00:07:15,850
method,
80

81
00:07:16,010 --> 00:07:22,310
and we're going to add it down here which are the ARSCNViewDelegate. And the one that we need
81

82
00:07:22,400 --> 00:07:28,440
is the same as the one that we used before which is renderer nodeFor anchor.
82

83
00:07:28,460 --> 00:07:37,070
So the anchor is the image that it found or recognized and we need to return a node to be displayed
83

84
00:07:37,130 --> 00:07:38,620
in our scene.
84

85
00:07:38,630 --> 00:07:41,800
So here is where we're going to be writing our code.
85

86
00:07:41,960 --> 00:07:49,740
And first and foremost, we're going to create a new node which is a SCNNode, and then we're going to
86

87
00:07:49,740 --> 00:07:59,100
check to see if this anchor is actually of the data type of ARImageAnchor, rather than just a generic
87

88
00:07:59,180 --> 00:08:00,150
ARAnchor.
88

89
00:08:00,840 --> 00:08:09,840
Let's go ahead and do an "if let" statement to check, if let imageAnchor equals the anchor that was
89

90
00:08:09,840 --> 00:08:11,640
found as,
90

91
00:08:11,700 --> 00:08:18,000
so casting its data type as a ARImageAnchor.
91

92
00:08:18,960 --> 00:08:23,550
If that is true, then we can do some stuff using this imageAnchor.
92

93
00:08:24,120 --> 00:08:29,250
And after all of that stuff is done, we're going to return on node.
93

94
00:08:29,400 --> 00:08:36,120
So up here, we've created our brand SceneKit node, and then we're going to use the imageAnchor which
94

95
00:08:36,120 --> 00:08:44,520
recognized the image on the scene to add our plane and our rectangle. And then inside here, we're going
95

96
00:08:44,520 --> 00:08:47,430
to add some 3D content to our node.
96

97
00:08:47,610 --> 00:08:54,240
And once all of that is done, we're going to return the node and render it onto the scene.
97

98
00:08:54,400 --> 00:09:02,260
So, now inside this "if let" statement, we need to create our plane. Let's create a plane that is, of course,
98

99
00:09:02,320 --> 00:09:07,450
of type SCNPlane, and we need to give it some dimensions.
99

100
00:09:07,450 --> 00:09:11,920
So we're going to use the anchor's physical dimensions.
100

101
00:09:11,920 --> 00:09:23,050
So we're going to say that the width is equal to imageAnchor.referenceImage.physicalSize.width.
101

102
00:09:23,440 --> 00:09:27,700
And the same thing for the height,
102

103
00:09:27,730 --> 00:09:35,090
so imageAnchor.referenceImage.physicalSize.height. Okay.
103

104
00:09:35,120 --> 00:09:47,720
So, now we have our plane created and we're going to set its material diffuse.content to a new
104

105
00:09:47,990 --> 00:09:52,370
UIColor, and it's just going to be a white square.
105

106
00:09:52,520 --> 00:09:58,940
So we're going to say 1.0 for white, completely white, and then the alpha will just put
106

107
00:09:58,940 --> 00:10:02,170
0.5 to give it a little bit of transparency.
107

108
00:10:02,510 --> 00:10:08,720
And now, that we've created are SCNPlane, let's create a planeNode.
108

109
00:10:08,930 --> 00:10:17,230
This is going to be of data type SCNNode and it has a geometry of the plane that we just created.
109

110
00:10:17,520 --> 00:10:25,070
And now, remember, that our plane always gets rendered at 90 degrees to the image that was recognized.
110

111
00:10:25,070 --> 00:10:32,690
So as we did in the last lesson, we need to change or rather rotate it by tapping into the eulerAngle
111

112
00:10:32,720 --> 00:10:43,510
property, and we're going to change its x dimension to minus pi divided by 2. And that just rotates
112

113
00:10:43,510 --> 00:10:48,350
at anticlockwise by half pi which is by 90 degrees.
113

114
00:10:48,350 --> 00:10:52,650
And so that it's flat and flush with the image that was recognized.
114

115
00:10:52,760 --> 00:10:58,120
And now, finally, we're going to tap into this node that we created right at the top.
115

116
00:10:58,190 --> 00:11:06,050
We're going to say addChildNode and that is going to be this plane node that we created. And now, we're
116

117
00:11:06,050 --> 00:11:14,420
ready to test our app and see if we managed to complete that challenge.
117

118
00:11:15,030 --> 00:11:25,260
So as soon as we load up the newspaper, you can see that our white rectangle gets rendered onto the newspaper
118

119
00:11:25,380 --> 00:11:30,450
and it's pretty much flush and matching the dimensions of that image.
119

120
00:11:30,540 --> 00:11:38,390
And even if I sort of moved around and bend it around it's still able to keep track of the location.
120

121
00:11:38,400 --> 00:11:39,500
So pretty neat, right?
121

122
00:11:40,170 --> 00:11:40,760
Well done
122

123
00:11:40,770 --> 00:11:43,000
if you managed to do this all by yourself.
123

124
00:11:43,170 --> 00:11:49,380
Don't worry if you've forgotten how to do it and you have to look back at the previous lessons.
124

125
00:11:49,380 --> 00:11:55,110
It's the repetition and the reminders that will really help you to turn this into your own.
125

126
00:11:55,140 --> 00:12:01,160
And so that in the future when you need to build this feature into your apps, you'll be reminded of this
126

127
00:12:01,180 --> 00:12:03,110
tutorial and of this challenge.
127

128
00:12:03,300 --> 00:12:09,480
Now, up till now, we haven't really done anything new that we haven't done in the last module. But in the
128

129
00:12:09,480 --> 00:12:16,980
next lesson is where we're going to start incorporating SpriteKits in order to render some video onto
129

130
00:12:16,980 --> 00:12:22,320
that plane that we just created based off the dimensions of the image that we detected.
130

131
00:12:22,320 --> 00:12:25,430
So all of that and more, I'll see you on the next lesson.
