Solved. Texture not lining up on one axis?

Godot Version

4.4

Question

Hey all, have a weird but simple problem, and I know the answer is simple but I just can’t figure it out. I have a MeshInstance3D That I have created and I am trying to add a texture to it. Right now I can just add the test texture to the albedo of the material. The texture applies, but incorrectly on one axis.

I want the texture to stretch over the whole mesh, and along the y axis it is working fine but along the x axis it is doing a weird thing:

You can see that it is cut up into lines. These lines happen every 1 unit, and I have no idea why. Here’s my code:

var st = SurfaceTool.new()
	st.begin(Mesh.PRIMITIVE_TRIANGLES)
	
	# Loop over grid cells (each cell is defined by four adjacent spheres)
	for x in range(chunk_size.x - 1):
		for y in range(chunk_size.y - 1):
			var idx = x * chunk_size.y + y
			# Get positions for the four corners of the cell
			var pos_a = spheres[idx].position                          # lower left
			var pos_b = spheres[idx + chunk_size.y].position              # lower right
			var pos_c = spheres[idx + 1].position                         # upper left
			var pos_d = spheres[idx + chunk_size.y + 1].position            # upper right
			
			# Compute UVs based on grid coordinates.
			var u_left = x / float(chunk_size.x - 1)
			var u_right = (x + 1) / float(chunk_size.x - 1)
			var v_top = y / float(chunk_size.y - 1)
			var v_bottom = (y + 1) / float(chunk_size.y - 1)
			
			# First triangle: lower left, lower right, upper left
			st.set_uv(Vector2(u_left, v_bottom))
			st.add_vertex(pos_a)
			
			st.set_uv(Vector2(u_right, v_bottom))
			st.add_vertex(pos_b)

			st.set_uv(Vector2(u_left, v_top))
			st.add_vertex(pos_c)

			# Second triangle: lower right, upper right, upper left
			st.set_uv(Vector2(u_right, v_bottom))
			st.add_vertex(pos_b)

			st.set_uv(Vector2(u_right, v_top))
			st.add_vertex(pos_d)

			st.set_uv(Vector2(u_left, v_top))
			st.add_vertex(pos_c)

Ok its not actually working on the X or the Y, it was just a lot harder to see. I made a new quick texture:

and this is what it looks like when applying it to my mesh:

Still looks like it’s just one axis, but every triangle having v_top and v_bottom swapped.

For x = 0 and y = 0, pos_a would receive the position of spheres[0], which I would assume to be one of the corners, yes?
But that corner’s v would be something like 1/31, not 0 or 1.

I think swapping the calculations of v_top and v_bottom should prevent the weird behavior.

			var v_bottom = y / float(chunk_size.y - 1)
			var v_top = (y + 1) / float(chunk_size.y - 1)

The texture is probably mirrored then, to fix this you can use the difference to 1 for both v values:

			var v_bottom = 1 - ( y / float(chunk_size.y - 1) )
			var v_top = 1 - ( (y + 1) / float(chunk_size.y - 1) )

That did the trick! Thanks! I was too tired to see how that needed to change, but it works perfectly now!

1 Like