Texture Sets in Xcode Asset Catalog


I want to use texture sets as discussed in chapter 6. However, I would like to store several texture sets inside a folder in the Assets.xcassets folder. So I could have a folder called Swimming Pool Tiles and then just store an Albedo texture set and a Roughness texture set inside this folder.

However, I’m not sure of the correct way to then refer to each texture when loading.

I would have assumed that you could just do:

textureLoader.newTexture(name: “Swimming Pool Tiles/Albedo”, scaleFactor: 1.0, bundle: Bundle.main, options: nil)

However, this does not work…

Also, does anyone know why build times are so slow when using texture sets?

The asset name needs to be unique. I think (on macOS), from looking at the app package that this is necessary because assets are placed in an Assets.car file. That’s why the initial build time is so long too, because it has a lot of interpretation to do.

I just found an amazing blog post that tells you everything you need to know about Assets.car! Reverse engineering the .car file format (compiled Asset Catalogs)

I prefix the file names, as for example in chapter 9 - racing-car-color and oilcan-color.

I have in the past used images with Folder References - that’s the blue folder group in Xcode rather than the yellow group. You can use the actual folder name when referencing files there.

Hi eoey - Could you let me know what’s happening when you run your code? Is the texture being returned nil?

I ask because there may be a bug with MTKTextureLoader when it interfaces with the asset catalog. I went in search of a solution on the Apple Developers Forum and at least one other person is having an issue where the texture loader always returns nil from images in the catalog.

I personally tried numerous variations of the code in Swift and Objective-C and with various naming conventions.

I’ll return to this at some point because I really like the asset catalog, but for now, I’m loading textures from a regular asset folder, which is working fine.

Thanks for your replies! I got slightly distracted with other things but will return to this soon.

@eoey1 @lducot2 Do you still have issues with this?

I managed to fix the issue. I’ll try to go back to my code and see what was causing the error. I think it had to do with the names of the textures. If I recall, there was a different naming convention I had to use when I placed a texture directly into the asset catalogue as opposed to creating a texture set and placing the texture in the texture set.

1 Like