【问题标题】:SCNScene: Calculate projected size of an objectSCNScene:计算对象的投影大小
【发布时间】:2015-02-26 02:38:48
【问题描述】:

与大多数游戏一样,我的游戏有一个场景和 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

    }

}

【问题讨论】:

  • 如果不改变xFov或yFov,发现Fov是60度。不过,不知道如何从点转换为像素。也想知道为什么我得到了否定。

标签: swift sprite-kit scenekit


【解决方案1】:
  1. 使用 getBoundingBoxMin(_:max:) 查找 3D 元素在 SceneKit 场景中占据的空间的角落。

  2. 使用projectPoint 将这些点从 3D 场景坐标转换为 2D 视图坐标。

  3. 使用 convertPointFromView 将 (UIKit) 视图坐标转换为 SpriteKit 场景的坐标系。

根据步骤 3 中的要点,您可以为 HUD 元素构建一个矩形或其他形状来避免。

【讨论】:

  • 我离找到解决方案还差得很远。
猜你喜欢
  • 2015-03-31
  • 2017-09-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-12
  • 2012-12-07
  • 2022-12-08
  • 1970-01-01
相关资源
最近更新 更多