OStack程序员社区-中国程序员成长平台

标题: ios - Spritekit - 沿 y 轴旋转 SK3dNode [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 12:23
标题: ios - Spritekit - 沿 y 轴旋转 SK3dNode

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



Best Answer-推荐答案


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

starScene.rootNode.addChildNode(cameraNode)

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

进口 cocoa 
导入场景套件
导入 SpriteKit
导入 Playground 支持

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

让 spriteKitScene = SKScene(大小:CGSize(宽度:480,高度:320))
spritekitView.showsFPS = true
spritekitView.presentScene(spriteKitScene)
PlaygroundSupport.PlaygroundPage.current.liveView = spritekitView

//让 starScene = SCNScene.init(named: "star.dae")
让starScene = SCNScene()
starScene.rootNode.addChildNode(SCNNode(几何:SCNBox(宽度:1,高度:2,长度:3,倒角半径:0.2)))

让star3d = SK3DNode(viewportSize:CGSize(宽度:330,高度:330))
star3d.position = CGPoint(x: spriteKitScene.frame.midX, y: spriteKitScene.frame.midY)
star3d.scnScene = starScene
star3d.isPlaying = true
spriteKitScene.addChild(star3d)

让相机 = SCNCamera()
相机.xFov = 60
camera.yFov = 60

var cameraNode = SCNNode()
cameraNode = SCNNode()
cameraNode.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

让 rotateVector = SCNVector4Make(0, 0, 1, CGFloat(2 * M_PI))
让旋转 = SCNAction.repeatForever(SCNAction.rotateBy(x: 0.0, y: 0, z: CGFloat(2 * M_PI), 持续时间: 10.0))
cameraNode.runAction(旋转)
让 rotationAnimation = CABasicAnimation(keyPath: "rotation")
rotationAnimation.toValue = NSValue(scnVector4: rotateVector)
旋转动画.duration = 6
旋转动画.repeatCount = FLT_MAX
//cameraNode.addAnimation(rotationAnimation, forKey: nil)

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

关于ios - Spritekit - 沿 y 轴旋转 SK3dNode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40655943/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4