ios - Spritekit - 沿 y 轴旋转 SK3dNode
<p><p>我在 spritekit 中有一个 3D 星星,让它沿 z 轴旋转,所以它看起来像是在旋转。 </p>
<p>但是我无法让它旋转。
该文件是我已导入项目的 <code>.dae</code> 文件。
星星本身加载但没有任何反应。有人知道吗?</p>
<pre><code>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)
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>您已将 <code>star3d.pointOfView</code> 正确设置为相机,但您尚未将 <code>cameraNode</code> 添加到场景中。</p>
<pre><code>starScene.rootNode.addChildNode(cameraNode)
</code></pre>
<p>这是一个完整版本,位于 macOSPlayground 。交换最后两行 (<code>current.liveView</code>) 以查看原始 SceneKit 渲染或组合的 SpriteKit 渲染。</p>
<pre>进口 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
</pre></p>
<p style="font-size: 20px;">关于ios - Spritekit - 沿 y 轴旋转 SK3dNode,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/40655943/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/40655943/
</a>
</p>
页:
[1]