我正在尝试在Swift中做this。但是我的SCNView没有显示任何内容。我检查了IB中的连接,一切都很好。我假设在将源代码从 Objective C 转换为 Swift 时出错。这是我的代码:
@IBOutlet var sceneview: SCNView
@IBOutlet var status: NSTextField
var statusCounter: Int = 1
@IBAction func paintRectButton (sender: AnyObject) {
status.stringValue = "Paint (#\(statusCounter++))"
var scene: SCNScene = SCNScene()
var cameraNode: SCNNode = SCNNode()
cameraNode.camera = SCNCamera()
cameraNode.position = SCNVector3Make(0, 15, 30)
cameraNode.transform = CATransform3DRotate(cameraNode.transform, 7.0, 1, 0, 0)
scene.rootNode.addChildNode(cameraNode)
var spotlight: SCNLight = SCNLight()
spotlight.type = SCNLightTypeSpot
spotlight.color = NSColor.redColor()
var spotlightNode: SCNNode = SCNNode()
spotlightNode.light = spotlight
spotlightNode.position = SCNVector3Make(-2, 1, 0)
cameraNode.addChildNode(spotlightNode)
let boxSide = 15.0
var box: SCNBox =
SCNBox(width: boxSide, height: boxSide, length: boxSide, chamferRadius: 0)
var boxNode: SCNNode = SCNNode(geometry: box)
boxNode.transform = CATransform3DMakeRotation(3, 0, 1, 0)
scene.rootNode.addChildNode(boxNode)
sceneview.scene = scene
}
答案 0 :(得分:1)
没有任何显示的原因是相机正在朝着没有任何要渲染的几何对象的方向看。 Objective-C代码使用-M_PI/7.0
(≈-0.4488弧度)作为相机的旋转角度,但你的Swift代码使用7.0
(≈0.7168弧度(除以π后的余数))。将Swift代码更改为:
cameraNode.transform = CATransform3DRotate(cameraNode.transform, -M_PI/7.0, 1, 0, 0)
在原始代码使用角度M_PI_2/3
并且Swift代码使用角度3
时,框的旋转似乎发生了类似的错误。
boxNode.transform = CATransform3DMakeRotation(M_PI_2/3.0, 0, 1, 0)