Search code examples
imageunity-game-enginespriteshader

Optimization of sprites (quantity vs redundant transparency)


What should i use in 2D game heavy populated with 2d transparent sprites.

  • Images that are cropped to the maximum with as less as possible transparency. But i would need a few of those to complete one image.
  • One big sprite where transparent piexls are more then 50% of the image.
  • Or mayby this doesnt matter, and i should just take diffrent approche then Sprite Renderers.

All sugestions appreciated. Examples below.

I will mention that i'm using Unity to make this game and it have to be as fast on mobile as posible. But an anwser doenst have to be Unity related.

Required multiple images to finish one image but without transparency

  • Required multiple images to finish one image but without mutch of transparency

Single image but with a lot or redundant piexels

  • Single image but with a lot or redundant piexels

Solution

  • After four years I can answer my question with definite confidence.

    Batching

    It's always better to use a smaller image with fewer pixels, and re-use it multiple times. Unity has a pipeline that will cache your image in GPU, and use it multiple times for the same shader code. All that with one CPU to GPU call. This effect is called "batching".

    Draw calls are often resource-intensive, with the graphics API doing significant work for every draw call, causing performance overhead on the CPU side. This is mostly caused by the state changes done between the draw calls (such as switching to a different Material), which causes resource-intensive validation and translation steps in the graphics driver.

    Batching: transforms vertices of a mesh (sprite) on the CPU, groups many similar vertices together, and draws them all in one go.

    Smaller images ensure less GPU memory is used for an operation that is almost "Free" with batching.

    Stenciling

    You can limit and simplify shared execution with stenciling by creating stencil buffers. Instead of rendering every single pixel (even with opacity 0), you can increase stencil value to have an alpha cut off at 0.3 or even 0.5 - as usually, you don't need that soft of a shadow or soft edges, and that speeds up executions time.

    The default sprite shader does this for you with very small stencil value, but you can find a multitude of plugins or shaders that can help you with that and increase the speed of your game with a small graphical tradeoff.