gpt4 book ai didi

swift - 使用 ARKit 将图库中的图像放置在墙上

转载 作者:搜寻专家 更新时间:2023-10-31 22:01:09 25 4
gpt4 key购买 nike

我有一个来自服务器并存储在图库中的图像列表。我想使用 ARKit 选择任何图像并将其放置在动态墙上,并希望将图像转换为 3d 图像 tp 执行缩放、移动图像等操作。

谁能指导我如何在 AR 中创建自定义对象?

最佳答案

要在 ARKit 中检测垂直表面(例如墙壁),您需要首先设置 ARWorldTrackingConfiguration,然后在您的应用中启用 planeDetection

因此,在您的类声明下,您将创建以下变量:

@IBOutlet var augmentedRealityView: ARSCNView!
let augmentedRealitySession = ARSession()
let configuration = ARWorldTrackingConfiguration()

然后初始化你的 ARSessionViewDidLoad 例如:

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) { }

这很简单:

Tells the delegate that a SceneKit node corresponding to a new AR anchor has been added to the scene.

在这个方法中,我们将明确地寻找任何已检测到的 ARPlaneAnchors,它为我们提供:

Information about the position and orientation of a real-world flat surface detected in a world-tracking AR session.

因此,将 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 都有一组 Material ,它们是:

A set of shading attributes that define the appearance of a geometry's surface when rendered.

在我们的例子中,我们正在寻找 Material 漫反射属性,即:

An object that manages the material’s diffuse response to lighting.

然后是内容属性:

The visual contents of the material property—a color, image, or source of animated content.

显然你需要处理这一切的后勤工作,但是一个非常基本的例子可能看起来像假设你将图像存储到一个 UIImage 数组中,例如:

let imageGallery = [UIImage(named: "StackOverflow"), UIImage(named: "GitHub")]

我创建了一个 IBAction,它将根据按下的 UIButton 标签更改我们的 SCNNode's Geometry 的图像,例如:

/// 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

}

这足以为您指明正确的方向...希望对您有所帮助...

关于swift - 使用 ARKit 将图库中的图像放置在墙上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51888939/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com