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.

ArrayMesh

繼承: Mesh < Resource < RefCounted < Object

Mesh 型別,可用來從陣列建構曲面。

說明

ArrayMesh 透過將屬性指定為陣列來建構 Mesh

最基本的範例是建立一個三角形:

var vertices = PackedVector3Array()
vertices.push_back(Vector3(0, 1, 0))
vertices.push_back(Vector3(1, 0, 0))
vertices.push_back(Vector3(0, 0, 1))

# 初始化 ArrayMesh。
var arr_mesh = ArrayMesh.new()
var arrays = []
arrays.resize(Mesh.ARRAY_MAX)
arrays[Mesh.ARRAY_VERTEX] = vertices

# 建立 Mesh。
arr_mesh.add_surface_from_arrays(Mesh.PRIMITIVE_TRIANGLES, arrays)
var m = MeshInstance3D.new()
m.mesh = arr_mesh

MeshInstance3D 已可加入 SceneTree 進行顯示。

如需程式化幾何產生,亦可參考 ImmediateMeshMeshDataToolSurfaceTool

注意:Godot 在三角形圖元模式的正面使用順時針 繞線順序

教學

屬性

BlendShapeMode

blend_shape_mode

1

AABB

custom_aabb

AABB(0, 0, 0, 0, 0, 0)

ArrayMesh

shadow_mesh

方法

void

add_blend_shape(name: StringName)

void

add_surface_from_arrays(primitive: PrimitiveType, arrays: Array, blend_shapes: Array[Array] = [], lods: Dictionary = {}, flags: BitField[ArrayFormat] = 0)

void

clear_blend_shapes()

void

clear_surfaces()

int

get_blend_shape_count() const

StringName

get_blend_shape_name(index: int) const

Error

lightmap_unwrap(transform: Transform3D, texel_size: float)

void

regen_normal_maps()

void

set_blend_shape_name(index: int, name: StringName)

int

surface_find_by_name(name: String) const

int

surface_get_array_index_len(surf_idx: int) const

int

surface_get_array_len(surf_idx: int) const

BitField[ArrayFormat]

surface_get_format(surf_idx: int) const

String

surface_get_name(surf_idx: int) const

PrimitiveType

surface_get_primitive_type(surf_idx: int) const

void

surface_remove(surf_idx: int)

void

surface_set_name(surf_idx: int, name: String)

void

surface_update_attribute_region(surf_idx: int, offset: int, data: PackedByteArray)

void

surface_update_skin_region(surf_idx: int, offset: int, data: PackedByteArray)

void

surface_update_vertex_region(surf_idx: int, offset: int, data: PackedByteArray)


屬性說明

BlendShapeMode blend_shape_mode = 1 🔗

混合形狀模式。


AABB custom_aabb = AABB(0, 0, 0, 0, 0, 0) 🔗

  • void set_custom_aabb(value: AABB)

  • AABB get_custom_aabb()

使用使用者自訂的 AABB 取代預設值,供視錐剔除使用。當著色器會偏移頂點時,可避免被意外剔除。


ArrayMesh shadow_mesh 🔗

可選的網格,可用於陰影與深度預傳(depth prepass)的繪製。若提供僅包含融合後頂點且只有頂點位置資料(無法線、UV、顏色等)的網格,可提升效能。

注意:此網格的頂點位置必須與來源網格完全一致(若來源網格有 LOD 亦須相同),否則將無法正確繪製。


方法說明

void add_blend_shape(name: StringName) 🔗

為將透過 add_surface_from_arrays() 加入的混合形狀設定名稱。必須在加入曲面前呼叫。


void add_surface_from_arrays(primitive: PrimitiveType, arrays: Array, blend_shapes: Array[Array] = [], lods: Dictionary = {}, flags: BitField[ArrayFormat] = 0) 🔗

建立新的曲面。Mesh.get_surface_count() 的返回值將成為此曲面的 surf_idx

曲面會以 primitive 指定的圖元型別進行繪製,可為 PrimitiveType 中的任一值。

arrays 是「陣列的陣列」。在 Mesh.ARRAY_MAX 個元素中,每個元素要嘛包含由 ArrayType 相對應成員所描述的曲面資料,要嘛為未使用時的 null。例如,arrays[0] 為頂點陣列;此子陣列為必填,其餘則為可選。若加上索引陣列,即會進入「索引模式」,此時頂點與其他陣列成為資料來源,而索引陣列決定頂點次序。除非使用 Mesh.ARRAY_INDEX,否則所有子陣列長度必須等於頂點陣列長度(或為其整數倍,當一個頂點對應多個子陣列元素時),或保持空陣列。

blend_shapes 是每個混合形狀的頂點資料陣列。其結構與 arrays 相同,但僅當 arrays 內有設定 Mesh.ARRAY_VERTEXMesh.ARRAY_NORMALMesh.ARRAY_TANGENT 時,對應項目才會被設定;其餘皆為 null

lods 為字典,鍵值為 float、內容為 PackedInt32Array。每一項代表此曲面的一級 LOD,其中值為該 LOD 使用的 Mesh.ARRAY_INDEX 陣列,鍵值大致對應開始啟用該 LOD 的距離;鍵越大,啟用距離亦越遠。

flags 為多個旗標的位元 OR:對於每個使用中的自訂通道,將 ArrayCustomFormat 的值左移 ARRAY_FORMAT_CUSTOMn_SHIFT;亦可加入 Mesh.ARRAY_FLAG_USE_DYNAMIC_UPDATEMesh.ARRAY_FLAG_USE_8_BONE_WEIGHTSMesh.ARRAY_FLAG_USES_EMPTY_VERTEX_ARRAY

注意:使用索引時建議僅使用點、線或三角形圖元。


void clear_blend_shapes() 🔗

移除此 ArrayMesh 的所有混合形狀。


void clear_surfaces() 🔗

移除此 ArrayMesh 的所有曲面。


int get_blend_shape_count() const 🔗

回傳此 ArrayMesh 包含的混合形狀數量。


StringName get_blend_shape_name(index: int) const 🔗

回傳此索引處混合形狀的名稱。


Error lightmap_unwrap(transform: Transform3D, texel_size: float) 🔗

ArrayMesh 執行 UV 展開,以便進行光照貼圖。


void regen_normal_maps() 🔗

重新產生此 ArrayMesh 每個曲面的切線。


void set_blend_shape_name(index: int, name: StringName) 🔗

設定此索引處混合形狀的名稱。


int surface_find_by_name(name: String) const 🔗

回傳此 ArrayMesh 中第一個具有指定名稱的曲面索引;若無則回傳 -1。


int surface_get_array_index_len(surf_idx: int) const 🔗

回傳所指定曲面之索引陣列的索引數量(參見 add_surface_from_arrays())。


int surface_get_array_len(surf_idx: int) const 🔗

回傳所指定曲面之頂點陣列的頂點數量(參見 add_surface_from_arrays())。


BitField[ArrayFormat] surface_get_format(surf_idx: int) const 🔗

回傳所指定曲面的格式遮罩(參見 add_surface_from_arrays())。


String surface_get_name(surf_idx: int) const 🔗

取得此曲面設定的名稱。


PrimitiveType surface_get_primitive_type(surf_idx: int) const 🔗

回傳所指定曲面的圖元型別(參見 add_surface_from_arrays())。


void surface_remove(surf_idx: int) 🔗

從 Mesh 中移除指定索引的曲面,並將較高索引的曲面依序下移一位。


void surface_set_name(surf_idx: int, name: String) 🔗

為指定曲面設定名稱。


void surface_update_attribute_region(surf_idx: int, offset: int, data: PackedByteArray) 🔗

Updates the attribute buffer of this mesh's surface with the given data. The expected data per attribute is 12 or 8 bytes (4 bytes per float, 2 floats per Vector2, and 3 floats per Vector3) depending on if the mesh is using Vector3 or Vector2 vertices. This value can be determined with RenderingServer.mesh_surface_get_format_attribute_stride().

The starting point of the updates can be changed with offset. The value of offset should be a multiple of 12 bytes in most cases to align to each attribute.

A PackedVector3Array of attribute locations can be converted into a PackedByteArray using PackedVector3Array.to_byte_array() for use in data.


void surface_update_skin_region(surf_idx: int, offset: int, data: PackedByteArray) 🔗

Updates the skin buffer of this mesh's surface with the given data. The expected data per skin is 12 or 8 bytes (4 bytes per float, 2 floats per Vector2, and 3 floats per Vector3) depending on if the mesh is using Vector3 or Vector2 vertices. This value can be determined with RenderingServer.mesh_surface_get_format_skin_stride().

The starting point of the updates can be changed with offset. The value of offset should be a multiple of 12 bytes in most cases to align to each skin.

A PackedVector3Array of skin locations can be converted into a PackedByteArray using PackedVector3Array.to_byte_array() for use in data.


void surface_update_vertex_region(surf_idx: int, offset: int, data: PackedByteArray) 🔗

Updates the vertex buffer of this mesh's surface with the given data. The expected data per vertex is 12 or 8 bytes (4 bytes per float, 2 floats per Vector2, and 3 floats per Vector3) depending on if the mesh is using Vector3 or Vector2 vertices. This value can be determined with RenderingServer.mesh_surface_get_format_vertex_stride().

The starting point of the updates can be changed with offset. The value of offset should be a multiple of 12 bytes in most cases to align to each vertex.

A PackedVector3Array of vertex locations can be converted into a PackedByteArray using PackedVector3Array.to_byte_array() for use in data.