Texture2D
Base two-dimensional texture resource for UI, 2D and 3D materials.
Texture2D defines a shared texture contract rather than loading a file through its constructor. Use ResourceLoader.Load<Texture2D> for imported assets, ImageTexture for generated pixels, and DpiTexture for SVG. Multiple objects can share a resource; modifying it affects all users.
GetSize returns logical size. GetImage reads data supplied by the concrete implementation; its resolution can differ from GetSize after a size override. HasAlpha does not prove transparent pixels exist. Current ImageTexture HasMipmaps/GetMipmapCount can report false/0 despite a GetImage mipmap chain; this is a confirmed limitation of that implementation.
Draw, DrawRect and DrawRectRegion submit 2D CanvasItem commands. They do not assign a texture to a 3D material. transpose swaps axes; srcRect uses texture coordinates rather than normalized UVs. Underscored methods are implementation hooks for custom resources; public methods and hooks have different roles.
Virtual dispatch, draw arguments, placeholders and resource queries were checked in Windows Forward+/Vulkan. Visual transpose/tile/clip output, arbitrary subclasses and 3D use were not established by those checks. Maximum size and memory usage depend on backend, GPU and format; a single number does not guarantee allocation.
Declarations checked against C# SDK 1.0.0. Expand a name to see declarations and documentation.
Constructors1
Texture2DConstructor
public Texture2D();Creates a base Texture2D resource without loading an image or supplying a ready pixel texture. Normally use a concrete subclass or ResourceLoader.Load<Texture2D>. Implement the required size and data methods in a custom subclass.
Methods23
CreatePlaceholderResource
public Resource CreatePlaceholder();Creates a separate PlaceholderTexture2D with the current logical size. Source pixels, format and mipmap chain are not copied. Use it as a resource stand-in rather than a smaller image copy.
ResourceResource whose concrete type is PlaceholderTexture2D, with original logical size but no source pixels.
Drawvoid
public void Draw(Rid canvasItem, Vector2 position, Color? modulate = default, bool transpose = false);Draws the entire texture at top-left position using its logical size. modulate multiplies color; C# null means white. transpose swaps axes; do not treat it as a general 90-degree rotation. Use a valid CanvasItem RID, such as GetCanvasItem() inside your Entity2D/Control _Draw. Request QueueRedraw on the CanvasItem after state changes. This is a 2D command rather than 3D material assignment.
| Name | Type | Default |
|---|---|---|
canvasItem | Rid | — |
position | Vector2 | — |
modulate | Color? | default |
transpose | bool | false |
canvasItemBorrowed valid target CanvasItem RID, not the texture RID.
positionTop-left position in the target CanvasItem local coordinates.
modulateRGBA multiplier; null in a public C# nullable parameter means white, while the hook receives a concrete Color.
transposeSwap axes when drawing; not an arbitrary rotation angle.
DrawRectvoid
public void DrawRect(Rid canvasItem, Rect2 rect, bool tile, Color? modulate = default, bool transpose = false);Draws the texture in rect. With tile = false it stretches; with true it repeats to fill the area. modulate multiplies color; transpose swaps axes rather than specifying an arbitrary angle. Use a valid CanvasItem RID, such as GetCanvasItem() inside your Entity2D/Control _Draw. Request QueueRedraw on the CanvasItem after state changes. This is a 2D command rather than 3D material assignment.
| Name | Type | Default |
|---|---|---|
canvasItem | Rid | — |
rect | Rect2 | — |
tile | bool | — |
modulate | Color? | default |
transpose | bool | false |
canvasItemBorrowed valid target CanvasItem RID, not the texture RID.
rectDestination rectangle in CanvasItem local coordinates.
tileRepeat the texture rather than stretching it over rect.
modulateRGBA multiplier; null in a public C# nullable parameter means white, while the hook receives a concrete Color.
transposeSwap axes when drawing; not an arbitrary rotation angle.
DrawRectRegionvoid
public void DrawRectRegion(Rid canvasItem, Rect2 rect, Rect2 srcRect, Color? modulate = default, bool transpose = false, bool clipUV = true);Draws srcRect into rect. Source coordinates are texture coordinates rather than normalized 0…1 UVs. clipUV restricts sampling to the source region; it is not CanvasItem geometry clipping. Default modulate is white. Use a valid CanvasItem RID, such as GetCanvasItem() inside your Entity2D/Control _Draw. Request QueueRedraw on the CanvasItem after state changes. This is a 2D command rather than 3D material assignment.
| Name | Type | Default |
|---|---|---|
canvasItem | Rid | — |
rect | Rect2 | — |
srcRect | Rect2 | — |
modulate | Color? | default |
transpose | bool | false |
clipUV | bool | true |
canvasItemBorrowed valid target CanvasItem RID, not the texture RID.
rectDestination rectangle in CanvasItem local coordinates.
srcRectSource rectangle in texture coordinates, not 0…1 UVs.
modulateRGBA multiplier; null in a public C# nullable parameter means white, while the hook receives a concrete Color.
transposeSwap axes when drawing; not an arbitrary rotation angle.
clipUVRestrict sampling to the source region; defaults to true on the public method.
GetFormatImage.Format
public Image.Format GetFormat();Returns the image format supplied by the texture implementation. Without _GetFormat, the base result Image.Format.Max indicates no concrete format; do not use it as a new Image format. Format alone does not specify material color space.
Image.FormatImplementation format, or Image.Format.Max without a provider.
GetHeightint
public int GetHeight();Returns logical texture height; a size override can make it differ from GetImage height. A custom subclass supplies the value through _GetHeight.
intLogical height in texture units.
GetImageImage
public Image GetImage();Obtains an Image with texture data when supplied by the implementation. For imported CompressedTexture2D this is a snapshot that may require GPU readback: editing it does not update the original texture. Avoid unnecessary per-frame readback.
ImageImage with available data, or null if the implementation does not supply an image. Do not assume the format is always uncompressed RGBA8.
GetMipmapCountint
public int GetMipmapCount();Returns the implementation count of additional mipmap levels, excluding the base level. Without _GetMipmapCount, the base result is 0. Current ImageTexture can still report 0 while GetImage contains mipmaps. Do not derive storage size from an unverified result.
intImplementation additional-level count; 0 for the base contract.
GetSizeVector2
public Vector2 GetSize();Returns a Vector2 composed of GetWidth and GetHeight in logical texture units. This is not Sprite2D world size, Control size or necessarily physical GetImage resolution after an override.
Vector2Vector2 containing current logical width and height.
GetWidthint
public int GetWidth();Returns logical texture width. For a resource with a size override, it can differ from GetImage width. A custom subclass supplies it through _GetWidth; do not infer GPU storage resolution from this method alone.
intLogical width in texture units.
HasAlphabool
public bool HasAlpha();Returns the implementation alpha-support flag rather than scanning pixels for transparency. The base result without _HasAlpha is true. Therefore true does not prove that any pixel is transparent.
boolAlpha-support flag, not proof of transparent pixels.
HasMipmapsbool
public bool HasMipmaps();Returns the implementation mipmap flag; the base result without _HasMipmaps is false. Current ImageTexture can return false despite a GetImage mipmap chain because it does not override the native method. Do not use this as the sole pixel-data check for every texture type.
boolImplementation value; false for the base contract.
_Drawvoid
public virtual void _Draw(Rid toCanvasItem, Vector2 pos, Color modulate, bool transpose);Override for a custom Texture2D Draw. When the hook handles the call, the base drawing command is not automatically appended; submit the required commands yourself. Do not call the same method on this from the hook: that recurses. Draws the entire texture at top-left position using its logical size. modulate multiplies color; C# null means white. transpose swaps axes; do not treat it as a general 90-degree rotation. Use a valid CanvasItem RID, such as GetCanvasItem() inside your Entity2D/Control _Draw. Request QueueRedraw on the CanvasItem after state changes. This is a 2D command rather than 3D material assignment.
| Name | Type | Default |
|---|---|---|
toCanvasItem | Rid | — |
pos | Vector2 | — |
modulate | Color | — |
transpose | bool | — |
toCanvasItemValid CanvasItem RID supplied by the native call.
posTop-left position in the target CanvasItem local coordinates.
modulateRGBA multiplier; null in a public C# nullable parameter means white, while the hook receives a concrete Color.
transposeSwap axes when drawing; not an arbitrary rotation angle.
_DrawRectvoid
public virtual void _DrawRect(Rid toCanvasItem, Rect2 rect, bool tile, Color modulate, bool transpose);Override for a custom Texture2D DrawRect. When the hook handles the call, the base drawing command is not automatically appended; submit the required commands yourself. Do not call the same method on this from the hook: that recurses. Draws the texture in rect. With tile = false it stretches; with true it repeats to fill the area. modulate multiplies color; transpose swaps axes rather than specifying an arbitrary angle. Use a valid CanvasItem RID, such as GetCanvasItem() inside your Entity2D/Control _Draw. Request QueueRedraw on the CanvasItem after state changes. This is a 2D command rather than 3D material assignment.
| Name | Type | Default |
|---|---|---|
toCanvasItem | Rid | — |
rect | Rect2 | — |
tile | bool | — |
modulate | Color | — |
transpose | bool | — |
toCanvasItemValid CanvasItem RID supplied by the native call.
rectDestination rectangle in CanvasItem local coordinates.
tileRepeat the texture rather than stretching it over rect.
modulateRGBA multiplier; null in a public C# nullable parameter means white, while the hook receives a concrete Color.
transposeSwap axes when drawing; not an arbitrary rotation angle.
_DrawRectRegionvoid
public virtual void _DrawRectRegion(Rid toCanvasItem, Rect2 rect, Rect2 srcRect, Color modulate, bool transpose, bool clipUV);Override for a custom Texture2D DrawRectRegion. When the hook handles the call, the base drawing command is not automatically appended; submit the required commands yourself. Do not call the same method on this from the hook: that recurses. Draws srcRect into rect. Source coordinates are texture coordinates rather than normalized 0…1 UVs. clipUV restricts sampling to the source region; it is not CanvasItem geometry clipping. Default modulate is white. Use a valid CanvasItem RID, such as GetCanvasItem() inside your Entity2D/Control _Draw. Request QueueRedraw on the CanvasItem after state changes. This is a 2D command rather than 3D material assignment.
| Name | Type | Default |
|---|---|---|
toCanvasItem | Rid | — |
rect | Rect2 | — |
srcRect | Rect2 | — |
modulate | Color | — |
transpose | bool | — |
clipUV | bool | — |
toCanvasItemValid CanvasItem RID supplied by the native call.
rectDestination rectangle in CanvasItem local coordinates.
srcRectSource rectangle in texture coordinates, not 0…1 UVs.
modulateRGBA multiplier; null in a public C# nullable parameter means white, while the hook receives a concrete Color.
transposeSwap axes when drawing; not an arbitrary rotation angle.
clipUVRestrict sampling to the source region; defaults to true on the public method.
_GetFormatImage.Format
public virtual Image.Format _GetFormat();Format provider for a custom Texture2D. Return the concrete Image.Format matching the supplied image; without an implementation, base GetFormat returns Max. It does not automatically change GPU texture format.
Image.FormatConcrete format of supplied image data.
_GetHeightint
public virtual int _GetHeight();Override for a custom Texture2D to return logical height. Native GetHeight and GetSize use this contract. Keep dimensions consistent with custom drawing.
intCustom texture logical height.
_GetImageImage
public virtual Image _GetImage();Image provider for a custom Texture2D. Return available data or null if readback is unsupported. The tested subclass returns independent images; the base API does not force every subclass to copy data identically. Do not release the returned resource before passing it to the caller.
ImageImage with available data, or null.
_GetMipmapCountint
public virtual int _GetMipmapCount();Provider of a custom Texture2D additional mipmap-level count, excluding the base level. Keep it consistent with _HasMipmaps and actual data. Without an implementation the base result is 0.
intAdditional-level count, excluding the base level.
_GetWidthint
public virtual int _GetWidth();Override for a custom Texture2D to return logical width. Native GetWidth and GetSize use this contract. It is a data query rather than a place to reload a resource every frame.
intCustom texture logical width.
_HasAlphabool
public virtual bool _HasAlpha();Alpha-support provider for a custom Texture2D. This declares implementation capability rather than scanning pixels. Without an override, base HasAlpha returns true.
boolCustom resource alpha-support flag.
_HasMipmapsbool
public virtual bool _HasMipmaps();Mipmap-flag provider for a custom Texture2D; keep it consistent with _GetMipmapCount and _GetImage data. The base result without an override is false; creating a chain in a separate Image does not automatically change this contract.
boolWhether supplied data includes a mipmap chain.
_IsPixelOpaquebool
public virtual bool _IsPixelOpaque(int x, int y);Custom Texture2D pixel-opacity hook used by native consumers such as Sprite2D.IsPixelOpaque. Coordinates are texture-local rather than screen coordinates. Handle bounds in your implementation. The base result without an override is true. This does not automatically create physics collision.
| Name | Type | Default |
|---|---|---|
x | int | — |
y | int | — |
xInteger horizontal texture-pixel coordinate.
yInteger vertical texture-pixel coordinate.
booltrue for a pixel your implementation considers opaque; otherwise false.