Swift错误–尝试将父节点添加为子节点

时间:2018-10-04 19:23:49

标签: swift nodes scenekit augmented-reality arkit

我正在尝试获取一个.dae文件,并在它识别出照片中的第一个图像文件后将其显示。不幸的是,我收到一个错误,将父节点添加为子节点,并且不了解发生在哪里。

有什么想法吗?

import UIKit
import SceneKit
import ARKit

class PlaneTrackingViewController: UIViewController, ARSCNViewDelegate {

    @IBOutlet weak var ARPlaneView: ARSCNView!

    override func viewDidLoad() {
        super.viewDidLoad()

        ARPlaneView.delegate = self

        let scene = SCNScene(named: "Art.scnassets/LoadScene.scn")!

        ARPlaneView.scene = scene
    }

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

        let configuration = ARImageTrackingConfiguration()

        guard let trackedImages = ARReferenceImage.referenceImages(inGroupNamed: "Photos", bundle: Bundle.main) else {print ("No Images Available")
            return
        }

        configuration.trackingImages = trackedImages
        configuration.maximumNumberOfTrackedImages = 1
        ARPlaneView.session.run(configuration)
    }

    override func viewWillDisappear(_ animated: Bool) {
        ARPlaneView.session.pause()
    }

    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, alpha: 0.6)

            let planeNode = SCNNode(geometry: plane)
            planeNode.eulerAngles.x = -.pi / 2

            let motorScene = SCNScene(named: "Art.scnassets/ACMotor/ACMotor.dae")!
            let motorNode = motorScene.rootNode.childNodes.first!

            motorNode.position = SCNVector3Zero
            motorNode.position.z = 0.3

            motorNode.addChildNode(motorNode)
            node.addChildNode(planeNode)
        }           
        return node
    }
}

2 个答案:

答案 0 :(得分:2)

  

应该这样写:scene.rootNode.addChildNode(node)

或您的情况:

motorScene.rootNode.addChildNode(motorNode)

答案 1 :(得分:1)

此行

motorNode.addChildNode(motorNode)

告诉我您正在尝试将motorNode添加到motorNode中,这显然是这里的错误-您正在向自身添加节点。