뷰포트와 캔버스 변형

소개

This is an overview of the 2D transforms going on for nodes from the moment they draw their content locally to the time they are drawn onto the screen. This overview discusses very low level details of the engine.

캔버스 변형

As mentioned in the previous tutorial, Canvas layers, every CanvasItem node (remember that Node2D and Control based nodes use CanvasItem as their common root) will reside in a Canvas Layer. Every canvas layer has a transform (translation, rotation, scale, etc.) that can be accessed as a Transform2D.

Also covered in the previous tutorial, nodes are drawn by default in Layer 0, in the built-in canvas. To put nodes in a different layer, a CanvasLayer node can be used.

전역 캔버스 변형(Global canvas transform)

Viewports also have a Global Canvas transform (also a Transform2D). This is the master transform and affects all individual Canvas Layer transforms. Generally, this transform is not of much use, but is used in the CanvasItem Editor in Godot's editor.

늘이기 변형(Stretch transform)

Finally, viewports have a Stretch Transform, which is used when resizing or stretching the screen. This transform is used internally (as described in Multiple resolutions), but can also be manually set on each viewport.

Input events received in the MainLoop._input_event() callback are multiplied by this transform but lack the ones above. To convert InputEvent coordinates to local CanvasItem coordinates, the CanvasItem.make_input_local() function was added for convenience.

변형 순서

CanvasItem 지역 속성의 좌표가 실제 화면 좌표가 되려면 다음과 같은 변형 체인이 적용되어야 합니다:

../../_images/viewport_transforms2.png

변형 기능들

각 변형을 얻으려면 다음과 같은 기능을 사용합니다:

타입

변형

CanvasItem

CanvasItem.get_global_transform()

CanvasLayer

CanvasItem.get_canvas_transform()

CanvasLayer+GlobalCanvas+Stretch

CanvasItem.get_viewport_transform()

마지막으로 CanvasItem 지역 좌표를 화면 좌표로 변환하려면 다음 순서로 곱하세요:

var screen_coord = get_viewport_transform() * (get_global_transform() * local_pos)

그러나 화면 좌표를 사용하여 작업하는 것은 일반적으로 바람직하지 않음을 명심하세요. 권장되는 접근방식은 Canvas 좌표(CanvasItem.get_global_transform())로 자동으로 화면 해상도를 제대로 크기 조정할 수 있게 간단히 작동해야 합니다.

Feeding custom input events

It is often desired to feed custom input events to the scene tree. With the above knowledge, to correctly do this, it must be done the following way:

var local_pos = Vector2(10, 20) # local to Control/Node2D
var ie = InputEventMouseButton.new()
ie.button_index = BUTTON_LEFT
ie.position = get_viewport_transform() * (get_global_transform() * local_pos)
get_tree().input_event(ie)