- Home /
Help Uv mapping !!!
I have made a script that procedurally creates a cube and i need help uv mapping it. When it generates the texture looks all messed up and i don't know how to fix it. I compared my procedural cube to the cube the one that's comes with unity and it seems that the unity cube has 24 vertexes while mine has 8. May that be the problem? Here's my script: var check : boolean = true;
function Start () {
var mesh : Mesh = GetComponent(MeshFilter).mesh;
var vertices : Vector3[] = new Vector3[8];
vertices[0] = new Vector3(.5, .5, .5); //high-top-left (.5, .5, .5)
vertices[1] = new Vector3(.5, .5, -.5); //high-top-right (.5, .5, -.5)
vertices[2] = new Vector3(-.5, .5, -.5); //high-bottom-left (-.5, .5, -.5)
vertices[3] = new Vector3(-.5, .5, .5); //high-bottom-right (-.5, .5, .5)
vertices[4] = new Vector3(.5, -.5, .5); //low-top-left
vertices[5] = new Vector3(.5, -.5, -.5); //low-top-right
vertices[6] = new Vector3(-.5, -.5, -.5); //low-bottom-left
vertices[7] = new Vector3(-.5, -.5, .5); //low-bottom-right
mesh.vertices = vertices;
if (check){
if (!Physics.Raycast (transform.position, Vector3.up, 1)) {
print("Creating a face on top");
mesh.triangles += [0, 1, 2, 0, 2, 3];
}
if (!Physics.Raycast (transform.position, Vector3.down, 1)) {
print("Creating a face on botton");
mesh.triangles += [7, 6, 4, 6, 5, 4];
}
if (!Physics.Raycast (transform.position, Vector3.right, 1)) {
print("Creating a face on right");
mesh.triangles += [0, 4, 5, 5, 1, 0];
}
if (!Physics.Raycast (transform.position, Vector3.left, 1)) {
print("Creating a face on left");
mesh.triangles += [3, 6, 7, 6, 3, 2];
}
if (!Physics.Raycast (transform.position, Vector3.forward, 1)) {
print("Creating a face on front");
mesh.triangles += [0, 3, 4, 7, 4, 3];
}
if (!Physics.Raycast (transform.position, Vector3.back, 1)) {
print("Creating a face on back");
mesh.triangles += [5, 2, 1, 2, 5, 6];
}
var uvs : Vector2[] = new Vector2[8];
uvs[0] = new Vector2(0, .5); //bottom-right
uvs[1] = new Vector2(.5, .5); //middle
uvs[2] = new Vector2(0,0); //bottom-left
uvs[3] = new Vector2(.5, 0); //top-left
uvs[4] = new Vector2(0, 0); //top-left
uvs[5] = new Vector2(0.5, 0.5); //top-right
uvs[6] = new Vector2(0, .5); //bottom-left
uvs[7] = new Vector2(.5, .5); //bottom-right
mesh.uv = uvs;
mesh.RecalculateNormals();
check = false;
}
}
PS: I'v researched this but did not fined a answer.
Answer by Owen-Reynolds · Apr 15, 2012 at 05:30 AM
Yes, 8 vs. 24 verts is the answer. If you search around for "unwrapping a cube," there's no good way to position 8 verts so all 6 sides look good. So, modellers makes seams. A seam duplicates the verts along that edge, which allows you to appear to position, for example, the lower-right corner in 1 spot for the front face, and another for the side face. 24 verts simply repeats each vert once for each face (or, each face "owns" its unique four verts.)
Also, your numbers are odd. You have (0.5,0.5) as the middle, top-right and bottom-right. Normally, for textures, (1,1) is top-right, (1,0) is bottom-right, (0,1) is top-left, ... .
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Blender to Mixamo to Unity3D 1 Answer
Create planar UVs on procedurally generated mesh 1 Answer
help making map! 1 Answer