6
6
//
7
7
8
8
import ARKit
9
- import SwiftUI
10
9
import RealityKit
10
+ import SwiftUI
11
11
12
- //Displays as a SwiftUI View
13
- struct ContentView : View {
12
+ // Displays as a SwiftUI View
13
+ struct ContentView : View {
14
14
var body : some View {
15
15
return ARViewContainer ( ) . edgesIgnoringSafeArea ( . all)
16
16
}
@@ -23,7 +23,7 @@ struct ARViewContainer: UIViewRepresentable {
23
23
Coordinator ( parent: self )
24
24
}
25
25
26
- class Coordinator : NSObject , ARSessionDelegate {
26
+ class Coordinator : NSObject , ARSessionDelegate {
27
27
var parent : ARViewContainer
28
28
var videoPlayer : AVPlayer !
29
29
@@ -37,7 +37,7 @@ struct ARViewContainer: UIViewRepresentable {
37
37
return
38
38
}
39
39
40
- //Assigns video to be overlaid
40
+ // Assigns video to be overlaid
41
41
guard let path = Bundle . main. path ( forResource: " iphonevideo " , ofType: " mp4 " ) else {
42
42
print ( " Unable to find video file. " )
43
43
return
@@ -48,30 +48,30 @@ struct ARViewContainer: UIViewRepresentable {
48
48
videoPlayer = AVPlayer ( playerItem: playerItem)
49
49
let videoMaterial = VideoMaterial ( avPlayer: videoPlayer)
50
50
51
- // size of video plane depending of the image
52
- let width : Float = Float ( imageAnchor. referenceImage. physicalSize. width * 1.03 )
53
- let height : Float = Float ( imageAnchor. referenceImage. physicalSize. height * 1.03 )
51
+ // size of video plane depending of the image
52
+ let width = Float ( imageAnchor. referenceImage. physicalSize. width * 1.03 )
53
+ let height = Float ( imageAnchor. referenceImage. physicalSize. height * 1.03 )
54
54
55
- //Sets the aspect ratio of the video to be played, and the corner radius of the video
55
+ // Sets the aspect ratio of the video to be played, and the corner radius of the video
56
56
let videoPlane = ModelEntity ( mesh: . generatePlane( width: width, depth: height, cornerRadius: 0.3 ) , materials: [ videoMaterial] )
57
57
58
- //Assigns reference image that will be detected
59
- if let imageName = imageAnchor. name, imageName == " xs " {
58
+ // Assigns reference image that will be detected
59
+ if let imageName = imageAnchor. name, imageName == " xs " {
60
60
let anchor = AnchorEntity ( anchor: imageAnchor)
61
- //Adds specified video to the anchor
61
+ // Adds specified video to the anchor
62
62
anchor. addChild ( videoPlane)
63
63
parent. arView. scene. addAnchor ( anchor)
64
64
}
65
65
}
66
66
67
- //Checks for tracking status
67
+ // Checks for tracking status
68
68
func session( _ session: ARSession , didUpdate anchors: [ ARAnchor ] ) {
69
69
guard let imageAnchor = anchors [ 0 ] as? ARImageAnchor else {
70
70
print ( " Problems loading anchor. " )
71
71
return
72
72
}
73
73
74
- //Plays/pauses the video when tracked/loses tracking
74
+ // Plays/pauses the video when tracked/loses tracking
75
75
if imageAnchor. isTracked {
76
76
videoPlayer. play ( )
77
77
} else {
@@ -82,19 +82,20 @@ struct ARViewContainer: UIViewRepresentable {
82
82
83
83
func makeUIView( context: Context ) -> ARView {
84
84
guard let referenceImages = ARReferenceImage . referenceImages (
85
- inGroupNamed: " AR Resources " , bundle: nil ) else {
86
- fatalError ( " Missing expected asset catalog resources. " )
87
- }
85
+ inGroupNamed: " AR Resources " , bundle: nil )
86
+ else {
87
+ fatalError ( " Missing expected asset catalog resources. " )
88
+ }
88
89
89
- //Assigns coordinator to delegate the AR View
90
+ // Assigns coordinator to delegate the AR View
90
91
arView. session. delegate = context. coordinator
91
92
92
93
let configuration = ARImageTrackingConfiguration ( )
93
94
configuration. isAutoFocusEnabled = true
94
95
configuration. trackingImages = referenceImages
95
96
configuration. maximumNumberOfTrackedImages = 1
96
97
97
- //Enables People Occulusion on supported iOS Devices
98
+ // Enables People Occulusion on supported iOS Devices
98
99
if ARWorldTrackingConfiguration . supportsFrameSemantics ( . personSegmentationWithDepth) {
99
100
configuration. frameSemantics. insert ( . personSegmentationWithDepth)
100
101
} else {
@@ -107,4 +108,3 @@ struct ARViewContainer: UIViewRepresentable {
107
108
108
109
func updateUIView( _ uiView: ARView , context: Context ) { }
109
110
}
110
-
0 commit comments