extends Node @export var central_height : float = 2.0 @export var central_height_variance : float = 0.3 @export var height : float = 2.0 @export var height_variance : float = 0.3 # 4 -- ( 4 ) -- 5 # / \ # (3) (5) # / \ # 3 0 # \ / # (2) (0) # \ / # 2 -- ( 1 ) -- 1 const BASALT_INNER_RADIUS : float = cos(PI / 6.0) const BASALT_BASIC_OFFSET : Vector2 = Vector2(-2.0 * BASALT_INNER_RADIUS, 2.0 * BASALT_INNER_RADIUS) const BASALT_CELLS : Array[int] = [ 0, 1, 2, 3, 4, 5, 6 ] const BASALT_CENTRAL_CELL : int = 4 const BASALT_ANGLES : Array[float] = [ 0, PI / 3.0, (2 * PI) / 3.0, PI, (4 * PI) / 3.0, (5 * PI) / 3.0 ] const BASALT_VECTORS_OFFSET : Array[Vector2] = [ Vector2(cos(BASALT_ANGLES[0]), sin(BASALT_ANGLES[0])), Vector2(cos(BASALT_ANGLES[1]), sin(BASALT_ANGLES[1])), Vector2(cos(BASALT_ANGLES[2]), sin(BASALT_ANGLES[2])), Vector2(cos(BASALT_ANGLES[3]), sin(BASALT_ANGLES[3])), Vector2(cos(BASALT_ANGLES[4]), sin(BASALT_ANGLES[4])), Vector2(cos(BASALT_ANGLES[5]), sin(BASALT_ANGLES[5])) ] const BASALT_CELLS_OFFSET : Array[Vector2] = [ Vector2(cos(PI/6.0), sin(PI/6.0)) * BASALT_BASIC_OFFSET, Vector2(cos(PI/2.0), sin(PI/2.0)) * BASALT_BASIC_OFFSET, Vector2(cos((5.0 * PI) / 6.0), sin((5.0 * PI) / 6.0)) * BASALT_BASIC_OFFSET, Vector2(cos((11.0 * PI) / 6.0), sin((11.0 * PI) / 6.0)) * BASALT_BASIC_OFFSET, Vector2(0.0, 0.0), Vector2(cos((3.0 * PI) / 2.0), sin((3.0 * PI) / 2.0)) * BASALT_BASIC_OFFSET, Vector2(cos((7.0 * PI) / 6.0), sin((7.0 * PI) / 6.0)) * BASALT_BASIC_OFFSET ] const BASALT_SIDE_NORMALS : Array[Vector3] = [ Vector3(cos(PI/6.0), 0.0, sin(PI/6.0)), Vector3(cos(PI/2.0), 0.0, sin(PI/2.0)), Vector3(cos((5.0 * PI) / 6.0), 0.0, sin((5.0 * PI)/ 6.0)), Vector3(cos((7.0 * PI) / 6.0), 0.0, sin((7.0 * PI)/ 6.0)), Vector3(cos((3.0 * PI) / 2.0), 0.0, sin((3.0 * PI)/ 2.0)), Vector3(cos((11.0 * PI) / 6.0), 0.0, sin((11.0 * PI)/ 6.0)) ] const BASALT_DEFAULT_SIDE_COLOR : Array[Color] = [ Color.BLACK, Color.BLACK, Color.BLACK, Color.BLACK ] const BASALT_DEFAULT_SIDE_UV : Array[Vector2] = [ Vector2.ZERO, Vector2.ZERO, Vector2.ZERO, Vector2.ZERO ] const BASALT_SIDE_NORMALS_ARRAY : Array[Array] = [ [ BASALT_SIDE_NORMALS[0], BASALT_SIDE_NORMALS[0], BASALT_SIDE_NORMALS[0], BASALT_SIDE_NORMALS[0] ], [ BASALT_SIDE_NORMALS[1], BASALT_SIDE_NORMALS[1], BASALT_SIDE_NORMALS[1], BASALT_SIDE_NORMALS[1] ], [ BASALT_SIDE_NORMALS[2], BASALT_SIDE_NORMALS[2], BASALT_SIDE_NORMALS[2], BASALT_SIDE_NORMALS[2] ], [ BASALT_SIDE_NORMALS[3], BASALT_SIDE_NORMALS[3], BASALT_SIDE_NORMALS[3], BASALT_SIDE_NORMALS[3] ], [ BASALT_SIDE_NORMALS[4], BASALT_SIDE_NORMALS[4], BASALT_SIDE_NORMALS[4], BASALT_SIDE_NORMALS[4] ], [ BASALT_SIDE_NORMALS[5], BASALT_SIDE_NORMALS[5], BASALT_SIDE_NORMALS[5], BASALT_SIDE_NORMALS[5] ], ] const BASALT_VERTICES_2D_POS : Array = [ [ BASALT_CELLS_OFFSET[0] + BASALT_VECTORS_OFFSET[0], BASALT_CELLS_OFFSET[0] + BASALT_VECTORS_OFFSET[1], BASALT_CELLS_OFFSET[0] + BASALT_VECTORS_OFFSET[2], BASALT_CELLS_OFFSET[0] + BASALT_VECTORS_OFFSET[3], BASALT_CELLS_OFFSET[0] + BASALT_VECTORS_OFFSET[4], BASALT_CELLS_OFFSET[0] + BASALT_VECTORS_OFFSET[5] ], [ BASALT_CELLS_OFFSET[1] + BASALT_VECTORS_OFFSET[0], BASALT_CELLS_OFFSET[1] + BASALT_VECTORS_OFFSET[1], BASALT_CELLS_OFFSET[1] + BASALT_VECTORS_OFFSET[2], BASALT_CELLS_OFFSET[1] + BASALT_VECTORS_OFFSET[3], BASALT_CELLS_OFFSET[1] + BASALT_VECTORS_OFFSET[4], BASALT_CELLS_OFFSET[1] + BASALT_VECTORS_OFFSET[5] ], [ BASALT_CELLS_OFFSET[2] + BASALT_VECTORS_OFFSET[0], BASALT_CELLS_OFFSET[2] + BASALT_VECTORS_OFFSET[1], BASALT_CELLS_OFFSET[2] + BASALT_VECTORS_OFFSET[2], BASALT_CELLS_OFFSET[2] + BASALT_VECTORS_OFFSET[3], BASALT_CELLS_OFFSET[2] + BASALT_VECTORS_OFFSET[4], BASALT_CELLS_OFFSET[2] + BASALT_VECTORS_OFFSET[5] ], [ BASALT_CELLS_OFFSET[3] + BASALT_VECTORS_OFFSET[0], BASALT_CELLS_OFFSET[3] + BASALT_VECTORS_OFFSET[1], BASALT_CELLS_OFFSET[3] + BASALT_VECTORS_OFFSET[2], BASALT_CELLS_OFFSET[3] + BASALT_VECTORS_OFFSET[3], BASALT_CELLS_OFFSET[3] + BASALT_VECTORS_OFFSET[4], BASALT_CELLS_OFFSET[3] + BASALT_VECTORS_OFFSET[5] ], [ BASALT_CELLS_OFFSET[4] + BASALT_VECTORS_OFFSET[0], BASALT_CELLS_OFFSET[4] + BASALT_VECTORS_OFFSET[1], BASALT_CELLS_OFFSET[4] + BASALT_VECTORS_OFFSET[2], BASALT_CELLS_OFFSET[4] + BASALT_VECTORS_OFFSET[3], BASALT_CELLS_OFFSET[4] + BASALT_VECTORS_OFFSET[4], BASALT_CELLS_OFFSET[4] + BASALT_VECTORS_OFFSET[5] ], [ BASALT_CELLS_OFFSET[5] + BASALT_VECTORS_OFFSET[0], BASALT_CELLS_OFFSET[5] + BASALT_VECTORS_OFFSET[1], BASALT_CELLS_OFFSET[5] + BASALT_VECTORS_OFFSET[2], BASALT_CELLS_OFFSET[5] + BASALT_VECTORS_OFFSET[3], BASALT_CELLS_OFFSET[5] + BASALT_VECTORS_OFFSET[4], BASALT_CELLS_OFFSET[5] + BASALT_VECTORS_OFFSET[5] ], [ BASALT_CELLS_OFFSET[6] + BASALT_VECTORS_OFFSET[0], BASALT_CELLS_OFFSET[6] + BASALT_VECTORS_OFFSET[1], BASALT_CELLS_OFFSET[6] + BASALT_VECTORS_OFFSET[2], BASALT_CELLS_OFFSET[6] + BASALT_VECTORS_OFFSET[3], BASALT_CELLS_OFFSET[6] + BASALT_VECTORS_OFFSET[4], BASALT_CELLS_OFFSET[6] + BASALT_VECTORS_OFFSET[5] ] ] const BASALT_HEXA_FACES_INDICES : Array[int] = [ 0, 1, 2, 5, 0, 2, 5, 2, 3, 4, 5, 3 ] const BASALT_DEFAULT_HEIGHTS : Array[float] = [ 4.6, 6.0, 4.2, 4.3, 0.0, 4.4, 4.5 ] const BASALT_CELL_COLOR : Array[Color] = [ Color.BLUE, Color.BLUE, Color.BLUE, Color.BLUE, Color.RED, Color.BLUE, Color.YELLOW ] const BASALT_SIDES : Array[Array] = [ [ 1, -1, -1, -1, 3, 4 ], [ -1, -1, -1, 0, 4, 2 ], [ -1, -1, 1, 4, 6, -1 ], [ 4, 0, -1, -1, -1, 5 ], [ 2, 1, 0, 3, 5, 6 ], [ 6, 4, 3, -1, -1, -1 ], [ -1, 2, 4, 5, -1, -1 ] ] const BASALT_SIDE_FACES_INDICES : Array = [ ] @onready var heights : Array[float] func generate_cluster() -> Array: var global_vertices : Array[Vector3] = [] var global_normals : Array[Vector3] = [] var global_indices : Array[int] = [] var global_uvs : Array[Vector2] = [] var global_colors : Array[Color] = [] heights = [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] for i in BASALT_CELLS: heights[i] = randf_range(height - height_variance, height + height_variance) if i != BASALT_CENTRAL_CELL else randf_range(central_height - central_height_variance, central_height + central_height_variance) for i in BASALT_CELLS: var id_offset : int = global_vertices.size() # First, the top. for j in range(6): var hrz_2d_pos : Vector2 = BASALT_VERTICES_2D_POS[i][j] var hrz_position : Vector3 = Vector3(hrz_2d_pos.x, heights[i], hrz_2d_pos.y) global_vertices.append(hrz_position) global_normals.append(Vector3.UP) global_uvs.append(Vector2.ZERO) global_colors.append(BASALT_CELL_COLOR[i]) for j in BASALT_HEXA_FACES_INDICES: global_indices.append(id_offset + j) # Then, sides. for j in range(6): var neighbor_cell_id : int = BASALT_SIDES[i][j] var upa : Vector3 = global_vertices[id_offset + j] var upb : Vector3 = global_vertices[id_offset + ((j + 1) % 6)] var cur_id : int = global_vertices.size() if neighbor_cell_id != -2 and (neighbor_cell_id == -1 or heights[i] > heights[neighbor_cell_id]): var h : float = 0.0 if neighbor_cell_id == -1 else heights[neighbor_cell_id] #var h : float = 0.0 var nxtA : Vector3 = Vector3(upa.x, h, upa.z) var nxtB : Vector3 = Vector3(upb.x, h, upb.z) var n : Vector3 = (upa - upb).cross(upa - nxtA).normalized() global_vertices.append_array([upa, upb, nxtA, nxtB]) global_normals.append_array([n, n, n, n]) global_uvs.append_array(BASALT_DEFAULT_SIDE_UV) global_colors.append_array([ BASALT_CELL_COLOR[i], BASALT_CELL_COLOR[i], BASALT_CELL_COLOR[i], BASALT_CELL_COLOR[i] ] ) global_indices.append_array([ cur_id, cur_id + 3, cur_id + 1, cur_id, cur_id + 2, cur_id + 3 ]) return [ global_vertices, global_normals, global_indices, global_uvs, global_colors ]
or share this direct link: