- Home /
Problem when creating UVs coordinates
Hi everyone, I'm trying to learn how to create a mesh form scratch. I Created a C# script that render a block, but i got stuck in this part. When I hit play the mesh is rendered good, but the texture has a wrong rotation
Here the code:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class CubeRender : MonoBehaviour {
protected List<Vector3> verts = new List<Vector3> ();
protected List<int> triangulos = new List<int> ();
protected List<Vector2> uvs = new List<Vector2> ();
private Mesh cubo;
private float uText = 0.25f;
void Start ()
{
cubo = GetComponent<MeshFilter> ().mesh;
float x = transform.position.x;
float z = transform.position.z;
float y = transform.position.y;
//Baixo
CriarFace (new Vector3(x, y, z), Vector3.forward, Vector3.right, true);
//Cima
CriarFace (new Vector3(x, y + 1, z), Vector3.forward, Vector3.right, false);
//Frente
CriarFace (new Vector3(x, y, z + 1), Vector3.up, Vector3.right, true);
//Tras
CriarFace (new Vector3(x, y, z), Vector3.up, Vector3.right, false);
//Esquerda
CriarFace (new Vector3(x, y, z), Vector3.up, Vector3.forward, true);
//Direita
CriarFace (new Vector3(x + 1, y, z ), Vector3.up, Vector3.forward, false);
cubo.Clear ();
cubo.vertices = verts.ToArray ();
cubo.triangles = triangulos.ToArray ();
cubo.uv = uvs.ToArray ();
cubo.Optimize ();
cubo.RecalculateNormals ();
}
void CriarFace(Vector3 origem, Vector3 direita, Vector3 cima, bool revertido)
{
int index = verts.Count;
verts.Add (origem);
verts.Add (origem + direita);
verts.Add (origem + cima);
verts.Add (origem + direita + cima);
if (invertido == true) {
triangulos.Add (index + 1);
triangulos.Add (index + 0);
triangulos.Add (index + 2);
triangulos.Add (index + 1);
triangulos.Add (index + 2);
triangulos.Add (index + 3);
}else{
triangulos.Add (index + 0);
triangulos.Add (index + 1);
triangulos.Add (index + 2);
triangulos.Add (index + 2);
triangulos.Add (index + 1);
triangulos.Add (index + 3);
}
uvs.Add(new Vector2 (0, 0));
uvs.Add(new Vector2 (uText, 0));
uvs.Add(new Vector2 (0, uText));
uvs.Add(new Vector2 (uText, uText));
}
void Update ()
{
}
}
The rotation of top and bottom textures doesn't matter, but the sides are wrong, the green part should be on the top.
Anyone has any idea of how I can solve this ?
Sorry about the awful explanation, and the horrible english :s
Answer by zharik86 · Sep 10, 2014 at 06:17 AM
Simple way, this rotation of a texture on 90 degrees counterclockwise in any graphics editor. For example, in Photoshop. But it is possible to leave and so. But then it will be necessary to change textural coordinates. I change part of your code:
void CriarFace(Vector3 origem, Vector3 direita, Vector3 cima, bool revertido) {
int index = verts.Count;
verts.Add (origem);
verts.Add (origem + direita);
verts.Add (origem + cima);
verts.Add (origem + direita + cima);
if (invertido == true) {
triangulos.Add (index + 1);
triangulos.Add (index + 0);
triangulos.Add (index + 2);
triangulos.Add (index + 1);
triangulos.Add (index + 2);
triangulos.Add (index + 3);
}else{
triangulos.Add (index + 0);
triangulos.Add (index + 1);
triangulos.Add (index + 2);
triangulos.Add (index + 2);
triangulos.Add (index + 1);
triangulos.Add (index + 3);
}
//little change
uvs.Add(new Vector2 (0, uText));
uvs.Add(new Vector2 (0, 0));
uvs.Add(new Vector2 (uText, uText));
uvs.Add(new Vector2 (uText, 0));
}
Or to change sequence of triangles. I hope that it willhelp you.
@$$anonymous$$artix10 If I helped you, you can mark my answer. Possibility of a mark of the answer to be is slightly below than vote marks. And on the future if somebody helped you and he/she gave the correct answer, it is possible to mark it. The person tried, spent time.