如何在realitykit中显示图库中的图像?

How to show image from gallery in realitykit?(如何在realitykit中显示图库中的图像?)
本文介绍了如何在realitykit中显示图库中的图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想展示画廊中的图像。 我正在使用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)
}

然后,我创建了一个类似于此的modEntity。

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
}

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

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

推荐答案

试试这个。考虑到,色调颜色乘以图像-因此,如果色调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)
    }
}

这篇关于如何在realitykit中显示图库中的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why local notification is not firing for UNCalendarNotificationTrigger(为什么没有为UNCalendarNotificationTrigger触发本地通知)
iOS VoiceOver functionality changes with Bundle Identifier(IOS画外音功能随捆绑包标识符而变化)
tabbar middle tab out of tabbar corner(选项卡栏中间的选项卡角外)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
Dropbox Files.download does not start when number of files in folder is gt; 1000(当文件夹中的文件数为1000时,Dropbox Files.Download不会启动)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)