Hello Guest

2D sprite animation: one or more textures?

  • 10 Replies
  • 29348 Views
2D sprite animation: one or more textures?
« on: June 06, 2009, 09:12:47 »
Hi there.

I wonder what could be the "best" method to handle 2D sprite animations (like walking characters). Each specific animation is stored in a single image, like these 10 frames :



This means the image contains all the frames of a single animation.

Now I wonder if it's better :
1) to split all the n frames in n textures
or
2) to keep the whole image in a single texture and shift texture coordinates in real-time
or anything else... :P

Thanks  ;)
« Last Edit: June 06, 2009, 14:06:07 by lorcán »

Re: 2D sprite animation: one or more textures?
« Reply #1 on: June 06, 2009, 12:33:59 »
I'm using 2D GL_Texture's for EACH sprite in every Animation. This works very well, raising ~30 FPS with LWJGL. Of course, the WHOLE animation must be loaded in GL cache before to use it.
Then it's possible to make 1000+ sprites in cache and play at 30 FPS. Notice that OpenGL Texture's are automatically cached and lift up in the VRAM as needed by the GL Thread !

Hence it is quite easy. Animation's would handle Texture "int" bindings in an usual array of int[]'s. :D

Re: 2D sprite animation: one or more textures?
« Reply #2 on: June 08, 2009, 12:21:45 »
OK

Quote
Hence it is quite easy. Animation's would handle Texture "int" bindings in an usual array of int[]'s. :D

Or maybe using a main single collection (hashmap or so) of java objects (each of these objects is a "human" object representation of a texture, and therefore contains the "int" binding of this existing texture in VRAM and a texture name as String...). So the animation would rather handle a table of string names, which would be the key in the main collection of java "texture" objects. I'm afraid that using the int values instead are a bit dangerous (after deleting a texture, and so on). What do you think?


« Last Edit: June 08, 2009, 12:24:49 by lorcán »

Re: 2D sprite animation: one or more textures?
« Reply #3 on: June 08, 2009, 13:04:04 »
That's right, deleting a texture would require to update the array. For I'm not telling you the exact manner, it is clearly the best way yours.
Let's say a collection of texture would imply 2 key-bindings : one for an unique identifier for the texture and the second (let it be the value of each mapping) be the OpenGL binding.
As the Texture is deleted, the corrsponding mapping must be cleared off.
I'm using for myself hashCode() and openGL bindings in a static HashMap that is updated each time it is needed.

One thing really I'm sure : storing int's--hashCode's are and bindings so they are--is faster than running over String's. BTW String's have unique hashCode's too.
At least you can (watch out you have to re-define hashCode() with correct values--e.g. System.nanoTime()) specify each texture as Object's with specific parameters such as size or color mode.

Re: 2D sprite animation: one or more textures?
« Reply #4 on: June 09, 2009, 08:40:12 »
Quote
I'm using for myself hashCode() and openGL bindings in a static HashMap that is updated each time it is needed.

One thing really I'm sure : storing int's--hashCode's are and bindings so they are--is faster than running over String's. BTW String's have unique hashCode's too.
At least you can (watch out you have to re-define hashCode() with correct values--e.g. System.nanoTime()) specify each texture as Object's with specific parameters such as size or color mode.

This interests me! Do you have some code samples? (eg your texture class and other related classes)

BTW aren't you French-speaking? ;)

Re: 2D sprite animation: one or more textures?
« Reply #5 on: June 09, 2009, 14:59:40 »

Re: 2D sprite animation: one or more textures?
« Reply #6 on: June 10, 2009, 10:19:57 »
Some sprite animations benefit from using a 3D texture, i.e. put frames on top of each other in a single 3D texture and animate by adjusting the R texture coordinate. This has the advantage that you can get linear filtering between frames.

It depends on the type of animation how it will turn out, though. Something like a character animation would look rather bad but effects like explosions, fire, smoke, etc. look very smooth. You can get away with fewer frames or play an animation slower without stuttering.

Edit:
I've been experimenting with this a bit lately. If you want to see how it looks in comparison, try this:
http://ciardhubh.de/download/node/19/3DTextureAnimation.jnlp (Webstart)

The top two quads are textured using an array of individual 2D textures. The lower two are textured using one 3D texture.
« Last Edit: June 11, 2009, 18:30:52 by Ciardhubh »

Re: 2D sprite animation: one or more textures?
« Reply #7 on: June 16, 2009, 09:26:30 »
Thanks Ciardhubh & broumbroum :)

Ciardhubh I don't think I'll use 3D textures in my current game however I'd be interested in your code for experimenting and maybe I'll use 3D textures in a future project :-) I'm quite amazed by the smooth transition between each animation frames. Really nice results! ;)

Re: 2D sprite animation: one or more textures?
« Reply #8 on: June 22, 2009, 11:34:41 »

Re: 2D sprite animation: one or more textures?
« Reply #9 on: June 22, 2009, 12:56:19 »
Thank you ! I was also looking for this.

Re: 2D sprite animation: one or more textures?
« Reply #10 on: June 23, 2009, 05:50:02 »
Thank you very much for your interesting article Ciardhubh! ;)