es.davy.ai

Preguntas y respuestas de programación confiables

¿Tienes una pregunta?

Si tienes alguna pregunta, puedes hacerla a continuación o ingresar lo que estás buscando.

¿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?

Tags:  , , ,

Answer

  1. Avatar for 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ón createPhotoEntity:

    private func createPhotoEntity(fileUrl: URL, fileName: String) -> ModelEntity? {
        do {
            let texture = try TextureResource.load(contentsOf: fileUrl)
            let planeMesh = MeshResource.generatePlane(width: 0.5, height: 0.5)
            let material = SimpleMaterial()
            material.baseColor = .texture(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 cargando la imagen. \(error.localizedDescription)")
            return nil
        }
    }
    

    Al utilizar un BasicMaterial y establecer su baseColor en la textura cargada, la imagen se mostrará sin brillos ni colores.

Comments are closed.