How to set a texture (spatial material) on a mesh instance?

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

I’m attempting to set a texture on a quad that I generate with the surface tool. On the mesh instance I set a material override of spatial material. I then set the texture to a png file in the albedo section. The texture I am using is a 16 x 16 png with an alpha channel. The top half of the texture is red and bottom half is blue. When I run the code I see a quad that is one solid color and that is purple. I’m new to godot and am completely lost to what is wrong. Any help is appreciated.

extends MeshInstance

func _ready():
  
  var surfTool = SurfaceTool.new()
  var mesh = Mesh.new()
  var vert_array = Array()
  var uv_array = Array()
  var st = SurfaceTool.new()
       
  vert_array.push_back(Vector3(0,0,0))
  vert_array.push_back(Vector3(0,1,0))
  vert_array.push_back(Vector3(1,1,0))
       
  vert_array.push_back(Vector3(0,0,0))
  vert_array.push_back(Vector3(1,1,0))
  vert_array.push_back(Vector3(1,0,0))
       
  uv_array.push_back(Vector2(0,0))
  uv_array.push_back(Vector2(0,1))
  uv_array.push_back(Vector2(1,1))
       
  uv_array.push_back(Vector2(0,0))
  uv_array.push_back(Vector2(1,1))
  uv_array.push_back(Vector2(1,0))
       
  st.begin(Mesh.PRIMITIVE_TRIANGLES)
  for i in range(6):
    st.add_vertex(vert_array[i])
    st.add_uv(uv_array[i])
  st.commit(mesh)
  self.set_mesh(mesh)
:bust_in_silhouette: Reply From: xrobledo

It turns out the add_uv line needs to be called before the add_vertex line. With those two lines swapped everything works fine.