title: Using a MTLTexture as the environment map of a SCNScene
body: <p>I want to set a <code>MTLTexture</code> object as the environment map of a scene, as it seems to be possible according to the <a href="https://developer.apple.com/documentation/scenekit/scnmaterialproperty/1395372-contents#discussion" rel="noreferrer">documentation</a>. I can set the environment map to be a <code>UIImage</code> with the following code:</p>
  
  <pre><code>let roomImage = UIImage(named: "room")
  scene.lightingEnvironment.contents = roomImage
  </code></pre>
  
  <p>This works and I see the reflection of the image on my metallic objects. I tried converting the image to a <code>MTLTexture</code> and setting it as the environment map with the following code:</p>
  
  <pre><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
  </code></pre>
  
  <p>However this does not work and I end up with a blank environment map with no reflection on my objects.</p>
  
  <p>Also, instead of setting the <code>options</code> as <code>nil</code>, I tried setting the <code>MTKTextureLoader.Option.textureUsage</code> key with <a href="https://developer.apple.com/documentation/metal/mtltextureusage#1604533" rel="noreferrer">every possible value</a> it can get, but that didn't work either.</p>
  
  <p>Edit: You can have a look at the example project in <a href="https://github.com/svtek/ARKitEnvironmentMapper" rel="noreferrer">this</a> repo and use it to reproduce this use case.</p>