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...
즉각적인 지오메트리(Immediate geometry)
:ref:`ImmediateMesh <class_ImmediateMesh>`는 OpenGL 1.x 스타일 API를 사용하여 동적 형상을 생성하는 편리한 도구입니다. 따라서 프레임마다 업데이트해야 하는 메시에 사용하기 쉽고 효율적입니다.
이 도구를 사용하여 복잡한 형상(수천 개의 정점)을 생성하는 것은 한 번만 수행하더라도 비효율적입니다. 대신, 모든 프레임을 변경하는 간단한 형상을 생성하도록 설계되었습니다.
먼저 :ref:`MeshInstance3D <class_meshinstance3d>`을 생성하고 인스펙터에 :ref:`ImmediateMesh <class_ImmediateMesh>`를 추가해야 합니다.
다음으로 MeshInstance3D에 스크립트를 추가합니다. ImmediateMesh의 코드는 각 프레임을 업데이트하려는 경우 _process() 함수에 들어가야 하고, 메시를 한 번 생성하고 업데이트하지 않으려는 경우 _ready() 함수에 들어가야 합니다. 표면을 한 번만 생성하는 경우 ImmediateMesh는 생성된 메시가 캐시되고 재사용되므로 다른 종류의 메시만큼 효율적입니다.
형상 생성을 시작하려면 ``surface_begin()``를 호출해야 합니다. ``surface_begin()``는 ``PrimitiveType``를 인수로 사용합니다. ``PrimitiveType``는 주어진 정점을 기반으로 삼각형, 선, 점 등인지 여부에 따라 기본 요소를 정렬하는 방법을 GPU에 지시합니다. 전체 목록은 Mesh 클래스 참조 페이지에서 찾을 수 있습니다.
surface_begin()``를 호출하면 정점 추가를 시작할 준비가 된 것입니다. 정점을 한 번에 하나씩 추가합니다. 먼저 ``surface_set_****()``(예: ``surface_set_normal())를 사용하여 법선이나 UV와 같은 정점별 속성을 추가합니다. 그런 다음 ``surface_add_vertex()``를 호출하여 해당 속성이 있는 꼭짓점을 추가합니다. 예를 들면:
# Add a vertex with normal and uv.
surface_set_normal(Vector3(0, 1, 0))
surface_set_uv(Vector2(1, 1))
surface_add_vertex(Vector3(0, 0, 1))
``surface_add_vertex()``를 호출하기 전에 추가된 속성만 해당 정점에 포함됩니다. ``surface_add_vertex()``를 호출하기 전에 속성을 두 번 추가하면 두 번째 호출만 사용됩니다.
마지막으로 모든 정점을 추가한 후 surface_end()``를 시그널로 호출하여 표면 생성을 완료했습니다. ``surface_begin() 및 ``surface_end()``를 여러 번 호출하여 메시에 대한 여러 표면을 생성할 수 있습니다.
어떻게 작동하는 지의 예제입니다:
extends MeshInstance3D
func _ready():
# Begin draw.
mesh.surface_begin(Mesh.PRIMITIVE_TRIANGLES)
# Prepare attributes for add_vertex.
mesh.surface_set_normal(Vector3(0, 0, 1))
mesh.surface_set_uv(Vector2(0, 0))
# Call last for each vertex, adds the above attributes.
mesh.surface_add_vertex(Vector3(-1, -1, 0))
mesh.surface_set_normal(Vector3(0, 0, 1))
mesh.surface_set_uv(Vector2(0, 1))
mesh.surface_add_vertex(Vector3(-1, 1, 0))
mesh.surface_set_normal(Vector3(0, 0, 1))
mesh.surface_set_uv(Vector2(1, 1))
mesh.surface_add_vertex(Vector3(1, 1, 0))
# End drawing.
mesh.surface_end()
ImmediateMesh는 여러 프레임에 걸쳐 사용할 수도 있습니다. surface_begin() 및 ``surface_end()``를 호출할 때마다 ImmediateMesh에 새 표면이 추가됩니다. 각 프레임마다 메시를 처음부터 다시 생성하려면 ``surface_begin()``를 호출하기 전에 ``clear_surfaces()``를 호출하세요.
extends MeshInstance3D
func _process(delta):
# Clean up before drawing.
mesh.clear_surfaces()
# Begin draw.
mesh.surface_begin(Mesh.PRIMITIVE_TRIANGLES)
# Draw mesh.
# End drawing.
mesh.surface_end()
위의 코드는 각 프레임마다 단일 표면을 동적으로 생성하고 그립니다.