3

I am trying to use MetalKit newTextureWithContentsOfURL to create the texture for a cow.

MTKTextureLoader *texture_loader = [[MTKTextureLoader alloc] initWithDevice:device];
NSURL *cow_image = [[NSBundle mainBundle] URLForResource:@"spot_texture" withExtension:@"png"];
NSError *error;
diffuse_texture = [texture_loader newTextureWithContentsOfURL:cow_image options:nil error:&error];

Here is the sample cow (top) and my cow (bottom) look.

Sample cow

My cow

I don't know the reason about this, but I think the options may help.

How to set this option using NSDictionary?

user16217248
  • 3,119
  • 19
  • 19
  • 37
BigFatTom
  • 149
  • 1
  • 6
  • I use NSDictionary *dict = [NSDictionary dictionaryWithObject:@(YES) forKey:MTKTextureLoaderOptionOrigin]; and it work, but I still do not know what this code mean. – BigFatTom Jul 12 '18 at 07:49
  • I'm surprised that that worked. That is the correct option name to use, but normally, you'd supply one of the origin values, such as `MTKTextureLoaderOriginFlippedVertically`, rather than an `NSNumber`. – warrenm Jul 13 '18 at 15:10

1 Answers1

1

Based on this Objective-C Literals

MTKTextureLoader *texture_loader = [[MTKTextureLoader alloc] initWithDevice:device];
NSURL *cow_image = [[NSBundle mainBundle] URLForResource:@"spot_texture" 
withExtension:@"png"];
NSError *error;
diffuse_texture = [texture_loader newTextureWithContentsOfURL:cow_image options:@{ MTKTextureLoaderOptionSRGB : @true, MTKTextureLoaderOptionOrigin : @true} 
error:&error];
Hamid Yusifli
  • 9,688
  • 2
  • 24
  • 48