4 # Texture Atlases {#textureatlases}
6 ## Example demo application
8 ![ ](../assets/img/texture-atlas/image-wall.jpg)
12 Application above is running slow as there are many small individual images displayed (50)
14 | Metric | Result | Explanation |
15 |--------|--------|-------------|
16 | Launch time | Slow | Has to perform: 50 file open requests and multiple reads for each image |
17 | Memory consumption| High | Has to create:50 Dali::Image objects,50 OpenGL Textures|
18 | Rendering | Slow | Has to perform: 50 glBindTexture calls per frame ( each OpenGL calls takes time) 50 a frame = 3000 calls per second @60 FPS.Texture switching is a major state change in the GPU|
22 ## Solutions to problem: Use a Texture Atlas
24 A texture atlas is simply one larger image that contains smaller images. A texture atlas is sometimes called a
25 sprite sheet, bitmap sheet or texture pack.
27 ![ ](../assets/img/texture-atlas/atlas.jpg)
30 Dali::ImageActor has the ability to display a portion of an image using ImageActor::PixelArea setting.
31 For example to display the first 3 image in the atlas
33 ![ ](../assets/img/texture-atlas/example-javascript-code.jpg)
34 ![ ](example-code.jpg)
36 ### Result of using an Atlas
38 | Metric | Result | Explanation |
39 |--------|--------|-------------|
40 | Launch time | Fast | Has to perform: - 1 file open request |
41 | Memory consumption| Low | Has to create: 1 Dali::Image objects 1 OpenGL Textures|
42 | Rendering | Fast | HHas to perform: 1 glBindTexture calls per frame ( each OpenGL calls takes time) 1 a frame = 6- calls per second @60 FPS.|
45 ## Atlas creation guide
47 Many tools exist for creating texture atlases.
48 In the following example we are using a tool called TexturePacker as DALi has an exporter script for it.
49 The exporter automatically generates a source file that has the ImageActor::PixelArea pre-defined.
51 - Download http://www.codeandweb.com/texturepacker
52 - Launch TexturePacker
53 - Go to the menu File -> Preferences
54 - Set the "Exporter directory" to be the location of dali-toolkit/texture-atlas-exporter
56 ![ ](../assets/img/texture-atlas/texture-packer-preferences.jpg)
57 ![ ](texture-packer-preferences.jpg)
59 - Restart the application!
60 - Select DALi 3D framework for new project
62 ![ ](../assets/img/texture-atlas/texture-packer-startup.jpg)
63 ![ ](texture-packer-startup.jpg)
66 ![ ](../assets/img/texture-atlas/texture-packer-add-sprites.jpg)
67 ![ ](texture-packer-add-sprites.jpg)
68 - Click publish to produce the files
69 ![ ](../assets/img/texture-atlas/texture-packer-publish.jpg)
70 ![ ](texture-packer-publish.jpg)
74 ## Using the generated cpp ( contains JavaScript code as well)
76 The generated cpp file contains 3 different ways of describing the atlas.
77 Copy and paste the section that best suits your application.
78 - Lookup table. Includes code for storing the table in a std::map for fast lookup.
80 - JavaScript property map
82 ### Using the JavaScript generated property map
84 The property map should be cut and paste in to your application. It just looks like
87 var ATLAS_IMAGE_LIST : [
88 { name: "add_user_usericon_bg", x: 2, y:109, w:105, h:105, blendMode:dali.BLENDING_ON },
89 { name: "app_background", x: 180, y:183, w:1, h:1, blendMode:dali.BLENDING_OFF },
90 { name: "btn_arrow_left", x: 141, y:183, w:11, h:20, blendMode:dali.BLENDING_ON },
91 { name: "btn_arrow_right", x: 154, y:183, w:11, h:20, blendMode:dali.BLENDING_ON },
92 { name: "icn_app_foc", x: 194, y:2, w:72, h:72, blendMode:dali.BLENDING_ON },
93 { name: "icn_app_nor", x: 109, y:109, w:72, h:72, blendMode:dali.BLENDING_ON }
95 var atlas = new dali.ResourceImage( { url: "atlas_filename.png" });
97 // display the user icon using the size / position data in the ATLAS_IMAGE_LIST
98 var userIconData = ATLAS_IMAGE_LIST[0];
99 var userIconRect = [ userIconData.x, userIconData.y,userIconData.w,userIconData.h];
101 var btnArrowLeft = new dali.ImageActor( atlas, userIconRect );
102 btnArrowLeft.setBlendMode(userIconData.blendMode);
106 ![ ](example-javascript-code.jpg)
109 ### Using the lookup table in C++
111 Cut and paste the lookup table code into your application.
115 // The following code is automatically generated when TexturePacker publishes to a cpp file.
116 const char* ATLAS_FILE_NAME( "my_first_atlas.png" ); ///< Atlas image filename
118 // Structure to hold image name and position within the atlas.
122 unsigned int x,y,w,h;
123 Dali::BlendingMode::Type blendMode; // only enable blending if image has alpha
128 const ImageInfo ImageAtlas[]=
130 { "blocks-ball", 2, 198, 51, 51, BlendingMode::ON },
131 { "bubble-ball", 288, 74, 32, 32, BlendingMode::ON },
132 { "gallery-small-52", 2, 2, 128, 128, BlendingMode::OFF },
133 { "icon-change", 219, 2, 37, 34, BlendingMode::ON },
134 { "icon-cluster-carousel", 180, 2, 37, 34, BlendingMode::ON }
137 const ImageInfo* GetImageInfo(const char* name)
139 typedef std::map< const char*, const ImageInfo* > LookupMap;
140 static LookupMap lookup;
143 for( unsigned int i = 0; i < ATLAS_IMAGE_COUNT; ++i)
145 lookup[ ImageAtlas[i].name ] = &ImageAtlas[i];
148 LookupMap::const_iterator iter = lookup.find(name);
149 if( iter != lookup.end() )
151 return (*iter).second;
153 DALI_ASSERT_ALWAYS(0 && "image name not found in atlas");
159 To use the lookup table you can do something like this:
162 // Example function on how to get an image info from the table
164 std::string fileName = std::string( DALI_IMAGE_DIR ) + ATLAS_FILE_NAME;
165 Image imageImage = Image::New( fileName );
167 const ImageInfo* info(NULL);
169 info = GetImageInfo("blocks-ball");
172 ImageActor ballActor = ImageActor::New( imageAtlas, ImageActor::PixelArea( info->x, info->y, info->w, info->h) );
173 ballActor->SetBlendMode( info->blendMode );
175 info = GetImageInfo("bubble-ball");
178 ImageActor bubbleActor = ImageActor::New( imageAtlas, ImageActor::PixelArea( info->x, info->y, info->w, info->h) );
179 bubbleActor->SetBlendMode( info->blendMode );
184 ### Using the constant definitions (C++)
186 1. Cut and paste the constant definition code into your application.
188 You'll notice the code below won't compile because C++ variables can't have a dash character.
189 E.g. BLOCKS-BALL, BUBBLE-BALL will cause errors. Do a search and replace for - and replace with underscores.
190 This is one reason why using lookup table which holds the filename as a string maybe easier to use.
194 // The following code is automatically generated when TexturePacker publishes to a cpp file.
195 const char* ATLAS_FILE_NAME( "my_first_atlas.png" );
198 // Structure to hold position / blend mode within the atlas.
201 ImageInfo(unsigned int x,unsigned int y,unsigned int w,unsigned int h, Dali::BlendingMode::Type mode)
202 :pixelArea(x,y,w,h),blendMode(mode)
204 ImageActor::PixelArea pixelArea;
205 Dali::BlendingMode::Type blendMode; // only enable blending if image has alpha
209 const ImageInfo BLOCKS-BALL( 2, 198, 51, 51 ,BlendingMode::ON );
210 const ImageInfo BUBBLE-BALL( 288, 74, 32, 32 ,BlendingMode::ON );
211 const ImageInfo GALLERY-SMALL-52( 2, 2, 128, 128 ,BlendingMode::OFF );
214 2. To use it, you can copy example code from the generated cpp file which looks
218 void LoadAtlasImages()
220 std::string fileName = std::string(DALI_IMAGE_DIR) + ATLAS_FILE_NAME;
221 Image atlasImage = Image::New( fileName );
222 ImageActor Blocksball = ImageActor::New( atlasImage, BLOCKS_BALL.pixelArea);
223 Blocksball.SetBlendMode( BLOCKS_BALL.blendMode );
225 ImageActor Bubbleball = ImageActor::New( atlasImage, BUBBLE_BALL.pixelArea);
226 Bubbleball.SetBlendMode( BUBBLE_BALL.blendMode );
231 ## Atlas creation tips
233 - Compress the atlas - \link texturecompression Compressing Textures \endlink
234 - Avoid adding large images to the Atlas.
235 - E.g. don't add background images to it. Medium to large images should be kept seperate.
237 ![ ](../assets/img/texture-atlas/atlas-size.jpg)
241 - Try to ensure the atlas contains only images that are frequently used. There's no point in having images taking up GPU texture memory if they're not displayed.
242 - Avoid very large atlases. Try to create multiple atlases based on how they are used within your application.
243 Alternatively Texture packer has the option to split atlases ( search help for Multipack)
247 @class _Guide_TextureAtlases