I can't figure this one out. I've been able to load image textures from a struct model but not a class Model for my modelEntity.
This for example, works for me, this is what I have been using up to now, without SwiftData, using a struct to hold my model
if let imageURL = model.imageURL {
let picInBox2 = ModelEntity(mesh: .generateBox(size: simd_make_float3(0.6, 0.5, 0.075), cornerRadius: 0.01))
picInBox2.position = simd_make_float3(0, 0, -0.8)
if let imageURL = model.imageURL {
if let texture = try? TextureResource.load(contentsOf: imageURL) {
var unlitMaterial = UnlitMaterial()
var imageMaterial = UnlitMaterial()
unlitMaterial.baseColor = MaterialColorParameter.texture(texture)
picInBox2.model?.materials = [imageMaterial]
}
}
However, when I try to use my SwiftData model it doesn't work. I need to convert Data to url and I am not able to do this.
This is what I would like to use for my image texture, from my SwiftData model
@Attribute(.externalStorage)
var image: Data?
If/when I try to do this, substitute
if let imageURL = item.image {
`
for the old
if let imageURL = model.imageURL {
in
if let imageURL = model.imageURL {
if let texture = try? TextureResource.load(contentsOf: imageURL) {
var unlitMaterial = UnlitMaterial()
var imageMaterial = UnlitMaterial()
unlitMaterial.baseColor = MaterialColorParameter.texture(texture)
picInBox2.model?.materials = [imageMaterial]
}
it doesn't work.
I get the error:
Cannot convert value of type 'Data' to expected argument type 'URL'
How can i convert the type 'Data' to expected argument type 'URL'?
The original imageURL I am using here comes from the struct Model where it's saved as a variable
var imageURL: URL? = Bundle.main.url(forResource: "cat", withExtension: "png")
I am at my wit's end. Thank you for any pointers!