【问题标题】:Swift Error – Attempting to add a Parent node as a Child nodeSwift 错误 – 尝试将父节点添加为子节点
【发布时间】:2018-10-04 19:23:48
【问题描述】:

我正在尝试获取我拥有的.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
    }
}

【问题讨论】:

    标签: swift nodes scenekit augmented-reality arkit


    【解决方案1】:

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

    或者在你的情况下:

    motorScene.rootNode.addChildNode(motorNode)
    

    【讨论】:

    • 谢谢! idk为什么我以前没有看到这个问题。虽然,我尝试实现“motorScene.rootNode.addChildNode(motorNode)”并尝试“(planeNode.addChildNode(motorNode)node.addChildNode(planeNode)”。两者都给我留下了错误消息(“Scene 在另一个场景的渲染回调")
    • 你在连接什么?请给我一份草稿!
    • let scene = SCNScene(named: "Art.scnassets/LoadScene.scn")! 是干什么用的?
    • 您可以使用let motorScene = SCNScene(named: "Art.scnassets/ACMotor/ACMotor.dae")!作为主要场景。
    • 就像我们最喜欢的船一样:let scene = SCNScene(named: "art.scnassets/ship.scn")!
    【解决方案2】:

    这一行

    motorNode.addChildNode(motorNode)
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-26
      • 2020-08-25
      相关资源
      最近更新 更多