我有一个spritekit场景,其中嵌入了一个小Scenekit场景。 即使我在相机上添加了SCNLookAtConstraint,它似乎也不指向盒子的中央。然后,每次单击时,我都会在x-z平面中将摄像机绕一个圆圈移动,因此我希望看到立方体在旋转,但是它会离开屏幕。有人知道我可能做错了吗?
代码如下:
import SpriteKit
import GameplayKit
class GameScene: SKScene {
var clicks: Int = 0
var cubeNodes = SCNNode()
var cameraNode = SCNNode()
var node = SK3DNode()
override func didMove(to view: SKView) {
let backgroundSquare = SKShapeNode(rectOf: CGSize(width: 400, height: 400))
backgroundSquare.position = CGPoint(x: 0, y: 0)
backgroundSquare.fillColor = UIColor.black
backgroundSquare.zPosition = 1
self.addChild(backgroundSquare)
let scnScene: SCNScene = SCNScene()
let cubeGeometrys = SCNBox(width: 2, height: 2, length: 2, chamferRadius: 0.0)
cubeGeometrys.name = "cubesG"
cubeGeometrys.firstMaterial?.diffuse.contents = UIColor.red
cubeNodes = SCNNode(geometry: cubeGeometrys)
cubeNodes.position = SCNVector3(0, 0, 0)
cubeNodes.name = "cubesN"
scnScene.rootNode.addChildNode(cubeNodes)
node = SK3DNode(viewportSize: CGSize(width: 400, height: 400))
node.scnScene = scnScene
node.position = CGPoint(x: 0, y: 0)
node.name = "3dnode"
let camera = SCNCamera()
let cameraNode = SCNNode()
cameraNode.camera = camera
node.pointOfView = cameraNode
let constraint = SCNLookAtConstraint(target: cubeNodes)
constraint.isGimbalLockEnabled = true
node.pointOfView?.constraints = [ constraint ]
node.pointOfView?.position = SCNVector3(x: 5*sin(0), y: 1, z: 5*cos(0))
node.zPosition = 2
self.addChild(node)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
node.pointOfView?.position.x = 5*sin(Float(clicks)/4)
node.pointOfView?.position.z = 5*cos(Float(clicks)/4)
clicks = clicks + 1
}
}