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.

GridMap

繼承: Node3D < Node < Object

基於 3D 圖塊地圖的節點。

說明

GridMap 允許以互動的方式將網格放置在柵格上。它在編輯器和腳本中均可使用,可幫助建立遊戲內的關卡編輯器。

GridMap 使用一個包含圖塊列表的 MeshLibrary。每個圖塊都是一個帶有材質的網格以及可選的碰撞和導覽形狀。

GridMap 包含一組儲存格。每個柵格儲存格引用 MeshLibrary 中的一個圖塊。地圖中的所有儲存格都具有相同的大小。

在內部,GridMap 會根據卦限進行拆分,形成卦限的稀疏合集,能夠進行高效的算繪和物理處理。每個卦限的大小是相同的,可以包含多個儲存格。

注意:GridMap 不是從 VisualInstance3D 擴充的,因此不能基於 VisualInstance3D.layers 隱藏或剔除遮罩。如果使燈光不影響第一層,則整個 GridMap 將不會被該燈光照亮。

教學

屬性

bool

bake_navigation

false

bool

cell_center_x

true

bool

cell_center_y

true

bool

cell_center_z

true

int

cell_octant_size

8

float

cell_scale

1.0

Vector3

cell_size

Vector3(2, 2, 2)

int

collision_layer

1

int

collision_mask

1

float

collision_priority

1.0

DebugVisibilityMode

collision_visibility_mode

0

MeshLibrary

mesh_library

PhysicsMaterial

physics_material

方法

void

clear()

void

clear_baked_meshes()

RID

get_bake_mesh_instance(idx: int)

Array

get_bake_meshes()

Basis

get_basis_with_orthogonal_index(index: int) const

int

get_cell_item(position: Vector3i) const

Basis

get_cell_item_basis(position: Vector3i) const

int

get_cell_item_orientation(position: Vector3i) const

bool

get_collision_layer_value(layer_number: int) const

bool

get_collision_mask_value(layer_number: int) const

Array

get_meshes() const

RID

get_navigation_map() const

Vector3i

get_octant_coords_from_cell_coords(cell_coords: Vector3i) const

Array[Vector3i]

get_octants_in_bounds(bounds: AABB) const

int

get_orthogonal_index_from_basis(basis: Basis) const

Array[Vector3i]

get_used_cells() const

Array[Vector3i]

get_used_cells_by_item(item: int) const

Array[Vector3i]

get_used_cells_in_octant(octant_coords: Vector3i) const

Array[Vector3i]

get_used_cells_in_octant_by_item(octant_coords: Vector3i, item: int) const

Array[Vector3i]

get_used_octants() const

Array[Vector3i]

get_used_octants_by_item(item: int) const

Array[Vector3i]

get_used_octants_in_bounds(bounds: AABB) const

Vector3i

local_to_map(local_position: Vector3) const

void

make_baked_meshes(gen_lightmap_uv: bool = false, lightmap_uv_texel_size: float = 0.1)

Vector3

map_to_local(map_position: Vector3i) const

void

resource_changed(resource: Resource)

void

set_cell_item(position: Vector3i, item: int, orientation: int = 0)

void

set_collision_layer_value(layer_number: int, value: bool)

void

set_collision_mask_value(layer_number: int, value: bool)

void

set_navigation_map(navigation_map: RID)


訊號

cell_size_changed(cell_size: Vector3) 🔗

cell_size 改變時觸發。


changed() 🔗

該 TileMap 的 TileSet 發生改變時發出。


列舉

enum DebugVisibilityMode: 🔗

DebugVisibilityMode DEBUG_VISIBILITY_MODE_DEFAULT = 0

Hide the collisions debug shapes in the editor, and use the debug settings to determine their visibility in game (i.e. SceneTree.debug_collisions_hint or SceneTree.debug_navigation_hint).

DebugVisibilityMode DEBUG_VISIBILITY_MODE_FORCE_SHOW = 1

Always show the collisions debug shapes.

DebugVisibilityMode DEBUG_VISIBILITY_MODE_FORCE_HIDE = 2

Always hide the collisions debug shapes.


常數

INVALID_CELL_ITEM = -1 🔗

可以在 set_cell_item() 中清除儲存格(或在 get_cell_item() 中重新代表一個空的儲存格)的無效儲存格。


屬性說明

bool bake_navigation = false 🔗

  • void set_bake_navigation(value: bool)

  • bool is_baking_navigation()

如果為 true,則為每個使用帶有導覽網格的 mesh_library 項目的儲存格,該 GridMap 將建立一個導覽區域。建立的導覽區域將使用分配給該 MeshLibrary 專案的導覽層位元遮罩。


bool cell_center_x = true 🔗

  • void set_center_x(value: bool)

  • bool get_center_x()

如果為 true,則網格項以 X 軸為中心。


bool cell_center_y = true 🔗

  • void set_center_y(value: bool)

  • bool get_center_y()

如果為 true,則網格項以 Y 軸為中心。


bool cell_center_z = true 🔗

  • void set_center_z(value: bool)

  • bool get_center_z()

如果為 true,則網格項以 Z 軸為中心。


int cell_octant_size = 8 🔗

  • void set_octant_size(value: int)

  • int get_octant_size()

每個卦限的大小,單位為儲存格的數量。適用於全部三個軸。


float cell_scale = 1.0 🔗

  • void set_cell_scale(value: float)

  • float get_cell_scale()

儲存格專案的比例。

這不會影響網格單元本身的大小,只會影響其中的專案。這可用於使儲存格專案與其鄰居重疊。


Vector3 cell_size = Vector3(2, 2, 2) 🔗

網格單元的尺寸。

這並不影響網格的尺寸大小。見 cell_scale


int collision_layer = 1 🔗

  • void set_collision_layer(value: int)

  • int get_collision_layer()

這個 GridMap 所處的實體層。

GridMap 作為靜態體,意味著它們不會受到重力或是其他力的影響。它們只會受到其他與它們碰撞的物理體的影響。


int collision_mask = 1 🔗

  • void set_collision_mask(value: int)

  • int get_collision_mask()

此 GridMap 偵測碰撞的實體層。更多資訊請參閱文件《碰撞層與遮罩》


float collision_priority = 1.0 🔗

  • void set_collision_priority(value: float)

  • float get_collision_priority()

發生穿透時用於解決碰撞的優先順序。優先順序越高,對物體的穿透度就越低。例如,可以用來防止玩家突破關卡的邊界。


DebugVisibilityMode collision_visibility_mode = 0 🔗

Show or hide the GridMap's collision shapes. If set to DEBUG_VISIBILITY_MODE_DEFAULT, this depends on the show collision debug settings.


MeshLibrary mesh_library 🔗

指定的 MeshLibrary


PhysicsMaterial physics_material 🔗

覆蓋整個 GridMap 的預設摩擦力和反彈物理屬性。


方法說明

void clear() 🔗

清除所有儲存格。


void clear_baked_meshes() 🔗

清除所有烘焙過的網格。見 make_baked_meshes()


RID get_bake_mesh_instance(idx: int) 🔗

返回是否存在帶有給定 ID 的節點。


Array get_bake_meshes() 🔗

Returns an array of ArrayMeshes and Transform3D references of all bake meshes that exist within the current GridMap. Even indices contain ArrayMeshes, while odd indices contain Transform3Ds that are always equal to Transform3D.IDENTITY.

This method relies on the output of make_baked_meshes(), which will be called with gen_lightmap_uv set to true and lightmap_uv_texel_size set to 0.1 if it hasn't been called yet.


Basis get_basis_with_orthogonal_index(index: int) const 🔗

返回沿向量 (x,y,z) 的 24 種可能旋轉中的一種,每個分量為 -1、0 或 1。有關詳細資訊,請參閱 Godot 原始程式碼。


int get_cell_item(position: Vector3i) const 🔗

位於給定柵格座標的 MeshLibrary 專案的索引。如果儲存格為空,則將返回 INVALID_CELL_ITEM


Basis get_cell_item_basis(position: Vector3i) const 🔗

返回為指定儲存格提供方向的基。


int get_cell_item_orientation(position: Vector3i) const 🔗

給定柵格座標處的儲存格的方向。如果該儲存格為空,則返回 -1


bool get_collision_layer_value(layer_number: int) const 🔗

返回 collision_layer 中是否啟用了指定的層,給定的 layer_number 應在 1 和 32 之間。


bool get_collision_mask_value(layer_number: int) const 🔗

返回 collision_mask 中是否啟用了指定的層,給定的 layer_number 應在 1 和 32 之間。


Array get_meshes() const 🔗

Returns an array of Transform3D and Mesh references corresponding to the non-empty cells in the grid. The transforms are specified in local space. Even indices contain Transform3Ds, while odd indices contain Meshes related to the Transform3D in the index preceding it.


RID get_navigation_map() const 🔗

返回該 GridMap 節點用於其儲存格烘焙導覽網格的導覽地圖的 RID

該函式始終返回在 GridMap 節點上設定的地圖,而不是 NavigationServer 上的地圖。如果直接使用 NavigationServer API 更改地圖,則 GridMap 節點將不會感知到地圖的更改。


Vector3i get_octant_coords_from_cell_coords(cell_coords: Vector3i) const 🔗

Returns the Vector3i octant coordinates of the octant that the cell at cell_coords belongs to.


Array[Vector3i] get_octants_in_bounds(bounds: AABB) const 🔗

Returns an array of Vector3i octant coordinates that are inside the given bounds, including octants that have no cells in use.


int get_orthogonal_index_from_basis(basis: Basis) const 🔗

該函式考慮將旋轉離散化為單位球體上的 24 個點,沿向量 (x,y,z) 放置,每個分量為 -1、0 或 1,並返回索引(在 0 到 23 的範圍內 ) 最能代表物體方向的點。 有關詳細資訊,請參閱 Godot 原始程式碼。


Array[Vector3i] get_used_cells() const 🔗

返回一個包含網格中非空儲存格座標的 Vector3 陣列。


Array[Vector3i] get_used_cells_by_item(item: int) const 🔗

返回所有具有 item 中指定的專案索引的儲存格的陣列。


Array[Vector3i] get_used_cells_in_octant(octant_coords: Vector3i) const 🔗

Returns an array of Vector3is with the cell coordinates of non-empty cells inside the octant at octant_coords.


Array[Vector3i] get_used_cells_in_octant_by_item(octant_coords: Vector3i, item: int) const 🔗

Returns an array of Vector3is with the cell coordinates of cells inside the octant at octant_coords that use the specified cell item.


Array[Vector3i] get_used_octants() const 🔗

Returns an array of Vector3is with the octant coordinates of the non-empty octants in the grid map.


Array[Vector3i] get_used_octants_by_item(item: int) const 🔗

Returns an array of Vector3is with the octant coordinates of the octants that use the specified item in the grid map.


Array[Vector3i] get_used_octants_in_bounds(bounds: AABB) const 🔗

Returns an array of Vector3is with the octant coordinates of non-empty octants that are inside the local bounds.


Vector3i local_to_map(local_position: Vector3) const 🔗

返回包含給定 local_position 的儲存格的地圖座標。如果 local_position 在全域座標中,請考慮在將其傳遞給該方法之前使用 Node3D.to_local()。另請參閱 map_to_local()


void make_baked_meshes(gen_lightmap_uv: bool = false, lightmap_uv_texel_size: float = 0.1) 🔗

Generates a baked mesh that represents all meshes in the assigned MeshLibrary for use with LightmapGI. If gen_lightmap_uv is true, UV2 data will be generated for each mesh currently used in the GridMap. Otherwise, only meshes that already have UV2 data present will be able to use baked lightmaps. When generating UV2, lightmap_uv_texel_size controls the texel density for lightmaps, with lower values resulting in more detailed lightmaps. lightmap_uv_texel_size is ignored if gen_lightmap_uv is false. See also get_bake_meshes(), which relies on the output of this method.

Note: Calling this method will not actually bake lightmaps, as lightmap baking is performed using the LightmapGI node.


Vector3 map_to_local(map_position: Vector3i) const 🔗

Returns the position of a grid cell in the GridMap's local coordinate space. To convert the returned value into global coordinates, use Node3D.to_global(). See also local_to_map().


void resource_changed(resource: Resource) 🔗

已棄用: Use Resource.changed instead.

This method does nothing.


void set_cell_item(position: Vector3i, item: int, orientation: int = 0) 🔗

設定由其柵格座標引用的儲存格的網格索引。

諸如 INVALID_CELL_ITEM 之類的負的專案索引將清除該儲存格。

可選地,可以傳入該專案的方向。有關有效的方向值,請參閱 get_orthogonal_index_from_basis()


void set_collision_layer_value(layer_number: int, value: bool) 🔗

根據 value,啟用或禁用 collision_layer 中指定的層,給定的 layer_number 應在 1 和 32 之間。


void set_collision_mask_value(layer_number: int, value: bool) 🔗

根據 value,啟用或禁用 collision_mask 中指定的層,給定的 layer_number 應在 1 和 32 之間。


void set_navigation_map(navigation_map: RID) 🔗

設定該導覽地圖的 RID,該 GridMap 節點應該用於其儲存格烘焙導覽網格。