Problems with UVs in a cube (add UV or Vertex first in SurfaceTool?)

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

Hi, I’m new to UV mapping and I’m having some problems.

I made a SpatialMaterial with a 16x16 texture that has drown an arrow. I’m trying to put that texture in all faces of a cube, but the cube is one color only. I show you my code.

First, the singleton VoxelData.gdthat contains information about the indices and faces:

extends Node

const voxelVertices : Array = [
Vector3(0,0,0),
Vector3(1,0,0),
Vector3(1,1,0),
Vector3(0,1,0),
Vector3(0,0,1),
Vector3(1,0,1),
Vector3(1,1,1),
Vector3(0,1,1)
]

const voxelFaces : Array = [
	[3, 0, 1, 1, 2, 3], # back face
	[6, 5, 4, 4, 7, 6], # front face
	[7, 3, 2, 2, 6, 7], # top face
	[0, 4, 5, 5, 1, 0], # bottom face
	[7, 4, 0, 0, 3, 7], # left face
	[2, 1, 5, 5, 6, 2] # right face
]

const voxelUVs : Array = [
	Vector2(0,1),
	Vector2(0,0),
	Vector2(1,0),
	Vector2(1,0),
	Vector2(1,1),
	Vector2(0,1)
]

Now the script that creates the cube and adds it to the world:

extends Spatial


func _init():
	var voxel = simpleVoxel.new()
	add_child(voxel)

class simpleVoxel extends Spatial:
	
	func _init():
		
		var surfaceTool : SurfaceTool = SurfaceTool.new()
		surfaceTool.begin(Mesh.PRIMITIVE_TRIANGLES)
		
		for i in  range(VoxelData.voxelFaces.size()):
			for j in range(6):
				surfaceTool.add_vertex(VoxelData.voxelVertices[VoxelData.voxelFaces[i][j]])
				surfaceTool.add_uv(VoxelData.voxelUVs[j])
		
		surfaceTool.generate_normals()
		
		var meshInstance : MeshInstance = MeshInstance.new()
		var mesh : Mesh = surfaceTool.commit()
		meshInstance.mesh = mesh
		
		var spatialMaterial : SpatialMaterial = preload("res://Materials/SimpleMaterial.tres")
		spatialMaterial.params_cull_mode = spatialMaterial.CULL_BACK
		meshInstance.material_override = spatialMaterial
		
		self.add_child(meshInstance)
:bust_in_silhouette: Reply From: klaas

Hi,
call add_uv before add_vertex …

     surfaceTool.add_uv(VoxelData.voxelUVs[j])
     surfaceTool.add_vertex(VoxelData.voxelVertices[VoxelData.voxelFaces[i][j]])