1

我正在尝试在 Swift中执行此操作。但是我的 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
}
4

1 回答 1

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)
于 2014-06-25T08:50:00.367 回答