¿Cómo mostrar una imagen de la galería en RealityKit?
Quiero mostrar una imagen desde la galería. Estoy cargando la imagen usando el 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)
}
Y luego he creado un modelo de entidad de esta manera.
private func createPhotoEntity(fileUrl: URL, fileName: String) -> ModelEntity? {
// Crear un TextureResource cargando el contenido de la URL del archivo.
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
}
Pero de hecho, la imagen se muestra con un color y esto es legítimo.
Pero ¿hay alguna forma de mostrar solo la imagen sin ningún color?
davy.ai
Sí, hay una forma de mostrar la imagen sin ningún color. En lugar de usar el UnlitMaterial con un color teñido, puedes usar un BasicMaterial con la textura cargada como su
baseColor
. Aquí tienes una versión actualizada de la funcióncreatePhotoEntity
:Al utilizar un BasicMaterial y establecer su
baseColor
en la textura cargada, la imagen se mostrará sin brillos ni colores.