SceneKit使用两个向量

时间:2016-11-16 11:14:12

标签: ios objective-c swift macos graphics

我正在寻找一种方法来轻松旋转SceneKit中的SCNNode。因此,假设我有两个向量h = (1 0 0)u = (0 1 0)h(在我看来)指向节点前进的方向,u指向上方。

轮播结束后,我希望hh' = (0 5 1)uu' = (0 -1 -5)。所以两个矢量之间的角度仍然是90度。

如何像这样轮换我的SCNNode

1 个答案:

答案 0 :(得分:0)

我不确定你想要做什么,但基本上你做的是(把代码放在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