class_imagetexture.rst 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. :github_url: hide
  2. .. DO NOT EDIT THIS FILE!!!
  3. .. Generated automatically from Godot engine sources.
  4. .. Generator: https://github.com/godotengine/godot/tree/3.5/doc/tools/make_rst.py.
  5. .. XML source: https://github.com/godotengine/godot/tree/3.5/doc/classes/ImageTexture.xml.
  6. .. _class_ImageTexture:
  7. ImageTexture
  8. ============
  9. **Inherits:** :ref:`Texture<class_Texture>` **<** :ref:`Resource<class_Resource>` **<** :ref:`Reference<class_Reference>` **<** :ref:`Object<class_Object>`
  10. A :ref:`Texture<class_Texture>` based on an :ref:`Image<class_Image>`.
  11. .. rst-class:: classref-introduction-group
  12. Description
  13. -----------
  14. A :ref:`Texture<class_Texture>` based on an :ref:`Image<class_Image>`. For an image to be displayed, an **ImageTexture** has to be created from it using the :ref:`create_from_image<class_ImageTexture_method_create_from_image>` method:
  15. ::
  16. var texture = ImageTexture.new()
  17. var image = Image.new()
  18. image.load("res://icon.png")
  19. texture.create_from_image(image)
  20. $Sprite.texture = texture
  21. This way, textures can be created at run-time by loading images both from within the editor and externally.
  22. \ **Warning:** Prefer to load imported textures with :ref:`@GDScript.load<class_@GDScript_method_load>` over loading them from within the filesystem dynamically with :ref:`Image.load<class_Image_method_load>`, as it may not work in exported projects:
  23. ::
  24. var texture = load("res://icon.png")
  25. $Sprite.texture = texture
  26. This is because images have to be imported as :ref:`StreamTexture<class_StreamTexture>` first to be loaded with :ref:`@GDScript.load<class_@GDScript_method_load>`. If you'd still like to load an image file just like any other :ref:`Resource<class_Resource>`, import it as an :ref:`Image<class_Image>` resource instead, and then load it normally using the :ref:`@GDScript.load<class_@GDScript_method_load>` method.
  27. But do note that the image data can still be retrieved from an imported texture as well using the :ref:`Texture.get_data<class_Texture_method_get_data>` method, which returns a copy of the data:
  28. ::
  29. var texture = load("res://icon.png")
  30. var image : Image = texture.get_data()
  31. An **ImageTexture** is not meant to be operated from within the editor interface directly, and is mostly useful for rendering images on screen dynamically via code. If you need to generate images procedurally from within the editor, consider saving and importing images as custom texture resources implementing a new :ref:`EditorImportPlugin<class_EditorImportPlugin>`.
  32. \ **Note:** The maximum texture size is 16384×16384 pixels due to graphics hardware limitations.
  33. .. rst-class:: classref-introduction-group
  34. Tutorials
  35. ---------
  36. - :doc:`Importing images <../tutorials/assets_pipeline/importing_images>`
  37. .. rst-class:: classref-reftable-group
  38. Properties
  39. ----------
  40. .. table::
  41. :widths: auto
  42. +-------------------------------------------+-----------------------------------------------------------------+----------------------------------------------------------------+
  43. | :ref:`int<class_int>` | flags | ``7`` (overrides :ref:`Texture<class_Texture_property_flags>`) |
  44. +-------------------------------------------+-----------------------------------------------------------------+----------------------------------------------------------------+
  45. | :ref:`float<class_float>` | :ref:`lossy_quality<class_ImageTexture_property_lossy_quality>` | ``0.7`` |
  46. +-------------------------------------------+-----------------------------------------------------------------+----------------------------------------------------------------+
  47. | :ref:`Storage<enum_ImageTexture_Storage>` | :ref:`storage<class_ImageTexture_property_storage>` | ``0`` |
  48. +-------------------------------------------+-----------------------------------------------------------------+----------------------------------------------------------------+
  49. .. rst-class:: classref-reftable-group
  50. Methods
  51. -------
  52. .. table::
  53. :widths: auto
  54. +---------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  55. | void | :ref:`create<class_ImageTexture_method_create>` **(** :ref:`int<class_int>` width, :ref:`int<class_int>` height, :ref:`Format<enum_Image_Format>` format, :ref:`int<class_int>` flags=7 **)** |
  56. +---------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  57. | void | :ref:`create_from_image<class_ImageTexture_method_create_from_image>` **(** :ref:`Image<class_Image>` image, :ref:`int<class_int>` flags=7 **)** |
  58. +---------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  59. | :ref:`Format<enum_Image_Format>` | :ref:`get_format<class_ImageTexture_method_get_format>` **(** **)** |const| |
  60. +---------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  61. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`load<class_ImageTexture_method_load>` **(** :ref:`String<class_String>` path **)** |
  62. +---------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  63. | void | :ref:`set_data<class_ImageTexture_method_set_data>` **(** :ref:`Image<class_Image>` image **)** |
  64. +---------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  65. | void | :ref:`set_size_override<class_ImageTexture_method_set_size_override>` **(** :ref:`Vector2<class_Vector2>` size **)** |
  66. +---------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  67. .. rst-class:: classref-section-separator
  68. ----
  69. .. rst-class:: classref-descriptions-group
  70. Enumerations
  71. ------------
  72. .. _enum_ImageTexture_Storage:
  73. .. rst-class:: classref-enumeration
  74. enum **Storage**:
  75. .. _class_ImageTexture_constant_STORAGE_RAW:
  76. .. rst-class:: classref-enumeration-constant
  77. :ref:`Storage<enum_ImageTexture_Storage>` **STORAGE_RAW** = ``0``
  78. :ref:`Image<class_Image>` data is stored raw and unaltered.
  79. .. _class_ImageTexture_constant_STORAGE_COMPRESS_LOSSY:
  80. .. rst-class:: classref-enumeration-constant
  81. :ref:`Storage<enum_ImageTexture_Storage>` **STORAGE_COMPRESS_LOSSY** = ``1``
  82. :ref:`Image<class_Image>` data is compressed with a lossy algorithm. You can set the storage quality with :ref:`lossy_quality<class_ImageTexture_property_lossy_quality>`.
  83. .. _class_ImageTexture_constant_STORAGE_COMPRESS_LOSSLESS:
  84. .. rst-class:: classref-enumeration-constant
  85. :ref:`Storage<enum_ImageTexture_Storage>` **STORAGE_COMPRESS_LOSSLESS** = ``2``
  86. :ref:`Image<class_Image>` data is compressed with a lossless algorithm.
  87. .. rst-class:: classref-section-separator
  88. ----
  89. .. rst-class:: classref-descriptions-group
  90. Property Descriptions
  91. ---------------------
  92. .. _class_ImageTexture_property_lossy_quality:
  93. .. rst-class:: classref-property
  94. :ref:`float<class_float>` **lossy_quality** = ``0.7``
  95. .. rst-class:: classref-property-setget
  96. - void **set_lossy_storage_quality** **(** :ref:`float<class_float>` value **)**
  97. - :ref:`float<class_float>` **get_lossy_storage_quality** **(** **)**
  98. The storage quality for :ref:`STORAGE_COMPRESS_LOSSY<class_ImageTexture_constant_STORAGE_COMPRESS_LOSSY>`.
  99. .. rst-class:: classref-item-separator
  100. ----
  101. .. _class_ImageTexture_property_storage:
  102. .. rst-class:: classref-property
  103. :ref:`Storage<enum_ImageTexture_Storage>` **storage** = ``0``
  104. .. rst-class:: classref-property-setget
  105. - void **set_storage** **(** :ref:`Storage<enum_ImageTexture_Storage>` value **)**
  106. - :ref:`Storage<enum_ImageTexture_Storage>` **get_storage** **(** **)**
  107. The storage type (raw, lossy, or compressed).
  108. .. rst-class:: classref-section-separator
  109. ----
  110. .. rst-class:: classref-descriptions-group
  111. Method Descriptions
  112. -------------------
  113. .. _class_ImageTexture_method_create:
  114. .. rst-class:: classref-method
  115. void **create** **(** :ref:`int<class_int>` width, :ref:`int<class_int>` height, :ref:`Format<enum_Image_Format>` format, :ref:`int<class_int>` flags=7 **)**
  116. Create a new **ImageTexture** with ``width`` and ``height``.
  117. \ ``format`` is a value from :ref:`Format<enum_Image_Format>`, ``flags`` is any combination of :ref:`Flags<enum_Texture_Flags>`.
  118. .. rst-class:: classref-item-separator
  119. ----
  120. .. _class_ImageTexture_method_create_from_image:
  121. .. rst-class:: classref-method
  122. void **create_from_image** **(** :ref:`Image<class_Image>` image, :ref:`int<class_int>` flags=7 **)**
  123. Initializes the texture by allocating and setting the data from an :ref:`Image<class_Image>` with ``flags`` from :ref:`Flags<enum_Texture_Flags>`. An sRGB to linear color space conversion can take place, according to :ref:`Format<enum_Image_Format>`.
  124. .. rst-class:: classref-item-separator
  125. ----
  126. .. _class_ImageTexture_method_get_format:
  127. .. rst-class:: classref-method
  128. :ref:`Format<enum_Image_Format>` **get_format** **(** **)** |const|
  129. Returns the format of the texture, one of :ref:`Format<enum_Image_Format>`.
  130. .. rst-class:: classref-item-separator
  131. ----
  132. .. _class_ImageTexture_method_load:
  133. .. rst-class:: classref-method
  134. :ref:`Error<enum_@GlobalScope_Error>` **load** **(** :ref:`String<class_String>` path **)**
  135. Loads an image from a file path and creates a texture from it.
  136. \ **Note:** This method is deprecated and will be removed in Godot 4.0, use :ref:`Image.load<class_Image_method_load>` and :ref:`create_from_image<class_ImageTexture_method_create_from_image>` instead.
  137. .. rst-class:: classref-item-separator
  138. ----
  139. .. _class_ImageTexture_method_set_data:
  140. .. rst-class:: classref-method
  141. void **set_data** **(** :ref:`Image<class_Image>` image **)**
  142. Replaces the texture's data with a new :ref:`Image<class_Image>`.
  143. \ **Note:** The texture has to be initialized first with the :ref:`create_from_image<class_ImageTexture_method_create_from_image>` method before it can be updated. The new image dimensions, format, and mipmaps configuration should match the existing texture's image configuration, otherwise it has to be re-created with the :ref:`create_from_image<class_ImageTexture_method_create_from_image>` method.
  144. Use this method over :ref:`create_from_image<class_ImageTexture_method_create_from_image>` if you need to update the texture frequently, which is faster than allocating additional memory for a new texture each time.
  145. .. rst-class:: classref-item-separator
  146. ----
  147. .. _class_ImageTexture_method_set_size_override:
  148. .. rst-class:: classref-method
  149. void **set_size_override** **(** :ref:`Vector2<class_Vector2>` size **)**
  150. Resizes the texture to the specified dimensions.
  151. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  152. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  153. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
  154. .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`