纹理不在 mainBundle 中的 GLKTextureLoader

GLKTextureLoader with texture not in mainBundle

我正在使用 GLKit 使用 OpenGL ES 2.0 制作一个 iPhone 应用程序。我正在使用 GLKTextureLoader 加载纹理。当我的纹理位于 mainBundle 中时 - 一切正常。我用命令 [[NSBundle mainBundle] pathForResource:@"brushRose.png" ofType:nil] 得到它的名字,纹理有以下路径:

/var/mobile/Containers/Bundle/Application/D79492CB-D03C-464D-B06E-00D0DE4389DF/Texture Test.app/brushRose.png

当我尝试从 Internet 下载完全相同的纹理并将其存储在应用程序的 Documents 文件夹中时。路径如下:

/var/mobile/Containers/Data/Application/CD3FBAAB-B8AE-47F5-9C6E-51C854FC1620/Documents/brushes/ps_roses.png

我得到了一个糟糕的最终结果,可以在下图中观察到: First row - texture from mainBundle, second row - from Documents

欢迎提出任何解决第二种情况的想法。

可以找到测试项目here

当您构建包含 PNG 的应用程序时,Xcode 运行s pngcrush 其中,pre-multiplies the alpha.

您正在使用的 "non main-Bundle" 文件没有 pre-multiplied alpha。这就解释了外观上的差异。

您的选择是 运行 pngcrush 在您的 url 纹理上,或者在您的项目中停止 pngcrush 运行ning,或者有条件地应用 GLKTextureLoaderApplyPremultiplication当您加载 non-crushed 个 PNG 时:

NSMutableDictionary *options = [@{ GLKTextureLoaderOriginBottomLeft : @NO} mutableCopy];

if ( /** png is uncrushed **/ ) {
    options[GLKTextureLoaderApplyPremultiplication] = @YES;
}

NSError* error;
GLKTextureInfo* texture = [GLKTextureLoader textureWithContentsOfFile:brushPath options:options error:&error];