Tangents generating incorrectly

Godot Version

4.4.1

Question

I am generating terrain using a Plane and generating the tangents works fine if the SubdivideDepth and Width are smaller or equal to the Size, but if they are higher than the Size the tangents output NaN, what could be causing this issue?

Tangent Calculation Code

	PlaneMesh plane = new PlaneMesh();
	plane.SubdivideDepth = resolution;
	plane.SubdivideWidth = resolution;

	plane.Size = new Vector2(size, size);

	Godot.Collections.Array surfaceArray = plane.GetMeshArrays();

	vertices = (Vector3[])surfaceArray[(int)Mesh.ArrayType.Vertex];
	normals = (Vector3[])surfaceArray[(int)Mesh.ArrayType.Normal];
	tangents = (float[])surfaceArray[(int)Mesh.ArrayType.Tangent];

	for (int i = 0; i < vertices.Length; i++)
	{
		Vector3 vertex = vertices[i];
		vertex = new Vector3(vertex.X, noise.GetNoise2D(vertex.X, vertex.Z) * height, vertex.Z);

		normals[i] = CalculateNormal(vertex.X, vertex.Z);
		Vector3 tangent = normals[i].Cross(Vector3.Up);
		tangents[4 * i] = tangent.X;
		tangents[4 * i + 1] = tangent.Y;
		tangents[4 * i + 2] = tangent.Z;

		vertices[i] = vertex;
	}

	surfaceArray[(int)Mesh.ArrayType.Vertex] = vertices;
	surfaceArray[(int)Mesh.ArrayType.Normal] = normals;
	surfaceArray[(int)Mesh.ArrayType.Tangent] = tangents;

	ArrayMesh arrayMesh = new ArrayMesh();
	arrayMesh.AddSurfaceFromArrays(Mesh.PrimitiveType.Triangles, surfaceArray);

	Mesh = arrayMesh;