class_imagetexture.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. :github_url: hide
  2. .. Generated automatically by doc/tools/make_rst.py in Godot's source tree.
  3. .. DO NOT EDIT THIS FILE, but the ImageTexture.xml source instead.
  4. .. The source is found in doc/classes or modules/<name>/doc_classes.
  5. .. _class_ImageTexture:
  6. ImageTexture
  7. ============
  8. **Inherits:** :ref:`Texture<class_Texture>` **<** :ref:`Resource<class_Resource>` **<** :ref:`Reference<class_Reference>` **<** :ref:`Object<class_Object>`
  9. A :ref:`Texture<class_Texture>` based on an :ref:`Image<class_Image>`.
  10. Description
  11. -----------
  12. 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:
  13. ::
  14. var texture = ImageTexture.new()
  15. var image = Image.new()
  16. image.load("res://icon.png")
  17. texture.create_from_image(image)
  18. $Sprite.texture = texture
  19. This way, textures can be created at run-time by loading images both from within the editor and externally.
  20. **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:
  21. ::
  22. var texture = load("res://icon.png")
  23. $Sprite.texture = texture
  24. 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.
  25. 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:
  26. ::
  27. var texture = load("res://icon.png")
  28. var image : Image = texture.get_data()
  29. 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>`.
  30. **Note:** The maximum texture size is 16384×16384 pixels due to graphics hardware limitations.
  31. Tutorials
  32. ---------
  33. - :doc:`../tutorials/assets_pipeline/importing_images`
  34. Properties
  35. ----------
  36. +-------------------------------------------+-----------------------------------------------------------------+---------------------------+
  37. | :ref:`int<class_int>` | flags | ``7`` *(parent override)* |
  38. +-------------------------------------------+-----------------------------------------------------------------+---------------------------+
  39. | :ref:`float<class_float>` | :ref:`lossy_quality<class_ImageTexture_property_lossy_quality>` | ``0.7`` |
  40. +-------------------------------------------+-----------------------------------------------------------------+---------------------------+
  41. | :ref:`Storage<enum_ImageTexture_Storage>` | :ref:`storage<class_ImageTexture_property_storage>` | ``0`` |
  42. +-------------------------------------------+-----------------------------------------------------------------+---------------------------+
  43. Methods
  44. -------
  45. +---------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  46. | 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 **)** |
  47. +---------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  48. | void | :ref:`create_from_image<class_ImageTexture_method_create_from_image>` **(** :ref:`Image<class_Image>` image, :ref:`int<class_int>` flags=7 **)** |
  49. +---------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  50. | :ref:`Format<enum_Image_Format>` | :ref:`get_format<class_ImageTexture_method_get_format>` **(** **)** |const| |
  51. +---------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  52. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`load<class_ImageTexture_method_load>` **(** :ref:`String<class_String>` path **)** |
  53. +---------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  54. | void | :ref:`set_data<class_ImageTexture_method_set_data>` **(** :ref:`Image<class_Image>` image **)** |
  55. +---------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  56. | void | :ref:`set_size_override<class_ImageTexture_method_set_size_override>` **(** :ref:`Vector2<class_Vector2>` size **)** |
  57. +---------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  58. Enumerations
  59. ------------
  60. .. _enum_ImageTexture_Storage:
  61. .. _class_ImageTexture_constant_STORAGE_RAW:
  62. .. _class_ImageTexture_constant_STORAGE_COMPRESS_LOSSY:
  63. .. _class_ImageTexture_constant_STORAGE_COMPRESS_LOSSLESS:
  64. enum **Storage**:
  65. - **STORAGE_RAW** = **0** --- :ref:`Image<class_Image>` data is stored raw and unaltered.
  66. - **STORAGE_COMPRESS_LOSSY** = **1** --- :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>`.
  67. - **STORAGE_COMPRESS_LOSSLESS** = **2** --- :ref:`Image<class_Image>` data is compressed with a lossless algorithm.
  68. Property Descriptions
  69. ---------------------
  70. .. _class_ImageTexture_property_lossy_quality:
  71. - :ref:`float<class_float>` **lossy_quality**
  72. +-----------+----------------------------------+
  73. | *Default* | ``0.7`` |
  74. +-----------+----------------------------------+
  75. | *Setter* | set_lossy_storage_quality(value) |
  76. +-----------+----------------------------------+
  77. | *Getter* | get_lossy_storage_quality() |
  78. +-----------+----------------------------------+
  79. The storage quality for :ref:`STORAGE_COMPRESS_LOSSY<class_ImageTexture_constant_STORAGE_COMPRESS_LOSSY>`.
  80. ----
  81. .. _class_ImageTexture_property_storage:
  82. - :ref:`Storage<enum_ImageTexture_Storage>` **storage**
  83. +-----------+--------------------+
  84. | *Default* | ``0`` |
  85. +-----------+--------------------+
  86. | *Setter* | set_storage(value) |
  87. +-----------+--------------------+
  88. | *Getter* | get_storage() |
  89. +-----------+--------------------+
  90. The storage type (raw, lossy, or compressed).
  91. Method Descriptions
  92. -------------------
  93. .. _class_ImageTexture_method_create:
  94. - void **create** **(** :ref:`int<class_int>` width, :ref:`int<class_int>` height, :ref:`Format<enum_Image_Format>` format, :ref:`int<class_int>` flags=7 **)**
  95. Create a new ``ImageTexture`` with ``width`` and ``height``.
  96. ``format`` is a value from :ref:`Format<enum_Image_Format>`, ``flags`` is any combination of :ref:`Flags<enum_Texture_Flags>`.
  97. ----
  98. .. _class_ImageTexture_method_create_from_image:
  99. - void **create_from_image** **(** :ref:`Image<class_Image>` image, :ref:`int<class_int>` flags=7 **)**
  100. 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>`.
  101. ----
  102. .. _class_ImageTexture_method_get_format:
  103. - :ref:`Format<enum_Image_Format>` **get_format** **(** **)** |const|
  104. Returns the format of the texture, one of :ref:`Format<enum_Image_Format>`.
  105. ----
  106. .. _class_ImageTexture_method_load:
  107. - :ref:`Error<enum_@GlobalScope_Error>` **load** **(** :ref:`String<class_String>` path **)**
  108. Loads an image from a file path and creates a texture from it.
  109. **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.
  110. ----
  111. .. _class_ImageTexture_method_set_data:
  112. - void **set_data** **(** :ref:`Image<class_Image>` image **)**
  113. Replaces the texture's data with a new :ref:`Image<class_Image>`.
  114. **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.
  115. 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.
  116. ----
  117. .. _class_ImageTexture_method_set_size_override:
  118. - void **set_size_override** **(** :ref:`Vector2<class_Vector2>` size **)**
  119. Resizes the texture to the specified dimensions.
  120. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  121. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  122. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`