0 votes

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)
Godot version 3.2.3
in Engine by (236 points)
edited by

1 Answer

+1 vote
Best answer

Hi,
call adduv before addvertex ...

     surfaceTool.add_uv(VoxelData.voxelUVs[j])
     surfaceTool.add_vertex(VoxelData.voxelVertices[VoxelData.voxelFaces[i][j]])
by (4,010 points)
selected by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.