使用SK3DNode来显示SpriteKit中的SceneKit粒子对象

时间:2023-01-22 22:54:28

I've seen that SceneKit contains a nice 3D starfield particle effect:

我看到SceneKit包含一个不错的3D starfield粒子效应:

使用SK3DNode来显示SpriteKit中的SceneKit粒子对象

I've tried to use a SK3DNode to add this particle effect to my background. Here's my code:

我尝试使用SK3DNode将这个粒子效果添加到我的背景中。这是我的代码:

    import SpriteKit
    import SceneKit

    class GameScene: SKScene {

    override func didMoveToView(view: SKView) {
...

      addStarfield()
    }



    func addStarfield() {
        // create a new scene
        let scene = SCNScene()

        // create and add a camera to the scene
        let cameraNode = SCNNode()
        cameraNode.camera = SCNCamera()
        scene.rootNode.addChildNode(cameraNode)

        // place the camera
        cameraNode.position = SCNVector3(x: 0, y: 0, z: 15)

        let ps = SCNParticleSystem(named: "Stars.scnp", inDirectory: "")
        scene.rootNode.addParticleSystem(ps)

        let starFieldNode = SK3DNode()
        starFieldNode.scnScene = scene
        starFieldNode.zPosition = -100
        self.addChild(starFieldNode)
    }
}

Unfortunately this doesn't work. I only see a gray background without stars. Also my normal SpriteKit sprites are no longer visible.

不幸的是,这是行不通的。我只看到没有星星的灰色背景。我的正常精灵精灵也不再可见。

What have I done wrong?

我做错了什么?

Thanks for your help, Stefan

谢谢你的帮助斯特凡

1 个解决方案

#1


1  

The SpriteKit renderer doesn't support all the features of SceneKit, particularly those that can involve multipass rendering like particles, depth of field, and techniques. This is probably a good bug to file.

SpriteKit渲染器不支持SceneKit的所有特性,特别是那些涉及到粒子、景深和技术等多路渲染的特性。这可能是一个很好的bug。

Some possible workarounds, depending on what else is going on in your app:

一些可能的解决办法,取决于你的应用里还有什么:

  • Use a physics field to make particles appear to flee from a center point, which should give you a similar starfield effect, especially if you couple it with a variation in particle scale over time.

    使用一个物理场使粒子看起来从中心点逃逸,这应该会给你一个类似的星场效应,特别是如果你把它与随时间变化的粒子尺度联系起来。

  • Instead of embedding a SceneKit scene in your SpriteKit view, embed a SpriteKit scene in a SceneKit view.

    与其将SceneKit场景嵌入到SpriteKit视图中,不如将SpriteKit场景嵌入到SceneKit视图中。

#1


1  

The SpriteKit renderer doesn't support all the features of SceneKit, particularly those that can involve multipass rendering like particles, depth of field, and techniques. This is probably a good bug to file.

SpriteKit渲染器不支持SceneKit的所有特性,特别是那些涉及到粒子、景深和技术等多路渲染的特性。这可能是一个很好的bug。

Some possible workarounds, depending on what else is going on in your app:

一些可能的解决办法,取决于你的应用里还有什么:

  • Use a physics field to make particles appear to flee from a center point, which should give you a similar starfield effect, especially if you couple it with a variation in particle scale over time.

    使用一个物理场使粒子看起来从中心点逃逸,这应该会给你一个类似的星场效应,特别是如果你把它与随时间变化的粒子尺度联系起来。

  • Instead of embedding a SceneKit scene in your SpriteKit view, embed a SpriteKit scene in a SceneKit view.

    与其将SceneKit场景嵌入到SpriteKit视图中,不如将SpriteKit场景嵌入到SceneKit视图中。