@tool

extends MeshInstance3D
class_name PlantetMeshFace

@export var normal : Vector3

func regenerate_mesh(planet_data):
	var arrays := []
	arrays.resize(Mesh.ARRAY_MAX)
	
	var vertex_array := PackedVector3Array()
	var uv_array := PackedVector2Array()
	var normal_array := PackedVector3Array()
	
	var index_array := PackedInt32Array()
	
	var resolution : int = planet_data.resolution
	var num_vertices : int = resolution * resolution
	var num_indices : int = (resolution-1) * (resolution-1) * 6
	
	vertex_array.resize(num_vertices)
	normal_array.resize(num_vertices)
	uv_array.resize(num_vertices)
	index_array.resize(num_indices)
	
	var tri_index : int = 0
	var a_axis := Vector3(normal.y, normal.z, normal.x)
	var b_axis : Vector3 = normal.cross(a_axis)
	for y in range(resolution):
		for x in range(resolution):
			var i : int = x + y*resolution
			var percent := Vector2(x,y) / (resolution-1)
			var point_on_cube : Vector3 = normal + (percent.x - 0.5)*2.0*a_axis + (percent.y - 0.5)*2.0*b_axis
			var point_on_sphere = point_on_cube.normalized()
			var point_on_planet = planet_data.point_on_planet(point_on_sphere)
			vertex_array[i] = point_on_planet
			if x != resolution-1 and y != resolution-1:
				index_array[tri_index + 2] = i
				index_array[tri_index + 1] = i+resolution+1
				index_array[tri_index] = i+resolution
				
				index_array[tri_index + 5] = i
				index_array[tri_index + 4] = i+1
				index_array[tri_index + 3] = i+resolution+1
				tri_index += 6
	
	for a in range(0, index_array.size(), 3):
		var b : int = a + 1
		var c : int = a + 2

		var ab : Vector3 = vertex_array[index_array[b]] - vertex_array[index_array[a]]
		var bc : Vector3 = vertex_array[index_array[c]] - vertex_array[index_array[b]]
		var ca : Vector3 = vertex_array[index_array[a]] - vertex_array[index_array[c]]
		
		var cross_ab_bc : Vector3 = bc.cross(ab)
		var cross_bc_ca : Vector3 = ca.cross(bc)
		var cross_ca_ab : Vector3 = ab.cross(ca)

		normal_array[index_array[a]] += cross_ab_bc + cross_bc_ca + cross_ca_ab
		normal_array[index_array[b]] += cross_ab_bc + cross_bc_ca + cross_ca_ab
		normal_array[index_array[c]] += cross_ab_bc + cross_bc_ca + cross_ca_ab

	for i in range(normal_array.size()):
		normal_array[i] = normal_array[i].normalized()

	arrays[Mesh.ARRAY_VERTEX] = vertex_array
	arrays[Mesh.ARRAY_NORMAL] = normal_array
	arrays[Mesh.ARRAY_TEX_UV] = uv_array
	arrays[Mesh.ARRAY_INDEX] = index_array

	call_deferred("_update_mesh", arrays)

func _update_mesh(arrays : Array):
	var _mesh := ArrayMesh.new()
	_mesh.add_surface_from_arrays(Mesh.PRIMITIVE_TRIANGLES, arrays)
	self.mesh = _mesh
