How to set a MTLTexture as an environment map in SceneKit?

I want to set a MTLTexture object as the environment map of a scene, as it seems to be possible according to the documentation. I can set the environment map to be a UIImage with the following code:

let roomImage = UIImage(named: "room")
scene.lightingEnvironment.contents = roomImage


This works and I see the reflection of the image on my metallic objects. I tried converting the image to a MTLTexture and setting it as the environment map with the following code:

let roomImage = UIImage(named: "room")
let loader = MTKTextureLoader(device: MTLCreateSystemDefaultDevice()!)
let envMap = try? loader.newTexture(cgImage: (roomImage?.cgImage)!, options: nil)
scene.lightingEnvironment.contents = envMap


However this does not work and I end up with a blank environment map with no reflection on my objects.

Also, instead of setting the options as nil, I tried setting the MTKTextureLoader.Option.textureUsage key with every possible value it can get, but that didn't work either.

Replies

your question was answered on StackOverflow which I assume it's also yours:

https://stackoverflow.com/questions/47739214/using-a-mtltexture-as-the-environment-map-of-a-scnscene

It's not exactly answered as I still can't set a MTLTexture as the environment map of my scene. I just got a comment saying I should file a bug report, which is what I did, but I would rather find a workaround and get this done until I get a response for my report.