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

elk*_*ner 5 augmented-reality swift arkit 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)
}
Run Code Online (Sandbox Code Playgroud)

然后我创建了一个像这样的 modelEntity 。

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
}
Run Code Online (Sandbox Code Playgroud)

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

但是 有什么办法只显示图像而不显示任何颜色吗?

ARG*_*Geo 3

尝试这个。考虑到,色调颜色乘以图像 \xe2\x80\x93 因此,如果 色调RGBA = [1,1,1,1],相乘的结果将是图像本身(没有着色)...

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

  • 在 The Foundry NUKE 工作 15 年以上后,我们对图像的数学和计算有了理解。)))) (2认同)