Spritekit - 沿y轴旋转SK3dNode

时间:2016-11-17 13:16:21

标签: ios sprite-kit scenekit

我在spritekit中有3D星,并且它沿着z轴旋转,所以它看起来像旋转。

然而,我不能让它旋转。 该文件是.dae文件,我已导入到我的项目中。 明星本身加载但没有任何反应。任何人都不知道吗?

import SpriteKit

override func didMove(to view: SKView) {
    let starScene = SCNScene.init(named: "star.dae")
    let star3d = SK3DNode(viewportSize: CGSize(width: 330, height: 330))
    star3d.position = CGPoint(x: self.frame.midX, y: self.frame.midY)
    star3d.scnScene = starScene
    star3d.zPosition = 3
    star3d.isPlaying = true
    star3d.scnScene = starScene

    let camera = SCNCamera()
    camera.xFov = 60
    camera.yFov = 60

    var cameraNode = SCNNode()
    cameraNode = SCNNode()
    cameraNode.camera = camera
    cameraNode.position = SCNVector3(x: 0, y: 0, z: 15)

    let rotationAnimation = CABasicAnimation(keyPath: "rotation")
    rotationAnimation.toValue = NSValue(scnVector4: SCNVector4Make(1, 2, 3, .pi * 2))
    rotationAnimation.duration = 6
    rotationAnimation.repeatCount = FLT_MAX

    starScene?.rootNode.addChildNode(cameraNode)
    star3d.pointOfView = cameraNode
    self.addChild(star3d)

    cameraNode.addAnimation(rotationAnimation, forKey: nil)
}

2 个答案:

答案 0 :(得分:2)

您已将star3d.pointOfView正确设置到相机,但尚未将cameraNode添加到场景中。

starScene.rootNode.addChildNode(cameraNode)

这是一个完整版本,位于macOS游乐场。交换最后两行(current.liveView)以查看原始SceneKit渲染或组合的SpriteKit渲染。

import Cocoa
import SceneKit
import SpriteKit
import PlaygroundSupport

let spritekitView = SKView(frame: CGRect(x: 0, y: 0, width: 800, height: 500))
let scenekitView = SCNView(frame: CGRect(x: 0, y: 0, width: 800, height: 500))

let spriteKitScene = SKScene(size: CGSize(width: 480, height: 320))
spritekitView.showsFPS = true
spritekitView.presentScene(spriteKitScene)
PlaygroundSupport.PlaygroundPage.current.liveView = spritekitView

// let starScene = SCNScene.init(named: "star.dae")
let starScene = SCNScene()
starScene.rootNode.addChildNode(SCNNode(geometry: SCNBox(width: 1, height: 2, length: 3, chamferRadius: 0.2)))

let star3d = SK3DNode(viewportSize: CGSize(width: 330, height: 330))
star3d.position = CGPoint(x: spriteKitScene.frame.midX, y: spriteKitScene.frame.midY)
star3d.scnScene = starScene
star3d.isPlaying = true
spriteKitScene.addChild(star3d)

let camera = SCNCamera()
camera.xFov = 60
camera.yFov = 60

var cameraNode = SCNNode()
cameraNode = SCNNode()
cameraNode.camera = camera
cameraNode.position = SCNVector3(x: 0, y: 3, z: 15)

starScene.rootNode.addChildNode(cameraNode)

star3d.pointOfView = cameraNode
scenekitView.scene = starScene
scenekitView.autoenablesDefaultLighting = true
scenekitView.pointOfView = cameraNode

let rotateVector = SCNVector4Make(0, 0, 1, CGFloat(2 * M_PI))
let rotate = SCNAction.repeatForever(SCNAction.rotateBy(x: 0.0, y: 0, z: CGFloat(2 * M_PI), duration: 10.0))
cameraNode.runAction(rotate)
let rotationAnimation = CABasicAnimation(keyPath: "rotation")
rotationAnimation.toValue = NSValue(scnVector4: rotateVector)
rotationAnimation.duration = 6
rotationAnimation.repeatCount = FLT_MAX
//cameraNode.addAnimation(rotationAnimation, forKey: nil)

PlaygroundPage.current.liveView = spritekitView
//PlaygroundPage.current.liveView = scenekitView

答案 1 :(得分:1)

感谢Hal Mueller。 使用以下代码将sk3dnode添加到spritekit中可以实现以下旋转星效果。

media0.giphy.com/media/rOhH4D15SlWCc/giphy.gif

如果动画未在您的设备上运行。 在项目.plist文件中添加以下行。 PrefersOpenGL = YES

let starScene = SCNScene.init(named: "Round_CStar_obj.obj.dae")
    let star3d = SK3DNode(viewportSize: CGSize(width: 300, height: 300))
    star3d.position = CGPoint(x: self.frame.midX, y: self.frame.midY)
    star3d.scnScene = starScene
    star3d.name = "star3d"
    star3d.zPosition = 3
    star3d.zRotation = 6
    star3d.isPlaying = true
    star3d.scnScene = starScene
    self.addChild(star3d)

    let camera = SCNCamera()
    camera.usesOrthographicProjection = true
    camera.orthographicScale = 9
    camera.zNear = 0
    camera.zFar = 200

    var cameraNode = SCNNode()
    cameraNode = SCNNode()
    cameraNode.camera = camera
    cameraNode.position = SCNVector3(x: 0, y: 0, z: 0)
    cameraNode.eulerAngles = SCNVector3(x: 0, y: 0, z: 0)
    starScene?.rootNode.addChildNode(cameraNode)
    star3d.pointOfView = cameraNode

    let rotationAnimation = CABasicAnimation(keyPath: "rotation")
    rotationAnimation.toValue = NSValue(scnVector4: SCNVector4Make(0, 1, 0, .pi * 2))
    rotationAnimation.duration = 2
    rotationAnimation.repeatCount = FLT_MAX
    cameraNode.addAnimation(rotationAnimation, forKey: nil)