和大多数游戏一样,我的游戏有一个场景和HUD (SKScene)。为了避免与HUD元素重叠,我需要计算主要对象的投影大小。因为对于每个iPhone模型,对象的HUD维度和投影大小是不同的,所以我需要一种动态地这样做的方法,为此我需要得到我的主对象的投影大小。
对我来说,最明显的方法是使用xFov和yFov,但出于某种原因,它们总是为零。usesOrthographicProjection设置为false,所以这并不是因为这个原因。
下面是一个简单的测试代码,它试图将场景的平面与HUD上的平面相等。
import UIKit
import QuartzCore
import SceneKit
import SpriteKit
class GameViewController: UIViewController {
var hud:SKScene!
override func viewDidLoad() {
super.viewDidLoad()
// create a new scene
let scene = SCNScene()
// create and add a camera to the scene
let cameraNode = SCNNode()
cameraNode.camera = SCNCamera()
scene.rootNode.addChildNode(cameraNode)
cameraNode.position = SCNVector3(x: 0, y: 0, z: 10)
// retrieve the SCNView
let scnView = self.view as SCNView
// set the scene to the view
scnView.scene = scene
let sCPlaneGeo = SCNPlane(width: 5, height: 5)
let sCPlane = SCNNode(geometry: sCPlaneGeo)
scene.rootNode.addChildNode(sCPlane)
hud = SKScene(size: view.bounds.size)
scnView.overlaySKScene = hud
let sKplane = SKShapeNode(rectOfSize: CGSizeMake(50, 50))
sKplane.lineWidth = 1
sKplane.position = CGPointMake(view.bounds.size.width/2, view.bounds.size.height/2)
sKplane.strokeColor = UIColor.redColor()
hud.addChild(sKplane)
println(cameraNode.camera?.xFov) //Prints Zero
}
}发布于 2014-12-30 06:53:18
getBoundingBoxMin(_:max:)查找3D元素在SceneKit场景中占据的空间的角落。projectPoint将这些点从三维场景坐标转换为2D视图坐标。convertPointFromView将(UIKit)视图坐标转换为SpriteKit场景的坐标系。从步骤3的点,您可以为您的HUD元素构造一个矩形或其他形状以避免。
https://stackoverflow.com/questions/27696847
复制相似问题