mlidal
mlidal

Reputation: 1153

How to make use of cube texture type in XCassets

I'm trying to learn SceneKit development and try to add a skybox in the background. To store the cube map textures I found that XCAssets has a type Cube Texture Set which seems to fit the bill perfectly.

enter image description here

However. I've not found any way to access the texture in code (e.g. as an image set where you call UIImage(named: "asset_name") ). I've tried creating an SKTexture, MDLTexture og MTKTexture from the asset but without success. Do anyone know how I can use the cube texture set?

Upvotes: 3

Views: 1118

Answers (1)

Caroline
Caroline

Reputation: 4970

You can load the cube texture from the asset catalog easily using MetalKit.

import MetalKit

at the top of your file. These two lines do the business:

  let textureLoader = MTKTextureLoader(device: scnView.device!)
  scene.background.contents = try! textureLoader.newTexture(name: textureName, 
                                       scaleFactor: 1.0, 
                                       bundle: .main, options: nil)

I tried this in a project created from the default SceneKit game template, and placed these two lines in GameViewController.swift after setting the view's background color

(I expect you can do it using the other technologies too, but this is how you can load a cube texture using Metal)

Upvotes: 5

Related Questions