2

I want to show image from gallery. i am loading the image using 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)
}

and then i have created a modelEntity like this.

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
}

But the fact is, image is being shown with a color and this is legit.

But Is there any way to show only image without any color?

elk_cloner
  • 1,953
  • 1
  • 11
  • 13

1 Answers1

1

Try this. Take into consideration, a tint color is multiplied by an image – so, if tint's RGBA = [1,1,1,1], a result of multiplication will be an image itself (without tinting)...

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
  • 36,357
  • 13
  • 107
  • 175
  • 1
    dude!! you rock. Thanks. its working. :) i have a lot to learn from you. how can i do it? – elk_cloner Dec 12 '21 at 12:25
  • 1
    Hi, @Andy_Jazz, could you please look into this question? share your thoughts regarding this? https://stackoverflow.com/questions/70321707/how-to-access-the-property-of-the-objects-in-a-scene-programmatically-in-reality – elk_cloner Dec 12 '21 at 12:56
  • 1
    The image's math & comp understanding came after 15+ years at The Foundry NUKE. )))) – Andy Jazz Dec 12 '21 at 13:49