Documentation
¶
Overview ¶
A Texture2D based on an Image. For an image to be displayed, an ImageTexture has to be created from it using the CreateFromImage method:
package main import ( "graphics.gd/classdb/Image" "graphics.gd/classdb/ImageTexture" "graphics.gd/classdb/Sprite2D" ) func ExampleImageTexture(sprite Sprite2D.Instance) { var image = Image.LoadFromFile("res://icon.svg") var texture = ImageTexture.CreateFromImage(image) sprite.SetTexture(texture.AsTexture2D()) }
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:
package main import ( "graphics.gd/classdb/Resource" "graphics.gd/classdb/Sprite2D" "graphics.gd/classdb/Texture2D" ) func ExampleLoadImageTexture(sprite Sprite2D.Instance) { var texture = Resource.Load[Texture2D.Instance]("res://icon.svg") sprite.SetTexture(texture.AsTexture2D()) }
This is because images have to be imported as a CompressedTexture2D 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.
Note: The image can be retrieved from an imported texture using the Texture2D.GetImage method, which returns a copy of the image:
package main import ( "graphics.gd/classdb/Resource" "graphics.gd/classdb/Sprite2D" "graphics.gd/classdb/Texture2D" ) func ExampleLoadImage(sprite Sprite2D.Instance) { var texture = Resource.Load[Texture2D.Instance]("res://icon.svg") var image = texture.GetImage() _ = image }
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.
Index ¶
- type Advanced
- type Any
- type Extension
- func (self *Extension[T]) AsImageTexture() Instance
- func (self *Extension[T]) AsObject() [1]gd.Object
- func (self *Extension[T]) AsRefCounted() [1]gd.RefCounted
- func (self *Extension[T]) AsResource() Resource.Instance
- func (self *Extension[T]) AsTexture() Texture.Instance
- func (self *Extension[T]) AsTexture2D() Texture2D.Instance
- type ID
- type Instance
- func (self Instance) AsImageTexture() Instance
- func (self Instance) AsObject() [1]gd.Object
- func (self Instance) AsRefCounted() [1]gd.RefCounted
- func (self Instance) AsResource() Resource.Instance
- func (self Instance) AsTexture() Texture.Instance
- func (self Instance) AsTexture2D() Texture2D.Instance
- func (self Instance) GetFormat() Image.Format
- func (self Instance) ID() ID
- func (self Instance) SetImage(image Image.Instance)
- func (self *Instance) SetObject(obj [1]gd.Object) bool
- func (self Instance) SetSizeOverride(size Vector2i.XY)
- func (self Instance) Update(image Image.Instance)
- func (self Instance) Virtual(name string) reflect.Value
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Advanced ¶
type Advanced = class
Advanced exposes a 1:1 low-level instance of the class, undocumented, for those who know what they are doing.
type Extension ¶
Extension can be embedded in a new struct to create an extension of this class. T should be the type that is embedding this Extension
func (*Extension[T]) AsImageTexture ¶
func (*Extension[T]) AsRefCounted ¶
func (self *Extension[T]) AsRefCounted() [1]gd.RefCounted
func (*Extension[T]) AsResource ¶
func (*Extension[T]) AsTexture2D ¶
type ID ¶
ID is a typed object ID (reference) to an instance of this class, use it to store references to objects with unknown lifetimes, as an ID will not panic on use if the underlying object has been destroyed.
type Instance ¶
type Instance [1]gdclass.ImageTexture
Instance of the class with convieniently typed arguments and results.
var Nil Instance
Nil is a nil/null instance of the class. Equivalent to the zero value.
func CreateFromImage ¶
Creates a new ImageTexture and initializes it by allocating and setting the data from an Image.
func (Instance) AsImageTexture ¶
func (Instance) AsRefCounted ¶
func (self Instance) AsRefCounted() [1]gd.RefCounted
func (Instance) AsResource ¶
func (Instance) AsTexture2D ¶
func (Instance) SetImage ¶
Replaces the texture's data with a new Image. This will re-allocate new memory for the texture.
If you want to update the image, but don't need to change its parameters (format, size), use Update instead for better performance.
func (Instance) SetSizeOverride ¶
Resizes the texture to the specified dimensions.
func (Instance) Update ¶
Replaces the texture's data with a new Image.
Note: The texture has to be created using CreateFromImage or initialized first with the SetImage method before it can be updated. The new image dimensions, format, and mipmaps configuration should match the existing texture's image configuration.
Use this method over SetImage if you need to update the texture frequently, which is faster than allocating additional memory for a new texture each time.