ARKIT:如何将Uiview添加到Arkit场景中



我正在使用arkit进行AR项目。

我想在Arkit场景中添加一个Uiview。当我点击对象时,我想将信息作为对象旁边的"弹出窗口"。此信息在uiview中。

是否可以将此Uiview添加到Arkit场景中?我将这个Uiview设置为一个场景,然后该怎么办?我可以给它一个节点,然后将其添加到Arkit场景中吗?如果是这样,它的工作原理?

或其他方法?

谢谢!

编辑:我的secondviewController的代码

class InformationViewController: UIViewController {
    @IBOutlet weak var secondView: UIView!
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view = secondView
    }
}

编辑2:FirstViewController中的代码

guard let secondViewController = storyboard?.instantiateViewController(withIdentifier: "SecondViewController") as? SecondViewController else {
    print ("No secondController")
    return
}
let plane = SCNPlane(width: CGFloat(0.1), height: CGFloat(0.1))       
plane.firstMaterial?.diffuse.contents = secondViewController.view
let node = SCNNode(geometry: plane)

我只能获得飞机的白色屏幕,而不是视图。

实现最简单的方法(尽管没有证件(是将视图控制器备份的UIView设置为SCNPlane上材料的弥散内容(或其他任何其他几何形状((或实际上都可以使用(出于明显的原因,最好使用飞机。

let plane = SCNPlane()
plane.firstMaterial?.diffuse.contents = someViewController.view
let planeNode = SCNNode(geometry: plane)

您将必须在某个地方持续存在视图控制器,否则它将被释放,而平面将看不到。仅使用UIView而无需任何UIViewController会丢下错误。

最好的事情是,它可以保留所有手势,并且实际上是一种简单的视图。例如,如果您使用UITableViewController的视图,则可以在场景中滚动它。

我尚未在iOS 10及更低的iOS上进行测试,但是到目前为止,它一直在iOS 11上进行测试。在平原场景和arkit中都可以工作。

我现在无法为您提供代码,但这是如何做的。

  1. 创建SCNPlane
  2. 用所需的所有元素创建UIView
  3. UIView创建图像上下文。
  4. 将此图像用作SCNPlane的材料。

,甚至更容易用标签制作SKScene,并将其添加为SCNPlane的材料。

示例:https://stackoverflow.com/a/74380559/294844

将文本放在世界上的标签中,然后将其绘制到图像中,然后将图像附加到SCNNode

例如:

let text = "Hello, Stack Overflow."
let font = UIFont(name: "Arial", size: CGFloat(size))
let width = 128
let height = 128
let fontAttrs: [NSAttributedStringKey: Any] = 
[NSAttributedStringKey.font: font as UIFont]
let stringSize = self.text.size(withAttributes: fontAttrs)
let rect = CGRect(x: CGFloat((width / 2.0) - (stringSize.width/2.0)),
                  y: CGFloat((height / 2.0) - (stringSize.height/2.0)),
                  width: CGFloat(stringSize.width),
                  height: CGFloat(stringSize.height))
let renderer = UIGraphicsImageRenderer(size: CGSize(width: CGFloat(width), height: CGFloat(height)))
let image = renderer.image { context in
    let color = UIColor.blue.withAlphaComponent(CGFloat(0.5))
    color.setFill()
    context.fill(rect)
    text.draw(with: rect, options: .usesLineFragmentOrigin, attributes: fontAttrs, context: nil)
}
let plane = SCNPlane(width: CGFloat(0.1), height: CGFloat(0.1))
plane.firstMaterial?.diffuse.contents = image
let node = SCNNode(geometry: plane)

编辑:我添加了这些行:

let color = UIColor.blue.withAlphaComponent(CGFloat(0.5))
color.setFill()
context.fill(rect)

这使您可以设置背景颜色和不透明度。还有其他方法可以做到这一点 - 这也让您绘制复杂的形状 - 但这是最简单的基本颜色。

编辑2:添加对stringSizerect

的引用

最新更新