user3482617 user3482617 - 18 days ago 11
iOS Question

Spritekit - Rotate a SK3dNode along its y axis

I have 3D star in spritekit and have it rotating along z axis so it looks like its spinning.

However I cannot get it to spin.
The file is a

.dae
file which I have imported into my project.
The star itself loads but nothing happens. Any idea anyone?

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)
}

Answer

You have correctly set star3d.pointOfView to the camera, but you haven't added cameraNodeto the scene.

starScene.rootNode.addChildNode(cameraNode)

Here's a complete version, in a macOS playground. Swap out the last two lines (current.liveView) to see the raw SceneKit render or the combined SpriteKit render.

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