SKTexture with rect from atlas texture

SKTexture works quite nicely in the sense that it doesn't matter whether it's referring to a separate texture, or a sub-texture in a texture atlas. It works completely transparently and identically regardless of where the image data is actually located.


Except for one thing: [SKTexture textureWithRect:inTexture:] just outright doesn't work properly if the parameter texture is referring to a sub-texture in a texture atlas. And what's worse, it cannot be made to work, no matter what.


For one, if the source sub-texture is rotated, the copy texture created with that method will be unrotated. What's worse, there's neither any way of knowing if the texture is rotated, nor is it possible to create a new rotated texture. The newly created texture will always be unrotated (and thus eg. SKSpriteNodes created from this copy texture will be wrong, with their texture rotated 90 degrees). There is just no way to make it work.


Secondly, if the sub-texture referred to by the parameter is trimmed (because of Xcode's texture atlas creation having trimmed fully transparent rows/columns of pixels from the original image), there is no way of knowing this either, nor to take it into account when creating the new texture.


For example, suppose you want to create a new texture that's exactly the left half of the original texture. There is absolutely no way of doing this with certainty. You can't know if the original texture is actually rotated, you can't create the new texture object with matching rotation, and moreover, there is no way of knowing if the original texture has been trimmed. If the original has been trimmed horizontally, if you try to take exactly the left half of it, you'll end up with the wrong result. The textureWithRect:inTexture: method just outright doesn't take into accout if the texture given as parameter points to a texture atlas and has been rotated and/or trimmed.


The correct way of doing this would be, of course, for that method to work transparently, exactly as if the source texture were pointing to its own texture (and as if the texture rect were covering the full image, ie (0,0)-(1,1).) This way you specify CGRectMake(0,0, 0.5,1.0) as the parameter, and then you create a sprite with this new texture, you would get exactly the left half of the original, regardless of whether it has been rotated and/or trimmed in the atlas.


If this cannot be done in textureWithRect:inTexture: itself due to backwards compatibility reasons, then create a new method for this purpose.


Or is there something I have missed here?

SKTexture with rect from atlas texture
 
 
Q