gpt4 book ai didi

ios - Spritekit - 沿其 y 轴旋转 SK3dNode

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:55:50 34 4
gpt4 key购买 nike

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

最佳答案

您已经正确地将star3d.pointOfView设置为相机,但是您还没有将cameraNode添加到场景中。

starScene.rootNode.addChildNode(cameraNode)

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

import Cocoaimport SceneKitimport SpriteKitimport PlaygroundSupportlet 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 = truespritekitView.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 = starScenestar3d.isPlaying = truespriteKitScene.addChild(star3d)let camera = SCNCamera()camera.xFov = 60camera.yFov = 60var cameraNode = SCNNode()cameraNode = SCNNode()cameraNode.camera = cameracameraNode.position = SCNVector3(x: 0, y: 3, z: 15)starScene.rootNode.addChildNode(cameraNode)star3d.pointOfView = cameraNodescenekitView.scene = starScenescenekitView.autoenablesDefaultLighting = truescenekitView.pointOfView = cameraNodelet 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 = 6rotationAnimation.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/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com