【问题标题】:How to show image from gallery in RealityKit?如何在realitykit中显示来自画廊的图像?
【发布时间】:2022-01-16 03:55:02
【问题描述】:

我想显示图库中的图像。 我正在使用 imagePicker 加载图像。

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
    guard let image = info[.originalImage] as? UIImage else { return }
  
    if let imgUrl = info[UIImagePickerController.InfoKey.imageURL] as? URL{            
        self.photoEntity = createPhotoEntity(fileUrl: imgUrl, fileName: imgName)
    }
    dismiss(animated: true)
}

然后我创建了一个这样的模型实体。

private func createPhotoEntity(fileUrl: URL, fileName: String) -> ModelEntity? {
    // Create a TextureResource by loading the contents of the file URL.
    do {
        let texture = try TextureResource.load(contentsOf: fileUrl)
        let planeMesh = MeshResource.generatePlane(width: 0.5, depth: 0.5)
        var material = UnlitMaterial()
        material.color = .init(tint: .red.withAlphaComponent(0.999), texture: .init(texture))
        let entity = ModelEntity(mesh: planeMesh, materials: [material])
        entity.generateCollisionShapes(recursive: true)
        ARView.installGestures([.scale, .translation], for: entity)
        return entity
    } catch(let error) {
        print("error loading. \(error.localizedDescription)")
    }
    return nil
}

但事实是,图像以颜色显示,这是合法的。

但是 有没有办法只显示没有颜色的图像?

【问题讨论】:

    标签: ios swift augmented-reality arkit realitykit


    【解决方案1】:

    试试这个。考虑到,tint 颜色乘以图像 - 所以,如果 tint 的 RGBA = [1,1,1,1],乘法的结果将是图像本身(没有着色)...

    import ARKit
    import RealityKit
    
    class ViewController: UIViewController {
        
        @IBOutlet var arView: ARView!
        var anchor: AnchorEntity!
        
        override func viewDidLoad() {
            super.viewDidLoad()
    
            self.anchor = AnchorEntity(world: [0,0,-1])
            
            let ball: MeshResource = .generateSphere(radius: 0.25)
            
            var material = UnlitMaterial()
            
            if #available(iOS 15.0, *) {
    
                material.color = try! .init(tint: .white,
                                         texture: .init(.load(named: "img", 
                                                                 in: nil)))
            }
            
            let ballEntity = ModelEntity(mesh: ball, materials: [material])
            
            self.anchor.addChild(ballEntity)
            
            self.arView.scene.anchors.append(self.anchor)
        }
    }
    

    【讨论】:

    • 老兄!!你摇滚。谢谢。它的工作。 :) 我有很多东西要向你学习。我该怎么做?
    • 嗨,@Andy_Jazz,你能看看这个问题吗?分享您对此的看法? stackoverflow.com/questions/70321707/…
    • 在 The Foundry NUKE 工作了 15 年以上之后,才对图像的数学和合成理解。 ))))
    猜你喜欢
    • 2014-08-12
    • 1970-01-01
    • 1970-01-01
    • 2015-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-25
    • 1970-01-01
    相关资源
    最近更新 更多