ImageTexture

Inherits: Texture < Resource < Reference < Object

A Texture based on an Image.

Description

A Texture based on an Image. For an image to be displayed, an ImageTexture has to be created from it using the create_from_image method:

  1. var texture = ImageTexture.new()
  2. var image = Image.new()
  3. image.load("res://icon.png")
  4. texture.create_from_image(image)
  5. $Sprite.texture = texture

This way, textures can be created at run-time by loading images both from within the editor and externally.

Warning: Prefer to load imported textures with @GDScript.load over loading them from within the filesystem dynamically with Image.load, as it may not work in exported projects:

  1. var texture = load("res://icon.png")
  2. $Sprite.texture = texture

This is because images have to be imported as StreamTexture first to be loaded with @GDScript.load. If you’d still like to load an image file just like any other Resource, import it as an Image resource instead, and then load it normally using the @GDScript.load method.

But do note that the image data can still be retrieved from an imported texture as well using the Texture.get_data method, which returns a copy of the data:

  1. var texture = load("res://icon.png")
  2. var image : Image = texture.get_data()

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 EditorImportPlugin.

Note: The maximum texture size is 16384×16384 pixels due to graphics hardware limitations.

Tutorials

Properties

intflags7 (parent override)
floatlossy_quality0.7
Storagestorage0

Methods

voidcreate ( int width, int height, Format format, int flags=7 )
voidcreate_from_image ( Image image, int flags=7 )
Formatget_format ( ) const
Errorload ( String path )
voidset_data ( Image image )
voidset_size_override ( Vector2 size )

Enumerations

enum Storage:

  • STORAGE_RAW = 0 —- Image data is stored raw and unaltered.
  • STORAGE_COMPRESS_LOSSY = 1 —- Image data is compressed with a lossy algorithm. You can set the storage quality with lossy_quality.
  • STORAGE_COMPRESS_LOSSLESS = 2 —- Image data is compressed with a lossless algorithm.

Property Descriptions

Default0.7
Setterset_lossy_storage_quality(value)
Getterget_lossy_storage_quality()

The storage quality for STORAGE_COMPRESS_LOSSY.


Default0
Setterset_storage(value)
Getterget_storage()

The storage type (raw, lossy, or compressed).

Method Descriptions

Create a new ImageTexture with width and height.

format is a value from Format, flags is any combination of Flags.


  • void create_from_image ( Image image, int flags=7 )

Initializes the texture by allocating and setting the data from an Image with flags from Flags. An sRGB to linear color space conversion can take place, according to Format.


Returns the format of the texture, one of Format.


Loads an image from a file path and creates a texture from it.

Note: the method is deprecated and will be removed in Godot 4.0, use Image.load and create_from_image instead.


  • void set_data ( Image image )

Replaces the texture’s data with a new Image.

Note: The texture has to be initialized first with the 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 create_from_image method.

Use this method over create_from_image if you need to update the texture frequently, which is faster than allocating additional memory for a new texture each time.


  • void set_size_override ( Vector2 size )

Resizes the texture to the specified dimensions.