gpt4 book ai didi

cocoa - Swift 中的 SCNScene 问题 : nothing shows in SCNView

转载 作者:行者123 更新时间:2023-11-28 07:18:03 25 4
gpt4 key购买 nike

我正在尝试做 this在 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
}

最佳答案

没有任何显示的原因是相机正在观察没有任何要渲染的几何对象的方向。 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)

关于cocoa - Swift 中的 SCNScene 问题 : nothing shows in SCNView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24403662/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com