我希望能够围绕立方体(立方体的集合)旋转相机,但出于某种原因,当我旋转时,旋转会向多个方向旋转。
我有一个对象,我希望旋转并将其设置为约束。此对象位于多维数据集的中间。
我有一个可以旋转的滑动手势识别器......但它刚刚关闭。我错过了一些东西......
RowCreated
编辑:
设置变换而不是旋转似乎有更好的效果
import UIKit
import QuartzCore
import SceneKit
import AVFoundation
class GameViewController: UIViewController {
var scene:SCNScene!
var tiles:[SCNNode] = [SCNNode]()
var cameraNode:SCNNode!
var centerNode:SCNNode!
override func viewDidLoad() {
super.viewDidLoad()
// create a new scene
scene = SCNScene()
let sideMaterial = SCNMaterial()
sideMaterial.diffuse.contents = UIColor.greenColor()
sideMaterial.locksAmbientWithDiffuse = true;
let geom = SCNBox(width: 32, height: 32.0, length: 32, chamferRadius: 8.0)
geom.widthSegmentCount = 16
geom.heightSegmentCount = 16
geom.lengthSegmentCount = 16
geom.materials = [sideMaterial, sideMaterial, sideMaterial, sideMaterial, sideMaterial, sideMaterial]
centerNode = SCNNode(geometry: geom)
centerNode.position = SCNVector3(x: 4*32, y: -4*32, z: -4*32)
scene.rootNode.addChildNode(centerNode)
// create and add a camera to the scene
cameraNode = SCNNode()
cameraNode.camera = SCNCamera()
scene.rootNode.addChildNode(cameraNode)
// place the camera
cameraNode.position = SCNVector3(x: -4*32, y: -4*32, z: 4*32)
cameraNode.pivot = SCNMatrix4MakeTranslation(4*32, -4*32, -4*32)
cameraNode.rotation = SCNVector4(x: 1, y: 0, z: 0, w: 1.360646)
let lookAt = SCNLookAtConstraint(target: centerNode)
//lookAt.gimbalLockEnabled = true
cameraNode.constraints = [lookAt]
cameraNode.camera?.xFov = 90
cameraNode.camera?.yFov = 90
cameraNode.camera?.zNear = 1
cameraNode.camera?.zFar = 5000
// create and add a light to the scene
let lightNode = SCNNode()
lightNode.light = SCNLight()
lightNode.light!.type = SCNLightTypeOmni
lightNode.position = SCNVector3(x: 4*32, y: -4*32, z: 10)
scene.rootNode.addChildNode(lightNode)
// create and add an ambient light to the scene
let ambientLightNode = SCNNode()
ambientLightNode.light = SCNLight()
ambientLightNode.light!.type = SCNLightTypeAmbient
ambientLightNode.light!.color = UIColor.darkGrayColor()
scene.rootNode.addChildNode(ambientLightNode)
// retrieve the SCNView
let scnView = self.view as! SCNView
setupTiles()
// set the scene to the view
scnView.scene = scene
// allows the user to manipulate the camera
//scnView.allowsCameraControl = true
// show statistics such as fps and timing information
scnView.showsStatistics = true
// configure the view
scnView.backgroundColor = UIColor.blackColor()
// Add gestures
var gestureRecognizers = [UIGestureRecognizer]()
let swipeDownGesture = UISwipeGestureRecognizer(target: self, action: "swipe:")
swipeDownGesture.direction = UISwipeGestureRecognizerDirection.Down
gestureRecognizers.append(swipeDownGesture)
let swipeUpGesture = UISwipeGestureRecognizer(target: self, action: "swipe:")
swipeUpGesture.direction = UISwipeGestureRecognizerDirection.Up
gestureRecognizers.append(swipeUpGesture)
let swipeLeftGesture = UISwipeGestureRecognizer(target: self, action: "swipe:")
swipeLeftGesture.direction = UISwipeGestureRecognizerDirection.Left
gestureRecognizers.append(swipeLeftGesture)
let swipeRightGesture = UISwipeGestureRecognizer(target: self, action: "swipe:")
swipeRightGesture.direction = UISwipeGestureRecognizerDirection.Right
gestureRecognizers.append(swipeRightGesture)
if let existingGestureRecognizers = scnView.gestureRecognizers as? [UIGestureRecognizer] {
gestureRecognizers.extend(existingGestureRecognizers)
}
scnView.gestureRecognizers = gestureRecognizers
}
func swipe(gestureRecognize: UISwipeGestureRecognizer) {
var leftRight:Float = 0
var upDown:Float = 0
var amount:Float = 1.5707
switch gestureRecognize.direction
{
case UISwipeGestureRecognizerDirection.Up:
upDown = 1
break
case UISwipeGestureRecognizerDirection.Down:
upDown = -1
break
case UISwipeGestureRecognizerDirection.Left:
leftRight = -1
break
case UISwipeGestureRecognizerDirection.Right:
leftRight = 1
break
default:
break
}
SCNTransaction.begin()
SCNTransaction.setAnimationDuration(3.0)
cameraNode.rotation = SCNVector4(x: leftRight, y: upDown, z: 0, w: amount)
SCNTransaction.commit()
}
override func shouldAutorotate() -> Bool {
return true
}
override func prefersStatusBarHidden() -> Bool {
return true
}
// override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
// if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
// return UIInterfaceOrientationMask.AllButUpsideDown
// } else {
// return UIInterfaceOrientationMask.All
// }
// }
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Release any cached data, images, etc that aren't in use.
}
func setupTiles()
{
for z in 0...7
{
for y in 0...7
{
for x in 0...7
{
if y > 0 && y < 7 && z > 0 && z < 7 && x > 0 && x < 7
{
continue
}
let topMaterial = SCNMaterial()
topMaterial.diffuse.contents = UIColor.whiteColor()
topMaterial.locksAmbientWithDiffuse = true;
let sideMaterial = SCNMaterial()
sideMaterial.diffuse.contents = UIColor.whiteColor()
sideMaterial.locksAmbientWithDiffuse = true;
let geom = SCNBox(width: 32, height: 32.0, length: 32, chamferRadius: 8.0)
geom.widthSegmentCount = 16
geom.heightSegmentCount = 16
geom.lengthSegmentCount = 16
geom.materials = [topMaterial, sideMaterial, sideMaterial, sideMaterial, sideMaterial, sideMaterial]
let node = SCNNode(geometry: geom)
node.position = SCNVector3(x: Float(x) * 32.0, y: -(Float(y) * 32.0), z: -(Float(z) * 32.0))
scene.rootNode.addChildNode(node)
tiles.append(node)
}
}
}
}
}