作者:沉稳之固_300 | 来源:互联网 | 2022-12-07 14:32
我有一个来自服务器并存储在图库中的图像列表.我想使用ARKit选择任何图像并放置在活动墙上,并希望将图像转换为3D图像,执行缩放,移动图像等操作.
任何人都可以指导我如何在AR中创建自定义对象?
1> BlackMirrorz..:
要检测垂直表面(例如墙壁),ARKit
首先需要先设置ARWorldTrackingConfiguration
,然后planeDetection
在应用程序中启用.
所以在你Class Declaration
的下面你会创建以下变量:
@IBOutlet var augmentedRealityView: ARSCNView!
let augmentedRealitySession = ARSession()
let cOnfiguration= ARWorldTrackingConfiguration()
然后你的初始化ARSession
和ViewDidLoad
例如如:
override func viewDidLoad() {
super.viewDidLoad()
//1. Set Up Our ARSession
augmentedRealityView.session = augmentedRealitySession
//2. Assign The ARSCNViewDelegate
augmentedRealityView.delegate = self
//3. Set Up Plane Detection
configuration.planeDetection = .vertical
//4. Run Our Configuration
augmentedRealitySession.run(configuration, options: [.resetTracking, .removeExistingAnchors])
}
现在您已设置为检测到垂直平面,您需要挂钩到以下ARSCNViewDelegate
方法:
func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) { }
简单地说:
告知委托者已将与新AR锚点对应的SceneKit节点添加到场景中.
在这种方法中,我们将明确地查找ARPlaneAnchors
已经检测到的任何为我们提供的内容:
有关在世界跟踪AR会话中检测到的真实世界平面的位置和方向的信息.
因此,SCNPlane
在检测到的垂直平面上放置一个很简单,如下所示:
//-------------------------
//MARK: - ARSCNViewDelegate
//-------------------------
extension ViewController: ARSCNViewDelegate{
func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
//1. Check We Have Detected An ARPlaneAnchor
guard let planeAnchor = anchor as? ARPlaneAnchor else { return }
//2. Get The Size Of The ARPlaneAnchor
let width = CGFloat(planeAnchor.extent.x)
let height = CGFloat(planeAnchor.extent.z)
//3. Create An SCNPlane Which Matches The Size Of The ARPlaneAnchor
let imageHolder = SCNNode(geometry: SCNPlane(width: width, height: height))
//4. Rotate It
imageHolder.eulerAngles.x = -.pi/2
//5. Set It's Colour To Red
imageHolder.geometry?.firstMaterial?.diffuse.cOntents= UIColor.red
//4. Add It To Our Node & Thus The Hiearchy
node.addChildNode(imageHolder)
}
}
将此应用于您的案例:
在您的情况下,我们需要做一些额外的工作,因为您希望能够允许用户将图像应用于垂直平面.
因此,最好的办法是让您刚刚添加变量的节点,例如
class ViewController: UIViewController {
@IBOutlet var augmentedRealityView: ARSCNView!
let augmentedRealitySession = ARSession()
let cOnfiguration= ARWorldTrackingConfiguration()
var nodeWeCanChange: SCNNode?
}
因此你Delegate Callback
可能会这样:
//-------------------------
//MARK: - ARSCNViewDelegate
//-------------------------
extension ViewController: ARSCNViewDelegate{
func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
//1. If We Havent Create Our Interactive Node Then Proceed
if nodeWeCanChange == nil{
//a. Check We Have Detected An ARPlaneAnchor
guard let planeAnchor = anchor as? ARPlaneAnchor else { return }
//b. Get The Size Of The ARPlaneAnchor
let width = CGFloat(planeAnchor.extent.x)
let height = CGFloat(planeAnchor.extent.z)
//c. Create An SCNPlane Which Matches The Size Of The ARPlaneAnchor
nodeWeCanChange = SCNNode(geometry: SCNPlane(width: width, height: height))
//d. Rotate It
nodeWeCanChange?.eulerAngles.x = -.pi/2
//e. Set It's Colour To Red
nodeWeCanChange?.geometry?.firstMaterial?.diffuse.cOntents= UIColor.red
//f. Add It To Our Node & Thus The Hiearchy
node.addChildNode(nodeWeCanChange!)
}
}
}
现在您可以nodeWeCanChange
随时参考设置它的图像很简单!
每个SCNGeometry
都有一组材料,它们是:
一组着色属性,用于在渲染时定义几何体表面的外观.
在我们的例子中,我们正在寻找以下材料diffuse property
:
管理材质对光照的漫反应的对象.
然后是contents属性:
材质属性的可视内容 - 动画内容的颜色,图像或来源.
显然你需要处理这个的完整后勤,但是一个非常基本的例子可能看起来像假设你将你的图像存储到UIImage数组中,例如:
let imageGallery = [UIImage(named: "StackOverflow"), UIImage(named: "GitHub")]
我创建了一个IBAction
会SCNNode's Geometry
根据所UIButton
按下的标签改变我们的图像,例如:
/// Changes The Material Of Our SCNNode's Gemeotry To The Image Selected By The User
///
/// - Parameter sender: UIButton
@IBAction func changeNodesImage(_ sender: UIButton){
guard let imageToApply = imageGallery[sender.tag], let node = nodeWeCanChange else { return}
node.geometry?.firstMaterial?.diffuse.cOntents= imageToApply
}
这足以让你指出正确的方向...希望它有所帮助......