SpriteKit draw calls not batched when using a texture atlas from data stored in the app bundle
Asked Answered
M

0

2

It seems that SpriteKit doesn't batch the draw calls for textures in the same texture atlas. There are two different behaviors, based on how the SKTextureAtlas gets initialized:

  1. The draw calls are not batched when the texture atlas is initialized using SKTextureAtlas(named:) (loading the texture atlas from data stored in the app bundle).

  2. But the draw calls seem to be batched when the texture atlas is created dynamically using SKTextureAtlas(dictionary:).

The following images show the two different behaviors and the SpriteAtlas inside the Assets Catalog.

1. Draw calls not batched:

Draw calls not batched with SpriteAtlas

2. Draw calls batched:

Draw calls batched with SpriteAtlas

The SpriteAtlas:

SpriteAtlas in the Assets Catalog

I created a sample Xcode 13 project to show the different behavior: https://github.com/clns/spritekit-atlas-batching.
I have tried this on the iOS 15 simulator on macOS Big Sur 11.6.1.

The code to reproduce is very simple:

import SpriteKit

class GameScene: SKScene {
    
    override func didMove(to view: SKView) {
        let atlas = SKTextureAtlas(named: "Sprites")
//        let atlas = SKTextureAtlas(dictionary: ["costume": UIImage(named: "costume")!, "tank": UIImage(named: "tank")!])
        
        let costume = SKSpriteNode(texture: atlas.textureNamed("costume"))
        costume.setScale(0.3)
        costume.position = CGPoint(x: 200, y: 650)
        
        let tank = SKSpriteNode(texture: atlas.textureNamed("tank"))
        tank.setScale(0.3)
        tank.position = CGPoint(x: 500, y: 650)
        
        addChild(costume)
        addChild(tank)
    }
}
Mauricemauricio answered 13/11, 2021 at 18:58 Comment(4)
I recommend, if you haven't already, filing a Feedback with Apple on this. I'll certainly be looking into this more now that you've done this extra investigation.Glossotomy
I haven't done it already, but I probably should. Although I am not sure if anything will come out of it...Mauricemauricio
I posted it on the Apple Dev Forums as well: developer.apple.com/forums/thread/694641Mauricemauricio
I submitted a feedback report with Apple: feedbackassistant.apple.com/feedback/9763803Mauricemauricio

© 2022 - 2024 — McMap. All rights reserved.