SurfaceTool UVs are not applying to mesh

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By MEDBVLL

I am following this video on how to make a minecraft-like voxel world. Like in the video, I am using SurfaceTool to render triangles, but for some reason UVs are not applied, the mesh looks like this:

enter image description here

The color has changed, because a material has been applied. This material contains my AtlasTexture image which is comprised of 11 textures, 4 rows and 3 columns, each texture having the width and height of 32px. The resulting resolution of this AtlasTexture is 128x96.

However as you can see, no textures are displayed, here’s my code:

func create_face(i, x, y, z, texture_atlas_offset):
	var offset = Vector3i(x, y, z)
	var a = VERTICES[i[0]] + offset
	var b = VERTICES[i[1]] + offset
	var c = VERTICES[i[2]] + offset
	var d = VERTICES[i[3]] + offset

	var uv_offset = Vector2(
		texture_atlas_offset.x / 128,
		texture_atlas_offset.y / 96
	)
	
	var uv_a = Vector2.ZERO + uv_offset
	var uv_b = Vector2(0, 1.0 / 96) + uv_offset
	var uv_c = Vector2(1.0 / 128, 1.0 / 96) + uv_offset
	var uv_d = Vector2(1.0 / 128, 0) + uv_offset

	st.add_triangle_fan([a, b, c], [uv_a, uv_b, uv_c])
	st.add_triangle_fan([a, c, d], [uv_a, uv_c, uv_d])

I made sure that the uv values span between 0 and 1.

Here’s the rest of the code where SurfaceTool is being used:

func update():
	if mesh_instance != null:
		mesh_instance.call_deferred("queue_free")
		mesh_instance = null
	
	array_mesh = ArrayMesh.new()
	mesh_instance = MeshInstance3D.new()
	st.begin(Mesh.PRIMITIVE_TRIANGLES)
	st.set_smooth_group(-1)
	
	for x in DIMENSION.x:
		for y in DIMENSION.y:
			for z in DIMENSION.z:
				create_block(x, y, z)
	
	st.generate_normals()
	st.set_material(material)
	st.commit(array_mesh)
	mesh_instance.set_mesh(array_mesh)
	
	add_child(mesh_instance)
	mesh_instance.create_trimesh_collision()

Did I miss something? Has anyone worked with this? I am genuinely confused.
Using Godot 4