Skip to content

Godot:CanvasItem

Base class of anything 2D.

GDScript Methods

void

_draw ( ) virtual

void

draw_arc ( Vector2 center, float radius, float start_angle, float end_angle, int point_count, Color color, float width=1.0, bool antialiased=false )

float

draw_char ( Font font, Vector2 position, String char, String next, Color modulate=Color( 1, 1, 1, 1 ) )

void

draw_circle ( Vector2 position, float radius, Color color )

void

draw_colored_polygon ( PoolVector2Array points, Color color, PoolVector2Array uvs=PoolVector2Array( ), Texture texture=null, Texture normal_map=null, bool antialiased=false )

void

draw_line ( Vector2 from, Vector2 to, Color color, float width=1.0, bool antialiased=false )

void

draw_mesh ( Mesh mesh, Texture texture, Texture normal_map=null, Transform2D transform=Transform2D( 1, 0, 0, 1, 0, 0 ), Color modulate=Color( 1, 1, 1, 1 ) )

void

draw_multiline ( PoolVector2Array points, Color color, float width=1.0, bool antialiased=false )

void

draw_multiline_colors ( PoolVector2Array points, PoolColorArray colors, float width=1.0, bool antialiased=false )

void

draw_multimesh ( MultiMesh multimesh, Texture texture, Texture normal_map=null )

void

draw_polygon ( PoolVector2Array points, PoolColorArray colors, PoolVector2Array uvs=PoolVector2Array( ), Texture texture=null, Texture normal_map=null, bool antialiased=false )

void

draw_polyline ( PoolVector2Array points, Color color, float width=1.0, bool antialiased=false )

void

draw_polyline_colors ( PoolVector2Array points, PoolColorArray colors, float width=1.0, bool antialiased=false )

void

draw_primitive ( PoolVector2Array points, PoolColorArray colors, PoolVector2Array uvs, Texture texture=null, float width=1.0, Texture normal_map=null )

void

draw_rect ( Rect2 rect, Color color, bool filled=true, float width=1.0, bool antialiased=false )

void

draw_set_transform ( Vector2 position, float rotation, Vector2 scale )

void

draw_set_transform_matrix ( Transform2D xform )

void

draw_string ( Font font, Vector2 position, String text, Color modulate=Color( 1, 1, 1, 1 ), int clip_w=-1 )

void

draw_style_box ( StyleBox style_box, Rect2 rect )

void

draw_texture ( Texture texture, Vector2 position, Color modulate=Color( 1, 1, 1, 1 ), Texture normal_map=null )

void

draw_texture_rect ( Texture texture, Rect2 rect, bool tile, Color modulate=Color( 1, 1, 1, 1 ), bool transpose=false, Texture normal_map=null )

void

draw_texture_rect_region ( Texture texture, Rect2 rect, Rect2 src_rect, Color modulate=Color( 1, 1, 1, 1 ), bool transpose=false, Texture normal_map=null, bool clip_uv=true )

void

force_update_transform ( )

RID

get_canvas ( ) const

RID

get_canvas_item ( ) const

Transform2D

get_canvas_transform ( ) const

Vector2

get_global_mouse_position ( ) const

Transform2D

get_global_transform ( ) const

Transform2D

get_global_transform_with_canvas ( ) const

Vector2

get_local_mouse_position ( ) const

Transform2D

get_transform ( ) const

Rect2

get_viewport_rect ( ) const

Transform2D

get_viewport_transform ( ) const

World2D

get_world_2d ( ) const

void

hide ( )

bool

is_local_transform_notification_enabled ( ) const

bool

is_set_as_toplevel ( ) const

bool

is_transform_notification_enabled ( ) const

bool

is_visible_in_tree ( ) const

Vector2

make_canvas_position_local ( Vector2 screen_point ) const

InputEvent

make_input_local ( InputEvent event ) const

void

set_as_toplevel ( bool enable )

void

set_notify_local_transform ( bool enable )

void

set_notify_transform ( bool enable )

void

show ( )

void

update ( )

draw_string

그려질 텍스트의 영역 계산 방법

Godot:Font#get_string_size 함수를 사용하면 된다.

draw_polyline

WARNING

작동 방식으로 인해 내장된 앤티앨리어싱은 반투명 폴리곤에 대해 올바르게 보이지 않으며 특정 플랫폼에서 작동하지 않을 수 있습니다. 이 문제를 해결하려면 Antialiased Line2D 추가 기능을 설치한 다음 AntialiasedPolygon2D 노드를 만드십시오. 해당 노드는 앤티앨리어싱을 수행하기 위해 사용자 정의 밉맵이 있는 텍스처에 의존합니다.

Line Connection 이슈

문서의 설명상으론 라인이 연결되어 있다 하지만 그렇지 않더라... 그래서 발생되는 Line Connection 이슈가 있다.

Hexagon 을 그릴 시 다음과 같이 그린다.

float COS30 = (float)Math.Sqrt(3f) / 2f;
float SIN30 = 0.5f;
var r = radius;
var x = COS30 * radius;
var y = SIN30 * radius;
var cx = center.x;
var cy = center.y;
var points = new List<Vector2>();
points.Add(new Vector2(cx + 0, cy - r));  // 12시
points.Add(new Vector2(cx + x, cy - y));  // 1시30분
points.Add(new Vector2(cx + x, cy + y));  // 4시30분
points.Add(new Vector2(cx + 0, cy + r));  // 6시
points.Add(new Vector2(cx - x, cy + y));  // 7시30분
points.Add(new Vector2(cx - x, cy - y));  // 10시30분
points.Add(new Vector2(cx + 0, cy - r));  // 12시

이 때, 선의 너비를 1보다 크게 하면 다음 그림과 같이 연결된 선이 부자연스럽게 보여진다. (12시 방향 꼭지점 연결 부분이 파여져 있는게 보일 것이다)

Godot_draw_polyline_lineWidth_issue_01.png

그래서 12시 방향 꼭지점 이후, 1시30분 방향 꼭지점 까지 추가 선을 연결하면

points.Add(new Vector2(cx + 0, cy - r));  // 12시
points.Add(new Vector2(cx + x, cy - y));  // 1시30분
points.Add(new Vector2(cx + x, cy + y));  // 4시30분
points.Add(new Vector2(cx + 0, cy + r));  // 6시
points.Add(new Vector2(cx - x, cy + y));  // 7시30분
points.Add(new Vector2(cx - x, cy - y));  // 10시30분
points.Add(new Vector2(cx + 0, cy - r));  // 12시
points.Add(new Vector2(cx + x, cy - y));  // 1시30분

다음과 같이 보인다:

Godot_draw_polyline_lineWidth_issue_02.png

얼핏 보면 잘 그려진듯 하지만 확대하면 이상한 점을 볼 수 있다.

Godot_draw_polyline_lineWidth_issue_03.png

1시30분 방향 꼭지점 부터 그려도 동일한 현상이 나타난다.

points.Add(new Vector2(cx + x, cy - y));  // 1시30분
points.Add(new Vector2(cx + x, cy + y));  // 4시30분
points.Add(new Vector2(cx + 0, cy + r));  // 6시
points.Add(new Vector2(cx - x, cy + y));  // 7시30분
points.Add(new Vector2(cx - x, cy - y));  // 10시30분
points.Add(new Vector2(cx + 0, cy - r));  // 12시
points.Add(new Vector2(cx + x, cy - y));  // 1시30분

Godot_draw_polyline_lineWidth_issue_04.png

이 현상은 1 초과의 선 두께를 "사각형 도형"의 연결로 표현해서 발생되는 이슈로 보인다.

Godot_draw_polyline_lineWidth_issue_06.png

따라서 두께 변화가 없는 직선 구간, 즉 3시 방향에 점을 추가하여 그린다:

Godot_draw_polyline_lineWidth_issue_05.png

var points = new List<Vector2>();
// points.Add(new Vector2(cx + 0, cy - r));  // 12시
// points.Add(new Vector2(cx + x, cy - y));  // 1시30분
points.Add(new Vector2(cx + x, cy)); // 3시
points.Add(new Vector2(cx + x, cy + y));  // 4시30분
points.Add(new Vector2(cx + 0, cy + r));  // 6시
points.Add(new Vector2(cx - x, cy + y));  // 7시30분
points.Add(new Vector2(cx - x, cy - y));  // 10시30분
points.Add(new Vector2(cx + 0, cy - r));  // 12시
points.Add(new Vector2(cx + x, cy - y));  // 1시30분
points.Add(new Vector2(cx + x, cy)); // 3시

See also

Favorite site