Godot Version
4.22
Question
I am playing around with generating the mesh for tiles. I first create a basic grid with an offset, then use those grid points to generate vertices for hexagons. I create the mesh using MeshArray and set the global_position of the mesh to the original grid point (the hexagons center). when I do this though, it seems to alter the size of the mesh being generated (specifically making them significantly smaller, to the point that they no longer touch sides).
I have run into this while changing up the order of the functions, and in previous grids I have created through mesh. Why would global_position affect the mesh size?
var size:float = 5
var width:float
var height:float
var horizontal_spacing:float
var vertical_spacing:float
@onready var tile_prefab = preload("res://tile.tscn")
var hexs:Array #array of all hexagons
var grid:Array #grid of center positions
func _ready():
hexs = []
grid = []
height = 2 * size
width = sqrt(3) * size
horizontal_spacing = width
vertical_spacing = 3 * height / 4
print("height: ", height, ", width: ", width, ", hor: ", horizontal_spacing, ", vert: ", vertical_spacing)
var start_position = $Camera3D.global_position
start_position.y = 0
generate_grid(5,3, start_position)
for c in grid:
var tile = tile_prefab.instantiate()
add_child(tile)
tile.create_mesh(generate_hex_vertices(c), [1,2])
#tile.get_child(0).rotate(Vector3(0, tile.global_position.y, 0), deg_to_rad(90))
tile.global_position = c
f
unc generate_grid(row:int, col:int, start_pos)->void:
#generate grid normally
var offset_row:bool = false
var offset_value:float = 0
for z in range(col):
for x in range(row):
if offset_row:
offset_value = horizontal_spacing / 2
var new_pos = Vector3(
start_pos.x + x * horizontal_spacing - offset_value,
start_pos.y,
start_pos.z + z * vertical_spacing)
grid.append(new_pos)
print(new_pos)
offset_row = !offset_row
offset_value = 0
func generate_hex_vertices(center:Vector3)->Array:
var verts:Array = []
for i in 6:
verts.append(hex_corner(center, size, i))
var vertices:Array = [
center, verts[1], verts[0], #lower left
center, verts[2], verts[1], #left
center, verts[3], verts[2], #upper left
center, verts[4], verts[3], #upper right
center, verts[5], verts[4], #right
center, verts[0], verts[5], #lower right
]
return vertices
func hex_corner(center, size, i)->Vector3:
var angle_rad = deg_to_rad(60 * i) #60 degrees for each vertex
var x = center.x + size * cos(angle_rad)
var z = center.z + size * sin(angle_rad)
return Vector3(x, center.y, z)
tile.gd
@onready var m = $MeshInstance3D
func create_mesh(verts:Array, indices:Array)->void:
#create mesh
var mesh_data = []
#load in vertices
var vertices = PackedVector3Array(verts)
mesh_data.resize(ArrayMesh.ARRAY_MAX)
mesh_data[ArrayMesh.ARRAY_VERTEX] = vertices
#mesh_data[ArrayMesh.ARRAY_NORMAL] = PackedVector3Array(norms)
#mesh_data[ArrayMesh.ARRAY_INDEX] = PackedInt32Array(indices)
#load data
m.mesh = ArrayMesh.new()
m.mesh.add_surface_from_arrays(Mesh.PRIMITIVE_TRIANGLES, mesh_data)
#global_rotate(Vector3(0,1,0), deg_to_rad(90))`Preformatted text`