How to make use of cube texture type in XCassets

大兔子大兔子 提交于 2019-12-23 19:04:14

问题


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.

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?


回答1:


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)



来源:https://stackoverflow.com/questions/51171590/how-to-make-use-of-cube-texture-type-in-xcassets

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!