OGeek|极客世界-中国程序员成长平台

标题: ios - ios中相应标记的标记检测和3d模型显示(使用ARkit) [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 19:00
标题: ios - ios中相应标记的标记检测和3d模型显示(使用ARkit)

以前我使用 vuforia(unity) 为 iOS 开发了一个 AR 应用程序。现在我必须使用 ARKit 实现相同的应用程序。

ARKit 很棒,除了没有标记检测。

我曾尝试使用视觉来检测标记,但到目前为止没有成功。 我可以提供一些用于标记检测和在 iOS 标记上显示 3d 模型的示例吗?

提前致谢。



Best Answer-推荐答案


有多种方法可以实现您的目标,但可以说最简单的方法是使用图像作为标记。

ARKit 1.5 开始,您可以使用 ReferenceImages 放置 AR 内容,这与您在 Vuforia 中使用的标记基本相同> 或 EasyAR.

referenceImage 仅供引用:

An image to be recognized in the real-world environment during a world-tracking AR session.

要使用这个功能,你需要传入一个:

collection of reference images to your session configuration's detectionImages property.

可以这样设置:

var detectionImages: Set<ARReferenceImage>! { get set }

ARKit 1.5 需要注意的重要一点是,与 Vuforia 不同,它可以允许对图像进行 扩展跟踪:

Image detection doesn't continuously track real-world movement of the image or track when the image disappears from view. Image detection works best for cases where AR content responds to static images in the scene—for example, identifying art in a museum or adding animated elements to a movie poster.

正如@Alexander 所说,了解这如何适合您的情况的最佳选择是在线查看 SampleCodeDocumentation,可在此处获得:

Recognizing Images In An AR Experience

然而,核心点是:

启用图像检测:

您需要先提供一个或多个ARReferenceImage 资源。这些可以使用 Xcode 中的 AR Assets 目录手动添加,记住您必须在 Xcode 中尽可能准确地输入图像的物理尺寸,因为 ARKit 依靠此信息来确定图像与相机的距离。

ARReferenceImages 也可以使用以下方法即时创建:

init(CGImage, orientation: CGImagePropertyOrientation, physicalWidth: CGFloat)

Which creates a new reference image from a Core Graphics image object.

init(CVPixelBuffer, orientation: CGImagePropertyOrientation, physicalWidth: CGFloat)

Which creates a new reference image from a Core Video pixel buffer.

完成此操作后,您需要创建一个世界跟踪配置,在其中传递您的 ARReferenceImages,然后再运行您的 ARSession,例如:

guard let referenceImages = ARReferenceImage.referenceImages(inGroupNamed: "AR Resources", bundle: nil) else { return }

let configuration = ARWorldTrackingConfiguration()
configuration.detectionImages = referenceImages
session.run(configuration, options: [.resetTracking, .removeExistingAnchors])

处理图像检测:

当您的 ARSession 检测到 ARReferenceImage 并创建 ARImageAnchor 时,它仅提供:

Information about the position and orientation of an image detected in a world-tracking AR session.

因此,如果图像检测成功,您将需要使用以下 ARSCNViewDelegate 回调来处理对象的放置等:

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

使用它来处理 3D 内容放置的示例如下:

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

        //1. If Out Target Image Has Been Detected Than Get The Corresponding Anchor
        guard let currentImageAnchor = anchor as? ARImageAnchor else { return }

        //2. Get The Targets Name
        let name = currentImageAnchor.referenceImage.name!

        //3. Get The Targets Width & Height
        let width = currentImageAnchor.referenceImage.physicalSize.width
        let height = currentImageAnchor.referenceImage.physicalSize.height

        //4. Log The Reference Images Information
        print("""
            Image Name = \(name)
            Image Width = \(width)
            Image Height = \(height)
            """)

        //5. Create A Plane Geometry To Cover The ARImageAnchor
        let planeNode = SCNNode()
        let planeGeometry = SCNPlane(width: width, height: height)
        planeGeometry.firstMaterial?.diffuse.contents = UIColor.white
        planeNode.opacity = 0.25
        planeNode.geometry = planeGeometry

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

        //7. The Node Is Centered In The Anchor (0,0,0)
        node.addChildNode(planeNode)

        //8. Create AN SCNBox
        let boxNode = SCNNode()
        let boxGeometry = SCNBox(width: 0.1, height: 0.1, length: 0.1, chamferRadius: 0)

        //9. Create A Different Colour For Each Face
        let faceColours = [UIColor.red, UIColor.green, UIColor.blue, UIColor.cyan, UIColor.yellow, UIColor.gray]
        var faceMaterials = [SCNMaterial]()

        //10. Apply It To Each Face
        for face in 0 ..< 5{
            let material = SCNMaterial()
            material.diffuse.contents = faceColours[face]
            faceMaterials.append(material)
        }
        boxGeometry.materials = faceMaterials
        boxNode.geometry = boxGeometry

        //11. Set The Boxes Position To Be Placed On The Plane (node.x + box.height)
        boxNode.position = SCNVector3(0 , 0.05, 0)

        //12. Add The Box To The Node
        node.addChildNode(boxNode)
    }

希望对你有帮助...

关于ios - ios中相应标记的标记检测和3d模型显示(使用ARkit),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46768493/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4