SCNScene:计算对象的投影大小 [英] SCNScene: Calculate projected size of an object

查看:70
本文介绍了SCNScene:计算对象的投影大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与大多数游戏一样,我的游戏有场景和 HUD (SKScene).为避免与 HUD 元素重叠,我需要计算主对象的投影大小.由于每个 iPhone 型号的 HUD 尺寸和对象的投影大小都不同,因此我需要一种动态执行此操作的方法,为此我需要获取主对象的投影大小.

My game has a scene and HUD (SKScene) as in most games. To avoid overlap with HUD elements, I need to calculate the projected size of my main object. Because the HUD dimensions and projected size of the object are different for each iPhone model, I need a way to do this dynamically, and for that I need to get the projected size of my main object.

对我来说显而易见的方法是使用 xFov 和 yFov,但出于某种原因,它们始终为零.usesOrthographicProjection 设置为 false 所以不是因为这个.

The obvious approach for me was to use the xFov and yFov, but for some reason they are always zero. usesOrthographicProjection is set to false so it's not because of that.

下面是一个简单的测试代码,它试图使场景平面与 HUD 上的平面相等.

Below a simple test code that trys to equal the plane of the scene with the plane on the 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

    }

}

推荐答案

  1. 使用 getBoundingBoxMin(_:max:) 找到您的 3D 元素在 SceneKit 场景中占据的空间的角落.

  1. Use getBoundingBoxMin(_:max:) to find the corners of the space your 3D element occupies in the SceneKit scene.

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

Use projectPoint to convert those points from 3D scene coordinates to 2D view coordinates.

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

Use convertPointFromView to convert from (UIKit) view coordinates to your SpriteKit scene's coordinate system.

根据步骤 3 中的要点,您可以为 HUD 元素构建矩形或其他形状以避免出现.

From the points in step 3 you can construct a rectangle or other shape for your HUD elements to avoid.

这篇关于SCNScene:计算对象的投影大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆