importing_textures.rst 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. .. _doc_importing_textures:
  2. Importing textures
  3. ==================
  4. Do NOT import them in most cases
  5. --------------------------------
  6. In most cases you **don't** want images imported when dealing with 2D
  7. and GUI. Just copy them to the filesystem. Read the tutorial on
  8. :ref:`doc_managing_image_files` before continuing! For 3D,
  9. textures are always imported by the 3D scene importer, so importing
  10. those is only useful when importing a texture used for 3D that doesn't
  11. come with the 3D scene (for example, in a shader). The flags and options
  12. are the same as here, so reading the rest of the document might help
  13. too.
  14. OK, you *might* want to import them
  15. -----------------------------------
  16. So, if you have read the previous tutorial on the texture exporter, the
  17. texture importer gives you more fine-grained control on how textures
  18. are imported. If you want to change flags such as repeat, filter,
  19. mipmaps, fix edges, etc. ***PER texture***, importing them is the best
  20. way to accomplish this (since you can't save such flags in a standard
  21. image file).
  22. Lack of MipMaps
  23. ---------------
  24. Images in 3D hardware are scaled with a (bi)linear filter, but this
  25. method has limitations. When images are shrunk too much, two problems
  26. arise:
  27. - **Aliasing**: Pixels are skipped too much, and the image shows
  28. discontinuities. This decreases quality.
  29. - **Cache Misses**: Pixels being read are too far apart, so texture
  30. cache reads a lot more data than it should. This decreases
  31. performance.
  32. .. image:: /img/imagemipmap.png
  33. To solve this, mipmaps are created. Mipmaps are versions of the image
  34. shrunk by half in both axis, recursively, until the image is 1 pixel of
  35. size. When the 3D hardware needs to shrink the image, it finds the
  36. largest mipmap it can scale from, and scales from there. This improves
  37. performance and image quality.
  38. .. image:: /img/mipmaps.png
  39. Godot automatically creates mipmaps upon load for standard image files.
  40. This process is time consuming (although not much) and makes load times
  41. a little worse. Pre-importing the textures allows the automatic
  42. generation of mipmaps.
  43. Unwanted MipMaps
  44. ----------------
  45. Remember the previous point about mipmaps? Yes, they are cool, but
  46. mobile GPUs only support them if the textures are in power of 2
  47. dimensions (i.e. 256x256 or 512x128). In these platforms, Godot will
  48. stretch and enlarge the texture to the closest power of 2 size and then
  49. generate the mipmaps. This process takes more of a performance hit and
  50. it might degrade the quality a little more.
  51. Because of this, there are some scenarios when it may be desirable to
  52. not use them, and just use a linear filter. One of them is when working
  53. with graphical user interfaces (GUIs). Usually they are made of large
  54. images and don't stretch much. Even if the screen resolution is in a
  55. larger or smaller value than original art, the amount of stretch is not
  56. as much and the art can retain the quality. Pre-importing the textures
  57. also allows the disabling of mipmap generation.
  58. Blending artifacts
  59. ------------------
  60. The `blending
  61. equation <http://en.wikipedia.org/wiki/Alpha_compositing>`__ used by
  62. applications like Photoshop is too complex for realtime. There are
  63. better approximations such as `pre-multiplied
  64. alpha <http://blogs.msdn.com/b/shawnhar/archive/2009/11/06/premultiplied-alpha.aspx?Redirected=true>`__,
  65. but they impose more stress in the asset pipeline. In the end, we are
  66. left with textures that have artifacts in the edges, because apps such
  67. as Photoshop store white pixels in completely transparent areas. Such
  68. white pixels end up showing thanks to the texture filter.
  69. Godot has an option to fix the edges of the image (by painting invisible
  70. pixels the same color as the visible neighbours):
  71. .. image:: /img/fixedborder.png
  72. However, this must be done every time the image changes. Pre-Importing
  73. the textures makes sure that every time the original file changes, this
  74. artifact is fixed upon automatic re-import.
  75. Texture flags
  76. -------------
  77. Textures have flags. The user can choose for them to repeat or clamp to
  78. edges (when UVs exceed the 0,0,1,1 boundary). The magnifying filter can
  79. also be turned off (for a Minecraft-like effect). Such values can not be
  80. edited in standard file formats (png, jpg, etc.), but can be edited and
  81. saved in Godot .tex files. Then again, the user may not want to change
  82. the values every time the texture changes. Pre-Importing the textures
  83. also takes care of that.
  84. Texture compression
  85. -------------------
  86. Aside from the typical texture compression, which saves space on disk
  87. (.png, jpg, etc.), there are also texture compression formats that save
  88. space in memory (more specifically video memory. This allows to have
  89. much better looking textures in games without running out of memory, and
  90. decrease memory bandwidth when reading them so they are a big plus.
  91. There are several video texture compression formats, none of which are
  92. standard. Apple uses PVRTC. PC GPUs, consoles and nVidia Android devices use
  93. S3TC (BC), other chipsets use other formats. OpenGL ES 3.0 standardized on ETC
  94. format, but we are still a few years away from that working everywhere.
  95. Still, when using this option, Godot converts and compresses to the
  96. relevant format depending on the target platform (as long as the user
  97. pre-imported the texture and specified video ram compression!).
  98. This kind of compression is often not desirable for many types of 2D games
  99. and UIs because it is lossy, creating visual artifacts. This is especially
  100. noticeable on games that use the trendy vectory social game artwork.
  101. However, the fact that it saves space and improves performance may make up for
  102. it.
  103. The 3D scene importer always imports textures with this option turned
  104. on.
  105. Atlases
  106. -------
  107. Remember how mobile GPUs have this limitation of textures having to be
  108. in power of 2 sizes to be able to generate mimpmaps for optimum
  109. stretching? What if we have a lot of images in different random sizes?
  110. All will have to be scaled and mipmapped when loaded (using more CPU and
  111. memory) or when imported (taking more storage space). This is probably still
  112. OK, but there is a tool that can help improve this situation.
  113. Atlases are big textures that fit a lot of small textures inside
  114. efficiently. Godot supports creating atlases in the importer, and the
  115. imported files are just small resources that reference a region of the
  116. bigger texture.
  117. Atlases can be a nice solution to save some space on GUI or 2D artwork
  118. by packing everything together. The current importer is not as useful
  119. for 3D though (3D Atlases are created differently, and not all 3D
  120. models can use them).
  121. As a small plus, atlases can decrease the amount of "state changes" when
  122. drawing. If a lot of objects that are drawn using several different
  123. textures are converted to an atlas, then the texture rebinds per object
  124. will go from dozens or hundreds to one. This will give the performance a
  125. small boost.
  126. Artists use PSD
  127. ---------------
  128. Still wondering whether to use the texture importer or not? Remember
  129. that in the end, artists will often use Photoshop anyway, so it may be
  130. wiser to just let the import subsystem to take care of importing and
  131. converting the PSD files instead of asking the artist to save a png and
  132. copy it to the project every time.
  133. Texture importer
  134. ----------------
  135. Finally! It's time to take a look at the texture importer. There are 3
  136. options in the import menu. They are pretty much (almost) the same
  137. dialog with a different set of defaults.
  138. .. image:: /img/importtex.png
  139. When selected, the texture import dialog will appear. This is the
  140. default one for 2D textures:
  141. .. image:: /img/import_images.png
  142. Each import option has a function, explained as follows:
  143. Source texture(s)
  144. ~~~~~~~~~~~~~~~~~
  145. One or more source images can be selected from the same folder (this
  146. importer can do batch-conversion). This can be from inside or outside
  147. the project.
  148. Target path
  149. ~~~~~~~~~~~
  150. A destination folder must be provided. It must be inside the project, as
  151. textures will be converted and saved to it. Extensions will be changed
  152. to .tex (Godot resource file for textures), but names will be kept.
  153. Texture format
  154. ~~~~~~~~~~~~~~
  155. This combo allows to change the texture format (compression in this
  156. case):
  157. .. image:: /img/compressopts.png
  158. Each of the four options described in this table together with their
  159. advantages and disadvantages ( |good| = Best, |bad| =Worst ):
  160. +----------------+------------------------+---------------------------+-------------------------+------------------------------------------------------+
  161. | | Uncompressed | Compress Lossless (PNG) | Compress Lossy (WebP) | Compress VRAM |
  162. +================+========================+===========================+=========================+======================================================+
  163. | Description | Stored as raw pixels | Stored as PNG | Stored as WebP | Stored as S3TC/BC,PVRTC/ETC, depending on platform |
  164. +----------------+------------------------+---------------------------+-------------------------+------------------------------------------------------+
  165. | Size on Disk | |bad| Large | |regular| Small | |good| Very Small | |regular| Small |
  166. +----------------+------------------------+---------------------------+-------------------------+------------------------------------------------------+
  167. | Memory Usage | |bad| Large | |bad| Large | |bad| Large | |good| Small |
  168. +----------------+------------------------+---------------------------+-------------------------+------------------------------------------------------+
  169. | Performance | |regular| Normal | |regular| Normal | |regular| Normal | |good| Fast |
  170. +----------------+------------------------+---------------------------+-------------------------+------------------------------------------------------+
  171. | Quality Loss | |good| None | |good| None | |regular| Slight | |bad| Moderate |
  172. +----------------+------------------------+---------------------------+-------------------------+------------------------------------------------------+
  173. | Load Time | |regular| Normal | |bad| Slow | |bad| Slow | |good| Fast |
  174. +----------------+------------------------+---------------------------+-------------------------+------------------------------------------------------+
  175. Texture options
  176. ~~~~~~~~~~~~~~~
  177. Provided are a small amount of options for fine grained import control:
  178. - **Streaming Format** - This does nothing as of yet, but a texture
  179. format for streaming different mipmap levels is planned. Big engines
  180. have support for this.
  181. - **Fix Border Alpha** - This will fix texture borders to avoid the
  182. white auras created by white invisible pixels (see the rant above).
  183. - **Alpha Bit Hint** - Godot auto-detects if the texture needs alpha
  184. bit support for transparency (instead of full range), which is useful
  185. for compressed formats such as BC. This forces alpha to be 0 or 1.
  186. - **Compress Extra** - Some VRAM compressions have alternate formats
  187. that compress more at the expense of quality (PVRTC2 for example). If
  188. this is ticked, texture will be smaller but look worse.
  189. - **No MipMaps** - Force imported texture to NOT use mipmaps. This may
  190. be desirable in some cases for 2D (as explained in the rant above),
  191. though it's NEVER desirable for 3D.
  192. - **Repeat** - Texture will repeat when UV coordinates go beyond 1 and
  193. below 0. This is often desirable in 3D, but may generate artifacts in
  194. 2D.
  195. - **Filter** - Enables linear filtering when a texture texel is larger
  196. than a screen pixel. This is usually turned on, unless it's required
  197. for artistic purposes (Minecraft look, for example).
  198. .. |bad| image:: /img/bad.png
  199. .. |good| image:: /img/good.png
  200. .. |regular| image:: /img/regular.png