The premise that
1. This part of the tutorial uses native iOS development. Unity is not currently involved, and SceneKit is mainly used.
2. Software version: Xcode 9.
3. Development language version: Swift 4
4. This series of columns aims to help you better understand the ARKit development process through concise language and pictures, and help you develop cool AR applications.
Step 1: Create a project
Open Xcode and go to File > New > Project… , select Single View App, click Next to create the project, name it ARKitDemo. As shown in the following figure:
Set the SceneKit View
Open main.storyboard and drag and drop the ARKit SceneKit view onto the view controller.
Then populate the entire view controller with the ARKit SceneKit view constraint.
Edit the code to implement the Open Camera
Connect the IBOutlet
Import ARKit by adding an import statement at the top of the viewController.swift file:
import ARKitCopy the code
Then hold down the Control key and drag from the ARKit SceneKit view to the viewController.swift file. When prompted, name IBOutlet sceneView. You can remove the didReceiveMemoryWarning() method; we don’t need it right now.
Configuration ARSCNView Session
Our AR app is looking at the world and its surroundings through a camera. So next we need to set the Camera:
Configure ARKit SceneKit View. Insert the following code into the ViewController class:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let configuration = ARWorldTrackingConfiguration()
sceneView.session.run(configuration)
}Copy the code
In the viewWillAppear (_) method, we initialize a called ARWorldTrackingConfiguration AR configuration. It is mainly used to implement World Tracking function.
The World Tracking configuration tracks The direction and position of a device, and it can also detect real-world surfaces using The device’s Camera.
Set up sceneView’s AR session to run the configuration we just initialized. AR Session manages motion tracking and camera image processing of view content.
Now add another method to the ViewController:
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
sceneView.session.pause()
}Copy the code
The main thing to do in the viewWillDisappear(_:) method is to stop tracking and the image of the view’s contents.
Authorization of the Camera
Before we run the app, we need to notify the user that we’re going to be using their device’s camera for augmented reality. This has been a requirement since the release of iOS 10.
Open the Info. The plist. Right-click on the blank area and select Add Row. Set the privacy camera instruction key. The value can be set to Augmented Reality. As shown in the figure below:
Next, let’s test in advance whether the Camera can be successfully invoked. Connect the test phone to the Mac. Build and run the project on Xcode. The app should prompt you to allow the camera in. As shown in the figure below:
Step 3: Add 3D objects
Insert the following code into your ViewController class:
func addBox() {
letBox = SCNBox(width: 0.1, height: 0.1, length: 0.1, chamferRadius: 0)letGeometry = box boxNode. Position = SCNVector3(0, 0, -0.2)let scene = SCNScene()
scene.rootNode.addChildNode(boxNode)
sceneView.scene = scene
}Copy the code
-
Create a Box with 1 Float = 1 meter.
-
Create a node. Node represents the position and coordinates of an object in three-dimensional space. Node itself has no visible content.
-
Give node a shape (Box).
-
Set the location of the box. This position is relative to the camera, and X is positive on the right and X is negative on the left. Y is positive up here, Y is negative down here. Z is positive backwards, Z is negative forwards.
-
Create a scene (SceneKit Scene) and add the box to the scene.
-
Set the sceneView’s scene to show the scene you just created.
override func viewDidLoad() {
super.viewDidLoad()
addBox()
}Copy the code
Next Build, open the camera, and a white box appears, as shown in the image below:
The addBox() method can also be written like this:
func addBox() {
letBox = SCNBox(width: 0.05, height: 0.05, length: 0.05, chamferRadius: 0)letboxNode = SCNNode() boxNode.geometry = box boxNode.position = SCNVector3(0, 0, - 0.2) sceneView. Scene. The rootNode. AddChildNode (boxNode)}Copy the code
Step 4: Add gestures (click to delete 3D objects)
Insert the following method into the viewController.swift file:
@objc func didTap(withGestureRecognizer recognizer: UIGestureRecognizer) {
let tapLocation = recognizer.location(in: sceneView)
let hitTestResults = sceneView.hitTest(tapLocation)
guard letnode = hitTestResults.first? .nodeelse { return }
node.removeFromParentNode()
}Copy the code
Creates a didTap(with Gesturerecognizer) method. Retrieves where the user clicked relative to the sceneView, then t checks to see if any node was clicked. Then find the first node from hitTestResults. If the result does contain at least one node, then we will remove the first node from its parent.
Before testing to delete objects, update the viewDidLoad() method with a call to the addTapGestureToSceneView() method:
override func viewDidLoad() {
super.viewDidLoad()
addBox()
addTapGestureToSceneView()
}Copy the code
Step 5: Add multiple 3D objects
Create an extension at the end of the ViewController class:
extension float4x4 {
var translation: float3 {
let translation = self.columns.3
return float3(translation.x, translation.y, translation.z)
}
}Copy the code
Extension converts the matrix to Float3. Modify the addBox() method:
Func addBox(x: Float = 0, y: Float = 0, z: Float = -0.2) {letBox = SCNBox(width: 0.1, height: 0.1, length: 0.1, chamferRadius: 0)let boxNode = SCNNode()
boxNode.geometry = box
boxNode.position = SCNVector3(x, y, z)
sceneView.scene.rootNode.addChildNode(boxNode)
}Copy the code
Modify the didTap(using gesturerecognizer:) method, inside the guard let statement and before the return statement. Add the following code:
let hitTestResultsWithFeaturePoints = sceneView.hitTest(tapLocation, types: .featurePoint)
if let hitTestResultWithFeaturePoints = hitTestResultsWithFeaturePoints.first {
let translation = hitTestResultWithFeaturePoints.worldTransform.translation
addBox(x: translation.x, y: translation.y, z: translation.z)
}Copy the code
The next implementation uses X, Y, and Z to add a new box when a click is detected. DidTap (withGestureRecognizer:) method
@objc func didTap(withGestureRecognizer recognizer: UIGestureRecognizer) {
let tapLocation = recognizer.location(in: sceneView)
let hitTestResults = sceneView.hitTest(tapLocation)
guard letnode = hitTestResults.first? .nodeelse {
let hitTestResultsWithFeaturePoints = sceneView.hitTest(tapLocation, types: .featurePoint)
if let hitTestResultWithFeaturePoints = hitTestResultsWithFeaturePoints.first {
let translation = hitTestResultWithFeaturePoints.worldTransform.translation
addBox(x: translation.x, y: translation.y, z: translation.z)
}
return
}
node.removeFromParentNode()
}Copy the code
The running effect is as follows:
Refer to the link: https://www.appcoda.com/arkit-introduction-scenekit/
——AR Portal (AR developer community)
Follow wechat public account (AR developer exchange community, provide AR development dry goods, promote THE development of AR content) : AR developer community