Loading a RealityKit image texture from a URL

I'm trying to load an image from the internet as an image texture for a material in RealityKit & running into trouble. I'm using code similar to the block below:

Code Block
var material = SimpleMaterial(color: .white, roughness: 0.9, isMetallic: true)
let url = URL("https://example.com/image.png")
material.baseColor = try! .texture(.load(contentsOf: url))


Unfortunately this leads to an error, with the texture failing to load the image from the URL.

Is it possible to load an image from the web as the texture for a RealityKit material? Is there a possible workaround if not directly?
Answered by DTS Engineer in 641793022
Hello,

You should be receiving this error: "URL passed to REAssetManagerTextureNoNetworkSyncMemoryAssetCreateWithURL must be a file URL"

This is saying that you cannot load a remote URL directly as a TextureResource, it will only handle file URLs. You can download the contents of the remote URL and store it in a file URL like this:

Code Block
let remoteURL = URL(string: "https://example.com/image.png")!
// Create a temporary file URL to store the image at the remote URL.
        let fileURL = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString)
        // Download contents of imageURL as Data.  Use a URLSession if you want to do this asynchronously.
        let data = try! Data(contentsOf: remoteURL)
        
        // Write the image Data to the file URL.
        try! data.write(to: fileURL)
        do {
            // Create a TextureResource by loading the contents of the file URL.
            let texture = try TextureResource.load(contentsOf: fileURL)
            var material = SimpleMaterial()
            material.baseColor = MaterialColorParameter.texture(texture)
            let entity = ModelEntity(mesh: .generateBox(size: 0.1), materials: [material])
            let anchor = AnchorEntity(.plane(.any, classification: .any, minimumBounds: .zero))
            anchor.addChild(entity)
            arView.scene.addAnchor(anchor)
        } catch {
            print(error.localizedDescription)
        }


Accepted Answer
Hello,

You should be receiving this error: "URL passed to REAssetManagerTextureNoNetworkSyncMemoryAssetCreateWithURL must be a file URL"

This is saying that you cannot load a remote URL directly as a TextureResource, it will only handle file URLs. You can download the contents of the remote URL and store it in a file URL like this:

Code Block
let remoteURL = URL(string: "https://example.com/image.png")!
// Create a temporary file URL to store the image at the remote URL.
        let fileURL = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString)
        // Download contents of imageURL as Data.  Use a URLSession if you want to do this asynchronously.
        let data = try! Data(contentsOf: remoteURL)
        
        // Write the image Data to the file URL.
        try! data.write(to: fileURL)
        do {
            // Create a TextureResource by loading the contents of the file URL.
            let texture = try TextureResource.load(contentsOf: fileURL)
            var material = SimpleMaterial()
            material.baseColor = MaterialColorParameter.texture(texture)
            let entity = ModelEntity(mesh: .generateBox(size: 0.1), materials: [material])
            let anchor = AnchorEntity(.plane(.any, classification: .any, minimumBounds: .zero))
            anchor.addChild(entity)
            arView.scene.addAnchor(anchor)
        } catch {
            print(error.localizedDescription)
        }


what is the plan here-->

"'baseColor' was deprecated in iOS 15.0: use color property instead"

var myMaterial = PhysicallyBasedMaterial()

        if let baseResource = try? TextureResource.load(named: "MyImage") {

            // Create a material parameter and assign it.

            let baseColor = MaterialParameters.Texture(baseResource)

            myMaterial.baseColor = PhysicallyBasedMaterial.BaseColor(texture:baseColor)

        }
Loading a RealityKit image texture from a URL
 
 
Q