My app currently animates thousands of "mini textures" with SpriteKit. The conversion process for each object of interest is UIView to UIImage to SKTexture to many, many, very small SKTextures (miniTextures). What I'm having trouble with is how to further process only those miniTextures that are "useful."
Many of the miniTextures, coming from blank parts of an object/view/image/texture, contain no useful visual information (i.e., they're blank). As such, it would be wasteful to animate them. The problem is, I don't know of any good, efficient test for this. I had hoped to somehow be able to examine each texture's pixels, and if every pixel in a texture had an alpha value of 0, that texture would not get processed (i.e., no SKSpriteNode would get created for it).
As a workaround, I tried creating an SKPhysicsBody for each miniTexture (see code below), using SKPhysicsBody(texture: alphaThreshold: size:), which returns nil when nothing in the texture exceeds the alphaThreshold. That then might have at least allowed me to test for and selectively process the useful miniTextures. But when doing that, I ran into two problems: Firstly, the simulator slowed to a virtual standstill. Secondly, when making the textures very small, all of them were determined to be nil, regardless of my alphaThreshold.
Insights? Solutions? Sympathy?
for j in stride(from: 0, to: 1, by: yFraction) {
for i in stride(from: 0, to: 1, by: xFraction) {
let miniTexture = SKTexture(rect: CGRect(x: CGFloat(i),
y: CGFloat(j),
width: CGFloat(xFraction),
height: CGFloat(yFraction)),
in: texture)
spriteNode.physicsBody = SKPhysicsBody(texture: miniTexture,
alphaThreshold: 0.5,
size: CGSize(width: 1,
height: 1))
// BUT WHEN THE miniTextures ARE VERY SMALL, THESE ALL ARE NIL!
// AND THE ANIMATION COMES TO A VIRTUAL STANDSTILL!
if spriteNode.physicsBody == 0 { continue }
// spriteNode.position details here
// spritNode.run(specificAnimation) details here
let spriteNode = SKSpriteNode(texture: miniTexture)
spriteNodes.addChild(spriteNode)
}
}
SKTexture(rect: CGRect(x: CGFloat(i), y: CGFloat(j), width: CGFloat(xFraction), height: CGFloat(yFraction)), in: texture)
is creating the subtexture that the atlas would. – Knight0fDragon