5

It seems like MTKTexureLoader newTextureWithContentsOfURL... automatically flips the image. This causes all of my models and meshes containing conventional UV coordinates to display incorrectly. I didn't see anything in the options to specify whether this happens or not. Is there some way to use MTKTextureLoader and maintain the orientation of the image?

user16217248
  • 3,119
  • 19
  • 19
  • 37
Curyous
  • 8,716
  • 15
  • 58
  • 83

3 Answers3

3

I don't see anyway to get MTKTextureLoader to flip your image, but you can simply flip your texture coordinates:

{ u, v } -> { u, 1-v }
Rhythmic Fistman
  • 34,352
  • 5
  • 87
  • 159
3

As of iOS 10, you can pass an option to the MTKTextureLoader initializer. For example loading a texture from a CGImage:

let textureOut = try textureLoader.newTexture(with: cgImage, 
    options: [MTKTextureLoaderOptionOrigin: MTKTextureLoaderOriginTopLeft as NSObject])

will flip images that initially have their origin in the bottom left corner.

If you have to deal with iOS < 9, you can force your users to update use both approaches from Rythmic Fistman or Denn Nevera in the answers.

Bernardo Santana
  • 454
  • 5
  • 18
0

I would suppose MTK PreLoader creates a right texture from image according to the MTL Coordinate system: ...The origin of the window coordinates is in the upper-left corner....

I think you have three simple options to process your image with normal orientation:

  1. prepare already flipped image
  2. flip your image in MSL graphic or kernel function in your app (as it has been offered in the first answer before, transformation matrix)
  3. in case when you need to present the image on an app view only, use view.layer.transfrom property, for instance: ...transfrom=CATransform3DMakeRotation(CGFloat(M_PI),1.0,0.0,0.0
Denn Nevera
  • 336
  • 2
  • 6