简体   繁体   English

从 URL 加载图像以获取具有 Swift 的材质纹理

[英]Loading images from a URL for a material texture with Swift

I'm trying to create a custom material from a remote image to add to a ModelEntity with RealityKit.我正在尝试从远程图像创建自定义材质,以使用 RealityKit 添加到 ModelEntity。 Works totally fine with a local image but trying anything remote I get a loading error.使用本地图像完全可以正常工作,但尝试任何远程操作都会出现加载错误。

Fatal error: 'try:' expression unexpectedly raised an error.致命错误:“try:”表达式意外引发错误。 RealityKit.TextureResource.(unknown context at $1b9817f08).LoadError.importFailureWithURL( https://upload.wikimedia.org/wikipedia/commons/1/1b/Demetrius_III_bust.png ) RealityKit.TextureResource.(未知上下文在 $1b9817f08).LoadError.importFailureWithURL( https://upload.wikimedia.org/wikipedia/commons/1/1b/Demetrius_III_bust.png

I've tried multiple images from different source, both http, https, .png, .jpg.我尝试了来自不同来源的多个图像,包括 http、https、.png、.jpg。 Can't find a working example of this anywhere.在任何地方都找不到这样的工作示例。 If it matters the the id I'm passing to withName is a UUID.如果重要的话,我传递给 withName 的 id 是一个 UUID。

func createMaterial(id: String, imageURL: String) -> Material{
        var material = SimpleMaterial()
        material.metallic = MaterialScalarParameter(floatLiteral: 0.0)
        material.roughness = MaterialScalarParameter(floatLiteral: 1.0)
        let texture: TextureResource = try! TextureResource.load(contentsOf: URL(string:imageURL)!, withName: id)

        let materialTexture: MaterialColorParameter = MaterialColorParameter.texture(texture)
        material.baseColor = materialTexture
        return material
 }

PS If anyone can recommend a good library to handle loading and caching images for this sort of use case that would be a huge help. PS如果有人可以推荐一个好的库来处理这种用例的加载和缓存图像,那将是一个巨大的帮助。 Everything I've found seems targeted towards UIImages or SwiftUI Images.我发现的所有内容似乎都针对 UIImages 或 SwiftUI 图像。

Update 1: Here is the full error after wrapping the lines in a catch:更新1:这是将行包装在一个catch中后的完整错误:

[API] URL passed to REAssetManagerTextureNoNetworkSyncMemoryAssetCreateWithURL must be a file URL: https://upload.wikimedia.org/wikipedia/commons/1/1b/Demetrius_III_bust.png [API] URL passed to REAssetManagerTextureNoNetworkSyncMemoryAssetCreateWithURL must be a file URL: https://upload.wikimedia.org/wikipedia/commons/1/1b/Demetrius_III_bust.png

Failed to import entity from " https://upload.wikimedia.org/wikipedia/commons/1/1b/Demetrius_III_bust.png "无法从“ https://upload.wikimedia.org/wikipedia/commons/1/1b/Demetrius_III_bust.png ”导入实体

I also update my Info.plist's App Transport Security Settings to Allow Arbitrary Loads我还更新了我的 Info.plist 的应用程序传输安全设置以允许任意加载

Looks like the contentsOf parameter of TextureResource.load() requires an internal file URL not an external URL.看起来TextureResource.load()contentsOf参数需要一个内部文件 URL 而不是外部 URL。 I solved this in my case by manually downloading the images I needed first.我通过手动下载我首先需要的图像解决了这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM