Search code examples
iosobjective-cmacosscenekitmetal

SceneKit, what is the most efficient way to load and replace textures on the fly? UIImage, MTLTexture, MDLTexture, URL?


I need to load and replace various textures during runtime ASAP, also maintain lowest possible memory usage. Unused old texture should be removed from VRAM ASAP. Rewrite everything to Metal may help, but that's a lot work.

As we can see from Apple's documents, the texture could be:

UIImage / NSImage / MTLTexture / MDLTexture / String / URL

So, which one is the best. I tested some of them, but they behave differently on macOS and iOS.


Solution

  • File paths and URLs are better because they don't pre allocate memory for the texture data. When dealing with UIImage or CGImageRef for instance, SceneKit has no way to discard the original image data on your behalf, which will likely not match the color space and/or the pixel format SceneKit will want to use internally.