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
