简体   繁体   English

将2D形状添加到SceneKit

[英]Add 2D Shape to SceneKit

I want to display a 3D model in SceneKit, but also want to place some 2D shapes to the model (eg a circle that can be tapped an triggers an interaction). 我想在SceneKit中显示3D模型,但也想在模型中放置一些2D形状(例如,可以点击一个圆圈来触发交互)。

What is the best way to achieve this? 实现此目标的最佳方法是什么?

How it should look like: 它应该是什么样的:

在此处输入图片说明

Unsuccessful approach: used a regular SCNNode with small hight, but this does not behave as a 2D graphic (perspective changes) 方法未成功:使用了常规的SCNNode ,但高度不高,但这不能用作2D图形(可能发生更改)

在此处输入图片说明

Edit: 编辑:

  • now I added the circle as a sprite kit node to the overlay view 现在我将圆圈添加为Sprite Kit节点到叠加视图

     sceneView.overlaySKScene = SKScene.init(size: sceneView.frame.size) sceneView.overlaySKScene?.addChild(circle) 
  • I also tried to get the screen-coordinates of the corresponding node, where I want to show the SpriteKit circle in front of: 我还尝试获取相应节点的屏幕坐标,我想在该坐标的前面显示SpriteKit圆圈:

     func renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval) { let screenPoint = sceneView.projectPoint(interactionNode!.position) print(screenPoint) circle.position = CGPoint.init(x: CGFloat(screenPoint.x), y: CGFloat(screenPoint.y)) 

    } }

However, sceneView.projectPoint gives me very weird results, even if the node is displayed in the middle of the screen, there are off-screen ranges returned... 但是,sceneView.projectPoint给我非常奇怪的结果,即使该节点显示在屏幕中间,也有返回的屏幕外范围...

You want to use overlaySKScene, which will allow you to place (performant) 2D Spritekit content "on top of" your Scenekit scene. 您想使用overlaySKScene,它将允许您将(高性能)2D Spritekit内容“放置”在Scenekit场景之上。 Here's how I do it in my app: 这是我在应用程序中执行的操作:

First define Spritekit content that you want in your HUD: 首先在HUD中定义所需的Spritekit内容:

class HUD: SKScene {
var shields: SKShapeNode(imageNamed: "shieldGrid")
var crosshairs = SKSpriteNode(imageNamed: "xenonHUD")
public var computerStatus = SKLabelNode()
public var enemyIndicator = SKLabelNode()

func shipHud.flashAlert( alert: String) {
   computerStatus.text = alert
}

Then, in your main view controller (where you instantiate your SceneView), make this class your scene's overview, and control it from there: 然后,在主视图控制器(在其中实例化SceneView)中,使此类成为场景的概述,并从那里进行控制:

class MyGameViewController: UIViewController, SCNPhysicsContactDelegate, SCNSceneRendererDelegate {
var scnView: SCNView! 
// setup HUD
    shipHud = HUD(size: self.view.bounds.size)
    scnView.overlaySKScene = shipHud
    shipHud.flashAlert("Oh My God!")

} }

使用overlaySKScene混合的SceneKit和Spritekit内容 Mixed SceneKit and Spritekit Content using overlaySKScene 使用overlaySKScene混合的SceneKit和Spritekit内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM