Copy // MARK: - Place AR content
- func addLineObject(sourcePoint: SCNVector3, destinationPoint: SCNVector3) {
- let lineNode = SCNLineNode(from: sourcePoint, to: destinationPoint, radius: 0.02, color: lineColor)
- guard let hitTestResult = sceneView
- .hitTest(self.viewCenter!, types: [.existingPlaneUsingGeometry, .estimatedHorizontalPlane])
- .first
+ func addLineAnchorForObject(sourcePoint: SCNVector3?, destinationPoint: SCNVector3?) {
+ guard let hitTestResult = sceneView.hitTest(self.viewCenter!, types: [.existingPlaneUsingGeometry, .estimatedHorizontalPlane]).first
else { return }
- lineNode.transform = SCNMatrix4(hitTestResult.worldTransform)
- sceneView.scene.rootNode.addChildNode(lineNode)
+
+ currentLineAnchorName = "virtualObject\(count)"
+ count = count + 1
+ // 每次都根據currentLineAnchorName產生唯一的line anchor
+ let lineAnchor = ARLineAnchor(name: currentLineAnchorName!, transform: hitTestResult.worldTransform, sourcePoint: sourcePoint, destinationPoint: destinationPoint)
+ // (這步就是讓session可以協助將anchor記錄起來)將這唯一的line anchor加入session中
+ sceneView.session.add(anchor: lineAnchor)
+ lineObjectAnchors.append(lineAnchor)
}
}