Reputation: 1252
I'm trying to do this in Swift. However my SCNView doesn't show anything. I checked the connections in IB and everything is fine. I assume that I made an error while translating the source code from Objective C to Swift. Here is my code:
@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
}
Upvotes: 1
Views: 1056
Reputation: 56625
The reason why nothing shows up is that the camera is looking in a direction where there isn't any geometry object to be rendered. The Objective-C code uses -M_PI/7.0
(≈ -0.4488 radians) for the rotation angle of the camera and but your Swift code is using 7.0
(≈ 0.7168 radians (the remainder after dividing by π)). Change the Swift code to:
cameraNode.transform = CATransform3DRotate(cameraNode.transform, -M_PI/7.0, 1, 0, 0)
A similar mistake seem to have happened with the rotation of the box where the original code uses the angle M_PI_2/3
and the Swift code is using the angle 3
.
boxNode.transform = CATransform3DMakeRotation(M_PI_2/3.0, 0, 1, 0)
Upvotes: 1