Swift 在 SceneKit 场景中持续渲染新的子节点对象

Continuously rendering new child node objects on SceneKit scene in Swift

我有一个关于我正在做的 Scene Kit 项目的问题。所以我有这个场景,我随机生成立方体并将它们添加到我的场景根节点,问题是它们在开始时被渲染,但很快它们就停止渲染了,所以我没有看到这些新对象被生成,我当我点击屏幕时会再次看到它们(所以当我在场景中或其他什么地方做一个动作时)或者有时它们中的一些是随机渲染的,我不知道为什么

我已经尝试将 rendersContinuously 设置为 true 但这并没有改变任何东西。

这是立方体生成线程:

DispatchQueue.global(qos: .userInitiated).async {
            while true {
                self.spawnShape()
                sleep(1)
            }
 }

以下是我将它们添加到子节点的方法:

let geometryNode = SCNNode(geometry: geometry)
geometryNode.simdWorldPosition = simd_float3(Float.random(in: -10..<10), 2, shipLocation+Float.random(in: 20..<30))

self.mainView.scene!.rootNode.addChildNode(geometryNode)

这是应用于相机和我的主节点的无尽动作:

ship.runAction(SCNAction.repeatForever(SCNAction.moveBy(x: 0, y: 0, z: 30, duration: 1)))
camera.runAction(SCNAction.repeatForever(SCNAction.moveBy(x: 0, y: 0, z: 30, duration: 1)))

添加的 geometryNode 立方体停止渲染,除非我点击屏幕

如何在不触摸屏幕的情况下强制在场景中渲染这些新的子节点对象?

谢谢

ZAY 要求编辑:

我的代码基本上是这样开始的:

    override func viewDidLoad() {
        super.viewDidLoad()
                
        guard let scene = SCNScene(named: "art.scnassets/ship.scn")
            else { fatalError("Unable to load scene file.") }

        let scnView = self.view as! SCNView
        self.mainView = scnView
        self.mainView.rendersContinuously = true
        
        self.ship = scene.rootNode.childNode(withName: "ship", recursively: true)!
        self.camera = scene.rootNode.childNode(withName: "camera", recursively: true)!
        
        self.ship.renderingOrder = 1
        
        self.ship.simdWorldPosition = simd_float3(0, 0, 0)
        self.camera.simdWorldPosition = simd_float3(0, 15, -35)
        
        self.mainView.scene = scene
        
        DispatchQueue.global(qos: .userInitiated).async {
            while true {
                self.spawnShape()
                sleep(1)
            }
        }
        
        ship.runAction(SCNAction.repeatForever(SCNAction.moveBy(x: 0, y: 0, z: 30, duration: 1)))
        camera.runAction(SCNAction.repeatForever(SCNAction.moveBy(x: 0, y: 0, z: 30, duration: 1)))
        
        let tap = UILongPressGestureRecognizer(target: self, action: #selector(tapHandler))
        tap.minimumPressDuration = 0
        self.mainView.addGestureRecognizer(tap)
    }

当我点击屏幕时,调用此代码:

    func tapHandler(gesture: UITapGestureRecognizer) {
        let p = gesture.location(in: self.mainView)
        let turnDuration: Double = 0.3
        print("x: \(p.x) y: \(p.y)")

        if gesture.state == .began {
            if p.x >= self.screenSize.width / 2 {
                self.delta = 0.5
            }
            else {
                self.delta = -0.5
            }
            self.ship.runAction(SCNAction.rotateBy(x: 0, y: 0, z: self.delta, duration: turnDuration))
            return
        }

        if gesture.state == .changed {
            return
        }

        self.ship.runAction(SCNAction.rotateBy(x: 0, y: 0, z: -self.delta, duration: turnDuration))
    }

基本上它会根据我点击屏幕的哪一侧将我的船向右或向左旋转,并显示我在控制台中点击的坐标。当我释放时,船回到最初的position/rotation。

DispatchQueue.global(qos: .userInitiated).async {
        while true {
            self.spawnShape()
            sleep(1)
        }
}

你这样做不是在阻塞共享队列吗?如果您想定期 运行 使用定时器。