ArrayMeshの使用

このチュートリアルでは、 ArrayMesh の使用の基本について説明します。

そのためには、最大5つのパラメータを取る関数 add_surface_from_arrays() を使用します。最初の2つは必須ですが、残りの3つはオプションです。

1つ目は PrimitiveType です。これは指定された頂点に基づいてプリミティブをどのように配置するか、つまり頂点が三角形、線、点など、どれを表すかを GPU に指示する OpenGL の概念です。使用可能なオプションについては Mesh.PrimitiveType を参照してください。

2番目のパラメータ arrays はメッシュ情報を格納する実際の配列です。配列は空の括弧 [] で構成された通常のGodot配列です。サーフェスの構築に使用される情報の種類ごとに、 Packed**Array (例: PackedVector3Array、PackedInt32Array など) を格納します。

arrays の共通要素と、それが arrays 内で指定する位置を以下に示します。完全なリストは Mesh.ArrayType を参照してください。

インデックス

Mesh.ArrayType

配列タイプ

0

ARRAY_VERTEX

PackedVector3Array または PackedVector2Array

1

ARRAY_NORMAL

PackedVector3Array

2

ARRAY_TANGENT

PackedFloat32Array または PackedFloat64Array の4つのfloatのグループ。最初の3つのfloatは接線を決定し、最後のfloatは従法線方向を -1 または 1 として指定します。

3

ARRAY_COLOR

PackedColorArray

4

ARRAY_TEX_UV

PackedVector2Array または PackedVector3Array

5

ARRAY_TEX_UV2

PackedVector2Array または PackedVector3Array

10

ARRAY_BONES

4つのfloatのグループの PackedFloat32Array 。または4つの int のグループの PackedInt32Array。各グループには特定の頂点に影響を与える4つのボーンのインデックスがリストされます。

11

ARRAY_WEIGHTS

PackedFloat32Array または PackedFloat64Array の4つのfloatのグループ。各floatには ARRAY_BONES 内の対応するボーンが特定の頂点に対して持つ重みの量がリストされます。

12

ARRAY_INDEX

PackedInt32Array

ほとんどのケースでメッシュを作成するときは頂点の位置を使ってメッシュを定義します。通常は頂点配列 (index=0) は必須ですが、インデックス配列 (index=12) はオプションであり、含まれている場合にのみ使用されます。インデックス配列のみで頂点配列を使用せずにメッシュを作成することも可能ですが、それはこのチュートリアルでは解説しません。

その他の全ての配列は、頂点に関する情報を保持します。これらもオプションであり、含まれている場合にのみ使用されます。これらの配列の一部 (例: ARRAY_COLOR) は、頂点ごとに1つのエントリを使用して、頂点に関する追加情報を提供します。これらは頂点配列と同じサイズである必要があります。その他の配列 (例: ARRAY_TANGENT) は、1つの頂点を記述するために4つのエントリを使用します。これらは、頂点配列のちょうど4倍の大きさである必要があります。

以上の配列を準備してメッシュを生成するには、関数 add_surface_from_arrays() を使用します。これは最大4つのパラメーターを指定します。始めの2つは必須ですが、残りの2つはオプションです。

ArrayMeshの設定

エディターで MeshInstance3D を作成し、インスペクターでそれに ArrayMesh を追加します。通常、エディターで ArrayMesh を追加しても意味がありませんが、この場合は ArrayMesh を作成しなくてもコードから ArrayMesh にアクセスできるようになります。

続いて、 MeshInstance3D にスクリプトを追加します。

_ready() 下に、新しい配列を作成します。

var surface_array = []

これは、サーフェス情報を保持する配列になります。サーフェスに必要なすべてのデータ配列が保持されます。Godot は、サイズが Mesh.ARRAY_MAX であると想定しているため、それに応じてサイズを変更します。

var surface_array = []
surface_array.resize(Mesh.ARRAY_MAX)

次に、使用する各データ型の配列を作成します。

var verts = PackedVector3Array()
var uvs = PackedVector2Array()
var normals = PackedVector3Array()
var indices = PackedInt32Array()

データ配列にジオメトリを入力したら、各配列を surface_array に追加し、メッシュにコミットすることでメッシュを作成できます。

surface_array[Mesh.ARRAY_VERTEX] = verts
surface_array[Mesh.ARRAY_TEX_UV] = uvs
surface_array[Mesh.ARRAY_NORMAL] = normals
surface_array[Mesh.ARRAY_INDEX] = indices

# No blendshapes, lods, or compression used.
mesh.add_surface_from_arrays(Mesh.PRIMITIVE_TRIANGLES, surface_array)

注釈

この例では、Mesh.PRIMITIVE_TRIANGLES を使用しましたが、メッシュから利用可能な任意のプリミティブタイプを使用できます。

完全なコードをまとめると次のようになります:

extends MeshInstance3D

func _ready():
    var surface_array = []
    surface_array.resize(Mesh.ARRAY_MAX)

    # PackedVector**Arrays for mesh construction.
    var verts = PackedVector3Array()
    var uvs = PackedVector2Array()
    var normals = PackedVector3Array()
    var indices = PackedInt32Array()

    #######################################
    ## Insert code here to generate mesh ##
    #######################################

    # Assign arrays to surface array.
    surface_array[Mesh.ARRAY_VERTEX] = verts
    surface_array[Mesh.ARRAY_TEX_UV] = uvs
    surface_array[Mesh.ARRAY_NORMAL] = normals
    surface_array[Mesh.ARRAY_INDEX] = indices

    # Create mesh surface from mesh array.
    # No blendshapes, lods, or compression used.
    mesh.add_surface_from_arrays(Mesh.PRIMITIVE_TRIANGLES, surface_array)

The code that goes in the middle can be whatever you want. Below we will present some example code for generating shapes, starting with a rectangle.

Generating a rectangle

Since we are using Mesh.PRIMITIVE_TRIANGLES to render, we will construct a rectangle with triangles.

A rectangle is formed by two triangles sharing four vertices. For our example, we will create a rectangle with its top left point at (0, 0, 0) with a width and length of one as shown below:

A rectangle made of two triangles sharing four vertices.

To draw this rectangle, define the coordinates of each vertex in the verts array.

verts = PackedVector3Array([
        Vector3(0, 0, 0),
        Vector3(0, 0, 1),
        Vector3(1, 0, 0),
        Vector3(1, 0, 1),
    ])

The uvs array helps describe where parts of a texture should go onto the mesh. The values range from 0 to 1. Depending on your texture, you may want to change these values.

uvs = PackedVector2Array([
        Vector2(0, 0),
        Vector2(1, 0),
        Vector2(0, 1),
        Vector2(1, 1),
    ])

The normals array is used to describe the direction the vertices face and is used in lighting calculations. For this example, we will default to the Vector3.UP direction.

normals = PackedVector3Array([
        Vector3.UP,
        Vector3.UP,
        Vector3.UP,
        Vector3.UP,
    ])

The indices array defines the order vertices are drawn. Godot renders in a clockwise direction, meaning that we must specify the vertices of a triangle we want to draw in clockwise order.

For example, to draw the first triangle, we will want to draw the vertices (0, 0, 0), (1, 0, 0), and (0, 0, 1) in that order. This is the same as drawing vert[0], vert[2], and vert[1], i.e., indices 0, 2, and 1, in the verts array. These index values are what the indices array defines.

インデックス

verts[Index]

uvs[Index]

normals[Index]

0

(0, 0, 0)

(0, 0)

Vector3.UP

1

(0, 0, 1)

(1, 0)

Vector3.UP

2

(1, 0, 0)

(0, 1)

Vector3.UP

3

(1, 0, 1)

(1, 1)

Vector3.UP

indices = PackedInt32Array([
        0, 2, 1, # Draw the first triangle.
        2, 3, 1, # Draw the second triangle.
    ])

Put together, the rectangle generation code looks like:

extends MeshInstance3D

func _ready():

  # Insert setting up the PackedVector**Arrays here.

  verts = PackedVector3Array([
          Vector3(0, 0, 0),
          Vector3(0, 0, 1),
          Vector3(1, 0, 0),
          Vector3(1, 0, 1),
      ])

  uvs = PackedVector2Array([
          Vector2(0, 0),
          Vector2(1, 0),
          Vector2(0, 1),
          Vector2(1, 1),
      ])

  normals = PackedVector3Array([
          Vector3.UP,
          Vector3.UP,
          Vector3.UP,
          Vector3.UP,
      ])

  indices = PackedInt32Array([
          0, 2, 1,
          2, 3, 1,
      ])

  # Insert committing to the ArrayMesh here.

For a more complex example, see the sphere generation section below.

Generating a sphere

球体を生成するためのサンプルコードを次に示します。コードはGDScriptで表示されますが、コードを生成する方法についてGodot固有のものはありません。この実装は、ArrayMeshとは特に関係がなく、球体を生成するための一般的なアプローチです。理解に問題がある場合、または手続き型ジオメトリ全般について詳しく知りたい場合は、オンラインで見つけたチュートリアルを使用できます。

extends MeshInstance3D

var rings = 50
var radial_segments = 50
var radius = 1

func _ready():

    # Insert setting up the PackedVector**Arrays here.

    # Vertex indices.
    var thisrow = 0
    var prevrow = 0
    var point = 0

    # Loop over rings.
    for i in range(rings + 1):
        var v = float(i) / rings
        var w = sin(PI * v)
        var y = cos(PI * v)

        # Loop over segments in ring.
        for j in range(radial_segments + 1):
            var u = float(j) / radial_segments
            var x = sin(u * PI * 2.0)
            var z = cos(u * PI * 2.0)
            var vert = Vector3(x * radius * w, y * radius, z * radius * w)
            verts.append(vert)
            normals.append(vert.normalized())
            uvs.append(Vector2(u, v))
            point += 1

            # Create triangles in ring using indices.
            if i > 0 and j > 0:
                indices.append(prevrow + j - 1)
                indices.append(prevrow + j)
                indices.append(thisrow + j - 1)

                indices.append(prevrow + j)
                indices.append(thisrow + j)
                indices.append(thisrow + j - 1)

        prevrow = thisrow
        thisrow = point

  # Insert committing to the ArrayMesh here.

保存

最後に、ArrayMesh を保存するために ResourceSaver クラスを使用できます。これは事前にメッシュを生成して保存しておき、後で使用したい場合に便利です。

# Saves mesh to a .tres file with compression enabled.
ResourceSaver.save(mesh, "res://sphere.tres", ResourceSaver.FLAG_COMPRESS)