Up to date

This page is up to date for Godot 4.2. If you still find outdated information, please open an issue.

手続き型ジオメトリ(幾何)

There are many ways to procedurally generate geometry in Godot. In this tutorial series, we will explore a few of them. Each technique has its own benefits and drawbacks, so it is best to understand each one and how it can be useful in a given situation.

注釈

All the procedural geometry generation methods described here run on the CPU. Godot doesn't support generating geometry on the GPU yet.

ジオメトリとは何ですか?

ジオメトリは、形を洒落た言い方で表現したものです。コンピュータグラフィックスでは、ジオメトリは通常「頂点」と呼ばれる位置の配列で表されます。 Godotでは、ジオメトリはメッシュで表されます。

メッシュとは何ですか?

Many things in Godot have mesh in their name: the Mesh, the ArrayMesh, the ImmediateMesh, the MeshInstance3D, the MultiMesh, and the MultiMeshInstance3D. While they are all related, they have slightly different uses.

MesheとArrayMeshは、MeshInstance3Dノードを使用して描画されるリソースです。 MesheやArrayMeshなどのリソースをシーンに直接追加することはできません。 MeshInstance3Dは、シーン内のメッシュの1つのインスタンスを表します。単一のメッシュを複数のMeshInstance3Dで再利用して、異なるマテリアルまたは変換(スケール、回転、位置など)を使用してシーンの異なる部分に描画することができます。

同じオブジェクトを何度も描画する場合は、MultiMeshとMultiMeshInstance3Dを使用すると便利です。 MultiMeshInstance3Dは非常に安価に何千回もメッシュを描画します。そのためには、ハードウェアのインスタンス化を利用します。 MultiMeshInstance3Dを使用する場合の欠点は、すべてのインスタンスで1つのマテリアルに制限されることです。インスタンス配列を使用して、インスタンスごとに異なる色と変換を保存しますが、すべてのインスタンスは同じマテリアルを使用します。

メッシュとは

メッシュは、1つ以上のサーフェスで構成されます。サーフェスは、頂点、法線、UVなどを含む複数のサブ配列で構成される配列です。通常、サーフェスとメッシュを構築するプロセスは、RenderingServer でユーザーから隠されていますが、ArrayMeshではユーザーは、サーフェス情報を含む配列を渡すことにより、メッシュを手動で構築できます。

サーフェス

各サーフェスには独自のマテリアルがあります。または、GeometryInstance3D.override_material を使用してMeshInstance3Dを使用するときに、メッシュ内のすべてのサーフェスのマテリアルをオーバーライドできます。

サーフェス配列

サーフェス配列は、長さ ArrayMesh.ARRAY_MAX の配列です。配列の各位置は、頂点ごとの情報を含むサブ配列で埋められます。たとえば、ArrayMesh.ARRAY_NORMAL にある配列は、頂点法線の PackedVector3Array です。

The surface array can be indexed or non-indexed. Creating a non-indexed array is as easy as not assigning an array at the index ArrayMesh.ARRAY_INDEX. A non-indexed array stores unique vertex information for every triangle, meaning that when two triangles share a vertex, the vertex is duplicated in the array. An indexed surface array only stores vertex information for each unique vertex and then also stores an array of indices which maps out how to construct the triangles from the vertex array. In general, using an indexed array is faster, but it means you have to share vertex data between triangles, which is not always desired (e.g. when you want per-face normals).

ツール

Godotは、ジオメトリにアクセスして操作するさまざまな方法を提供します。それぞれの詳細については、次のチュートリアルで説明します。

ArrayMesh

The ArrayMesh resource extends Mesh to add a few different quality of life functions and, most importantly, the ability to construct a Mesh surface through scripting.

ArrayMeshの詳細については、ArrayMesh tutoria を参照してください。

MeshDataTool

MeshDataToolは、メッシュデータを、実行時に変更できる頂点、面、およびエッジの配列に変換するリソースです。

MeshDataToolの詳細については、MeshDataTool tutorial を参照してください。

SurfaceTool

SurfaceToolでは、OpenGL 1.xイミディエイトモードスタイルのインターフェイスを使用してメッシュを作成できます。

SurfaceToolの詳細については、SurfaceTool tutorial を参照してください。

ImmediateMesh

ImmediateMesh is a mesh that uses an immediate mode style interface (like SurfaceTool) to draw objects. The difference between ImmediateMesh and the SurfaceTool is that ImmediateMesh is drawn directly with code dynamically, while the SurfaceTool is used to generated a Mesh that you can do whatever you want with.

ImmediateMesh is useful for prototyping because of its straightforward API, but it is slow because the geometry is rebuilt each time you make a change. It is most useful for adding simple geometry for visual debugging (e.g. by drawing lines to visualize physics raycasts etc.).

For more information about ImmediateMesh, please see the ImmediateMesh tutorial.

どちらを使うべきですか?

Which approach you use depends on what you are trying to do and what kind of procedure you are comfortable with.

SurfaceToolとArrayMeshはどちらも、時間とともに変化しない静的なジオメトリ(メッシュ)の生成に最適です。

Using an ArrayMesh is slightly faster than using a SurfaceTool, but the API is a little more challenging. Additionally, SurfaceTool has a few quality of life methods such as generate_normals() and index().

ImmediateMesh is more limited than both ArrayMesh and SurfaceTool. However, if you need the geometry to change every frame anyway, it provides a much easier interface that can be slightly faster than generating an ArrayMesh every frame.

The MeshDataTool is not fast, but it gives you access to all kinds of properties of the mesh that you don't get with the others (edges, faces, etc.). It is incredibly useful when you need that sort of data to transform the mesh, but it is not a good idea to use it if that extra information is not needed. The MeshDataTool is best used if you are going to be using an algorithm that requires access to the face or edge array.