使用arkit2检测对象时无法绘制boundBox

问题描述 投票:1回答:1

我正在开发iOS应用,该应用将在现实世界中用轮廓框突出显示对象(不是我们使用.arobject文件所做的特定对象)。想法是仅实现this文档/示例中的boundBox绘图。

this stackoverflow答案中得到了一些想法,但仍然无法将轮廓定的boundBox绘制到扫描的对象。

// Declaration
let configuration = ARObjectScanningConfiguration()
let augmentedRealitySession = ARSession()

// viewWillAppear(_ animated: Bool)
configuration.planeDetection = .horizontal
sceneView.session.run(configuration, options: .resetTracking)

// renderer
func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
        //print("\(self.detectionObjects.debugDescription)")
        guard let objectAnchor = anchor as? ARObjectAnchor else { return }

        //2. Create A Bounding Box Around Our Object
        let scale = CGFloat(objectAnchor.referenceObject.scale.x)
        let boundingBoxNode = BlackMirrorzBoundingBox(points: objectAnchor.referenceObject.rawFeaturePoints.points, scale: scale)
        node.addChildNode(boundingBoxNode)

    }

// BlackMirrorzBoundingBox class

init(points: [float3], scale: CGFloat, color: UIColor = .cyan) {
        super.init()

        var localMin = float3(repeating: Float.greatestFiniteMagnitude)
        var localMax = float3(repeating: -Float.greatestFiniteMagnitude)

        for point in points {
            localMin = min(localMin, point)
            localMax = max(localMax, point)
        }

        self.simdPosition += (localMax + localMin) / 2
        let extent = localMax - localMin

        let wireFrame = SCNNode()
        let box = SCNBox(width: CGFloat(extent.x), height: CGFloat(extent.y), length: CGFloat(extent.z), chamferRadius: 0)
        box.firstMaterial?.diffuse.contents = color
        box.firstMaterial?.isDoubleSided = true
        wireFrame.geometry = box
        setupShaderOnGeometry(box)
        self.addChildNode(wireFrame)
    }

func setupShaderOnGeometry(_ geometry: SCNBox) {
        guard let path = Bundle.main.path(forResource: "wireframe_shader", ofType: "metal", inDirectory: "art.scnassets"),
            let shader = try? String(contentsOfFile: path, encoding: .utf8) else {

                return
        }

        geometry.firstMaterial?.shaderModifiers = [.surface: shader]
    }

根据上述逻辑,我只在平面上得到框,而不是this图片中的轮廓框。

ios swift arkit
1个回答
0
投票

您可能缺少wireframe_shader着色器文件。确保将其添加到art.scnassets项目中,然后尝试重新加载该应用程序。

您可以在this repository中找到类似的着色器,不要忘记在代码中更改着色器文件的名称或资源名称。

© www.soinside.com 2019 - 2024. All rights reserved.