Attention: Here be dragons
This is the latest
(unstable) version of this documentation, which may document features
not available in or compatible with released stable versions of Godot.
Checking the stable version of the documentation...
Texture2D
继承: Texture < Resource < RefCounted < Object
派生: AnimatedTexture, AtlasTexture, CameraTexture, CanvasTexture, CompressedTexture2D, CurveTexture, CurveXYZTexture, ExternalTexture, GradientTexture1D, GradientTexture2D, ImageTexture, MeshTexture, NoiseTexture2D, PlaceholderTexture2D, PortableCompressedTexture2D, Texture2DRD, ViewportTexture
用于 2D 和 3D 的纹理。
描述
纹理的工作原理是在视频硬件中注册图像,该图像在注册后就可以在 3D 模型、2D Sprite2D、GUI Control 中使用。
纹理通常是通过从文件中加载来创建的。见 @GDScript.load()。
Texture2D 是其他资源的基类,无法直接使用。
注意:由于图形硬件的限制,最大的纹理尺寸是 16384×16384 像素。较大的纹理可能无法导入。
方法
void |
_draw(to_canvas_item: RID, pos: Vector2, modulate: Color, transpose: bool) virtual const |
void |
_draw_rect(to_canvas_item: RID, rect: Rect2, tile: bool, modulate: Color, transpose: bool) virtual const |
void |
_draw_rect_region(to_canvas_item: RID, rect: Rect2, src_rect: Rect2, modulate: Color, transpose: bool, clip_uv: bool) virtual const |
_get_height() virtual const |
|
_get_width() virtual const |
|
_has_alpha() virtual const |
|
_is_pixel_opaque(x: int, y: int) virtual const |
|
create_placeholder() const |
|
void |
draw(canvas_item: RID, position: Vector2, modulate: Color = Color(1, 1, 1, 1), transpose: bool = false) const |
void |
draw_rect(canvas_item: RID, rect: Rect2, tile: bool, modulate: Color = Color(1, 1, 1, 1), transpose: bool = false) const |
void |
draw_rect_region(canvas_item: RID, rect: Rect2, src_rect: Rect2, modulate: Color = Color(1, 1, 1, 1), transpose: bool = false, clip_uv: bool = true) const |
get_height() const |
|
get_image() const |
|
get_size() const |
|
get_width() const |
|
has_alpha() const |
方法说明
void _draw(to_canvas_item: RID, pos: Vector2, modulate: Color, transpose: bool) virtual const 🔗
当请求在 CanvasItem 上绘制整个 Texture2D 时调用,左上角的偏移量由 pos
指定。modulate
指定绘制颜色的乘数,transpose
指定绘制是否应按列主顺序而不是行主顺序执行(会导致顺时针旋转 90 度)。
注意:仅用于 2D 渲染,不用于 3D。
void _draw_rect(to_canvas_item: RID, rect: Rect2, tile: bool, modulate: Color, transpose: bool) virtual const 🔗
请求将 Texture2D 绘制到 CanvasItem 的指定 rect
上时调用。modulate
指定的是绘制时颜色的乘数,而 transpose
指定的是绘制时是否使用列优先顺序,而不是使用行优先顺序(产生顺时针 90 度旋转)。
注意:仅在 2D 渲染时使用,3D 不使用。
void _draw_rect_region(to_canvas_item: RID, rect: Rect2, src_rect: Rect2, modulate: Color, transpose: bool, clip_uv: bool) virtual const 🔗
请求将 Texture2D 由 src_rect
的坐标指定的部分绘制到 CanvasItem 的指定 rect
上时调用。modulate
指定的是绘制时颜色的乘数,而 transpose
指定的是绘制时是否使用列优先顺序,而不是使用行优先顺序(产生顺时针 90 度旋转)。
注意:仅在 2D 渲染时使用,3D 不使用。
int _get_height() virtual const 🔗
查询该 Texture2D 的高度时调用。
int _get_width() virtual const 🔗
查询该 Texture2D 的宽度时调用。
bool _has_alpha() virtual const 🔗
查询该 Texture2D 是否存在 alpha 通道时调用。
bool _is_pixel_opaque(x: int, y: int) virtual const 🔗
查询 Texture2D 中指定位置 (x, y)
的像素的不透明状态时调用。
Resource create_placeholder() const 🔗
创建该资源的占位符版本(PlaceholderTexture2D)。
void draw(canvas_item: RID, position: Vector2, modulate: Color = Color(1, 1, 1, 1), transpose: bool = false) const 🔗
使用 RenderingServer API 在 CanvasItem 上的指定位置 position
绘制该纹理。
void draw_rect(canvas_item: RID, rect: Rect2, tile: bool, modulate: Color = Color(1, 1, 1, 1), transpose: bool = false) const 🔗
使用 RenderingServer API 在 CanvasItem 上绘制该纹理。
void draw_rect_region(canvas_item: RID, rect: Rect2, src_rect: Rect2, modulate: Color = Color(1, 1, 1, 1), transpose: bool = false, clip_uv: bool = true) const 🔗
使用 RenderingServer API 在 CanvasItem 上绘制纹理的一部分。
返回该纹理的高度,单位为像素。
返回一个 Image,该对象是这个 Texture2D 中数据的副本(每次都会新建一个 Image)。可以直接访问并操作 Image。
注意:如果该 Texture2D 无效,则会返回 null
。
注意:这个函数会从 GPU 获取纹理数据,过度使用可能会引起性能问题。请避免每一帧都调用 get_image(),尤其是大型纹理。
返回该纹理的大小,单位为像素。
返回该纹理的宽度,单位为像素。
如果该 Texture2D 具有 Alpha 通道,则返回 true
。