ArrayMesh Material Appears Rotated

Godot Version

4.3.stable

Question

I’m attempting to render a very simple mesh via ArrayMesh: two square planes, one on top of the other, with separate surfaces. I have this part working, but for some reason whenever I apply a texture as the material, it appears rotated on the mesh. Any idea how to fix this?

extends MeshInstance3D

func _ready() -> void:
	var data = []
	data.resize(ArrayMesh.ARRAY_MAX)
	data[ArrayMesh.ARRAY_VERTEX] = PackedVector3Array([
		Vector3(0,0,0),
		Vector3(1,1,0),
		Vector3(1,0,0),
		
		Vector3(0,0,0),
		Vector3(0,1,0),
		Vector3(1,1,0)
	])
	data[ArrayMesh.ARRAY_TEX_UV] = PackedVector2Array([
		Vector2(0,0),
		Vector2(1,1),
		Vector2(1,0),
		
		Vector2(0,0),
		Vector2(0,1),
		Vector2(1,1)
	])
	mesh = ArrayMesh.new()
	mesh.add_surface_from_arrays(Mesh.PRIMITIVE_TRIANGLES,data)
	
	#second surface
	data[ArrayMesh.ARRAY_VERTEX] = PackedVector3Array([
		Vector3(0,1,0),
		Vector3(1,2,0),
		Vector3(1,1,0),
		
		Vector3(0,1,0),
		Vector3(0,2,0),
		Vector3(1,2,0)
	])
	data[ArrayMesh.ARRAY_TEX_UV] = PackedVector2Array([
		Vector2(0,1),
		Vector2(1,2),
		Vector2(1,1),
		
		Vector2(0,1),
		Vector2(0,2),
		Vector2(1,2)
	])
	mesh.add_surface_from_arrays(Mesh.PRIMITIVE_TRIANGLES,data)
	ResourceSaver.save(mesh, "res://mesh_test_flip3.tres", ResourceSaver.FLAG_COMPRESS)

The highlights of the tile should be facing the top left not the bottom left. No matter what I change in the vertex ordering it doesn’t seem to change this.

I might switch to an immediatemesh if I can’t figure this out

Shift (or flip) the UVs.

1 Like

Every time I change them to not match the vertices it leads to some weird errors

Well they need to match the vertices. Just change the order. Draw your triangles on a piece of paper.

I’ve tried that a few times, but it doesn’t seem to rotate it

Post the results you’re getting vs what you’re expecting to get.

The first image is what I’m getting (with a material applied to surface 0 of the mesh). The second image is what I expect to get (which is the same material but properly oriented)

That looks like you need to rotate the UVs clockwise. You can also just rotate the texture in an image editing app.

UVs rotated clockwise:
0,0 becomes 0,1
0,1 becomes 1,1
1,1 becomes 1,0
1,0 becomes 0,0

Thank you! That seemed to rotate it counterclockwise not clockwise for some reason, but I did it three times and it worked.

Well it depends from which side you look at the polygons. To rotate it in opposite direction just do the reverse of what I wrote above so:
0,1 → 0,0
1,1 → 0,1
1,0 → 1,1
0,0 → 1,0

1 Like

For some reason this only seems to work for some textures. I have a handful of 64x64 textures, and 2 of them work fine for this while a third one appears rotated and flipped despite being fine elsewhere

Keep your UVs in 0-1 range. The code you initially posted seems to be having UVs that are larger than 1.

1 Like

Oh wait I realized what it was. My own sprite confused me, the original code wasn’t rotated it was flipped. Thanks!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.