gpt4 book ai didi

swift - ARSceneView 中使用哪个图像作为引用?

转载 作者:行者123 更新时间:2023-11-30 10:44:48 25 4
gpt4 key购买 nike

我目前正在制作一个使用图像跟踪的 AR 应用程序。我希望它在引用图像文件夹中有 2 个图像(图像 A:~670x210,图像 B:~1123x794)。

如何检查相机正在检测哪张图像?例如,如果相机看到图像 A - 我想制作一个两倍于物理对象大小的平面,如果它看到图像 B - 我想使平面与物理对象大小相同。

如果需要的话,这是我的代码片段:


override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)

// Create a session configuration
let configuration = ARImageTrackingConfiguration()

if let trackedImage = ARReferenceImage.referenceImages(inGroupNamed: "ARpaperImage", bundle: Bundle.main) {

configuration.trackingImages = trackedImage
configuration.maximumNumberOfTrackedImages = 1

}

// Run the view's session
sceneView.session.run(configuration)
}


// MARK: - ARSCNViewDelegate

func renderer(_ renderer: SCNSceneRenderer, nodeFor anchor: ARAnchor) -> SCNNode? {

let node = SCNNode()

if let imageAnchor = anchor as? ARImageAnchor {


let plane = SCNPlane(width: imageAnchor.referenceImage.physicalSize.width, height: imageAnchor.referenceImage.physicalSize.height)
plane.firstMaterial?.diffuse.contents = UIColor(white: 1.0, alpha: 0,5)

let planeNode = SCNNode(geometry: plane)

planeNode.eulerAngles.x = -.pi / 2

node.addChildNode(planeNode)

}

return node

}


最佳答案

ARReferenceImage 有一个 name 属性,您可以使用它来确定检测到哪个 ReferenceImage,这很简单:

A descriptive name for the image

因此,当您将 ARReferenceImages 放入资源包中时,您可以(可能已经)给它们命名,例如:

enter image description here

您可以在以下委托(delegate)回调中使用这些名称来满足您的要求。

func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) 

因此,假设我们有两个名为 Brown 和 Choco 的 ARReferenceImages。使用这些名称,我们可以创建逻辑来显示不同的内容,例如:

  //-------------------------
//MARK: - ARSCNViewDelegate
//-------------------------

extension ViewController: ARSCNViewDelegate{

func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {

//1. Check We Have Detected An ARImageAnchor
guard let validAnchor = anchor as? ARImageAnchor, let referenceImageName = validAnchor.referenceImage.name else { return }

//2. Get The Physical Size Of The Reference Image (This Is Specificed When You Create Your Target In The ARResource Bundle)
let physicalSizeOfReferenceImage = CGSize(width: validAnchor.referenceImage.physicalSize.width, height: validAnchor.referenceImage.physicalSize.height)

///3. Log The Data For Debugging Purposes
print("""
Detected ARAnchorID = \(validAnchor.identifier)
Detected Reference Image Name = \(referenceImageName)
Detected Reference Image Physical Size = (width) \(physicalSizeOfReferenceImage.width),
Detected Reference Image Physical Size = (height) \(physicalSizeOfReferenceImage.height)
""")


//4. Perform Instanciation & Other Login Based On The Reference Image Detected
if referenceImageName == "Brown"{

node.addChildNode(planeNodeOfSize(CGSize(width: physicalSizeOfReferenceImage.width * 2, height: physicalSizeOfReferenceImage.height * 2)))

}else if referenceImageName == "Choco"{

node.addChildNode(planeNodeOfSize(CGSize(width: physicalSizeOfReferenceImage.width, height: physicalSizeOfReferenceImage.height)))

}
}

/// Generates An SCNNode With An SCNPlaneGeometry Of A Specified Width & Height
///
/// - Parameter size: CGSize
/// - Returns: SCNNode
func planeNodeOfSize(_ size: CGSize) -> SCNNode{

//1. Create An SCNPlane Of Our Chosen Size
let planeNode = SCNNode()
let planeGeometry = SCNPlane(width: size.width, height: size.height )
planeGeometry.firstMaterial?.diffuse.contents = UIColor.white
planeNode.opacity = 0.25
planeNode.geometry = planeGeometry

//2. Rotate The PlaneNode To Horizontal
planeNode.eulerAngles.x = -.pi/2

//3. Return Our Node
return planeNode
}

}

希望对你有帮助...

关于swift - ARSceneView 中使用哪个图像作为引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55960317/

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