我正在寻找一种在 SceneKit 中轻松旋转 SCNNode
的方法。因此,假设我有两个向量 h = (1 0 0)
和 u = (0 1 0)
。 h
(在我看来)指向节点前进的方向,而u
指向上方。
旋转后,我希望 h
为 h' = (0 5 1)
且 u
为 u' = (0 -1 -5)
。所以两个向量之间的角度仍然是 90 度。
我怎样才能像这样旋转我的SCNNode
?
最佳答案
我不确定你到底想做什么,但基本上你所做的是(将代码放在 PlayGround 中):
import UIKit
import SceneKit
import PlaygroundSupport
let scnView = SCNView(frame: CGRect(x: 0, y: 0, width: 500, height: 500))
scnView.autoenablesDefaultLighting = true
let scene = SCNScene()
scnView.scene = scene
let light = SCNLight()
light.type = .directional
let lightNode = SCNNode()
lightNode.light = light
scene.rootNode.addChildNode(lightNode)
let camera = SCNCamera()
let cameraNode = SCNNode()
cameraNode.camera = camera
cameraNode.position = SCNVector3(0,0,10)
scene.rootNode.addChildNode(cameraNode)
let box = SCNBox(width: 1, height: 1, length: 1, chamferRadius: 0)
let boxNode = SCNNode(geometry: box)
boxNode.geometry?.firstMaterial?.diffuse.contents = UIColor.red
scene.rootNode.addChildNode(boxNode)
let rotateAction = SCNAction.rotateBy(x: 0, y: -1, z: 5, duration: 1)
let repeatAction = SCNAction.repeatForever(rotateAction)
SCNTransaction.begin()
SCNTransaction.animationDuration = 1
boxNode.runAction(repeatAction)
SCNTransaction.commit()
PlaygroundPage.current.liveView = scnView
关于ios - SceneKit 使用两个向量设置旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40630724/