【问题标题】:Is there a way to render Views as Entities in RealityKit?有没有办法在 RealityKit 中将视图呈现为实体?
【发布时间】:2020-01-20 20:55:27
【问题描述】:

我一直在尝试找到一种将 UIView 显示为我的 ARView 中的 3d 对象的方法。我一直在尝试创建一种可以用作显示以包含 UIView 但没有运气的盒子。

This answer 为 SceneKit 提供了解决方案,但在 RealityKit 中似乎不起作用。 到目前为止,我的想法如下,但找不到真正连接 UIView 的方法。

let box = MeshResource.generateBox(width: 0.6, height: 0.3, depth: 0.02, cornerRadius: 0.03)
let material = SimpleMaterial()
material.baseColor =  --SET UIVIEW AS DIFFUSE OF MATERIAL?--
let entity  = ModelEntity(mesh: box, materials: [material])
anchorEntity.addChild(entity)

【问题讨论】:

    标签: swift view uiview augmented-reality realitykit


    【解决方案1】:

    我看到以下解决方案:您可以使用snapshotView()snapshot() 实例方法来获取当前UIView 内容的快照(然后您必须将其转换为.jpg 并将其用作纹理对于 3D 模型)或以.hdr 获取快照(然后也将其转换为.jpg)。

    arView.snapshotView(afterScreenUpdates: false)
    

    或:

    arView.snapshot(saveToHDR: true, completion: { (image) in ... } )
    

    向您展示如何在 RealityKit 的 box 原语上应用纹理的方法:

    @IBOutlet var arView: ARView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        arView.environment.background = .color(.systemGray2)
        let scene = try! Experience.loadBox()
        
        var material = SimpleMaterial()
        material.baseColor = try! .texture(.load(named: "snapshot.jpg"))
        
        let mesh = MeshResource.generateBox(size: 0.3)
    
        let model = ModelEntity(mesh: mesh, materials: [material])
        model.position.x = 0.3
        model.setParent(scene)
    
        arView.scene.anchors.append(scene)
    }
    

    另外,考虑到 RealityKit 2.0 通过VideoMaterial 支持视频纹理。

    【讨论】:

      猜你喜欢
      • 2012-09-27
      • 1970-01-01
      • 2010-12-01
      • 1970-01-01
      • 2021-04-18
      • 1970-01-01
      • 1970-01-01
      • 2014-11-29
      • 1970-01-01
      相关资源
      最近更新 更多